Search in sources :

Example 16 with ArrayOfStringsSerDe

use of org.apache.datasketches.ArrayOfStringsSerDe in project sketches-core by DataSketches.

the class VarOptItemsUnionTest method unionEmptySketch.

@Test
public void unionEmptySketch() {
    final int k = 2048;
    final ArrayOfStringsSerDe serDe = new ArrayOfStringsSerDe();
    // we'll union from Memory for good measure
    final byte[] sketchBytes = VarOptItemsSketch.<String>newInstance(k).toByteArray(serDe);
    final Memory mem = Memory.wrap(sketchBytes);
    final VarOptItemsUnion<String> union = VarOptItemsUnion.newInstance(k);
    union.update(mem, serDe);
    final VarOptItemsSketch<String> result = union.getResult();
    assertEquals(result.getN(), 0);
    assertEquals(result.getHRegionCount(), 0);
    assertEquals(result.getRRegionCount(), 0);
    assertTrue(Double.isNaN(result.getTau()));
}
Also used : ArrayOfStringsSerDe(org.apache.datasketches.ArrayOfStringsSerDe) Memory(org.apache.datasketches.memory.Memory) WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 17 with ArrayOfStringsSerDe

use of org.apache.datasketches.ArrayOfStringsSerDe in project sketches-core by DataSketches.

the class VarOptItemsUnionTest method serializeEmptyUnion.

@Test
public void serializeEmptyUnion() {
    final int k = 100;
    final VarOptItemsUnion<String> union = VarOptItemsUnion.newInstance(k);
    // null inputs to update() should leave the union empty
    union.update((VarOptItemsSketch<String>) null);
    union.update(null, new ArrayOfStringsSerDe());
    final ArrayOfStringsSerDe serDe = new ArrayOfStringsSerDe();
    final byte[] bytes = union.toByteArray(serDe);
    assertEquals(bytes.length, 8);
    final Memory mem = Memory.wrap(bytes);
    final VarOptItemsUnion<String> rebuilt = VarOptItemsUnion.heapify(mem, serDe);
    final VarOptItemsSketch<String> sketch = rebuilt.getResult();
    assertEquals(sketch.getN(), 0);
    assertEquals(rebuilt.toString(), union.toString());
}
Also used : ArrayOfStringsSerDe(org.apache.datasketches.ArrayOfStringsSerDe) Memory(org.apache.datasketches.memory.Memory) WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 18 with ArrayOfStringsSerDe

use of org.apache.datasketches.ArrayOfStringsSerDe in project sketches-core by DataSketches.

the class ReservoirItemsUnionTest method checkBadSerVer.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBadSerVer() {
    final ReservoirItemsUnion<String> riu = ReservoirItemsUnion.newInstance(1024);
    final WritableMemory mem = WritableMemory.writableWrap(riu.toByteArray(new ArrayOfStringsSerDe()));
    // corrupt the serialization version
    mem.putByte(SER_VER_BYTE, (byte) 0);
    ReservoirItemsUnion.heapify(mem, new ArrayOfStringsSerDe());
    fail();
}
Also used : ArrayOfStringsSerDe(org.apache.datasketches.ArrayOfStringsSerDe) WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 19 with ArrayOfStringsSerDe

use of org.apache.datasketches.ArrayOfStringsSerDe in project sketches-core by DataSketches.

the class ItemsSketchTest method oneItemUtf8.

@Test
public void oneItemUtf8() {
    ItemsSketch<String> sketch1 = new ItemsSketch<>(1 << LG_MIN_MAP_SIZE);
    sketch1.update("\u5fb5");
    Assert.assertFalse(sketch1.isEmpty());
    Assert.assertEquals(sketch1.getNumActiveItems(), 1);
    Assert.assertEquals(sketch1.getStreamLength(), 1);
    Assert.assertEquals(sketch1.getEstimate("\u5fb5"), 1);
    byte[] bytes = sketch1.toByteArray(new ArrayOfStringsSerDe());
    ItemsSketch<String> sketch2 = ItemsSketch.getInstance(Memory.wrap(bytes), new ArrayOfStringsSerDe());
    Assert.assertFalse(sketch2.isEmpty());
    Assert.assertEquals(sketch2.getNumActiveItems(), 1);
    Assert.assertEquals(sketch2.getStreamLength(), 1);
    Assert.assertEquals(sketch2.getEstimate("\u5fb5"), 1);
}
Also used : ArrayOfStringsSerDe(org.apache.datasketches.ArrayOfStringsSerDe) Test(org.testng.annotations.Test)

Example 20 with ArrayOfStringsSerDe

use of org.apache.datasketches.ArrayOfStringsSerDe in project sketches-core by DataSketches.

the class ItemsSketchTest method serializeDeserializeUft8Strings.

@Test
public void serializeDeserializeUft8Strings() {
    ItemsSketch<String> sketch1 = new ItemsSketch<>(1 << LG_MIN_MAP_SIZE);
    sketch1.update("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    sketch1.update("bbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
    sketch1.update("ccccccccccccccccccccccccccccc");
    sketch1.update("ddddddddddddddddddddddddddddd");
    byte[] bytes = sketch1.toByteArray(new ArrayOfStringsSerDe());
    ItemsSketch<String> sketch2 = ItemsSketch.getInstance(Memory.wrap(bytes), new ArrayOfStringsSerDe());
    sketch2.update("bbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
    sketch2.update("ccccccccccccccccccccccccccccc");
    sketch2.update("bbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
    Assert.assertFalse(sketch2.isEmpty());
    Assert.assertEquals(sketch2.getNumActiveItems(), 4);
    Assert.assertEquals(sketch2.getStreamLength(), 7);
    Assert.assertEquals(sketch2.getEstimate("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), 1);
    Assert.assertEquals(sketch2.getEstimate("bbbbbbbbbbbbbbbbbbbbbbbbbbbbb"), 3);
    Assert.assertEquals(sketch2.getEstimate("ccccccccccccccccccccccccccccc"), 2);
    Assert.assertEquals(sketch2.getEstimate("ddddddddddddddddddddddddddddd"), 1);
}
Also used : ArrayOfStringsSerDe(org.apache.datasketches.ArrayOfStringsSerDe) Test(org.testng.annotations.Test)

Aggregations

ArrayOfStringsSerDe (org.apache.datasketches.ArrayOfStringsSerDe)20 Test (org.testng.annotations.Test)19 WritableMemory (org.apache.datasketches.memory.WritableMemory)15 Memory (org.apache.datasketches.memory.Memory)10 SketchesArgumentException (org.apache.datasketches.SketchesArgumentException)1