Search in sources :

Example 36 with SketchesArgumentException

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

the class ArrayOfDoublesAnotBImpl method update.

@Override
public void update(final ArrayOfDoublesSketch skA, final ArrayOfDoublesSketch skB) {
    if (skA == null || skB == null) {
        throw new SketchesArgumentException("Neither argument may be null.");
    }
    numValues_ = skA.getNumValues();
    seedHash_ = skA.getSeedHash();
    if (numValues_ != skB.getNumValues()) {
        throw new SketchesArgumentException("Inputs cannot have different numValues");
    }
    if (seedHash_ != skB.getSeedHash()) {
        throw new SketchesArgumentException("Inputs cannot have different seedHashes");
    }
    final long thetaLongA = skA.getThetaLong();
    final int countA = skA.getRetainedEntries();
    final boolean emptyA = skA.isEmpty();
    final long thetaLongB = skB.getThetaLong();
    final int countB = skB.getRetainedEntries();
    final boolean emptyB = skB.isEmpty();
    final int id = SetOperationCornerCases.createCornerCaseId(thetaLongA, countA, emptyA, thetaLongB, countB, emptyB);
    final CornerCase cCase = CornerCase.caseIdToCornerCase(id);
    final AnotbAction anotbAction = cCase.getAnotbAction();
    final long minThetaLong = min(thetaLongA, thetaLongB);
    switch(anotbAction) {
        case EMPTY_1_0_T:
            {
                reset();
                break;
            }
        case DEGEN_MIN_0_F:
            {
                keys_ = null;
                values_ = null;
                thetaLong_ = minThetaLong;
                empty_ = false;
                count_ = 0;
                break;
            }
        case DEGEN_THA_0_F:
            {
                keys_ = null;
                values_ = null;
                thetaLong_ = thetaLongA;
                empty_ = false;
                count_ = 0;
                break;
            }
        case TRIM_A:
            {
                final DataArrays daA = new DataArrays(skA.getKeys(), skA.getValuesAsOneDimension(), countA);
                final DataArrays da = trimDataArrays(daA, minThetaLong, numValues_);
                keys_ = da.hashArr;
                values_ = da.valuesArr;
                thetaLong_ = minThetaLong;
                empty_ = skA.isEmpty();
                count_ = da.count;
                break;
            }
        case SKETCH_A:
            {
                final ArrayOfDoublesCompactSketch csk = skA.compact();
                keys_ = csk.getKeys();
                values_ = csk.getValuesAsOneDimension();
                thetaLong_ = csk.thetaLong_;
                empty_ = csk.isEmpty();
                count_ = csk.getRetainedEntries();
                break;
            }
        case FULL_ANOTB:
            {
                // both A and B should have valid entries.
                final long[] keysA = skA.getKeys();
                final double[] valuesA = skA.getValuesAsOneDimension();
                final DataArrays daR = getResultArrays(minThetaLong, countA, keysA, valuesA, skB);
                count_ = daR.count;
                keys_ = (count_ == 0) ? null : daR.hashArr;
                values_ = (count_ == 0) ? null : daR.valuesArr;
                thetaLong_ = minThetaLong;
                empty_ = (minThetaLong == Long.MAX_VALUE) && (count_ == 0);
                break;
            }
    }
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) CornerCase(org.apache.datasketches.SetOperationCornerCases.CornerCase) AnotbAction(org.apache.datasketches.SetOperationCornerCases.AnotbAction)

Example 37 with SketchesArgumentException

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

the class DirectQuickSelectSketchTest method checkBadSerVer.

// (expectedExceptions = SketchesArgumentException.class)
@Test
public void checkBadSerVer() {
    int k = 512;
    try (WritableHandle h = makeNativeMemory(k)) {
        WritableMemory mem = h.getWritable();
        UpdateSketch usk = UpdateSketch.builder().setNominalEntries(k).build(mem);
        // for internal checks
        DirectQuickSelectSketch sk1 = (DirectQuickSelectSketch) usk;
        assertTrue(usk.isEmpty());
        for (int i = 0; i < k; i++) {
            usk.update(i);
        }
        assertFalse(usk.isEmpty());
        assertEquals(usk.getEstimate(), k, 0.0);
        assertEquals(sk1.getRetainedEntries(false), k);
        // corrupt the SerVer byte
        mem.putByte(SER_VER_BYTE, (byte) 0);
        Sketch.wrap(mem);
    } catch (final Exception e) {
        if (e instanceof SketchesArgumentException) {
        } else {
            throw new RuntimeException(e);
        }
    }
}
Also used : WritableHandle(org.apache.datasketches.memory.WritableHandle) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) WritableMemory(org.apache.datasketches.memory.WritableMemory) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) Test(org.testng.annotations.Test)

Example 38 with SketchesArgumentException

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

the class DirectQuickSelectSketchTest method checkConstructorMemTooSmall.

