001    /*
002    // $Id: //open/mondrian/src/main/mondrian/rolap/RolapCacheRegion.java#1 $
003    // This software is subject to the terms of the Common Public License
004    // Agreement, available at the following URL:
005    // http://www.opensource.org/licenses/cpl.html.
006    // Copyright (C) 2007-2007 Julian Hyde
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.*;
013    
014    /**
015     * A <code>RolapCacheRegion</code> represents a region of multidimensional space
016     * in the cache.
017     *
018     * <p>The region is represented in terms of the columns of a given
019     * {@link mondrian.rolap.RolapStar}, and constraints on those columns.
020     *
021     * <p>Compare with {@link mondrian.olap.CacheControl.CellRegion}: a
022     * <code>CellRegion</code> is in terms of {@link mondrian.olap.Member} objects
023     * (logical); whereas a <code>RolapCacheRegion</code> is in terms of columns
024     * (physical).
025     */
026    public class RolapCacheRegion {
027        private final BitKey bitKey;
028        private final Map<Integer, StarColumnPredicate> columnPredicates =
029            new HashMap<Integer, StarColumnPredicate>();
030        private Map<List<RolapStar.Column>, StarPredicate> predicates =
031            new HashMap<List<RolapStar.Column>, StarPredicate>();
032    
033        public RolapCacheRegion(RolapStar star, List<RolapStar.Measure> starMeasureList) {
034            bitKey = BitKey.Factory.makeBitKey(star.getColumnCount());
035            for (RolapStar.Measure measure : starMeasureList) {
036                bitKey.set(measure.getBitPosition());
037            }
038        }
039    
040        public BitKey getConstrainedColumnsBitKey() {
041            return bitKey;
042        }
043    
044        /**
045         * Adds a predicate which applies to a single column.
046         *
047         * @param column Constrained column
048         * @param predicate Predicate
049         */
050        public void addPredicate(
051            RolapStar.Column column,
052            StarColumnPredicate predicate)
053        {
054            int bitPosition = column.getBitPosition();
055            assert !bitKey.get(bitPosition);
056            bitKey.set(bitPosition);
057            columnPredicates.put(bitPosition, predicate);
058        }
059    
060        /**
061         * Returns the predicate associated with the
062         * <code>columnOrdinal</code>th column.
063         *
064         * @param columnOrdinal Column ordinal
065         * @return Predicate, or null if not constrained
066         */
067        public StarColumnPredicate getPredicate(int columnOrdinal) {
068            return columnPredicates.get(columnOrdinal);
069        }
070    
071        /**
072         * Adds a predicate which applies to multiple columns.
073         *
074         * <p>The typical example of a multi-column predicate is a member
075         * constraint. For example, the constraint "m between 1997.Q3 and
076         * 1998.Q2" translates into "year = 1997 and quarter >= Q3 or year =
077         * 1998 and quarter <= Q2".
078         *
079         * @param predicate Predicate
080         */
081        public void addPredicate(StarPredicate predicate)
082        {
083            final List<RolapStar.Column> columnList =
084                predicate.getConstrainedColumnList();
085            predicates.put(
086                new ArrayList<RolapStar.Column>(columnList),
087                predicate);
088            for (RolapStar.Column column : columnList) {
089                bitKey.set(column.getBitPosition());
090            }
091        }
092    
093        /**
094         * Returns a collection of all multi-column predicates.
095         *
096         * @return Collection of all multi-column constraints
097         */
098        public Collection<StarPredicate> getPredicates() {
099            return predicates.values();
100        }
101    }
102    
103    // End RolapCacheRegion.java