提交 f6e90b78 authored 作者: noelgrandin's avatar noelgrandin

Save some memory. There is no need to store a dataType field in the…

Save some memory. There is no need to store a dataType field in the AggregateData structure when we can just pass it down. 
上级 faa073c9
......@@ -240,7 +240,7 @@ public class Aggregate extends Expression {
AggregateData data = (AggregateData) group.get(this);
if (data == null) {
data = AggregateData.create(type, dataType);
data = AggregateData.create(type);
group.put(this, data);
}
Value v = on == null ? null : on.getValue(session);
......@@ -259,7 +259,7 @@ public class Aggregate extends Expression {
}
}
}
data.add(session.getDatabase(), distinct, v);
data.add(session.getDatabase(), dataType, distinct, v);
}
@Override
......@@ -297,9 +297,9 @@ public class Aggregate extends Expression {
}
AggregateData data = (AggregateData) group.get(this);
if (data == null) {
data = AggregateData.create(type, dataType);
data = AggregateData.create(type);
}
Value v = data.getValue(session.getDatabase(), distinct);
Value v = data.getValue(session.getDatabase(), dataType, distinct);
if (type == GROUP_CONCAT) {
ArrayList<Value> list = ((AggregateDataGroupConcat)data).getList();
if (list == null || list.size() == 0) {
......
......@@ -18,21 +18,20 @@ abstract class AggregateData {
* Create an AggregateData object of the correct subtype.
*
* @param aggregateType the type of the aggregate operation
* @param dataType the datatype of the computed result
*/
static AggregateData create(int aggregateType, int dataType) {
static AggregateData create(int aggregateType) {
if (aggregateType == Aggregate.SELECTIVITY) {
return new AggregateDataSelectivity(dataType);
return new AggregateDataSelectivity();
} else if (aggregateType == Aggregate.GROUP_CONCAT) {
return new AggregateDataGroupConcat();
} else if (aggregateType == Aggregate.COUNT_ALL) {
return new AggregateDataCountAll(dataType);
return new AggregateDataCountAll();
} else if (aggregateType == Aggregate.COUNT) {
return new AggregateDataCount(dataType);
return new AggregateDataCount();
} else if (aggregateType == Aggregate.HISTOGRAM) {
return new AggregateDataHistogram(dataType);
return new AggregateDataHistogram();
} else {
return new AggregateDataDefault(aggregateType, dataType);
return new AggregateDataDefault(aggregateType);
}
}
......@@ -40,17 +39,19 @@ abstract class AggregateData {
* Add a value to this aggregate.
*
* @param database the database
* @param dataType the datatype of the computed result
* @param distinct if the calculation should be distinct
* @param v the value
*/
abstract void add(Database database, boolean distinct, Value v);
abstract void add(Database database, int dataType, boolean distinct, Value v);
/**
* Get the aggregate result.
*
* @param database the database
* @param dataType the datatype of the computed result
* @param distinct if distinct is used
* @return the value
*/
abstract Value getValue(Database database, boolean distinct);
abstract Value getValue(Database database, int dataType, boolean distinct);
}
......@@ -15,26 +15,13 @@ import org.h2.value.ValueNull;
* Data stored while calculating an aggregate.
*/
class AggregateDataCount extends AggregateData {
private final int dataType;
private long count;
private ValueHashMap<AggregateDataCount> distinctValues;
/**
* @param dataType the datatype of the computed result
*/
AggregateDataCount(int dataType) {
this.dataType = dataType;
}
AggregateDataCount() {}
/**
* Add a value to this aggregate.
*
* @param database the database
* @param distinct if the calculation should be distinct
* @param v the value
*/
@Override
void add(Database database, boolean distinct, Value v) {
void add(Database database, int dataType, boolean distinct, Value v) {
if (v == ValueNull.INSTANCE) {
return;
}
......@@ -48,15 +35,8 @@ class AggregateDataCount extends AggregateData {
}
}
/**
* Get the aggregate result.
*
* @param database the database
* @param distinct if distinct is used
* @return the value
*/
@Override
Value getValue(Database database, boolean distinct) {
Value getValue(Database database, int dataType, boolean distinct) {
if (distinct) {
if (distinctValues != null) {
count = distinctValues.size();
......
......@@ -15,37 +15,20 @@ import org.h2.value.ValueNull;
* Data stored while calculating a COUNT(*) aggregate.
*/
class AggregateDataCountAll extends AggregateData {
private final int dataType;
private long count;
AggregateDataCountAll(int dataType) {
this.dataType = dataType;
}
AggregateDataCountAll() {}
/**
* Add a value to this aggregate.
*
* @param database the database
* @param distinct if the calculation should be distinct
* @param v the value
*/
@Override
void add(Database database, boolean distinct, Value v) {
void add(Database database, int dataType, boolean distinct, Value v) {
if (distinct) {
throw DbException.throwInternalError();
}
count++;
}
/**
* Get the aggregate result.
*
* @param database the database
* @param distinct if distinct is used
* @return the value
*/
@Override
Value getValue(Database database, boolean distinct) {
Value getValue(Database database, int dataType, boolean distinct) {
if (distinct) {
throw DbException.throwInternalError();
}
......
......@@ -21,7 +21,6 @@ import org.h2.value.ValueNull;
*/
class AggregateDataDefault extends AggregateData {
private final int aggregateType;
private final int dataType;
private long count;
private ValueHashMap<AggregateDataDefault> distinctValues;
private Value value;
......@@ -29,22 +28,13 @@ class AggregateDataDefault extends AggregateData {
/**
* @param aggregateType the type of the aggregate operation
* @param dataType the datatype of the computed result
*/
AggregateDataDefault(int aggregateType, int dataType) {
AggregateDataDefault(int aggregateType) {
this.aggregateType = aggregateType;
this.dataType = dataType;
}
/**
* Add a value to this aggregate.
*
* @param database the database
* @param distinct if the calculation should be distinct
* @param v the value
*/
@Override
void add(Database database, boolean distinct, Value v) {
void add(Database database, int dataType, boolean distinct, Value v) {
if (v == ValueNull.INSTANCE) {
return;
}
......@@ -122,18 +112,11 @@ class AggregateDataDefault extends AggregateData {
}
}
/**
* Get the aggregate result.
*
* @param database the database
* @param distinct if distinct is used
* @return the value
*/
@Override
Value getValue(Database database, boolean distinct) {
Value getValue(Database database, int dataType, boolean distinct) {
if (distinct) {
count = 0;
groupDistinct(database);
groupDistinct(database, dataType);
}
Value v = null;
switch (aggregateType) {
......@@ -193,13 +176,13 @@ class AggregateDataDefault extends AggregateData {
return a;
}
private void groupDistinct(Database database) {
private void groupDistinct(Database database, int dataType) {
if (distinctValues == null) {
return;
}
count = 0;
for (Value v : distinctValues.keys()) {
add(database, false, v);
add(database, dataType, false, v);
}
}
......
......@@ -22,7 +22,7 @@ class AggregateDataGroupConcat extends AggregateData {
AggregateDataGroupConcat() {}
@Override
void add(Database database, boolean distinct, Value v) {
void add(Database database, int dataType, boolean distinct, Value v) {
if (v == ValueNull.INSTANCE) {
return;
}
......@@ -40,9 +40,9 @@ class AggregateDataGroupConcat extends AggregateData {
}
@Override
Value getValue(Database database, boolean distinct) {
Value getValue(Database database, int dataType, boolean distinct) {
if (distinct) {
groupDistinct(database);
groupDistinct(database, dataType);
}
return null;
}
......@@ -51,12 +51,12 @@ class AggregateDataGroupConcat extends AggregateData {
return list;
}
private void groupDistinct(Database database) {
private void groupDistinct(Database database, int dataType) {
if (distinctValues == null) {
return;
}
for (Value v : distinctValues.keys()) {
add(database, false, v);
add(database, dataType, false, v);
}
}
}
......@@ -19,30 +19,20 @@ import org.h2.value.ValueLong;
* Data stored while calculating a HISTOGRAM aggregate.
*/
class AggregateDataHistogram extends AggregateData {
private final int dataType;
private long count;
private ValueHashMap<AggregateDataHistogram> distinctValues;
AggregateDataHistogram(int dataType) {
this.dataType = dataType;
}
AggregateDataHistogram() {}
/**
* Add a value to this aggregate.
*
* @param database the database
* @param distinct if the calculation should be distinct
* @param v the value
*/
@Override
void add(Database database, boolean distinct, Value v) {
void add(Database database, int dataType, boolean distinct, Value v) {
if (distinctValues == null) {
distinctValues = ValueHashMap.newInstance();
}
AggregateDataHistogram a = distinctValues.get(v);
if (a == null) {
if (distinctValues.size() < Constants.SELECTIVITY_DISTINCT_COUNT) {
a = new AggregateDataHistogram(dataType);
a = new AggregateDataHistogram();
distinctValues.put(v, a);
}
}
......@@ -51,18 +41,11 @@ class AggregateDataHistogram extends AggregateData {
}
}
/**
* Get the aggregate result.
*
* @param database the database
* @param distinct if distinct is used
* @return the value
*/
@Override
Value getValue(Database database, boolean distinct) {
Value getValue(Database database, int dataType, boolean distinct) {
if (distinct) {
count = 0;
groupDistinct(database);
groupDistinct(database, dataType);
}
ValueArray[] values = new ValueArray[distinctValues.size()];
int i = 0;
......@@ -84,13 +67,13 @@ class AggregateDataHistogram extends AggregateData {
return v.convertTo(dataType);
}
private void groupDistinct(Database database) {
private void groupDistinct(Database database, int dataType) {
if (distinctValues == null) {
return;
}
count = 0;
for (Value v : distinctValues.keys()) {
add(database, false, v);
add(database, dataType, false, v);
}
}
......
......@@ -16,17 +16,14 @@ import org.h2.value.ValueInt;
* Data stored while calculating a SELECTIVITY aggregate.
*/
class AggregateDataSelectivity extends AggregateData {
private final int dataType;
private long count;
private IntIntHashMap distinctHashes;
private double m2;
AggregateDataSelectivity(int dataType) {
this.dataType = dataType;
}
AggregateDataSelectivity() {}
@Override
void add(Database database, boolean distinct, Value v) {
void add(Database database, int dataType, boolean distinct, Value v) {
count++;
if (distinctHashes == null) {
distinctHashes = new IntIntHashMap();
......@@ -42,7 +39,7 @@ class AggregateDataSelectivity extends AggregateData {
}
@Override
Value getValue(Database database, boolean distinct) {
Value getValue(Database database, int dataType, boolean distinct) {
if (distinct) {
count = 0;
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论