Search in sources :

Example 1 with SketchesStateException

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

the class DirectAuxHashMapTest method checkMustReplace.

@Test
public void checkMustReplace() {
    int lgK = 7;
    int bytes = HllSketch.getMaxUpdatableSerializationBytes(lgK, TgtHllType.HLL_4);
    WritableMemory wmem = WritableMemory.allocate(bytes);
    HllSketch sk = new HllSketch(lgK, TgtHllType.HLL_4, wmem);
    for (int i = 0; i < 25; i++) {
        sk.update(i);
    }
    DirectHllArray dHllArr = (DirectHllArray) sk.hllSketchImpl;
    AuxHashMap map = dHllArr.getNewAuxHashMap();
    map.mustAdd(100, 5);
    int val = map.mustFindValueFor(100);
    assertEquals(val, 5);
    map.mustReplace(100, 10);
    val = map.mustFindValueFor(100);
    assertEquals(val, 10);
    assertTrue(map.isMemory());
    assertFalse(map.isOffHeap());
    assertNull(map.copy());
    assertNull(map.getAuxIntArr());
    try {
        map.mustAdd(100, 12);
        fail();
    } catch (SketchesStateException e) {
    // expected
    }
    try {
        map.mustFindValueFor(101);
        fail();
    } catch (SketchesStateException e) {
    // expected
    }
    try {
        map.mustReplace(101, 5);
        fail();
    } catch (SketchesStateException e) {
    // expected
    }
}
Also used : WritableMemory(org.apache.datasketches.memory.WritableMemory) SketchesStateException(org.apache.datasketches.SketchesStateException) Test(org.testng.annotations.Test)

Example 2 with SketchesStateException

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

the class PreambleUtilTest method checkStreamErrors.

@Test
public void checkStreamErrors() {
    WritableMemory wmem = WritableMemory.allocate(4 * 10);
    putEmptyMerged(wmem, (byte) 12, defaultSeedHash);
    try {
        getSvStreamOffset(wmem);
        fail();
    } catch (SketchesArgumentException e) {
    }
    wmem.putByte(5, (byte) (7 << 2));
    try {
        getSvStreamOffset(wmem);
        fail();
    } catch (SketchesStateException e) {
    }
    wmem.putByte(5, (byte) 0);
    try {
        getWStreamOffset(wmem);
        fail();
    } catch (SketchesArgumentException e) {
    }
    wmem.putByte(5, (byte) (7 << 2));
    try {
        getWStreamOffset(wmem);
        fail();
    } catch (SketchesStateException e) {
    }
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) WritableMemory(org.apache.datasketches.memory.WritableMemory) SketchesStateException(org.apache.datasketches.SketchesStateException) Test(org.testng.annotations.Test)

Example 3 with SketchesStateException

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

the class ToByteArrayImpl method toCouponByteArray.

// To byte array for coupons
static final byte[] toCouponByteArray(final AbstractCoupons impl, final boolean dstCompact) {
    final int srcCouponCount = impl.getCouponCount();
    final int srcLgCouponArrInts = impl.getLgCouponArrInts();
    final int srcCouponArrInts = 1 << srcLgCouponArrInts;
    final byte[] byteArrOut;
    final boolean list = impl.getCurMode() == CurMode.LIST;
    // prepare switch
    final int sw = (impl.isMemory() ? 0 : 4) | (impl.isCompact() ? 0 : 2) | (dstCompact ? 0 : 1);
    switch(sw) {
        case 0:
            {
                // Src Memory, Src Compact, Dst Compact
                final Memory srcMem = impl.getMemory();
                final int bytesOut = impl.getMemDataStart() + (srcCouponCount << 2);
                byteArrOut = new byte[bytesOut];
                srcMem.getByteArray(0, byteArrOut, 0, bytesOut);
                break;
            }
        case 1:
            {
                // Src Memory, Src Compact, Dst Updatable
                final int dataStart = impl.getMemDataStart();
                final int bytesOut = dataStart + (srcCouponArrInts << 2);
                byteArrOut = new byte[bytesOut];
                final WritableMemory memOut = WritableMemory.writableWrap(byteArrOut);
                copyCommonListAndSet(impl, memOut);
                insertCompactFlag(memOut, dstCompact);
                final int[] tgtCouponIntArr = new int[srcCouponArrInts];
                final PairIterator itr = impl.iterator();
                while (itr.nextValid()) {
                    final int pair = itr.getPair();
                    final int idx = find(tgtCouponIntArr, srcLgCouponArrInts, pair);
                    if (idx < 0) {
                        // found EMPTY
                        // insert
                        tgtCouponIntArr[~idx] = pair;
                        continue;
                    }
                    throw new SketchesStateException("Error: found duplicate.");
                }
                memOut.putIntArray(dataStart, tgtCouponIntArr, 0, srcCouponArrInts);
                if (list) {
                    insertListCount(memOut, srcCouponCount);
                } else {
                    insertHashSetCount(memOut, srcCouponCount);
                }
                break;
            }
        case 2:
            {
                // Src Memory, Src Updatable, Dst Compact
                final int dataStart = impl.getMemDataStart();
                final int bytesOut = dataStart + (srcCouponCount << 2);
                byteArrOut = new byte[bytesOut];
                final WritableMemory memOut = WritableMemory.writableWrap(byteArrOut);
                copyCommonListAndSet(impl, memOut);
                insertCompactFlag(memOut, dstCompact);
                final PairIterator itr = impl.iterator();
                int cnt = 0;
                while (itr.nextValid()) {
                    insertInt(memOut, dataStart + (cnt++ << 2), itr.getPair());
                }
                if (list) {
                    insertListCount(memOut, srcCouponCount);
                } else {
                    insertHashSetCount(memOut, srcCouponCount);
                }
                break;
            }
        case 3:
            {
                // Src Memory, Src Updatable, Dst Updatable
                final Memory srcMem = impl.getMemory();
                final int bytesOut = impl.getMemDataStart() + (srcCouponArrInts << 2);
                byteArrOut = new byte[bytesOut];
                srcMem.getByteArray(0, byteArrOut, 0, bytesOut);
                break;
            }
        case 6:
            {
                // Src Heap, Src Updatable, Dst Compact
                final int dataStart = impl.getMemDataStart();
                final int bytesOut = dataStart + (srcCouponCount << 2);
                byteArrOut = new byte[bytesOut];
                final WritableMemory memOut = WritableMemory.writableWrap(byteArrOut);
                copyCommonListAndSet(impl, memOut);
                insertCompactFlag(memOut, dstCompact);
                final PairIterator itr = impl.iterator();
                int cnt = 0;
                while (itr.nextValid()) {
                    insertInt(memOut, dataStart + (cnt++ << 2), itr.getPair());
                }
                if (list) {
                    insertListCount(memOut, srcCouponCount);
                } else {
                    insertHashSetCount(memOut, srcCouponCount);
                }
                break;
            }
        case 7:
            {
                // Src Heap, Src Updatable, Dst Updatable
                final int dataStart = impl.getMemDataStart();
                final int bytesOut = dataStart + (srcCouponArrInts << 2);
                byteArrOut = new byte[bytesOut];
                final WritableMemory memOut = WritableMemory.writableWrap(byteArrOut);
                copyCommonListAndSet(impl, memOut);
                memOut.putIntArray(dataStart, impl.getCouponIntArr(), 0, srcCouponArrInts);
                if (list) {
                    insertListCount(memOut, srcCouponCount);
                } else {
                    insertHashSetCount(memOut, srcCouponCount);
                }
                break;
            }
        default:
            throw new SketchesStateException("Corruption, should not happen: " + sw);
    }
    return byteArrOut;
}
Also used : Memory(org.apache.datasketches.memory.Memory) WritableMemory(org.apache.datasketches.memory.WritableMemory) WritableMemory(org.apache.datasketches.memory.WritableMemory) SketchesStateException(org.apache.datasketches.SketchesStateException)

