Search in sources :

Example 71 with WritableMemory

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

the class DirectUnionTest method checkExactUnionNoOverlap.

@Test
public void checkExactUnionNoOverlap() {
    // 512
    final int lgK = 9;
    final int k = 1 << lgK;
    final int u = k;
    final UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
    final UpdateSketch usk2 = UpdateSketch.builder().setNominalEntries(k).build();
    for (int i = 0; i < u / 2; i++) {
        // 256
        usk1.update(i);
    }
    for (int i = u / 2; i < u; i++) {
        // 256 no overlap
        usk2.update(i);
    }
    // exact, no overlap
    assertEquals(u, usk1.getEstimate() + usk2.getEstimate(), 0.0);
    final WritableMemory uMem = WritableMemory.writableWrap(new byte[getMaxUnionBytes(k)]);
    final Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
    // update with heap UpdateSketch
    union.union(usk1);
    // update with heap UpdateSketch
    union.union(usk2);
    testAllCompactForms(union, u, 0.0);
}
Also used : WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 72 with WritableMemory

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

the class DirectUnionTest method checkWrapExact.

// these parallel the checkHeapifyExact, etc.
@Test
public void checkWrapExact() {
    // 512
    final int lgK = 9;
    final int k = 1 << lgK;
    final int u = k;
    final UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
    final UpdateSketch usk2 = UpdateSketch.builder().setNominalEntries(k).build();
    for (int i = 0; i < u / 2; i++) {
        // 256
        usk1.update(i);
    }
    for (int i = u / 2; i < u; i++) {
        // 256 no overlap
        usk2.update(i);
    }
    // exact, no overlap
    assertEquals(u, usk1.getEstimate() + usk2.getEstimate(), 0.0);
    final WritableMemory uMem = WritableMemory.writableWrap(new byte[getMaxUnionBytes(k)]);
    final Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
    // update with heap UpdateSketch
    union.union(usk1);
    // update with heap UpdateSketch
    union.union(usk2);
    testAllCompactForms(union, u, 0.0);
    final Union union2 = Sketches.wrapUnion(WritableMemory.writableWrap(union.toByteArray()));
    testAllCompactForms(union2, u, 0.0);
}
Also used : WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 73 with WritableMemory

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

the class DirectUnionTest method checkUnionMemToString.

@Test
public void checkUnionMemToString() {
    final int k = 64;
    // union memory
    final WritableMemory uMem = WritableMemory.writableWrap(new byte[getMaxUnionBytes(k)]);
    SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
}
Also used : WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 74 with WritableMemory

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

the class DirectUnionTest method checkExactUnionWithOverlap.

@Test
public void checkExactUnionWithOverlap() {
    // 512
    final int lgK = 9;
    final int k = 1 << lgK;
    final int u = k;
    final UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
    final UpdateSketch usk2 = UpdateSketch.builder().setNominalEntries(k).build();
    for (int i = 0; i < u / 2; i++) {
        // 256
        usk1.update(i);
    }
    for (int i = 0; i < u; i++) {
        // 512, 256 overlapped
        usk2.update(i);
    }
    // exact, overlapped
    assertEquals(u, usk1.getEstimate() + usk2.getEstimate() / 2, 0.0);
    final WritableMemory uMem = WritableMemory.writableWrap(new byte[getMaxUnionBytes(k)]);
    final Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
    // update with heap UpdateSketch
    union.union(usk1);
    // update with heap UpdateSketch
    union.union(usk2);
    testAllCompactForms(union, u, 0.0);
}
Also used : WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 75 with WritableMemory

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

the class DirectUnionTest method checkSerVer2Handling.

@Test
public void checkSerVer2Handling() {
    // 4096
    final int lgK = 12;
    final int k = 1 << lgK;
    final int u1 = 2 * k;
    // smaller exact sketch forces early stop
    final int u2 = 1024;
    final int totU = u1 + u2;
    final UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
    final UpdateSketch usk2 = UpdateSketch.builder().setNominalEntries(k).build();
    for (int i = 0; i < u1; i++) {
        // 2*k
        usk1.update(i);
    }
    for (int i = u1; i < totU; i++) {
        // 2*k + 1024 no overlap
        usk2.update(i);
    }
    final Memory v2mem1 = convertSerVer3toSerVer2(usk1.compact(true, null), Util.DEFAULT_UPDATE_SEED);
    final Memory v2mem2 = convertSerVer3toSerVer2(usk2.compact(true, null), Util.DEFAULT_UPDATE_SEED);
    // union memory
    final WritableMemory uMem = WritableMemory.writableWrap(new byte[getMaxUnionBytes(k)]);
    final Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
    union.union(v2mem1);
    union.union(v2mem2);
    final CompactSketch cOut = union.getResult(true, null);
    assertEquals(cOut.getEstimate(), totU, .05 * k);
}
Also used : Memory(org.apache.datasketches.memory.Memory) WritableMemory(org.apache.datasketches.memory.WritableMemory) WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Aggregations

WritableMemory (org.apache.datasketches.memory.WritableMemory)429 Test (org.testng.annotations.Test)308 Memory (org.apache.datasketches.memory.Memory)55 SketchesArgumentException (org.apache.datasketches.SketchesArgumentException)51 WritableHandle (org.apache.datasketches.memory.WritableHandle)38 SketchesReadOnlyException (org.apache.datasketches.SketchesReadOnlyException)25 ArrayOfLongsSerDe (org.apache.datasketches.ArrayOfLongsSerDe)11 ByteBuffer (java.nio.ByteBuffer)8 Test (org.junit.Test)8 DefaultMemoryRequestServer (org.apache.datasketches.memory.DefaultMemoryRequestServer)7 ArrayOfStringsSerDe (org.apache.datasketches.ArrayOfStringsSerDe)6 SketchesStateException (org.apache.datasketches.SketchesStateException)5 SharedLocal (org.apache.datasketches.theta.ConcurrentHeapQuickSelectSketchTest.SharedLocal)5 AggregatorAdapters (org.apache.druid.query.aggregation.AggregatorAdapters)5 Union (org.apache.datasketches.hll.Union)4 ResizeFactor (org.apache.datasketches.ResizeFactor)3 HllSketch (org.apache.datasketches.hll.HllSketch)3 PreambleUtil.insertLgResizeFactor (org.apache.datasketches.theta.PreambleUtil.insertLgResizeFactor)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2