Search in sources :

Example 16 with ResizeFactor

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

the class DirectQuickSelectSketchTest method checkConstructorSrcMemCorruptions.

@Test
public void checkConstructorSrcMemCorruptions() {
    // lgNomLongs = 10
    int k = 1024;
    // exact mode, lgArrLongs = 11
    int u = k;
    int bytes = Sketches.getMaxUpdateSketchBytes(k);
    byte[] arr1 = new byte[bytes];
    WritableMemory mem1 = WritableMemory.writableWrap(arr1);
    // 0
    ResizeFactor rf = ResizeFactor.X1;
    UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).setResizeFactor(rf).build(mem1);
    for (int i = 0; i < u; i++) {
        usk1.update(i);
    }
    // println(PreambleUtil.toString(mem1));
    @SuppressWarnings("unused") UpdateSketch usk2;
    // corrupt Family by setting to Compact
    mem1.putByte(FAMILY_BYTE, (byte) 3);
    try {
        usk2 = DirectQuickSelectSketch.writableWrap(mem1, DEFAULT_UPDATE_SEED);
        fail("Expected SketchesArgumentException");
    } catch (SketchesArgumentException e) {
    // Pass
    }
    // fix Family
    mem1.putByte(FAMILY_BYTE, (byte) 2);
    // corrupt preLongs
    mem1.putByte(PREAMBLE_LONGS_BYTE, (byte) 1);
    try {
        usk2 = DirectQuickSelectSketch.writableWrap(mem1, DEFAULT_UPDATE_SEED);
        fail("Expected SketchesArgumentException");
    } catch (SketchesArgumentException e) {
    // pass
    }
    // fix preLongs
    mem1.putByte(PREAMBLE_LONGS_BYTE, (byte) 3);
    // corrupt serVer
    mem1.putByte(SER_VER_BYTE, (byte) 2);
    try {
        usk2 = DirectQuickSelectSketch.writableWrap(mem1, DEFAULT_UPDATE_SEED);
        fail("Expected SketchesArgumentException");
    } catch (SketchesArgumentException e) {
    // pass
    }
    // fix serVer
    mem1.putByte(SER_VER_BYTE, (byte) 3);
    // corrupt theta and
    mem1.putLong(THETA_LONG, Long.MAX_VALUE >>> 1);
    // corrupt lgArrLongs
    mem1.putByte(LG_ARR_LONGS_BYTE, (byte) 10);
    try {
        usk2 = DirectQuickSelectSketch.writableWrap(mem1, DEFAULT_UPDATE_SEED);
        fail("Expected SketchesArgumentException");
    } catch (SketchesArgumentException e) {
    // pass
    }
    // fix theta and
    mem1.putLong(THETA_LONG, Long.MAX_VALUE);
    // fix lgArrLongs
    mem1.putByte(LG_ARR_LONGS_BYTE, (byte) 11);
    byte badFlags = (byte) (BIG_ENDIAN_FLAG_MASK | COMPACT_FLAG_MASK | READ_ONLY_FLAG_MASK | ORDERED_FLAG_MASK);
    mem1.putByte(FLAGS_BYTE, badFlags);
    try {
        usk2 = DirectQuickSelectSketch.writableWrap(mem1, DEFAULT_UPDATE_SEED);
        fail("Expected SketchesArgumentException");
    } catch (SketchesArgumentException e) {
    // pass
    }
    // corrupt length
    byte[] arr2 = Arrays.copyOfRange(arr1, 0, bytes - 1);
    WritableMemory mem2 = WritableMemory.writableWrap(arr2);
    try {
        usk2 = DirectQuickSelectSketch.writableWrap(mem2, DEFAULT_UPDATE_SEED);
        fail("Expected SketchesArgumentException");
    } catch (SketchesArgumentException e) {
    // pass
    }
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) WritableMemory(org.apache.datasketches.memory.WritableMemory) ResizeFactor(org.apache.datasketches.ResizeFactor) PreambleUtil.insertLgResizeFactor(org.apache.datasketches.theta.PreambleUtil.insertLgResizeFactor) Test(org.testng.annotations.Test)

Example 17 with ResizeFactor

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

the class SetOperationTest method checkBuilderBadSeedHashes.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBuilderBadSeedHashes() {
    final int k = 2048;
    final long seed = 1021;
    final UpdateSketch usk1 = UpdateSketch.builder().setSeed(seed).setNominalEntries(k).build();
    final UpdateSketch usk2 = UpdateSketch.builder().setNominalEntries(k).build();
    for (int i = 0; i < k / 2; i++) {
        // 256
        usk1.update(i);
    }
    for (int i = k / 2; i < k; i++) {
        // 256 no overlap
        usk2.update(i);
    }
    final ResizeFactor rf = X4;
    final Union union = SetOperation.builder().setSeed(seed).setResizeFactor(rf).setNominalEntries(k).buildUnion();
    union.union(usk1);
    // throws seed exception here
    union.union(usk2);
}
Also used : ResizeFactor(org.apache.datasketches.ResizeFactor) Test(org.testng.annotations.Test)

Example 18 with ResizeFactor

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

the class SetOperationTest method checkBuilder.

