1 13 14 package mondrian.rolap.agg; 15 16 import mondrian.olap.MondrianProperties; 17 import mondrian.olap.Util; 18 import mondrian.rolap.*; 19 import mondrian.rolap.aggmatcher.AggStar; 20 21 import org.apache.log4j.Logger; 22 23 import java.util.*; 24 25 33 public class AggregationManager extends RolapAggregationManager { 34 private static final Logger LOGGER = 35 Logger.getLogger(AggregationManager.class); 36 37 private static AggregationManager instance; 38 39 40 public static synchronized AggregationManager instance() { 41 if (instance == null) { 42 instance = new AggregationManager(); 43 } 44 return instance; 45 } 46 47 AggregationManager() { 48 super(); 49 } 50 51 public Logger getLogger() { 52 return LOGGER; 53 } 54 55 65 public void loadAggregation( 66 RolapStar.Measure[] measures, 67 RolapStar.Column[] columns, 68 BitKey constrainedColumnsBitKey, 69 StarColumnPredicate[] predicates, 70 PinSet pinnedSegments) { 71 RolapStar star = measures[0].getStar(); 72 Aggregation aggregation = 73 star.lookupOrCreateAggregation(constrainedColumnsBitKey); 74 75 synchronized (aggregation) { 77 predicates = aggregation.optimizePredicates(columns, predicates); 80 81 aggregation.load(columns, measures, predicates, pinnedSegments); 82 } 83 } 84 85 public Object getCellFromCache(CellRequest request) { 86 RolapStar.Measure measure = request.getMeasure(); 87 Aggregation aggregation = measure.getStar().lookupAggregation( 88 request.getConstrainedColumnsBitKey()); 89 90 if (aggregation == null) { 91 return null; 93 } 94 synchronized (aggregation) { 96 Object o = aggregation.getCellValue( 97 measure, request.getSingleValues(), null); 98 if (o != null) { 99 return o; 100 } 101 } 102 throw Util.newInternal("not found"); 103 } 104 105 public Object getCellFromCache(CellRequest request, PinSet pinSet) { 106 Util.assertPrecondition(pinSet != null); 107 108 RolapStar.Measure measure = request.getMeasure(); 109 Aggregation aggregation = measure.getStar().lookupAggregation( 110 request.getConstrainedColumnsBitKey()); 111 112 if (aggregation == null) { 113 return null; 115 } else { 116 synchronized (aggregation) { 118 return aggregation.getCellValue(measure, 119 request.getSingleValues(), pinSet); 120 } 121 } 122 } 123 124 public String getDrillThroughSql( 125 final CellRequest request, 126 boolean countOnly) 127 { 128 DrillThroughQuerySpec spec = 129 new DrillThroughQuerySpec( 130 request, 131 countOnly); 132 String sql = spec.generateSqlQuery(); 133 134 if (getLogger().isDebugEnabled()) { 135 StringBuilder buf = new StringBuilder (256); 136 buf.append("DrillThroughSQL: "); 137 buf.append(sql); 138 buf.append(Util.nl); 139 getLogger().debug(buf.toString()); 140 } 141 142 return sql; 143 } 144 145 149 public String generateSql( 150 final Segment[] segments, 151 final BitKey levelBitKey, 152 final BitKey measureBitKey) { 153 if (MondrianProperties.instance().UseAggregates.get()) { 155 RolapStar star = segments[0].aggregation.getStar(); 156 157 final boolean[] rollup = {false}; 158 AggStar aggStar = findAgg(star, levelBitKey, measureBitKey, rollup); 159 160 if (aggStar != null) { 161 163 if (getLogger().isDebugEnabled()) { 164 StringBuilder buf = new StringBuilder (256); 165 buf.append("MATCH: "); 166 buf.append(star.getFactTable().getAlias()); 167 buf.append(" foreign="); 168 buf.append(levelBitKey); 169 buf.append(Util.nl); 170 buf.append(" measure="); 171 buf.append(measureBitKey); 172 buf.append(Util.nl); 173 buf.append(" aggstar="); 174 buf.append(aggStar.getBitKey()); 175 buf.append(Util.nl); 176 buf.append("AggStar="); 177 buf.append(aggStar.getFactTable().getName()); 178 buf.append(Util.nl); 179 for (AggStar.Table.Column column : aggStar.getFactTable() 180 .getColumns()) { 181 buf.append(" "); 182 buf.append(column); 183 buf.append(Util.nl); 184 } 185 getLogger().debug(buf.toString()); 186 } 187 188 AggQuerySpec aggQuerySpec = 189 new AggQuerySpec(aggStar, segments, rollup[0]); 190 String sql = aggQuerySpec.generateSqlQuery(); 191 return sql; 192 } 193 194 } 196 197 if (getLogger().isDebugEnabled()) { 198 RolapStar star = segments[0].aggregation.getStar(); 199 200 StringBuilder buf = new StringBuilder (256); 201 buf.append("NO MATCH: "); 202 buf.append(star.getFactTable().getAlias()); 203 buf.append(Util.nl); 204 buf.append(" foreign="); 205 buf.append(levelBitKey); 206 buf.append(Util.nl); 207 buf.append(" measure="); 208 buf.append(measureBitKey); 209 buf.append(Util.nl); 210 211 getLogger().debug(buf.toString()); 212 } 213 214 215 SegmentArrayQuerySpec spec = 217 new SegmentArrayQuerySpec(segments); 218 String sql = spec.generateSqlQuery(); 219 return sql; 220 } 221 222 238 public AggStar findAgg( 239 RolapStar star, 240 final BitKey levelBitKey, 241 final BitKey measureBitKey, 242 boolean[] rollup) { 243 assert rollup != null; 253 BitKey fullBitKey = levelBitKey.or(measureBitKey); 254 255 for (AggStar aggStar : star.getAggStars()) { 258 if (!aggStar.superSetMatch(fullBitKey)) { 260 continue; 261 } 262 263 boolean isDistinct = measureBitKey.intersects( 264 aggStar.getDistinctMeasureBitKey()); 265 266 if (!isDistinct) { 269 rollup[0] = !aggStar.getLevelBitKey().equals(levelBitKey); 270 return aggStar; 271 } 272 273 276 281 final BitKey distinctMeasuresBitKey = 287 measureBitKey.and(aggStar.getDistinctMeasureBitKey()); 288 final BitSet distinctMeasures = distinctMeasuresBitKey.toBitSet(); 289 BitKey combinedLevelBitKey = null; 290 for (int k = distinctMeasures.nextSetBit(0); k >= 0; 291 k = distinctMeasures.nextSetBit(k + 1)) { 292 final AggStar.FactTable.Measure distinctMeasure = 293 aggStar.lookupMeasure(k); 294 BitKey rollableLevelBitKey = 295 distinctMeasure.getRollableLevelBitKey(); 296 if (combinedLevelBitKey == null) { 297 combinedLevelBitKey = rollableLevelBitKey; 298 } else { 299 combinedLevelBitKey = 301 combinedLevelBitKey.and(rollableLevelBitKey); 302 } 303 } 304 305 if (aggStar.hasForeignKeys()) { 306 337 341 BitKey fkBitKey = aggStar.getForeignKeyBitKey().copy(); 346 Iterator mit = aggStar.getFactTable().getMeasures().iterator(); 347 for (AggStar.FactTable.Measure measure : aggStar.getFactTable() 348 .getMeasures()) { 349 if (measure.isDistinct()) { 350 if (measureBitKey.get(measure.getBitPosition())) { 351 fkBitKey.clear(measure.getBitPosition()); 352 } 353 } 354 } 355 if (!fkBitKey.isEmpty()) { 356 continue; 359 } 360 } 361 362 if (!aggStar.select( 363 levelBitKey, combinedLevelBitKey, measureBitKey)) { 364 continue; 365 } 366 367 rollup[0] = !aggStar.getLevelBitKey().equals(levelBitKey); 368 return aggStar; 369 } 370 return null; 371 } 372 373 public PinSet createPinSet() { 374 return new PinSetImpl(); 375 } 376 377 381 public static class PinSetImpl extends HashSet<Segment> 382 implements RolapAggregationManager.PinSet { 383 } 384 } 385 386 | Popular Tags |