public static ArrayOfDoublesSketch deserializeFromByteArray(final byte[] data) { final Memory mem = Memory.wrap(data); return ArrayOfDoublesSketches.wrapSketch(mem); }
@Override public ArrayOfDoublesSketch fromByteBuffer(final ByteBuffer buffer, final int numBytes) { return ArrayOfDoublesSketches.wrapSketch(Memory.wrap(buffer, ByteOrder.LITTLE_ENDIAN).region(buffer.position(), numBytes)); }
/** * This method uses locks because it can be used during indexing, * and Druid can call aggregate() and get() concurrently * https://github.com/apache/incubator-druid/pull/3956 * The returned sketch is a separate instance of ArrayOfDoublesCompactSketch * representing the current state of the aggregation, and is not affected by consequent * aggregate() calls */ @Override public Object get(final ByteBuffer buf, final int position) { final WritableMemory mem = WritableMemory.wrap(buf, ByteOrder.LITTLE_ENDIAN); final WritableMemory region = mem.writableRegion(position, maxIntermediateSize); final Lock lock = stripedLock.getAt(lockIndex(position)).readLock(); lock.lock(); try { final ArrayOfDoublesUpdatableSketch sketch = (ArrayOfDoublesUpdatableSketch) ArrayOfDoublesSketches .wrapSketch(region); return sketch.compact(); } finally { lock.unlock(); } }
/** * Wrap the given Memory as an ArrayOfDoublesSketch * @param mem the given Memory * @return an ArrayOfDoublesSketch */ public static ArrayOfDoublesSketch wrapSketch(final Memory mem) { return wrapSketch(mem, DEFAULT_UPDATE_SEED); }
/** * Wrap the given Memory as an ArrayOfDoublesSketch * @param mem the given Memory * @return an ArrayOfDoublesSketch */ public static ArrayOfDoublesSketch wrapSketch(final Memory mem) { return wrapSketch(mem, DEFAULT_UPDATE_SEED); }
@Test public void serializeDeserializeEstimation() { ArrayOfDoublesUpdatableSketch us = new ArrayOfDoublesUpdatableSketchBuilder().build(WritableMemory.wrap(new byte[1000000])); for (int i = 0; i < 8192; i++) { us.update(i, new double[] {1.0}); } ArrayOfDoublesCompactSketch sketch1 = us.compact(WritableMemory.wrap(new byte[1000000])); ArrayOfDoublesSketch sketch2 = ArrayOfDoublesSketches.wrapSketch(WritableMemory.wrap(sketch1.toByteArray())); Assert.assertFalse(sketch2.isEmpty()); Assert.assertTrue(sketch2.isEstimationMode()); Assert.assertEquals(sketch2.getEstimate(), sketch1.getEstimate()); Assert.assertEquals(sketch2.getThetaLong(), sketch1.getThetaLong()); }
@Test public void serializeDeserializeSmallExact() { ArrayOfDoublesUpdatableSketch us = new ArrayOfDoublesUpdatableSketchBuilder().build(WritableMemory.wrap(new byte[1000000])); us.update("a", new double[] {1.0}); us.update("b", new double[] {1.0}); us.update("c", new double[] {1.0}); ArrayOfDoublesCompactSketch sketch1 = us.compact(WritableMemory.wrap(new byte[1000000])); ArrayOfDoublesSketch sketch2 = ArrayOfDoublesSketches.wrapSketch(WritableMemory.wrap(sketch1.toByteArray())); Assert.assertFalse(sketch2.isEmpty()); Assert.assertFalse(sketch2.isEstimationMode()); Assert.assertEquals(sketch2.getEstimate(), 3.0); Assert.assertEquals(sketch2.getLowerBound(1), 3.0); Assert.assertEquals(sketch2.getUpperBound(1), 3.0); Assert.assertEquals(sketch2.getRetainedEntries(), 3); Assert.assertEquals(sketch2.getThetaLong(), Long.MAX_VALUE); Assert.assertEquals(sketch2.getTheta(), 1.0); double[][] values = sketch2.getValues(); Assert.assertEquals(values.length, 3); for (double[] array: values) { Assert.assertEquals(array[0], 1.0); } }
@Test public void wrapAndTryUpdatingSketch() { ArrayOfDoublesUpdatableSketch sketch1 = new ArrayOfDoublesUpdatableSketchBuilder().build(); sketch1.update(1, new double[] {1}); ArrayOfDoublesUpdatableSketch sketch2 = (ArrayOfDoublesUpdatableSketch) ArrayOfDoublesSketches.wrapSketch(Memory.wrap(sketch1.toByteArray())); Assert.assertEquals(sketch2.getEstimate(), 1.0); sketch2.toByteArray(); boolean thrown = false; try { sketch2.update(2, new double[] {1}); } catch (SketchesReadOnlyException e) { thrown = true; } try { sketch2.trim(); } catch (SketchesReadOnlyException e) { thrown = true; } Assert.assertTrue(thrown); }
@Test(expectedExceptions = SketchesArgumentException.class) public void deserializeWithWrongSeed() { ArrayOfDoublesUpdatableSketch us = new ArrayOfDoublesUpdatableSketchBuilder().build(WritableMemory.wrap(new byte[1000000])); for (int i = 0; i < 8192; i++) { us.update(i, new double[] {1.0}); } ArrayOfDoublesCompactSketch sketch1 = us.compact(WritableMemory.wrap(new byte[1000000])); ArrayOfDoublesSketches.wrapSketch(WritableMemory.wrap(sketch1.toByteArray()), 123); }
public static ArrayOfDoublesSketch deserializeFromByteArray(final byte[] data) { final Memory mem = Memory.wrap(data); return ArrayOfDoublesSketches.wrapSketch(mem); }
@Override public ArrayOfDoublesSketch fromByteBuffer(final ByteBuffer buffer, final int numBytes) { return ArrayOfDoublesSketches.wrapSketch(Memory.wrap(buffer).region(buffer.position(), numBytes)); }
/** * This method uses locks because it can be used during indexing, * and Druid can call aggregate() and get() concurrently * https://github.com/apache/incubator-druid/pull/3956 * The returned sketch is a separate instance of ArrayOfDoublesCompactSketch * representing the current state of the aggregation, and is not affected by consequent * aggregate() calls */ @Override public Object get(final ByteBuffer buf, final int position) { final WritableMemory mem = WritableMemory.wrap(buf); final WritableMemory region = mem.writableRegion(position, maxIntermediateSize); final Lock lock = stripedLock.getAt(lockIndex(position)).readLock(); lock.lock(); try { final ArrayOfDoublesUpdatableSketch sketch = (ArrayOfDoublesUpdatableSketch) ArrayOfDoublesSketches .wrapSketch(region); return sketch.compact(); } finally { lock.unlock(); } }