// (expectedExceptions = SketchesArgumentException.class)
@Test
public void checkConstructorMemTooSmall() {
    int k = 16;
    try (WritableHandle h = makeNativeMemory(k / 2)) {
        WritableMemory mem = h.getWritable();
        UpdateSketch.builder().setNominalEntries(k).build(mem);
    } catch (final Exception e) {
        if (e instanceof SketchesArgumentException) {
        } else {
            throw new RuntimeException(e);
        }
    }
}
Also used : WritableHandle(org.apache.datasketches.memory.WritableHandle) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) WritableMemory(org.apache.datasketches.memory.WritableMemory) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) Test(org.testng.annotations.Test)

Example 39 with SketchesArgumentException

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

the class DirectQuickSelectSketchTest method checkDQStoCompactEmptyForms.

@Test
public void checkDQStoCompactEmptyForms() {
    int k = 512;
    try (WritableHandle h = makeNativeMemory(k)) {
        WritableMemory mem = h.getWritable();
        UpdateSketch usk = UpdateSketch.builder().setNominalEntries(k).build(mem);
        // empty
        // exercise toString
        usk.toString(false, true, 0, false);
        assertEquals(usk.getClass().getSimpleName(), "DirectQuickSelectSketch");
        double uskEst = usk.getEstimate();
        double uskLB = usk.getLowerBound(2);
        double uskUB = usk.getUpperBound(2);
        assertEquals(usk.isEstimationMode(), false);
        // compact form
        int bytes = usk.getCompactBytes();
        assertEquals(bytes, 8);
        byte[] memArr2 = new byte[bytes];
        WritableMemory mem2 = WritableMemory.writableWrap(memArr2);
        CompactSketch csk2 = usk.compact(false, mem2);
        assertEquals(csk2.getEstimate(), uskEst);
        assertEquals(csk2.getLowerBound(2), uskLB);
        assertEquals(csk2.getUpperBound(2), uskUB);
        assertEquals(csk2.isEmpty(), true);
        assertEquals(csk2.isEstimationMode(), false);
        assertEquals(csk2.getClass().getSimpleName(), "DirectCompactSketch");
        CompactSketch csk3 = usk.compact(true, mem2);
        csk3.toString(false, true, 0, false);
        csk3.toString();
        assertEquals(csk3.getEstimate(), uskEst);
        assertEquals(csk3.getLowerBound(2), uskLB);
        assertEquals(csk3.getUpperBound(2), uskUB);
        assertEquals(csk3.isEmpty(), true);
        assertEquals(csk3.isEstimationMode(), false);
        assertEquals(csk3.getClass().getSimpleName(), "DirectCompactSketch");
    } catch (final Exception e) {
        // if (e instanceof SketchesArgumentException) {}
        throw new RuntimeException(e);
    }
}
Also used : WritableHandle(org.apache.datasketches.memory.WritableHandle) WritableMemory(org.apache.datasketches.memory.WritableMemory) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) Test(org.testng.annotations.Test)

Example 40 with SketchesArgumentException

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

the class DirectIntersectionTest method checkIntersectionNull.

@Test
public void checkIntersectionNull() {
    final int lgK = 9;
    final int k = 1 << lgK;
    final int memBytes = getMaxIntersectionBytes(k);
    final byte[] memArr = new byte[memBytes];
    final WritableMemory iMem = WritableMemory.writableWrap(memArr);
    final Intersection inter = SetOperation.builder().buildIntersection(iMem);
    final UpdateSketch sk = null;
    try {
        inter.intersect(sk);
        fail();
    } catch (final SketchesArgumentException e) {
    }
    try {
        inter.intersect(sk, sk);
        fail();
    } catch (final SketchesArgumentException e) {
    }
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Aggregations

SketchesArgumentException (org.apache.datasketches.SketchesArgumentException)64 WritableMemory (org.apache.datasketches.memory.WritableMemory)38 Test (org.testng.annotations.Test)29 Family (org.apache.datasketches.Family)14 Memory (org.apache.datasketches.memory.Memory)13 Family.idToFamily (org.apache.datasketches.Family.idToFamily)10 SketchesReadOnlyException (org.apache.datasketches.SketchesReadOnlyException)7 WritableHandle (org.apache.datasketches.memory.WritableHandle)6 ResizeFactor (org.apache.datasketches.ResizeFactor)5 ArrayOfLongsSerDe (org.apache.datasketches.ArrayOfLongsSerDe)4 AnotbAction (org.apache.datasketches.SetOperationCornerCases.AnotbAction)3 CornerCase (org.apache.datasketches.SetOperationCornerCases.CornerCase)3 PreambleUtil.extractResizeFactor (org.apache.datasketches.sampling.PreambleUtil.extractResizeFactor)3 ArrayList (java.util.ArrayList)2 SketchesStateException (org.apache.datasketches.SketchesStateException)2 PreambleUtil.insertLgResizeFactor (org.apache.datasketches.theta.PreambleUtil.insertLgResizeFactor)2 UpdateSketch (org.apache.datasketches.theta.UpdateSketch)2 UpdateSketchBuilder (org.apache.datasketches.theta.UpdateSketchBuilder)2 BigDecimal (java.math.BigDecimal)1 ArrayOfBooleansSerDe (org.apache.datasketches.ArrayOfBooleansSerDe)1