Search in sources :

Example 6 with SketchesReadOnlyException

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

the class ReadOnlyMemoryTest method wrapEmptyUpdateSketch.

@Test
public void wrapEmptyUpdateSketch() {
    final UpdateDoublesSketch s1 = DoublesSketch.builder().build();
    final Memory mem = Memory.wrap(s1.toByteArray());
    UpdateDoublesSketch s2 = (UpdateDoublesSketch) DoublesSketch.wrap(mem);
    Assert.assertTrue(s2.isEmpty());
    // ensure the various put calls fail
    try {
        s2.putMinValue(-1.0);
        fail();
    } catch (final SketchesReadOnlyException e) {
    // expected
    }
    try {
        s2.putMaxValue(1.0);
        fail();
    } catch (final SketchesReadOnlyException e) {
    // expected
    }
    try {
        s2.putN(1);
        fail();
    } catch (final SketchesReadOnlyException e) {
    // expected
    }
    try {
        s2.putBitPattern(1);
        fail();
    } catch (final SketchesReadOnlyException e) {
    // expected
    }
    try {
        s2.reset();
        fail();
    } catch (final SketchesReadOnlyException e) {
    // expected
    }
    try {
        s2.putBaseBufferCount(5);
        fail();
    } catch (final SketchesReadOnlyException e) {
    // expected
    }
    try {
        s2.putCombinedBuffer(new double[16]);
        fail();
    } catch (final SketchesReadOnlyException e) {
    // expected
    }
    try {
        final int currCap = s2.getCombinedBufferItemCapacity();
        s2.growCombinedBuffer(currCap, 2 * currCap);
        fail();
    } catch (final SketchesReadOnlyException e) {
    // expected
    }
}
Also used : Memory(org.apache.datasketches.memory.Memory) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) Test(org.testng.annotations.Test)

Example 7 with SketchesReadOnlyException

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

the class DirectIntersectionTest method checkOverlappedDirect.

// Check Alex's bug intersecting 2 direct full sketches with only overlap of 2
// 
@Test
public void checkOverlappedDirect() {
    final int k = 1 << 4;
    // plenty of room
    final int memBytes = 2 * k * 16 + PREBYTES;
    final UpdateSketch sk1 = Sketches.updateSketchBuilder().setNominalEntries(k).build();
    final UpdateSketch sk2 = Sketches.updateSketchBuilder().setNominalEntries(k).build();
    for (int i = 0; i < k; i++) {
        sk1.update(i);
        // overlap by 2
        sk2.update(k - 2 + i);
    }
    final WritableMemory memIn1 = WritableMemory.writableWrap(new byte[memBytes]);
    final WritableMemory memIn2 = WritableMemory.writableWrap(new byte[memBytes]);
    final WritableMemory memInter = WritableMemory.writableWrap(new byte[memBytes]);
    final WritableMemory memComp = WritableMemory.writableWrap(new byte[memBytes]);
    final CompactSketch csk1 = sk1.compact(true, memIn1);
    final CompactSketch csk2 = sk2.compact(true, memIn2);
    final Intersection inter = Sketches.setOperationBuilder().buildIntersection(memInter);
    inter.intersect(csk1);
    inter.intersect(csk2);
    final CompactSketch cskOut = inter.getResult(true, memComp);
    assertEquals(cskOut.getEstimate(), 2.0, 0.0);
    final Intersection interRO = (Intersection) SetOperation.wrap((Memory) memInter);
    try {
        interRO.intersect(sk1, sk2);
        fail();
    } catch (final SketchesReadOnlyException e) {
    }
    try {
        interRO.reset();
        fail();
    } catch (final SketchesReadOnlyException e) {
    }
}
Also used : Memory(org.apache.datasketches.memory.Memory) WritableMemory(org.apache.datasketches.memory.WritableMemory) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 8 with SketchesReadOnlyException

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

the class IntersectionImpl method intersect.

