@Override public boolean isEmpty() { return (gadget_ == null) || gadget_.isEmpty(); }
@Test public void wrapEmptyUpdateSketch() { final UpdateDoublesSketch s1 = DoublesSketch.builder().build(); final WritableMemory mem = WritableMemory.wrap(ByteBuffer.wrap(s1.toByteArray()).order(ByteOrder.nativeOrder())); final UpdateDoublesSketch s2 = DirectUpdateDoublesSketch.wrapInstance(mem); assertTrue(s2.isEmpty()); assertEquals(s2.getN(), 0); assertTrue(Double.isNaN(s2.getMinValue())); assertTrue(Double.isNaN(s2.getMaxValue())); s2.reset(); // empty: so should be a no-op assertEquals(s2.getN(), 0); }
@Test public void checkPutCombinedBuffer() { final int k = PreambleUtil.DEFAULT_K; final int cap = 32 + ((2 * k) << 3); WritableMemory mem = WritableMemory.wrap(new byte[cap]); final UpdateDoublesSketch qs = DoublesSketch.builder().setK(k).build(mem); mem = qs.getMemory(); assertEquals(mem.getCapacity(), cap); assertTrue(qs.isEmpty()); final int n = 16; final double[] data = new double[n]; for (int i = 0; i < n; ++i) { data[i] = i + 1; } qs.putBaseBufferCount(n); qs.putN(n); qs.putCombinedBuffer(data); final double[] combBuf = qs.getCombinedBuffer(); assertEquals(combBuf, data); // shouldn't have changed min/max values assertTrue(Double.isNaN(qs.getMinValue())); assertTrue(Double.isNaN(qs.getMaxValue())); }
@Override public boolean isEmpty() { return (gadget_ == null) || gadget_.isEmpty(); }
final Memory mem = Memory.wrap(s1.toByteArray()); UpdateDoublesSketch s2 = (UpdateDoublesSketch) DoublesSketch.wrap(mem); Assert.assertTrue(s2.isEmpty());
@Test public void checkGrowFromWrappedEmptySketch() { final int k = 16; final int n = 0; final int initBytes = DoublesSketch.getUpdatableStorageBytes(k, n); //8 bytes final UpdateDoublesSketch usk1 = DoublesSketch.builder().setK(k).build(); final Memory origSketchMem = Memory.wrap(usk1.toByteArray()); try (WritableDirectHandle memHandle = WritableMemory.allocateDirect(initBytes)) { WritableMemory mem = memHandle.get(); origSketchMem.copyTo(0, mem, 0, initBytes); UpdateDoublesSketch usk2 = DirectUpdateDoublesSketch.wrapInstance(mem); assertTrue(mem.isSameResource(usk2.getMemory())); assertEquals(mem.getCapacity(), initBytes); assertTrue(mem.isDirect()); assertTrue(usk2.isEmpty()); //update the sketch forcing it to grow on-heap for (int i = 1; i <= 5; i++) { usk2.update(i); } assertEquals(usk2.getN(), 5); WritableMemory mem2 = usk2.getMemory(); assertFalse(mem.isSameResource(mem2)); assertFalse(mem2.isDirect()); //should now be on-heap final int expectedSize = COMBINED_BUFFER + ((2 * k) << 3); assertEquals(mem2.getCapacity(), expectedSize); } }
@Test public void checkLimitedMemoryScenarios() { //Requesting application final int k = 128; final int u = 40 * k; final int initBytes = ((2 * k) + 4) << 3; //just the BB //########## Owning Implementation // This part would actually be part of the Memory owning implemention so it is faked here try (WritableDirectHandle wdh = WritableMemory.allocateDirect(initBytes)) { final WritableMemory wmem = wdh.get(); println("Initial mem size: " + wmem.getCapacity()); //########## Receiving Application // The receiving application has been given wmem to use for a sketch, // but alas, it is not ultimately large enough. final UpdateDoublesSketch usk1 = DoublesSketch.builder().setK(k).build(wmem); assertTrue(usk1.isEmpty()); //Load the sketch for (int i = 0; i < u; i++) { // The sketch uses The MemoryRequest, acquired from wmem, to acquire more memory as // needed, and requests via the MemoryRequest to free the old allocations. usk1.update(i); } final double result = usk1.getQuantile(0.5); println("Result: " + result); assertEquals(result, u / 2.0, 0.05 * u); //Success //########## Owning Implementation //The actual Memory has been re-allocated several times, // so the above wmem reference is invalid. println("\nFinal mem size: " + wmem.getCapacity()); } }
if (sketch.isEmpty()) { insertFlags(dstMem, flags | EMPTY_FLAG_MASK); } else {
@SuppressWarnings("null") static UpdateDoublesSketch updateLogic(final int myMaxK, final UpdateDoublesSketch myQS, final DoublesSketch other) { int sw1 = ((myQS == null) ? 0 : myQS.isEmpty() ? 4 : 8); sw1 |= ((other == null) ? 0 : other.isEmpty() ? 1 : 2); int outCase = 0; //0=null, 1=NOOP, 2=copy, 3=merge if (myQS.isEmpty()) { if (myQS.isDirect()) { final WritableMemory mem = myQS.getMemory(); //myQS is empty, ok to reconfigure
if (sketch.isEmpty()) { insertFlags(dstMem, flags | EMPTY_FLAG_MASK); } else {
@SuppressWarnings("null") static UpdateDoublesSketch updateLogic(final int myMaxK, final UpdateDoublesSketch myQS, final DoublesSketch other) { int sw1 = ((myQS == null) ? 0 : myQS.isEmpty() ? 4 : 8); sw1 |= ((other == null) ? 0 : other.isEmpty() ? 1 : 2); int outCase = 0; //0=null, 1=NOOP, 2=copy, 3=merge if (myQS.isEmpty()) { if (myQS.isDirect()) { final WritableMemory mem = myQS.getMemory(); //myQS is empty, ok to reconfigure