Search in sources :

Example 61 with Memory

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

the class DoublesUtilTest method checkCopyToHeap.

@Test
public void checkCopyToHeap() {
    final int k = 128;
    final int n = 400;
    // HeapUpdateDoublesSketch
    final HeapUpdateDoublesSketch huds = (HeapUpdateDoublesSketch) buildAndLoadQS(k, n);
    final HeapUpdateDoublesSketch target1 = DoublesUtil.copyToHeap(huds);
    DoublesSketchTest.testSketchEquality(huds, target1);
    // DirectUpdateDoublesSketch
    final WritableMemory mem1 = WritableMemory.wrap(huds.toByteArray());
    final DirectUpdateDoublesSketch duds = (DirectUpdateDoublesSketch) UpdateDoublesSketch.wrap(mem1);
    final HeapUpdateDoublesSketch target2 = DoublesUtil.copyToHeap(duds);
    DoublesSketchTest.testSketchEquality(huds, duds);
    DoublesSketchTest.testSketchEquality(duds, target2);
    // HeapCompactDoublesSketch
    final CompactDoublesSketch hcds = huds.compact();
    final HeapUpdateDoublesSketch target3 = DoublesUtil.copyToHeap(hcds);
    DoublesSketchTest.testSketchEquality(huds, hcds);
    DoublesSketchTest.testSketchEquality(hcds, target3);
    // DirectCompactDoublesSketch
    final Memory mem2 = Memory.wrap(hcds.toByteArray());
    final DirectCompactDoublesSketch dcds = (DirectCompactDoublesSketch) DoublesSketch.wrap(mem2);
    final HeapUpdateDoublesSketch target4 = DoublesUtil.copyToHeap(dcds);
    DoublesSketchTest.testSketchEquality(huds, dcds);
    DoublesSketchTest.testSketchEquality(dcds, target4);
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 62 with Memory

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

the class ForwardCompatibilityTest method getAndCheck.

private void getAndCheck(String ver, int n, double quantile) {
    //make deterministic
    DoublesSketch.rand.setSeed(131);
    //create fileName
    int k = 128;
    double nf = 0.5;
    String fileName = String.format("Qk%d_n%d_v%s.bin", k, n, ver);
    println("fullName: " + fileName);
    println("Old Median: " + quantile);
    //create & Read File
    File file = new File(getClass().getClassLoader().getResource(fileName).getFile());
    byte[] byteArr2 = readFile(file);
    Memory srcMem = Memory.wrap(byteArr2);
    // heapify as update sketch
    DoublesSketch qs2 = UpdateDoublesSketch.heapify(srcMem);
    //Test the quantile
    double q2 = qs2.getQuantile(nf);
    println("New Median: " + q2);
    Assert.assertEquals(q2, quantile, 0.0);
    // same thing with compact sketch
    qs2 = CompactDoublesSketch.heapify(srcMem);
    //Test the quantile
    q2 = qs2.getQuantile(nf);
    println("New Median: " + q2);
    Assert.assertEquals(q2, quantile, 0.0);
}
Also used : Memory(com.yahoo.memory.Memory) File(java.io.File)

Example 63 with Memory

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

the class SketchTest method checkSerVer.

@Test
public void checkSerVer() {
    UpdateSketch sketch = UpdateSketch.builder().setNominalEntries(1024).build();
    byte[] sketchArray = sketch.toByteArray();
    Memory mem = Memory.wrap(sketchArray);
    int serVer = Sketch.getSerializationVersion(mem);
    assertEquals(serVer, 3);
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 64 with Memory

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

the class SketchTest method checkHeapifyFamilyExcep.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkHeapifyFamilyExcep() {
    int k = 512;
    Union union = SetOperation.builder().setNominalEntries(k).buildUnion();
    byte[] byteArray = union.toByteArray();
    Memory mem = Memory.wrap(byteArray);
    //Improper use
    Sketch.heapify(mem);
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 65 with Memory

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

the class SketchesTest method checkSetOpMethods.

@Test
public void checkSetOpMethods() {
    int k = 1024;
    Memory mem1 = getCompactSketch(k, 0, k);
    Memory mem2 = getCompactSketch(k, k / 2, 3 * k / 2);
    SetOperationBuilder bldr = setOperationBuilder();
    Union union = bldr.setNominalEntries(2 * k).buildUnion();
    union.update(mem1);
    CompactSketch cSk = union.getResult(true, null);
    assertEquals((int) cSk.getEstimate(), k);
    union.update(mem2);
    cSk = union.getResult(true, null);
    assertEquals((int) cSk.getEstimate(), 3 * k / 2);
    byte[] ubytes = union.toByteArray();
    WritableMemory uMem = WritableMemory.wrap(ubytes);
    Union union2 = (Union) heapifySetOperation(uMem);
    cSk = union2.getResult(true, null);
    assertEquals((int) cSk.getEstimate(), 3 * k / 2);
    union2 = (Union) heapifySetOperation(uMem, Util.DEFAULT_UPDATE_SEED);
    cSk = union2.getResult(true, null);
    assertEquals((int) cSk.getEstimate(), 3 * k / 2);
    union2 = (Union) wrapSetOperation(uMem);
    cSk = union2.getResult(true, null);
    assertEquals((int) cSk.getEstimate(), 3 * k / 2);
    union2 = (Union) wrapSetOperation(uMem, Util.DEFAULT_UPDATE_SEED);
    cSk = union2.getResult(true, null);
    assertEquals((int) cSk.getEstimate(), 3 * k / 2);
    int serVer = getSerializationVersion(uMem);
    assertEquals(serVer, 3);
}
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