@Override
public void intersect(final Sketch sketchIn) {
    if (sketchIn == null) {
        throw new SketchesArgumentException("Intersection argument must not be null.");
    }
    if (wmem_ != null && readOnly_) {
        throw new SketchesReadOnlyException();
    }
    if (empty_ || sketchIn.isEmpty()) {
        // empty rule
        // Because of the def of null above and the Empty Rule (which is OR), empty_ must be true.
        // Whatever the current internal state, we make our local empty.
        resetToEmpty();
        return;
    }
    Util.checkSeedHashes(seedHash_, sketchIn.getSeedHash());
    // Set minTheta
    // Theta rule
    thetaLong_ = min(thetaLong_, sketchIn.getThetaLong());
    empty_ = false;
    if (wmem_ != null) {
        insertThetaLong(wmem_, thetaLong_);
        // false
        clearEmpty(wmem_);
    }
    // The truth table for the following state machine. MinTheta is set above.
    // Incoming sketch is not null and not empty, but could have 0 count and Theta < 1.0
    // Case  curCount  sketchInEntries | Actions
    // 1      <0            0        | First intersect, set curCount = 0; HT = null; minTh; exit
    // 2       0            0        | set curCount = 0; HT = null; minTh; exit
    // 3      >0            0        | set curCount = 0; HT = null; minTh; exit
    // 4                             | Not used
    // 5      <0           >0        | First intersect, clone SketchIn; exit
    // 6       0           >0        | set curCount = 0; HT = null; minTh; exit
    // 7      >0           >0        | Perform full intersect
    final int sketchInEntries = sketchIn.getRetainedEntries(true);
    // states 1,2,3,6
    if (curCount_ == 0 || sketchInEntries == 0) {
        curCount_ = 0;
        if (wmem_ != null) {
            insertCurCount(wmem_, 0);
        }
        // No need for a HT. Don't bother clearing mem if valid
        hashTable_ = null;
    } else // state 5
    if (curCount_ < 0 && sketchInEntries > 0) {
        curCount_ = sketchIn.getRetainedEntries(true);
        final int requiredLgArrLongs = minLgHashTableSize(curCount_, REBUILD_THRESHOLD);
        // prior only used in error message
        final int priorLgArrLongs = lgArrLongs_;
        lgArrLongs_ = requiredLgArrLongs;
        if (wmem_ != null) {
            // Off heap, check if current dstMem is large enough
            insertCurCount(wmem_, curCount_);
            insertLgArrLongs(wmem_, lgArrLongs_);
            if (requiredLgArrLongs <= maxLgArrLongs_) {
                // clear only what required
                wmem_.clear(CONST_PREAMBLE_LONGS << 3, 8 << lgArrLongs_);
            } else {
                // not enough space in dstMem
                final int requiredBytes = (8 << requiredLgArrLongs) + 24;
                final int givenBytes = (8 << priorLgArrLongs) + 24;
                throw new SketchesArgumentException("Insufficient internal Memory space: " + requiredBytes + " > " + givenBytes);
            }
        } else {
            // On the heap, allocate a HT
            hashTable_ = new long[1 << lgArrLongs_];
        }
        moveDataToTgt(sketchIn.getCache(), curCount_);
    } else // state 7
    if (curCount_ > 0 && sketchInEntries > 0) {
        // Sets resulting hashTable, curCount and adjusts lgArrLongs
        performIntersect(sketchIn);
    } else // end of state 7
    {
        assert false : "Should not happen";
    }
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException)

Example 9 with SketchesReadOnlyException

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

the class DirectQuickSelectSketchTest method checkReadOnlyRebuildResize.

@Test
public void checkReadOnlyRebuildResize() {
    int k = 1 << 12;
    int u = 2 * k;
    int bytes = Sketches.getMaxUpdateSketchBytes(k);
    try (WritableHandle wdh = WritableMemory.allocateDirect(bytes / 2)) {
        // will request
        WritableMemory wmem = wdh.getWritable();
        UpdateSketch sketch = Sketches.updateSketchBuilder().setNominalEntries(k).build(wmem);
        for (int i = 0; i < u; i++) {
            sketch.update(i);
        }
        double est1 = sketch.getEstimate();
        byte[] ser = sketch.toByteArray();
        Memory mem = Memory.wrap(ser);
        UpdateSketch roSketch = (UpdateSketch) Sketches.wrapSketch(mem);
        double est2 = roSketch.getEstimate();
        assertEquals(est2, est1);
        try {
            roSketch.rebuild();
            fail();
        } catch (SketchesReadOnlyException e) {
        // expected
        }
        try {
            roSketch.reset();
            fail();
        } catch (SketchesReadOnlyException e) {
        // expected
        }
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : WritableHandle(org.apache.datasketches.memory.WritableHandle) Memory(org.apache.datasketches.memory.Memory) WritableMemory(org.apache.datasketches.memory.WritableMemory) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) WritableMemory(org.apache.datasketches.memory.WritableMemory) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) Test(org.testng.annotations.Test)

Example 10 with SketchesReadOnlyException

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

the class ReadOnlyMemoryTest method wrapAndTryUpdatingSparseSketch.

@Test
public void wrapAndTryUpdatingSparseSketch() {
    UpdateDoublesSketch s1 = DoublesSketch.builder().build();
    s1.update(1);
    s1.update(2);
    final byte[] bytes = s1.toByteArray(false);
    // 32 + MIN_K(=2) * 2 * 8 = 64
    Assert.assertEquals(bytes.length, 64);
    // final Memory mem = Memory.wrap(ByteBuffer.wrap(bytes)
    // .asReadOnlyBuffer().order(ByteOrder.nativeOrder()));
    final Memory mem = Memory.wrap(bytes);
    final UpdateDoublesSketch s2 = (UpdateDoublesSketch) DoublesSketch.wrap(mem);
    Assert.assertEquals(s2.getMinValue(), 1.0);
    Assert.assertEquals(s2.getMaxValue(), 2.0);
    try {
        s2.update(3);
        fail();
    } catch (final SketchesReadOnlyException e) {
    // expected
    }
}
Also used : Memory(org.apache.datasketches.memory.Memory) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) Test(org.testng.annotations.Test)

Aggregations

SketchesReadOnlyException (org.apache.datasketches.SketchesReadOnlyException)12 Test (org.testng.annotations.Test)10 Memory (org.apache.datasketches.memory.Memory)8 WritableMemory (org.apache.datasketches.memory.WritableMemory)3 SketchesArgumentException (org.apache.datasketches.SketchesArgumentException)2 ArrayOfDoublesUpdatableSketch (org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketch)2 ArrayOfDoublesUpdatableSketchBuilder (org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketchBuilder)2 WritableHandle (org.apache.datasketches.memory.WritableHandle)1 ArrayOfDoublesSetOperationBuilder (org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesSetOperationBuilder)1 ArrayOfDoublesSketch (org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesSketch)1 ArrayOfDoublesUnion (org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUnion)1