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-2010 Pentaho
009// All Rights Reserved.
010*/
011package mondrian.rolap;
012
013import mondrian.rolap.cache.SmartCache;
014import mondrian.rolap.cache.SoftSmartCache;
015import mondrian.rolap.sql.SqlConstraint;
016import mondrian.util.Pair;
017
018/**
019 * Uses a {@link mondrian.rolap.cache.SmartCache} to store lists of members,
020 * where the key depends on a {@link mondrian.rolap.sql.SqlConstraint}.
021 *
022 * <p>Example 1:
023 *
024 * <pre>
025 *   select ...
026 *   [Customer].[Name].members on rows
027 *   ...
028 * </pre>
029 *
030 * <p>Example 2:
031 * <pre>
032 *   select ...
033 *   NON EMPTY [Customer].[Name].members on rows
034 *   ...
035 *   WHERE ([Store#14], [Product].[Product#1])
036 * </pre>
037 *
038 * <p>The first set, <em>all</em> customers are computed, in the second only
039 * those, who have bought Product#1 in Store#14. We want to put both results
040 * into the cache. Then the key for the cache entry is the Level that the
041 * members belong to <em>plus</em> the costraint that restricted the amount of
042 * members fetched. For Level.Members the key consists of the Level and the
043 * cacheKey of the {@link mondrian.rolap.sql.SqlConstraint}.
044 *
045 * @see mondrian.rolap.sql.SqlConstraint#getCacheKey
046 *
047 * @author av
048 * @since Nov 21, 2005
049 */
050public class SmartMemberListCache <K, V> {
051    SmartCache<Pair<K, Object>, V> cache;
052
053    public SmartMemberListCache() {
054        cache = new SoftSmartCache<Pair<K, Object>, V>();
055    }
056
057    public Object put(K key, SqlConstraint constraint, V value) {
058        Object cacheKey = constraint.getCacheKey();
059        if (cacheKey == null) {
060            return null;
061        }
062        Pair<K, Object> key2 = new Pair<K, Object>(key, cacheKey);
063        return cache.put(key2, value);
064    }
065
066    public V get(K key, SqlConstraint constraint) {
067        Pair<K, Object> key2 =
068            new Pair<K, Object>(key, constraint.getCacheKey());
069        return cache.get(key2);
070    }
071
072    public void clear() {
073        cache.clear();
074    }
075
076    SmartCache<Pair<K, Object>, V> getCache() {
077        return cache;
078    }
079
080    void setCache(SmartCache<Pair<K, Object>, V> cache) {
081        this.cache = cache;
082    }
083}
084
085// End SmartMemberListCache.java