📄 aggregation.java
字号:
SchemaReader scr = star.getSchema().getSchemaReader();
nChildren = scr.getChildrenCountFromCache(parent);
if (nChildren == -1) {
// nothing gotten from cache
if (!parent.isAll()) {
// parent is in constraints
// no information about children cardinality
// constraints must not be optimized away
bloats[i] = 0.0;
done = true;
}
} else {
bloats[i] = constraintLength / nChildren;
done = true;
}
}
}
if (!done && level != null) {
// if the level members are cached, we do not need "count *"
int nMembers = -1;
SchemaReader scr = star.getSchema().getSchemaReader();
nMembers = scr.getLevelCardinalityFromCache(level);
if ( nMembers > 0 ) {
bloats[i] = constraintLength / nMembers;
done = true;
}
}
if (!done) {
bloats[i] = constraintLength / columns[i].getCardinality();
}
}
} // for i < newConstraintses.length
// build a list of constraints sorted by 'bloat factor'
ConstraintComparator comparator = new ConstraintComparator(bloats);
Integer[] indexes = new Integer[columns.length];
for (int i = 0; i < columns.length; i++) {
indexes[i] = new Integer(i);
}
// sort indexes by bloat descending
Arrays.sort(indexes, comparator);
// eliminate constraints one by one, until the estimated cell count
// doubles. We can not have an absolute value here, because its
// very different if we fetch data for 2 years or 10 years (5 times
// more means 5 times slower). So a relative comparison is ok here
// but not an absolute one.
double abloat = 1.0;
final double aBloatLimit = 0.5;
for (int i = 0; i < indexes.length; i++) {
int j = indexes[i].intValue();
abloat = abloat * bloats[j];
if (abloat > aBloatLimit) {
// eliminate this constraint
newConstraintses[j] = null;
} else {
break;
}
}
return newConstraintses;
}
private class ConstraintComparator implements Comparator {
private final double[] bloats;
ConstraintComparator(double[] bloats) {
this.bloats = bloats;
}
// implement Comparator
// order by bloat descending
public int compare(Object o0, Object o1) {
double bloat0 = bloats[((Integer)o0).intValue()];
double bloat1 = bloats[((Integer)o1).intValue()];
return (bloat0 == bloat1)
? 0
: (bloat0 < bloat1)
? 1
: -1;
}
}
/**
* Retrieves the value identified by <code>keys</code>.
*
* <p>If <code>pinSet</code> is not null, pins the
* segment which holds it. <code>pinSet</code> ensures that a segment is
* only pinned once.
*
* Returns <code>null</code> if no segment contains the cell.
**/
public synchronized Object get(RolapStar.Measure measure,
Object[] keys,
Collection pinSet) {
for (Iterator it = segmentRefs.iterator(); it.hasNext();) {
SoftReference ref = (SoftReference) it.next();
Segment segment = (Segment) ref.get();
if (segment == null) {
it.remove();
continue; // it's being garbage-collected
}
if (segment.measure != measure) {
continue;
}
if (segment.isReady()) {
Object o = segment.get(keys);
if (o != null) {
if (pinSet != null) {
pinSet.add(segment);
}
return o;
}
} else {
if (segment.wouldContain(keys)) {
if (pinSet != null) {
pinSet.add(segment);
}
return null;
}
}
}
return null;
}
/**
* This is called during Sql generation.
*
* @return
*/
RolapStar.Column[] getColumns() {
return columns;
}
/**
* This is called during Sql generation.
*
* @return
*/
RolapStar getStar() {
return star;
}
// -- classes -------------------------------------------------------------
static class Axis {
private final RolapStar.Column column;
// null if no constraint
private final ColumnConstraint[] constraints;
// inversion of keys
private final Map mapKeyToOffset;
// actual keys retrieved
private Object[] keys;
Axis(RolapStar.Column column,
ColumnConstraint[] constraints) {
this.column = column;
this.constraints = constraints;
this.mapKeyToOffset = new HashMap();
}
ColumnConstraint[] getConstraints() {
return constraints;
}
Object[] getKeys() {
return this.keys;
}
int loadKeys() {
int size = mapKeyToOffset.size();
this.keys = new Object[size];
Iterator it = mapKeyToOffset.entrySet().iterator();
while(it.hasNext()) {
Map.Entry e = (Map.Entry) it.next();
Object key = e.getKey();
Integer offsetInteger = (Integer) e.getValue();
int offset = offsetInteger.intValue();
this.keys[offset] = key;
}
return size;
}
Integer getOffset(Object key) {
return (Integer) mapKeyToOffset.get(key);
}
void addNextOffset(Object key) {
int size = mapKeyToOffset.size();
mapKeyToOffset.put(key, new Integer(size));
}
boolean contains(Object key) {
if (constraints == null) {
return true;
}
for (int i = 0; i < constraints.length; i++) {
if (constraints[i].getValue().equals(key)) {
return true;
}
}
return false;
}
double getBytes() {
return (keys == null)
? 0
: 16 + 8 * keys.length;
}
}
}
// End Aggregation.java
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -