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