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