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) 2004-2005 TONBELLER AG
008// Copyright (C) 2006-2009 Pentaho and others
009// All Rights Reserved.
010*/
011package mondrian.rolap;
012
013import mondrian.olap.*;
014
015import java.util.HashMap;
016import java.util.Map;
017
018/**
019 * Composite of {@link RolapNative}s. Uses chain of responsibility
020 * to select the appropriate {@link RolapNative} evaluator.
021 */
022public class RolapNativeRegistry extends RolapNative {
023
024    private Map<String, RolapNative> nativeEvaluatorMap =
025        new HashMap<String, RolapNative>();
026
027    public RolapNativeRegistry() {
028        super.setEnabled(true);
029
030        /*
031         * Mondrian functions which might be evaluated natively.
032         */
033        register("NonEmptyCrossJoin".toUpperCase(), new RolapNativeCrossJoin());
034        register("CrossJoin".toUpperCase(), new RolapNativeCrossJoin());
035        register("TopCount".toUpperCase(), new RolapNativeTopCount());
036        register("Filter".toUpperCase(), new RolapNativeFilter());
037    }
038
039    /**
040     * Returns the matching NativeEvaluator or null if <code>fun</code> can not
041     * be executed in SQL for the given context and arguments.
042     */
043    public NativeEvaluator createEvaluator(
044        RolapEvaluator evaluator, FunDef fun, Exp[] args)
045    {
046        if (!isEnabled()) {
047            return null;
048        }
049
050        RolapNative rn = nativeEvaluatorMap.get(fun.getName().toUpperCase());
051
052        if (rn == null) {
053            return null;
054        }
055
056        NativeEvaluator ne = rn.createEvaluator(evaluator, fun, args);
057
058        if (ne != null) {
059            if (listener != null) {
060                NativeEvent e = new NativeEvent(this, ne);
061                listener.foundEvaluator(e);
062            }
063        }
064        return ne;
065    }
066
067    public void register(String funName, RolapNative rn) {
068        nativeEvaluatorMap.put(funName, rn);
069    }
070
071    /** for testing */
072    void setListener(Listener listener) {
073        super.setListener(listener);
074        for (RolapNative rn : nativeEvaluatorMap.values()) {
075            rn.setListener(listener);
076        }
077    }
078
079    /** for testing */
080    void useHardCache(boolean hard) {
081        for (RolapNative rn : nativeEvaluatorMap.values()) {
082            rn.useHardCache(hard);
083        }
084    }
085}
086
087// End RolapNativeRegistry.java