use of io.druid.hll.HyperLogLogCollector in project druid by druid-io.
the class CardinalityBufferAggregator method aggregate.
@Override
public void aggregate(ByteBuffer buf, int position) {
// Save position, limit and restore later instead of allocating a new ByteBuffer object
final int oldPosition = buf.position();
final int oldLimit = buf.limit();
buf.limit(position + HyperLogLogCollector.getLatestNumBytesForDenseStorage());
buf.position(position);
try {
final HyperLogLogCollector collector = HyperLogLogCollector.makeCollector(buf);
if (byRow) {
CardinalityAggregator.hashRow(selectorPluses, collector);
} else {
CardinalityAggregator.hashValues(selectorPluses, collector);
}
} finally {
buf.limit(oldLimit);
buf.position(oldPosition);
}
}
use of io.druid.hll.HyperLogLogCollector in project druid by druid-io.
the class HyperUniquesSerdeForTest method getObjectStrategy.
@Override
public ObjectStrategy getObjectStrategy() {
return new ObjectStrategy<HyperLogLogCollector>() {
@Override
public Class<? extends HyperLogLogCollector> getClazz() {
return HyperLogLogCollector.class;
}
@Override
public HyperLogLogCollector fromByteBuffer(ByteBuffer buffer, int numBytes) {
final ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
readOnlyBuffer.limit(readOnlyBuffer.position() + numBytes);
return HyperLogLogCollector.makeCollector(readOnlyBuffer);
}
@Override
public byte[] toBytes(HyperLogLogCollector collector) {
if (collector == null) {
return new byte[] {};
}
ByteBuffer val = collector.toByteBuffer();
byte[] retVal = new byte[val.remaining()];
val.asReadOnlyBuffer().get(retVal);
return retVal;
}
@Override
public int compare(HyperLogLogCollector o1, HyperLogLogCollector o2) {
return comparator.compare(o1, o2);
}
};
}
use of io.druid.hll.HyperLogLogCollector in project druid by druid-io.
the class HyperUniquesSerdeForTest method getExtractor.
@Override
public ComplexMetricExtractor getExtractor() {
return new ComplexMetricExtractor() {
@Override
public Class<HyperLogLogCollector> extractedClass() {
return HyperLogLogCollector.class;
}
@Override
public HyperLogLogCollector extractValue(InputRow inputRow, String metricName) {
Object rawValue = inputRow.getRaw(metricName);
if (rawValue instanceof HyperLogLogCollector) {
return (HyperLogLogCollector) rawValue;
} else {
HyperLogLogCollector collector = HyperLogLogCollector.makeLatestCollector();
List<String> dimValues = inputRow.getDimension(metricName);
if (dimValues == null) {
return collector;
}
for (String dimensionValue : dimValues) {
collector.add(hashFn.hashBytes(StringUtils.toUtf8(dimensionValue)).asBytes());
}
return collector;
}
}
};
}
use of io.druid.hll.HyperLogLogCollector in project druid by druid-io.
the class MetricManipulatorFnsTest method constructorFeeder.
@Parameterized.Parameters(name = "{0}")
public static Iterable<Object[]> constructorFeeder() {
final ArrayList<Object[]> constructorArrays = new ArrayList<>();
final long longVal = 13789;
LongMinAggregator longMinAggregator = new LongMinAggregator(new TestLongColumnSelector() {
@Override
public long get() {
return longVal;
}
});
LongMinAggregatorFactory longMinAggregatorFactory = new LongMinAggregatorFactory(NAME, FIELD);
constructorArrays.add(new Object[] { longMinAggregatorFactory, longMinAggregator, longMinAggregator, longMinAggregator, longVal, longVal });
HyperUniquesAggregatorFactory hyperUniquesAggregatorFactory = new HyperUniquesAggregatorFactory(NAME, FIELD);
HyperLogLogCollector collector = HyperLogLogCollector.makeLatestCollector();
collector.add((short) 1, (byte) 5);
constructorArrays.add(new Object[] { hyperUniquesAggregatorFactory, collector, collector, collector.estimateCardinality(), collector.toByteArray(), collector });
LongSumAggregatorFactory longSumAggregatorFactory = new LongSumAggregatorFactory(NAME, FIELD);
LongSumAggregator longSumAggregator = new LongSumAggregator(new TestLongColumnSelector() {
@Override
public long get() {
return longVal;
}
});
constructorArrays.add(new Object[] { longSumAggregatorFactory, longSumAggregator, longSumAggregator, longSumAggregator, longVal, longVal });
for (Object[] argList : constructorArrays) {
Assert.assertEquals(String.format("Arglist %s is too short. Expected 6 found %d", Arrays.toString(argList), argList.length), 6, argList.length);
}
return constructorArrays;
}
Aggregations