001/*
002// This software is subject to the terms of the Eclipse Public License v1.0
003// Agreement, available at the following URL:
004// http://www.eclipse.org/legal/epl-v10.html.
005// You must accept the terms of that agreement to use this software.
006//
007// Copyright (C) 2003-2005 Julian Hyde
008// Copyright (C) 2005-2009 Pentaho
009// Copyright (C) 2004-2005 TONBELLER AG
010// All Rights Reserved.
011*/
012package mondrian.rolap;
013
014import mondrian.olap.*;
015
016import java.util.EventObject;
017
018/**
019 * A factory for {@link mondrian.olap.NativeEvaluator}.
020 * If the instance returns null,
021 * then the interpreter must compute the result itself.
022 * If it returns a NativeEvaluator
023 * the interpreter may choose to let the NativeEvaluator compute the result.
024 *
025 * <p>There exist multiple RolapNative implementations, e.g. for CrossJoin,
026 * TopCount, Filter etc. If the arguments of these functions are simple enough
027 * to be evaluated in SQL then a NativeEvaluator will be returned that performs
028 * the computations in SQL. Otherwise null will be returned.
029 */
030public abstract class RolapNative {
031
032    private boolean enabled;
033
034    static class NativeEvent extends EventObject {
035        private final NativeEvaluator neval;
036        public NativeEvent(Object source, NativeEvaluator neval) {
037            super(source);
038            this.neval = neval;
039        }
040        NativeEvaluator getNativeEvaluator() {
041            return neval;
042        }
043    }
044
045    static class TupleEvent extends EventObject {
046        private final TupleReader tupleReader;
047
048        public TupleEvent(Object source, TupleReader tupleReader) {
049            super(source);
050            this.tupleReader = tupleReader;
051        }
052
053        TupleReader getTupleReader() {
054            return tupleReader;
055        }
056    }
057
058    interface Listener {
059        void foundEvaluator(NativeEvent e);
060        void foundInCache(TupleEvent e);
061        void executingSql(TupleEvent e);
062    }
063
064    protected Listener listener;
065
066    /**
067     * If function can be implemented in SQL, returns a NativeEvaluator that
068     * computes the result; otherwise returns null.
069     */
070    abstract NativeEvaluator createEvaluator(
071        RolapEvaluator evaluator,
072        FunDef fun,
073        Exp[] args);
074
075    /**
076     * if enabled == false, then createEvaluator will always return null
077     */
078    boolean isEnabled() {
079        return enabled;
080    }
081
082    void setEnabled(boolean enabled) {
083        this.enabled = enabled;
084    }
085
086    Listener getListener() {
087        return listener;
088    }
089
090    void setListener(Listener listener) {
091        this.listener = listener;
092    }
093
094    /**
095     * Sets whether to use hard caching for testing.
096     * When using soft references, we can not test caching
097     * because things may be garbage collected during the tests.
098     */
099    abstract void useHardCache(boolean hard);
100}
101
102// End RolapNative.java