Search in sources :

Example 66 with Memory

use of com.yahoo.memory.Memory in project sketches-core by DataSketches.

the class SketchesTest method getCompactSketch.

private static Memory getCompactSketch(int k, int from, int to) {
    UpdateSketch sk1 = updateSketchBuilder().setNominalEntries(k).build();
    for (int i = from; i < to; i++) sk1.update(i);
    CompactSketch csk = sk1.compact(true, null);
    byte[] sk1bytes = csk.toByteArray();
    Memory mem = Memory.wrap(sk1bytes);
    return mem;
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory)

Example 67 with Memory

use of com.yahoo.memory.Memory in project sketches-core by DataSketches.

the class DoublesUnionBuilderTest method checkBuilds.

@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);
    //virgin union
    DoublesUnion union = bldr.build();
    union = DoublesUnionBuilder.heapify(srcMem);
    DoublesSketch qs2 = union.getResult();
    assertEquals(qs1.getCompactStorageBytes(), qs2.getCompactStorageBytes());
    union = DoublesUnionBuilder.heapify(qs2);
    DoublesSketch qs3 = union.getResult();
    assertEquals(qs2.getCompactStorageBytes(), qs3.getCompactStorageBytes());
    assertFalse(qs2 == qs3);
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 68 with Memory

use of com.yahoo.memory.Memory in project sketches-core by DataSketches.

the class DoublesUnionBuilderTest method checkDeprecated1.

@SuppressWarnings("deprecation")
@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);
    //virgin union
    DoublesUnion union = bldr.build();
    //heapify
    union = DoublesUnionBuilder.heapify(srcMem);
    DoublesSketch qs2 = union.getResult();
    assertEquals(qs1.getCompactStorageBytes(), qs2.getCompactStorageBytes());
    assertEquals(qs1.getUpdatableStorageBytes(), qs2.getUpdatableStorageBytes());
    //heapify again
    union = DoublesUnionBuilder.heapify(qs2);
    DoublesSketch qs3 = union.getResult();
    assertEquals(qs2.getCompactStorageBytes(), qs3.getCompactStorageBytes());
    assertEquals(qs2.getUpdatableStorageBytes(), qs3.getUpdatableStorageBytes());
    //different objects
    assertFalse(qs2 == qs3);
    DoublesUnion union2 = DoublesUnionBuilder.copyBuild(qs3);
    DoublesSketch qs4 = union2.getResult();
    assertEquals(qs3.getCompactStorageBytes(), qs4.getCompactStorageBytes());
    assertEquals(qs3.getUpdatableStorageBytes(), qs4.getUpdatableStorageBytes());
    //different objects
    assertFalse(qs3 == qs4);
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 69 with Memory

use of com.yahoo.memory.Memory in project sketches-core by DataSketches.

the class DoublesUnionImplTest method checkUnionQuantiles.

@Test
public void checkUnionQuantiles() {
    final int k = 128;
    final int n1 = k * 13;
    final int n2 = k * 8 + k / 2;
    final int n = n1 + n2;
    // assuming k = 128
    final double errorTolerance = 0.0175 * n;
    final UpdateDoublesSketch sketch1 = buildAndLoadQS(k, n1);
    final CompactDoublesSketch sketch2 = buildAndLoadQS(k, n2, n1).compact();
    //virgin 256
    final DoublesUnion union = DoublesUnion.builder().setMaxK(256).build();
    union.update(sketch2);
    union.update(sketch1);
    final Memory mem = Memory.wrap(union.getResult().toByteArray(true));
    final DoublesSketch result = DoublesSketch.wrap(mem);
    assertEquals(result.getN(), n1 + n2);
    assertEquals(result.getK(), k);
    for (double fraction = 0.05; fraction < 1.0; fraction += 0.05) {
        assertEquals(result.getQuantile(fraction), fraction * n, errorTolerance);
    }
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 70 with Memory

use of com.yahoo.memory.Memory in project sketches-core by DataSketches.

the class DoublesUnionImplTest method checkUpdateMemory.

@Test
public void checkUpdateMemory() {
    DoublesSketch qs1 = buildAndLoadQS(256, 1000);
    int bytes = qs1.getCompactStorageBytes();
    WritableMemory dstMem = WritableMemory.wrap(new byte[bytes]);
    qs1.putMemory(dstMem);
    Memory srcMem = dstMem;
    //virgin
    DoublesUnion union = DoublesUnion.builder().build();
    union.update(srcMem);
    for (int i = 1000; i < 2000; i++) union.update(i);
    DoublesSketch qs2 = union.getResult();
    assertEquals(qs2.getMaxValue(), 1999, 0.0);
    String s = union.toString();
    //enable printing to see
    println(s);
    //sets to null
    union.reset();
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Aggregations

Memory (com.yahoo.memory.Memory)141 Test (org.testng.annotations.Test)121 WritableMemory (com.yahoo.memory.WritableMemory)111 ArrayOfLongsSerDe (com.yahoo.sketches.ArrayOfLongsSerDe)11 ArrayOfStringsSerDe (com.yahoo.sketches.ArrayOfStringsSerDe)10 SketchesArgumentException (com.yahoo.sketches.SketchesArgumentException)10 SketchesReadOnlyException (com.yahoo.sketches.SketchesReadOnlyException)6 MemoryRegion (com.yahoo.memory.MemoryRegion)2 NativeMemory (com.yahoo.memory.NativeMemory)2 ArrayOfNumbersSerDe (com.yahoo.sketches.ArrayOfNumbersSerDe)2 WritableDirectHandle (com.yahoo.memory.WritableDirectHandle)1 Util.checkIsCompactMemory (com.yahoo.sketches.quantiles.Util.checkIsCompactMemory)1 CompactSketch.createCompactSketch (com.yahoo.sketches.theta.CompactSketch.createCompactSketch)1 Union (com.yahoo.sketches.theta.Union)1 File (java.io.File)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1