Search in sources :

Example 1 with ObjectStrategy

use of io.druid.segment.data.ObjectStrategy in project druid by druid-io.

the class ApproximateHistogramFoldingSerde method getObjectStrategy.

public ObjectStrategy getObjectStrategy() {
    return new ObjectStrategy<ApproximateHistogram>() {

        @Override
        public Class<? extends ApproximateHistogram> getClazz() {
            return ApproximateHistogram.class;
        }

        @Override
        public ApproximateHistogram fromByteBuffer(ByteBuffer buffer, int numBytes) {
            final ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
            readOnlyBuffer.limit(readOnlyBuffer.position() + numBytes);
            return ApproximateHistogram.fromBytes(readOnlyBuffer);
        }

        @Override
        public byte[] toBytes(ApproximateHistogram h) {
            if (h == null) {
                return new byte[] {};
            }
            return h.toBytes();
        }

        @Override
        public int compare(ApproximateHistogram o1, ApproximateHistogram o2) {
            return comparator.compare(o1, o2);
        }
    };
}
Also used : ObjectStrategy(io.druid.segment.data.ObjectStrategy) ByteBuffer(java.nio.ByteBuffer)

Example 2 with ObjectStrategy

use of io.druid.segment.data.ObjectStrategy in project druid by druid-io.

the class VarianceSerdeTest method testSerde.

@Test
public void testSerde() {
    Random r = new Random();
    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());
    }
}
Also used : Random(java.util.Random) ObjectStrategy(io.druid.segment.data.ObjectStrategy) Test(org.junit.Test)

Example 3 with ObjectStrategy

use of io.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<? 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);
        }
    };
}
Also used : HyperLogLogCollector(io.druid.hll.HyperLogLogCollector) ObjectStrategy(io.druid.segment.data.ObjectStrategy) ByteBuffer(java.nio.ByteBuffer)

Example 4 with ObjectStrategy

use of io.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 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);
        }
    };
}
Also used : HyperLogLogCollector(io.druid.hll.HyperLogLogCollector) ObjectStrategy(io.druid.segment.data.ObjectStrategy) ByteBuffer(java.nio.ByteBuffer)

Example 5 with ObjectStrategy

use of io.druid.segment.data.ObjectStrategy in project druid by druid-io.

the class VarianceSerde method getObjectStrategy.

@Override
public ObjectStrategy getObjectStrategy() {
    return new ObjectStrategy<VarianceAggregatorCollector>() {

        @Override
        public Class<? extends VarianceAggregatorCollector> getClazz() {
            return VarianceAggregatorCollector.class;
        }

        @Override
        public VarianceAggregatorCollector fromByteBuffer(ByteBuffer buffer, int numBytes) {
            final ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
            readOnlyBuffer.limit(readOnlyBuffer.position() + numBytes);
            return VarianceAggregatorCollector.from(readOnlyBuffer);
        }

        @Override
        public byte[] toBytes(VarianceAggregatorCollector collector) {
            return collector == null ? new byte[] {} : collector.toByteArray();
        }

        @Override
        public int compare(VarianceAggregatorCollector o1, VarianceAggregatorCollector o2) {
            return comparator.compare(o1, o2);
        }
    };
}
Also used : ObjectStrategy(io.druid.segment.data.ObjectStrategy) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ObjectStrategy (io.druid.segment.data.ObjectStrategy)5 ByteBuffer (java.nio.ByteBuffer)4 HyperLogLogCollector (io.druid.hll.HyperLogLogCollector)2 Random (java.util.Random)1 Test (org.junit.Test)1