@Test
public void checkBuilder() {
    final int k = 2048;
    final long seed = 1021;
    final UpdateSketch usk1 = UpdateSketch.builder().setSeed(seed).setNominalEntries(k).build();
    final UpdateSketch usk2 = UpdateSketch.builder().setSeed(seed).setNominalEntries(k).build();
    for (int i = 0; i < k / 2; i++) {
        // 256
        usk1.update(i);
    }
    for (int i = k / 2; i < k; i++) {
        // 256 no overlap
        usk2.update(i);
    }
    final ResizeFactor rf = X4;
    // use default size
    final Union union = SetOperation.builder().setSeed(seed).setResizeFactor(rf).buildUnion();
    union.union(usk1);
    union.union(usk2);
    final double exactUnionAnswer = k;
    // ordered: false
    final CompactSketch comp1 = union.getResult(false, null);
    final double compEst = comp1.getEstimate();
    assertEquals(compEst, exactUnionAnswer, 0.0);
}
Also used : ResizeFactor(org.apache.datasketches.ResizeFactor) Test(org.testng.annotations.Test)

Example 19 with ResizeFactor

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

the class SetOperationTest method checkBuilder2.

@Test
public void checkBuilder2() {
    final SetOperationBuilder bldr = SetOperation.builder();
    final long seed = 12345L;
    bldr.setSeed(seed);
    assertEquals(bldr.getSeed(), seed);
    final float p = (float) 0.5;
    bldr.setP(p);
    assertEquals(bldr.getP(), p);
    final ResizeFactor rf = ResizeFactor.X4;
    bldr.setResizeFactor(rf);
    assertEquals(bldr.getResizeFactor(), rf);
    final int lgK = 10;
    final int k = 1 << lgK;
    bldr.setNominalEntries(k);
    assertEquals(bldr.getLgNominalEntries(), lgK);
    final MemoryRequestServer mrs = new DefaultMemoryRequestServer();
    bldr.setMemoryRequestServer(mrs);
    assertEquals(bldr.getMemoryRequestServer(), mrs);
    println(bldr.toString());
}
Also used : DefaultMemoryRequestServer(org.apache.datasketches.memory.DefaultMemoryRequestServer) ResizeFactor(org.apache.datasketches.ResizeFactor) DefaultMemoryRequestServer(org.apache.datasketches.memory.DefaultMemoryRequestServer) MemoryRequestServer(org.apache.datasketches.memory.MemoryRequestServer) Test(org.testng.annotations.Test)

Example 20 with ResizeFactor

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

the class UpdateSketchTest method checkBuilder.

@Test
public void checkBuilder() {
    UpdateSketchBuilder bldr = UpdateSketch.builder();
    long seed = 12345L;
    bldr.setSeed(seed);
    assertEquals(bldr.getSeed(), seed);
    float p = (float) 0.5;
    bldr.setP(p);
    assertEquals(bldr.getP(), p);
    ResizeFactor rf = ResizeFactor.X4;
    bldr.setResizeFactor(rf);
    assertEquals(bldr.getResizeFactor(), rf);
    Family fam = Family.ALPHA;
    bldr.setFamily(fam);
    assertEquals(bldr.getFamily(), fam);
    int lgK = 10;
    int k = 1 << lgK;
    bldr.setNominalEntries(k);
    assertEquals(bldr.getLgNominalEntries(), lgK);
    MemoryRequestServer mrs = new DefaultMemoryRequestServer();
    bldr.setMemoryRequestServer(mrs);
    assertEquals(bldr.getMemoryRequestServer(), mrs);
    println(bldr.toString());
}
Also used : Family(org.apache.datasketches.Family) DefaultMemoryRequestServer(org.apache.datasketches.memory.DefaultMemoryRequestServer) ResizeFactor(org.apache.datasketches.ResizeFactor) PreambleUtil.insertLgResizeFactor(org.apache.datasketches.theta.PreambleUtil.insertLgResizeFactor) DefaultMemoryRequestServer(org.apache.datasketches.memory.DefaultMemoryRequestServer) MemoryRequestServer(org.apache.datasketches.memory.MemoryRequestServer) Test(org.testng.annotations.Test)

Aggregations

ResizeFactor (org.apache.datasketches.ResizeFactor)24 Test (org.testng.annotations.Test)13 PreambleUtil.insertLgResizeFactor (org.apache.datasketches.theta.PreambleUtil.insertLgResizeFactor)6 SketchesArgumentException (org.apache.datasketches.SketchesArgumentException)5 PreambleUtil.extractLgResizeFactor (org.apache.datasketches.theta.PreambleUtil.extractLgResizeFactor)5 Family (org.apache.datasketches.Family)4 ArrayList (java.util.ArrayList)3 WritableMemory (org.apache.datasketches.memory.WritableMemory)3 PreambleUtil.extractResizeFactor (org.apache.datasketches.sampling.PreambleUtil.extractResizeFactor)3 SketchesException (org.apache.datasketches.SketchesException)2 DefaultMemoryRequestServer (org.apache.datasketches.memory.DefaultMemoryRequestServer)2 MemoryRequestServer (org.apache.datasketches.memory.MemoryRequestServer)2 ArrayOfBooleansSerDe (org.apache.datasketches.ArrayOfBooleansSerDe)1