@Override public UpdateDoublesSketch getResult(final WritableMemory dstMem) { final long memCapBytes = dstMem.getCapacity(); if (gadget_ == null) { if (memCapBytes < DoublesSketch.getUpdatableStorageBytes(0, 0)) { throw new SketchesArgumentException("Insufficient capacity for result: " + memCapBytes); } return DirectUpdateDoublesSketch.newInstance(maxK_, dstMem); } gadget_.putMemory(dstMem, false); return DirectUpdateDoublesSketch.wrapInstance(dstMem); }
@Test public void testIt() { java.nio.ByteBuffer bb = java.nio.ByteBuffer.allocate(1<<20).order(ByteOrder.nativeOrder()); WritableMemory mem = WritableMemory.wrap(bb); int k = 1024; DoublesSketch qsk = new DoublesSketchBuilder().setK(k).build(); DoublesUnion u1 = DoublesUnion.heapify(qsk); u1.getResult().putMemory(mem); DoublesUnion u2 = DoublesUnion.heapify(mem); DoublesSketch qsk2 = u2.getResult(); assertTrue(qsk2.isEmpty()); }
@Test(expectedExceptions = SketchesArgumentException.class) public void checkPutMemoryTooSmall() { UpdateDoublesSketch qs1 = DoublesSketch.builder().build(); //k = 128 for (int i=0; i<1000; i++) { qs1.update(i); } int bytes = qs1.getCompactStorageBytes(); WritableMemory dstMem = WritableMemory.wrap(new byte[bytes-1]); //too small qs1.putMemory(dstMem); }
@Test public void checkPutMemory() { UpdateDoublesSketch qs1 = DoublesSketch.builder().build(); //k = 128 for (int i=0; i<1000; i++) { qs1.update(i); } int bytes = qs1.getUpdatableStorageBytes(); WritableMemory dstMem = WritableMemory.wrap(new byte[bytes]); qs1.putMemory(dstMem, false); Memory srcMem = dstMem; DoublesSketch qs2 = DoublesSketch.heapify(srcMem); assertEquals(qs1.getMinValue(), qs2.getMinValue(), 0.0); assertEquals(qs1.getMaxValue(), qs2.getMaxValue(), 0.0); }
@Test public void checkBuilds() { UpdateDoublesSketch qs1 = DoublesSketch.builder().build(); for (int i=0; i<1000; i++) { qs1.update(i); } int bytes = qs1.getCompactStorageBytes(); WritableMemory dstMem = WritableMemory.wrap(new byte[bytes]); qs1.putMemory(dstMem); Memory srcMem = dstMem; DoublesUnionBuilder bldr = new DoublesUnionBuilder(); bldr.setMaxK(128); DoublesUnion union = bldr.build(); //virgin union union = DoublesUnion.heapify(srcMem); DoublesSketch qs2 = union.getResult(); assertEquals(qs1.getCompactStorageBytes(), qs2.getCompactStorageBytes()); union = DoublesUnion.heapify(qs2); DoublesSketch qs3 = union.getResult(); assertEquals(qs2.getCompactStorageBytes(), qs3.getCompactStorageBytes()); assertFalse(qs2 == qs3); }
@Test public void checkDeprecated1() { UpdateDoublesSketch qs1 = DoublesSketch.builder().build(); for (int i=0; i<1000; i++) { qs1.update(i); } int bytes = qs1.getCompactStorageBytes(); WritableMemory dstMem = WritableMemory.wrap(new byte[bytes]); qs1.putMemory(dstMem); Memory srcMem = dstMem; DoublesUnionBuilder bldr = new DoublesUnionBuilder(); bldr.setMaxK(128); DoublesUnion union = bldr.build(); //virgin union union = DoublesUnion.heapify(srcMem); //heapify DoublesSketch qs2 = union.getResult(); assertEquals(qs1.getCompactStorageBytes(), qs2.getCompactStorageBytes()); assertEquals(qs1.getUpdatableStorageBytes(), qs2.getUpdatableStorageBytes()); union = DoublesUnion.heapify(qs2); //heapify again DoublesSketch qs3 = union.getResult(); assertEquals(qs2.getCompactStorageBytes(), qs3.getCompactStorageBytes()); assertEquals(qs2.getUpdatableStorageBytes(), qs3.getUpdatableStorageBytes()); assertFalse(qs2 == qs3); //different objects }
@Override public UpdateDoublesSketch getResult(final WritableMemory dstMem) { final long memCapBytes = dstMem.getCapacity(); if (gadget_ == null) { if (memCapBytes < DoublesSketch.getUpdatableStorageBytes(0, 0)) { throw new SketchesArgumentException("Insufficient capacity for result: " + memCapBytes); } return DirectUpdateDoublesSketch.newInstance(maxK_, dstMem); } gadget_.putMemory(dstMem, false); return DirectUpdateDoublesSketch.wrapInstance(dstMem); }