001    /*
002    // $Id: //open/mondrian-release/3.2/src/main/mondrian/rolap/SmartMemberListCache.java#2 $
003    // This software is subject to the terms of the Eclipse Public License v1.0
004    // Agreement, available at the following URL:
005    // http://www.eclipse.org/legal/epl-v10.html.
006    // Copyright (C) 2004-2005 TONBELLER AG
007    // Copyright (C) 2006-2009 Julian Hyde
008    // All Rights Reserved.
009    // You must accept the terms of that agreement to use this software.
010    */
011    package mondrian.rolap;
012    
013    import mondrian.rolap.cache.SmartCache;
014    import mondrian.rolap.cache.SoftSmartCache;
015    import mondrian.rolap.sql.SqlConstraint;
016    import 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     * @version $Id: //open/mondrian-release/3.2/src/main/mondrian/rolap/SmartMemberListCache.java#2 $
050     */
051    public class SmartMemberListCache <K, V> {
052        SmartCache<Pair<K, Object>, V> cache;
053    
054        public SmartMemberListCache() {
055            cache = new SoftSmartCache<Pair<K, Object>, V>();
056        }
057    
058        public Object put(K key, SqlConstraint constraint, V value) {
059            Object cacheKey = constraint.getCacheKey();
060            if (cacheKey == null) {
061                return null;
062            }
063            Pair<K, Object> key2 = new Pair<K, Object>(key, cacheKey);
064            return cache.put(key2, value);
065        }
066    
067        public V get(K key, SqlConstraint constraint) {
068            Pair<K, Object> key2 =
069                new Pair<K, Object>(key, constraint.getCacheKey());
070            return cache.get(key2);
071        }
072    
073        public void clear() {
074            cache.clear();
075        }
076    
077        SmartCache<Pair<K, Object>, V> getCache() {
078            return cache;
079        }
080    
081        void setCache(SmartCache<Pair<K, Object>, V> cache) {
082            this.cache = cache;
083        }
084    }
085    
086    // End SmartMemberListCache.java