Search in sources :

Example 26 with Memory

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

the class ReservoirLongsUnionTest method checkInstantiation.

@Test
public void checkInstantiation() {
    final int n = 100;
    final int k = 25;
    // create empty unions
    ReservoirLongsUnion rlu = ReservoirLongsUnion.newInstance(k);
    assertNull(rlu.getResult());
    rlu.update(5);
    assertNotNull(rlu.getResult());
    // pass in a sketch, as both an object and memory
    final ReservoirLongsSketch rls = ReservoirLongsSketch.newInstance(k);
    for (int i = 0; i < n; ++i) {
        rls.update(i);
    }
    rlu.reset();
    assertEquals(rlu.getResult().getN(), 0);
    rlu.update(rls);
    assertEquals(rlu.getResult().getN(), rls.getN());
    final byte[] sketchBytes = rls.toByteArray();
    final Memory mem = Memory.wrap(sketchBytes);
    rlu = ReservoirLongsUnion.newInstance(rls.getK());
    rlu.update(mem);
    assertNotNull(rlu.getResult());
    println(rlu.toString());
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 27 with Memory

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

the class VarOptItemsSketchTest method checkMalformedPreamble.

@Test
public void checkMalformedPreamble() {
    final int k = 50;
    final VarOptItemsSketch<Long> sketch = getUnweightedLongsVIS(k, k);
    final byte[] sketchBytes = sketch.toByteArray(new ArrayOfLongsSerDe());
    final Memory srcMem = Memory.wrap(sketchBytes);
    // we'll use the same initial sketch a few times, so grab a copy of it
    final byte[] copyBytes = new byte[sketchBytes.length];
    final WritableMemory mem = WritableMemory.wrap(copyBytes);
    // may be null
    final Object memObj = mem.getArray();
    final long memAddr = mem.getCumulativeOffset(0L);
    // copy the bytes
    srcMem.copyTo(0, mem, 0, sketchBytes.length);
    assertEquals(PreambleUtil.extractPreLongs(mem), PreambleUtil.VO_WARMUP_PRELONGS);
    // no items in R but max preLongs
    try {
        PreambleUtil.insertPreLongs(memObj, memAddr, Family.VAROPT.getMaxPreLongs());
        VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
        fail();
    } catch (final SketchesArgumentException e) {
        assertTrue(e.getMessage().startsWith("Possible Corruption: " + Family.VAROPT.getMaxPreLongs() + " preLongs but"));
    }
    // refresh the copy
    srcMem.copyTo(0, mem, 0, sketchBytes.length);
    assertEquals(PreambleUtil.extractPreLongs(mem), PreambleUtil.VO_WARMUP_PRELONGS);
    // negative H region count
    try {
        PreambleUtil.insertHRegionItemCount(memObj, memAddr, -1);
        VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
        fail();
    } catch (final SketchesArgumentException e) {
        assertTrue(e.getMessage().equals("Possible Corruption: H region count cannot be negative: -1"));
    }
    // refresh the copy
    srcMem.copyTo(0, mem, 0, sketchBytes.length);
    assertEquals(PreambleUtil.extractHRegionItemCount(mem), k);
    // negative R region count
    try {
        PreambleUtil.insertRRegionItemCount(memObj, memAddr, -128);
        VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
        fail();
    } catch (final SketchesArgumentException e) {
        assertTrue(e.getMessage().equals("Possible Corruption: R region count cannot be negative: -128"));
    }
    // refresh the copy
    srcMem.copyTo(0, mem, 0, sketchBytes.length);
    assertEquals(PreambleUtil.extractRRegionItemCount(mem), 0);
    // invalid k < 2
    try {
        PreambleUtil.insertK(memObj, memAddr, 0);
        VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
        fail();
    } catch (final SketchesArgumentException e) {
        assertTrue(e.getMessage().equals("Possible Corruption: k must be at least 2: 0"));
    }
    // refresh the copy
    srcMem.copyTo(0, mem, 0, sketchBytes.length);
    assertEquals(PreambleUtil.extractK(mem), k);
    // invalid n < 0
    try {
        PreambleUtil.insertN(memObj, memAddr, -1024);
        VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
        fail();
    } catch (final SketchesArgumentException e) {
        assertTrue(e.getMessage().equals("Possible Corruption: n cannot be negative: -1024"));
    }
}
Also used : SketchesArgumentException(com.yahoo.sketches.SketchesArgumentException) ArrayOfLongsSerDe(com.yahoo.sketches.ArrayOfLongsSerDe) Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 28 with Memory

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

the class VarOptItemsSketchTest method checkEndOfWarmupSketchSerialization.

@Test
public void checkEndOfWarmupSketchSerialization() {
    final int k = 2048;
    final VarOptItemsSketch<Long> sketch = getUnweightedLongsVIS(k, k);
    final byte[] bytes = sketch.toByteArray(new ArrayOfLongsSerDe());
    final Memory mem = Memory.wrap(bytes);
    // ensure still only 2 preLongs
    assertEquals(PreambleUtil.extractPreLongs(mem), PreambleUtil.VO_WARMUP_PRELONGS);
    final VarOptItemsSketch<Long> rebuilt = VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
    checkIfEqual(rebuilt, sketch);
}
Also used : ArrayOfLongsSerDe(com.yahoo.sketches.ArrayOfLongsSerDe) Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 29 with Memory

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

the class VarOptItemsSketchTest method checkUnderFullSketchSerialization.

@Test
public void checkUnderFullSketchSerialization() {
    final VarOptItemsSketch<Long> sketch = VarOptItemsSketch.newInstance(2048);
    for (long i = 0; i < 10; ++i) {
        sketch.update(i, 1.0);
    }
    assertEquals(sketch.getNumSamples(), 10);
    final byte[] bytes = sketch.toByteArray(new ArrayOfLongsSerDe());
    final Memory mem = Memory.wrap(bytes);
    // ensure correct number of preLongs
    assertEquals(PreambleUtil.extractPreLongs(mem), PreambleUtil.VO_WARMUP_PRELONGS);
    final VarOptItemsSketch<Long> rebuilt = VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
    checkIfEqual(rebuilt, sketch);
}
Also used : ArrayOfLongsSerDe(com.yahoo.sketches.ArrayOfLongsSerDe) Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 30 with Memory

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

the class VarOptItemsSketchTest method checkBadMemory.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBadMemory() {
    byte[] bytes = new byte[4];
    Memory mem = Memory.wrap(bytes);
    try {
        PreambleUtil.getAndCheckPreLongs(mem);
        fail();
    } catch (final SketchesArgumentException e) {
    // expected
    }
    bytes = new byte[8];
    // only 1 preLong worth of items in bytearray
    bytes[0] = 2;
    mem = Memory.wrap(bytes);
    PreambleUtil.getAndCheckPreLongs(mem);
}
Also used : SketchesArgumentException(com.yahoo.sketches.SketchesArgumentException) Memory(com.yahoo.memory.Memory) 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