Search in sources :

Example 56 with Memory

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

the class HeapUpdateDoublesSketchTest method quantilesCheckViaMemory.

//@Test  //visual only
public void quantilesCheckViaMemory() {
    int k = 256;
    int n = 1000000;
    DoublesSketch qs = buildAndLoadQS(k, n);
    double[] ranks = { 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0 };
    String s = getRanksTable(qs, ranks);
    println(s);
    println("");
    Memory srcMem = Memory.wrap(qs.toByteArray());
    HeapUpdateDoublesSketch qs2 = HeapUpdateDoublesSketch.heapifyInstance(srcMem);
    println(getRanksTable(qs2, ranks));
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory)

Example 57 with Memory

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

the class HeapUpdateDoublesSketchTest method serializeDeserializeEmptyNonCompact.

@Test
public void serializeDeserializeEmptyNonCompact() {
    UpdateDoublesSketch sketch1 = DoublesSketch.builder().build();
    //Ordered, Not Compact, Empty
    byte[] byteArr = sketch1.toByteArray(false);
    assertEquals(byteArr.length, sketch1.getStorageBytes());
    Memory mem = Memory.wrap(byteArr);
    UpdateDoublesSketch sketch2 = (UpdateDoublesSketch) DoublesSketch.heapify(mem);
    for (int i = 0; i < 1000; i++) {
        sketch2.update(i);
    }
    assertEquals(sketch2.getMinValue(), 0.0);
    assertEquals(sketch2.getMaxValue(), 999.0);
    assertEquals(sketch2.getQuantile(0.5), 500.0, 4.0);
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 58 with Memory

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

the class ItemsSketchTest method checkGoodSerDeId.

@Test
public void checkGoodSerDeId() {
    ItemsSketch<String> sketch = ItemsSketch.getInstance(Comparator.naturalOrder());
    byte[] byteArr = sketch.toByteArray(new ArrayOfStringsSerDe());
    Memory mem = Memory.wrap(byteArr);
    //println(PreambleUtil.toString(mem));
    ItemsSketch.getInstance(mem, Comparator.naturalOrder(), new ArrayOfStringsSerDe());
}
Also used : ArrayOfStringsSerDe(com.yahoo.sketches.ArrayOfStringsSerDe) Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 59 with Memory

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

the class ItemsSketchTest method checkToFromByteArray2.

private static void checkToFromByteArray2(int k, int n) {
    ItemsSketch<String> is = buildStringIS(k, n);
    byte[] byteArr;
    Memory mem;
    ItemsSketch<String> is2;
    ArrayOfStringsSerDe serDe = new ArrayOfStringsSerDe();
    //ordered
    byteArr = is.toByteArray(true, serDe);
    mem = Memory.wrap(byteArr);
    is2 = ItemsSketch.getInstance(mem, Comparator.naturalOrder(), serDe);
    for (double f = 0.1; f < 0.95; f += 0.1) {
        assertEquals(is.getQuantile(f), is2.getQuantile(f));
    }
    //Not-ordered
    byteArr = is.toByteArray(false, serDe);
    mem = Memory.wrap(byteArr);
    is2 = ItemsSketch.getInstance(mem, Comparator.naturalOrder(), serDe);
    for (double f = 0.1; f < 0.95; f += 0.1) {
        assertEquals(is.getQuantile(f), is2.getQuantile(f));
    }
}
Also used : ArrayOfStringsSerDe(com.yahoo.sketches.ArrayOfStringsSerDe) Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory)

Example 60 with Memory

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

the class DirectIntersectionTest method checkHeapify.

@Test
public void checkHeapify() {
    int lgK = 9;
    int k = 1 << lgK;
    Intersection inter;
    UpdateSketch sk1, sk2;
    CompactSketch comp1, comp2;
    double est, est2;
    int memBytes = getMaxIntersectionBytes(k);
    byte[] memArr = new byte[memBytes];
    WritableMemory iMem = WritableMemory.wrap(memArr);
    //1st call = valid
    sk1 = UpdateSketch.builder().setNominalEntries(k).build();
    //est mode
    for (int i = 0; i < 2 * k; i++) sk1.update(i);
    println("sk1: " + sk1.getEstimate());
    inter = SetOperation.builder().buildIntersection(iMem);
    inter.update(sk1);
    //2nd call = valid intersecting
    sk2 = UpdateSketch.builder().setNominalEntries(k).build();
    //est mode
    for (int i = 0; i < 2 * k; i++) sk2.update(i);
    println("sk2: " + sk2.getEstimate());
    inter.update(sk2);
    comp1 = inter.getResult(false, null);
    est = comp1.getEstimate();
    assertTrue(est > k);
    println("Est: " + est);
    byte[] byteArray = inter.toByteArray();
    Memory mem = Memory.wrap(byteArray);
    Intersection inter2 = (Intersection) SetOperation.heapify(mem);
    comp2 = inter2.getResult(false, null);
    est2 = comp2.getEstimate();
    println("Est2: " + est2);
}
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