Search in sources :

Example 31 with SketchesArgumentException

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

the class VarOptItemsSketchTest method checkBadPreLongs.

@Test
public void checkBadPreLongs() {
    final VarOptItemsSketch<Long> sketch = getUnweightedLongsVIS(32, 33);
    final byte[] bytes = sketch.toByteArray(new ArrayOfLongsSerDe());
    final WritableMemory mem = WritableMemory.writableWrap(bytes);
    // corrupt the preLongs count to 0
    mem.putByte(PREAMBLE_LONGS_BYTE, (byte) (Family.VAROPT.getMinPreLongs() - 1));
    try {
        VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
        fail();
    } catch (final SketchesArgumentException e) {
    // expected
    }
    // corrupt the preLongs count to 2
    mem.putByte(PREAMBLE_LONGS_BYTE, (byte) 2);
    try {
        VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
        fail();
    } catch (final SketchesArgumentException e) {
    // expected
    }
    // corrupt the preLongs count to be too large
    mem.putByte(PREAMBLE_LONGS_BYTE, (byte) (Family.VAROPT.getMaxPreLongs() + 1));
    try {
        VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
        fail();
    } catch (final SketchesArgumentException e) {
    // expected
    }
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) ArrayOfLongsSerDe(org.apache.datasketches.ArrayOfLongsSerDe) WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 32 with SketchesArgumentException

use of org.apache.datasketches.SketchesArgumentException 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 33 with SketchesArgumentException

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

the class SetOperation method heapify.

/**
 * Heapify takes the SetOperation image in Memory and instantiates an on-heap
 * SetOperation using the given expectedSeed.
 * The resulting SetOperation will not retain any link to the source Memory.
 *
 * <p>Note: Only certain set operators during stateful operations can be serialized and thus
 * heapified.</p>
 *
 * @param srcMem an image of a SetOperation where the hash of the given expectedSeed matches the image seed hash.
 * <a href="{@docRoot}/resources/dictionary.html#mem">See Memory</a>
 * @param expectedSeed the seed used to validate the given Memory image.
 * <a href="{@docRoot}/resources/dictionary.html#seed">See Update Hash Seed</a>.
 * @return a Heap-based SetOperation from the given Memory
 */
public static SetOperation heapify(final Memory srcMem, final long expectedSeed) {
    final byte famID = srcMem.getByte(FAMILY_BYTE);
    final Family family = idToFamily(famID);
    switch(family) {
        case UNION:
            {
                return UnionImpl.heapifyInstance(srcMem, expectedSeed);
            }
        case INTERSECTION:
            {
                return IntersectionImpl.heapifyInstance(srcMem, expectedSeed);
            }
        default:
            {
                throw new SketchesArgumentException("SetOperation cannot heapify family: " + family.toString());
            }
    }
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) Family.idToFamily(org.apache.datasketches.Family.idToFamily) Family(org.apache.datasketches.Family)

Example 34 with SketchesArgumentException

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

the class UpdateSketch method checkUnionQuickSelectFamily.

static void checkUnionQuickSelectFamily(final Memory mem, final int preambleLongs, final int lgNomLongs) {
    // Check Family
    // byte 2
    final int familyID = extractFamilyID(mem);
    final Family family = Family.idToFamily(familyID);
    if (family.equals(Family.UNION)) {
        if (preambleLongs != Family.UNION.getMinPreLongs()) {
            throw new SketchesArgumentException("Possible corruption: Invalid PreambleLongs value for UNION: " + preambleLongs);
        }
    } else if (family.equals(Family.QUICKSELECT)) {
        if (preambleLongs != Family.QUICKSELECT.getMinPreLongs()) {
            throw new SketchesArgumentException("Possible corruption: Invalid PreambleLongs value for QUICKSELECT: " + preambleLongs);
        }
    } else {
        throw new SketchesArgumentException("Possible corruption: Invalid Family: " + family.toString());
    }
    // Check lgNomLongs
    if (lgNomLongs < MIN_LG_NOM_LONGS) {
        throw new SketchesArgumentException("Possible corruption: Current Memory lgNomLongs < min required size: " + lgNomLongs + " < " + MIN_LG_NOM_LONGS);
    }
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) Family(org.apache.datasketches.Family)

Example 35 with SketchesArgumentException

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

the class UpdateSketch method wrap.

/**
 * Wrap takes the sketch image in Memory and refers to it directly. There is no data copying onto
 * the java heap. Only "Direct" Serialization Version 3 (i.e, OpenSource) sketches that have
 * been explicitly stored as direct objects can be wrapped.
 * An attempt to "wrap" earlier version sketches will result in a "heapified", normal
 * Java Heap version of the sketch where all data will be copied to the heap.
 * @param srcMem an image of a Sketch where the image seed hash matches the given seed hash.
 * <a href="{@docRoot}/resources/dictionary.html#mem">See Memory</a>
 * @param expectedSeed the seed used to validate the given Memory image.
 * <a href="{@docRoot}/resources/dictionary.html#seed">See Update Hash Seed</a>.
 * Compact sketches store a 16-bit hash of the seed, but not the seed itself.
 * @return a UpdateSketch backed by the given Memory
 */
public static UpdateSketch wrap(final WritableMemory srcMem, final long expectedSeed) {
    final int preLongs = srcMem.getByte(PREAMBLE_LONGS_BYTE) & 0X3F;
    final int serVer = srcMem.getByte(SER_VER_BYTE) & 0XFF;
    final int familyID = srcMem.getByte(FAMILY_BYTE) & 0XFF;
    final Family family = Family.idToFamily(familyID);
    if (family != Family.QUICKSELECT) {
        throw new SketchesArgumentException("A " + family + " sketch cannot be wrapped as an UpdateSketch.");
    }
    if ((serVer == 3) && (preLongs == 3)) {
        return DirectQuickSelectSketch.writableWrap(srcMem, expectedSeed);
    } else {
        throw new SketchesArgumentException("Corrupted: An UpdateSketch image: must have SerVer = 3 and preLongs = 3");
    }
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) Family(org.apache.datasketches.Family)

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