use of org.apache.druid.segment.data.ObjectStrategy in project druid by druid-io.
the class VarianceSerdeTest method testSerde.
@Test
public void testSerde() {
Random r = ThreadLocalRandom.current();
VarianceAggregatorCollector holder = new VarianceAggregatorCollector();
ObjectStrategy strategy = new VarianceSerde().getObjectStrategy();
Assert.assertEquals(VarianceAggregatorCollector.class, strategy.getClazz());
for (int i = 0; i < 100; i++) {
byte[] array = strategy.toBytes(holder);
Assert.assertArrayEquals(array, holder.toByteArray());
Assert.assertEquals(holder, strategy.fromByteBuffer(ByteBuffer.wrap(array), array.length));
holder.add(r.nextFloat());
}
}
use of org.apache.druid.segment.data.ObjectStrategy 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 ByteArrays.EMPTY_ARRAY;
}
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 org.apache.druid.segment.data.ObjectStrategy in project druid by druid-io.
the class HyperUniquesSerde method getObjectStrategy.
@Override
public ObjectStrategy getObjectStrategy() {
return new ObjectStrategy<HyperLogLogCollector>() {
@Override
public Class<HyperLogLogCollector> getClazz() {
return HyperLogLogCollector.class;
}
@Override
public HyperLogLogCollector fromByteBuffer(ByteBuffer buffer, int numBytes) {
// make a copy of buffer, because the given buffer is not duplicated in HyperLogLogCollector.makeCollector() and
// stored in a field.
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 org.apache.druid.segment.data.ObjectStrategy in project druid by druid-io.
the class SerializablePairLongStringSerde method getObjectStrategy.
@Override
public ObjectStrategy getObjectStrategy() {
return new ObjectStrategy<SerializablePairLongString>() {
@Override
public int compare(@Nullable SerializablePairLongString o1, @Nullable SerializablePairLongString o2) {
return StringFirstAggregatorFactory.VALUE_COMPARATOR.compare(o1, o2);
}
@Override
public Class<? extends SerializablePairLongString> getClazz() {
return SerializablePairLongString.class;
}
@Override
public SerializablePairLongString fromByteBuffer(ByteBuffer buffer, int numBytes) {
final ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
long lhs = readOnlyBuffer.getLong();
int stringSize = readOnlyBuffer.getInt();
String lastString = null;
if (stringSize > 0) {
byte[] stringBytes = new byte[stringSize];
readOnlyBuffer.get(stringBytes, 0, stringSize);
lastString = StringUtils.fromUtf8(stringBytes);
}
return new SerializablePairLongString(lhs, lastString);
}
@Override
public byte[] toBytes(SerializablePairLongString val) {
String rhsString = val.rhs;
ByteBuffer bbuf;
if (rhsString != null) {
byte[] rhsBytes = StringUtils.toUtf8(rhsString);
bbuf = ByteBuffer.allocate(Long.BYTES + Integer.BYTES + rhsBytes.length);
bbuf.putLong(val.lhs);
bbuf.putInt(Long.BYTES, rhsBytes.length);
bbuf.position(Long.BYTES + Integer.BYTES);
bbuf.put(rhsBytes);
} else {
bbuf = ByteBuffer.allocate(Long.BYTES + Integer.BYTES);
bbuf.putLong(val.lhs);
bbuf.putInt(Long.BYTES, 0);
}
return bbuf.array();
}
};
}
Aggregations