提交 dfa90cbc 编写于 作者: V Vlad Ilyushchenko

#6 replaced arrays with explicit variables for clarity and performance.

上级 0e002897
/*
* Copyright (c) 2014. Vlad Ilyushchenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.nfsdb.lang.cst.impl.agg;
import com.nfsdb.factory.configuration.ColumnMetadata;
public abstract class AbstractAggregatorFunction implements AggregatorFunction {
private int map[];
@Override
public final ColumnMetadata[] getColumns() {
ColumnMetadata[] m = getColumnsInternal();
map = new int[m.length];
return m;
}
@Override
public final void mapColumn(int k, int i) {
map[k] = i;
}
protected abstract ColumnMetadata[] getColumnsInternal();
protected int map(int x) {
return map[x];
}
}
/*
* Copyright (c) 2014. Vlad Ilyushchenko
* Copyright (c) 2014-2015. Vlad Ilyushchenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -20,25 +20,27 @@ import com.nfsdb.factory.configuration.ColumnMetadata;
import com.nfsdb.lang.cst.impl.qry.Record;
import com.nfsdb.lang.cst.impl.qry.RecordSource;
public abstract class AbstractSingleColumnAggregatorFunction extends AbstractAggregatorFunction {
public abstract class AbstractSingleColumnAggregatorFunction implements AggregatorFunction {
private final ColumnMetadata[] meta = new ColumnMetadata[1];
private int columnIndex;
protected int recordIndex;
protected int valueIndex;
public AbstractSingleColumnAggregatorFunction(ColumnMetadata meta) {
this.meta[0] = meta;
}
@Override
protected final ColumnMetadata[] getColumnsInternal() {
public ColumnMetadata[] getColumns() {
return meta;
}
protected final int getColumnIndex() {
return columnIndex;
@Override
public void mapColumn(int k, int i) {
valueIndex = i;
}
@Override
public final void prepareSource(RecordSource<? extends Record> source) {
this.columnIndex = source.getMetadata().getColumnIndex(meta[0].name);
public void prepareSource(RecordSource<? extends Record> source) {
this.recordIndex = source.getMetadata().getColumnIndex(meta[0].name);
}
}
......@@ -22,30 +22,24 @@ import com.nfsdb.factory.configuration.ColumnMetadata;
import com.nfsdb.lang.cst.impl.qry.Record;
import com.nfsdb.lang.cst.impl.qry.RecordSource;
public class CountIntAggregatorFunction extends AbstractAggregatorFunction {
public class CountIntAggregatorFunction extends AbstractSingleColumnAggregatorFunction {
private final ColumnMetadata[] meta = new ColumnMetadata[1];
public CountIntAggregatorFunction(String name) {
this.meta[0] = new ColumnMetadata().setName(name).setType(ColumnType.INT);
}
@Override
protected ColumnMetadata[] getColumnsInternal() {
return meta;
super(new ColumnMetadata().setName(name).setType(ColumnType.INT));
}
@Override
public void calculate(Record rec, MapValues values) {
if (values.isNew()) {
values.putInt(map(0), 1);
values.putInt(valueIndex, 1);
} else {
values.putInt(map(0), values.getInt(map(0)) + 1);
values.putInt(valueIndex, values.getInt(valueIndex) + 1);
}
}
@Override
public void prepareSource(RecordSource<? extends Record> source) {
// do not call parent method, which will be trying to lookup column in record source.
}
}
......@@ -22,32 +22,23 @@ import com.nfsdb.factory.configuration.ColumnMetadata;
import com.nfsdb.lang.cst.impl.qry.Record;
import com.nfsdb.lang.cst.impl.qry.RecordSource;
public class CountLongAggregatorFunction extends AbstractAggregatorFunction {
private final ColumnMetadata[] meta = new ColumnMetadata[1];
public class CountLongAggregatorFunction extends AbstractSingleColumnAggregatorFunction {
public CountLongAggregatorFunction(String name) {
this.meta[0] = new ColumnMetadata();
this.meta[0].name = name;
this.meta[0].type = ColumnType.LONG;
}
@Override
protected ColumnMetadata[] getColumnsInternal() {
return meta;
super(new ColumnMetadata().setName(name).setType(ColumnType.LONG));
}
@Override
public void calculate(Record rec, MapValues values) {
if (values.isNew()) {
values.putLong(map(0), 1);
values.putLong(valueIndex, 1);
} else {
values.putLong(map(0), values.getLong(map(0)) + 1);
values.putLong(valueIndex, values.getLong(valueIndex) + 1);
}
}
@Override
public void prepareSource(RecordSource<? extends Record> source) {
// do not call parent method, which will be trying to lookup column in record source.
}
}
/*
* Copyright (c) 2014. Vlad Ilyushchenko
* Copyright (c) 2014-2015. Vlad Ilyushchenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -29,7 +29,7 @@ public class FirstDoubleAggregationFunction extends AbstractSingleColumnAggregat
@Override
public void calculate(Record rec, MapValues values) {
if (values.isNew()) {
values.putDouble(map(0), rec.getDouble(getColumnIndex()));
values.putDouble(valueIndex, rec.getDouble(recordIndex));
}
}
}
......@@ -29,7 +29,7 @@ public class FirstLongAggregationFunction extends AbstractSingleColumnAggregator
@Override
public void calculate(Record rec, MapValues values) {
if (values.isNew()) {
values.putLong(map(0), rec.getLong(getColumnIndex()));
values.putLong(valueIndex, rec.getLong(recordIndex));
}
}
}
/*
* Copyright (c) 2014. Vlad Ilyushchenko
* Copyright (c) 2014-2015. Vlad Ilyushchenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -28,6 +28,6 @@ public class LastDoubleAggregationFunction extends AbstractSingleColumnAggregato
@Override
public void calculate(Record rec, MapValues values) {
values.putDouble(map(0), rec.getDouble(getColumnIndex()));
values.putDouble(valueIndex, rec.getDouble(recordIndex));
}
}
......@@ -28,6 +28,6 @@ public class LastLongAggregationFunction extends AbstractSingleColumnAggregatorF
@Override
public void calculate(Record rec, MapValues values) {
values.putLong(map(0), rec.getLong(getColumnIndex()));
values.putLong(valueIndex, rec.getLong(recordIndex));
}
}
......@@ -28,10 +28,9 @@ public class SumDoubleAggregationFunction extends AbstractSingleColumnAggregator
@Override
public void calculate(Record rec, MapValues values) {
if (values.isNew()) {
values.putDouble(map(0), rec.getDouble(getColumnIndex()));
values.putDouble(valueIndex, rec.getDouble(recordIndex));
} else {
int c = map(0);
values.putDouble(c, values.getDouble(c) + rec.getDouble(getColumnIndex()));
values.putDouble(valueIndex, values.getDouble(valueIndex) + rec.getDouble(recordIndex));
}
}
}
......@@ -28,10 +28,9 @@ public class SumIntAggregationFunction extends AbstractSingleColumnAggregatorFun
@Override
public void calculate(Record rec, MapValues values) {
if (values.isNew()) {
values.putInt(map(0), rec.getInt(getColumnIndex()));
values.putInt(valueIndex, rec.getInt(recordIndex));
} else {
int c = map(0);
values.putInt(c, values.getInt(c) + rec.getInt(getColumnIndex()));
values.putInt(valueIndex, values.getInt(valueIndex) + rec.getInt(recordIndex));
}
}
}
......@@ -29,10 +29,9 @@ public class SumIntToLongAggregationFunction extends AbstractSingleColumnAggrega
@Override
public void calculate(Record rec, MapValues values) {
if (values.isNew()) {
values.putLong(map(0), rec.getInt(getColumnIndex()));
values.putLong(valueIndex, rec.getInt(recordIndex));
} else {
int c = map(0);
values.putLong(c, values.getLong(c) + rec.getInt(getColumnIndex()));
values.putLong(valueIndex, values.getLong(valueIndex) + rec.getInt(recordIndex));
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册