Example 4 with SketchesStateException

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

the class AnotB method getCopyOfResultArraysTheta.

@SuppressWarnings("unchecked")
private static <S extends Summary> DataArrays<S> getCopyOfResultArraysTheta(final long minThetaLong, final int countA, final long[] hashArrA, final S[] summaryArrA, final org.apache.datasketches.theta.Sketch skB) {
    final DataArrays<S> daB = new DataArrays<>();
    // Rebuild/get hashtable of skB
    // read only
    final long[] hashTableB;
    final long[] hashCacheB;
    try {
        hashCacheB = (long[]) GET_CACHE.invoke(skB);
    } catch (final Exception e) {
        throw new SketchesStateException("Reflection Exception " + e);
    }
    if (skB instanceof org.apache.datasketches.theta.CompactSketch) {
        final int countB = skB.getRetainedEntries(true);
        hashTableB = convertToHashTable(hashCacheB, countB, minThetaLong, REBUILD_THRESHOLD);
    } else {
        hashTableB = hashCacheB;
    }
    // build temporary result arrays of skA
    final long[] tmpHashArrA = new long[countA];
    final S[] tmpSummaryArrA = Util.newSummaryArray(summaryArrA, countA);
    // search for non matches and build temp arrays
    final int lgHTBLen = simpleLog2OfLong(hashTableB.length);
    int nonMatches = 0;
    for (int i = 0; i < countA; i++) {
        final long hash = hashArrA[i];
        if (hash != 0 && hash < minThetaLong) {
            // skips hashes of A >= minTheta
            final int index = hashSearch(hashTableB, lgHTBLen, hash);
            if (index == -1) {
                // not found
                tmpHashArrA[nonMatches] = hash;
                tmpSummaryArrA[nonMatches] = (S) summaryArrA[i].copy();
                nonMatches++;
            }
        }
    }
    // trim the arrays
    daB.hashArr = Arrays.copyOfRange(tmpHashArrA, 0, nonMatches);
    daB.summaryArr = Arrays.copyOfRange(tmpSummaryArrA, 0, nonMatches);
    return daB;
}
Also used : SketchesStateException(org.apache.datasketches.SketchesStateException) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) SketchesStateException(org.apache.datasketches.SketchesStateException)

Example 5 with SketchesStateException

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

the class PreambleUtilTest method checkStreamErrors2.

@Test
public void checkStreamErrors2() {
    WritableMemory wmem = WritableMemory.allocate(4 * 10);
    int[] svStream = { 1 };
    int[] wStream = { 2 };
    try {
        putPinnedSlidingMerged(wmem, 4, 0, 1, 1, 1, 0, (short) 0, svStream, wStream);
        fail();
    } catch (SketchesStateException e) {
    }
    assertTrue(PreambleUtil.isCompressed(wmem));
}
Also used : WritableMemory(org.apache.datasketches.memory.WritableMemory) SketchesStateException(org.apache.datasketches.SketchesStateException) Test(org.testng.annotations.Test)

Aggregations

SketchesStateException (org.apache.datasketches.SketchesStateException)6 WritableMemory (org.apache.datasketches.memory.WritableMemory)4 Test (org.testng.annotations.Test)4 SketchesArgumentException (org.apache.datasketches.SketchesArgumentException)3 Memory (org.apache.datasketches.memory.Memory)1 UpdateSketch (org.apache.datasketches.theta.UpdateSketch)1 UpdateSketchBuilder (org.apache.datasketches.theta.UpdateSketchBuilder)1 Intersection (org.apache.datasketches.tuple.Intersection)1