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) 2007-2012 Pentaho
008// All Rights Reserved.
009*/
010package mondrian.rolap;
011
012import 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 */
026public class RolapCacheRegion {
027    private final BitKey bitKey;
028    private final Map<Integer, StarColumnPredicate> columnPredicates =
029        new HashMap<Integer, StarColumnPredicate>();
030    private final Map<String, StarColumnPredicate> columnPredicatesByName =
031        new HashMap<String, StarColumnPredicate>();
032    private Map<List<RolapStar.Column>, StarPredicate> predicates =
033        new HashMap<List<RolapStar.Column>, StarPredicate>();
034
035    public RolapCacheRegion(
036        RolapStar star,
037        List<RolapStar.Measure> starMeasureList)
038    {
039        bitKey = BitKey.Factory.makeBitKey(star.getColumnCount());
040        for (RolapStar.Measure measure : starMeasureList) {
041            bitKey.set(measure.getBitPosition());
042        }
043    }
044
045    public BitKey getConstrainedColumnsBitKey() {
046        return bitKey;
047    }
048
049    /**
050     * Adds a predicate which applies to a single column.
051     *
052     * @param column Constrained column
053     * @param predicate Predicate
054     */
055    public void addPredicate(
056        RolapStar.Column column,
057        StarColumnPredicate predicate)
058    {
059        int bitPosition = column.getBitPosition();
060        assert !bitKey.get(bitPosition);
061        bitKey.set(bitPosition);
062        columnPredicates.put(bitPosition, predicate);
063        columnPredicatesByName.put(
064            column.getExpression().getGenericExpression(),
065            predicate);
066    }
067
068    /**
069     * Returns the predicate associated with the
070     * <code>columnOrdinal</code>th column.
071     *
072     * @param columnOrdinal Column ordinal
073     * @return Predicate, or null if not constrained
074     */
075    public StarColumnPredicate getPredicate(int columnOrdinal) {
076        return columnPredicates.get(columnOrdinal);
077    }
078
079    /**
080     * Returns the predicate associated with the
081     * <code>columnName</code>, where column name is
082     * the generic SQL expression in the form of:
083     *
084     * <p>&nbsp;&nbsp;&nbsp;&nbsp;table.column
085     *
086     * @param columnName Column name
087     * @return Predicate, or null if not constrained
088     */
089    public StarColumnPredicate getPredicate(String columnName) {
090        return columnPredicatesByName.get(columnName);
091    }
092
093    /**
094     * Adds a predicate which applies to multiple columns.
095     *
096     * <p>The typical example of a multi-column predicate is a member
097     * constraint. For example, the constraint "m between 1997.Q3 and
098     * 1998.Q2" translates into "year = 1997 and quarter >= Q3 or year =
099     * 1998 and quarter <= Q2".
100     *
101     * @param predicate Predicate
102     */
103    public void addPredicate(StarPredicate predicate)
104    {
105        final List<RolapStar.Column> columnList =
106            predicate.getConstrainedColumnList();
107        predicates.put(
108            new ArrayList<RolapStar.Column>(columnList),
109            predicate);
110        for (RolapStar.Column column : columnList) {
111            bitKey.set(column.getBitPosition());
112        }
113    }
114
115    /**
116     * Returns a collection of all multi-column predicates.
117     *
118     * @return Collection of all multi-column constraints
119     */
120    public Collection<StarPredicate> getPredicates() {
121        return predicates.values();
122    }
123
124    /**
125     * Returns the list of all column predicates.
126     */
127    public Collection<StarColumnPredicate> getColumnPredicates() {
128        return columnPredicates.values();
129    }
130}
131
132// End RolapCacheRegion.java