Search in sources :

Example 11 with SketchesArgumentException

use of com.yahoo.sketches.SketchesArgumentException in project sketches-core by DataSketches.

the class IntersectionImpl method update.

@Override
public void update(final Sketch sketchIn) {
    final boolean firstCall = curCount_ < 0;
    final Object memObj = mem_ != null ? mem_.getArray() : null;
    final long memAdd = mem_ != null ? mem_.getCumulativeOffset(0) : 0;
    //Corner cases
    if (sketchIn == null) {
        //null -> Th = 1.0, count = 0, empty = true
        //No seedHash to check
        //Because of the def of null above and the Empty Rule (which is OR) empty_ must be null.
        empty_ = true;
        //if Nth call, stays the same
        thetaLong_ = firstCall ? Long.MAX_VALUE : thetaLong_;
        curCount_ = 0;
        if (mem_ != null) {
            PreambleUtil.setEmpty(memObj, memAdd);
            insertThetaLong(memObj, memAdd, thetaLong_);
            insertCurCount(memObj, memAdd, 0);
        }
        return;
    }
    //Checks
    Util.checkSeedHashes(seedHash_, sketchIn.getSeedHash());
    //Theta rule
    thetaLong_ = min(thetaLong_, sketchIn.getThetaLong());
    //Empty rule
    empty_ = empty_ || sketchIn.isEmpty();
    if (mem_ != null) {
        insertThetaLong(memObj, memAdd, thetaLong_);
        if (empty_) {
            PreambleUtil.setEmpty(memObj, memAdd);
        } else {
            clearEmpty(memObj, memAdd);
        }
    }
    final int sketchInEntries = sketchIn.getRetainedEntries(true);
    if ((curCount_ == 0) || (sketchInEntries == 0)) {
        //Cases 1,2,3,5
        //All future intersections result in zero data, but theta can still be reduced.
        curCount_ = 0;
        if (mem_ != null) {
            insertCurCount(memObj, memAdd, 0);
        }
        //No need for a HT. Don't bother clearing mem if valid
        hashTable_ = null;
    } else if (firstCall) {
        //Case 4: Clone the incoming sketch
        curCount_ = sketchIn.getRetainedEntries(true);
        final int requiredLgArrLongs = computeMinLgArrLongsFromCount(curCount_);
        //prior only used in error message
        final int priorLgArrLongs = lgArrLongs_;
        lgArrLongs_ = requiredLgArrLongs;
        if (mem_ != null) {
            //Off heap, check if current dstMem is large enough
            insertCurCount(memObj, memAdd, curCount_);
            insertLgArrLongs(memObj, memAdd, lgArrLongs_);
            if (requiredLgArrLongs <= maxLgArrLongs_) {
                //OK
                //clear only what required
                mem_.clear(CONST_PREAMBLE_LONGS << 3, 8 << lgArrLongs_);
            } else {
                //not enough space in dstMem //TODO move to request model?
                throw new SketchesArgumentException("Insufficient dstMem hash table space: " + (1 << requiredLgArrLongs) + " > " + (1 << priorLgArrLongs));
            }
        } else {
            //On the heap, allocate a HT
            hashTable_ = new long[1 << lgArrLongs_];
        }
        moveDataToTgt(sketchIn.getCache(), curCount_);
    } else {
        //Case 6: Perform full intersect
        //Sets resulting hashTable, curCount and adjusts lgArrLongs
        performIntersect(sketchIn);
    }
}
Also used : SketchesArgumentException(com.yahoo.sketches.SketchesArgumentException)

Example 12 with SketchesArgumentException

use of com.yahoo.sketches.SketchesArgumentException in project sketches-core by DataSketches.

the class DoublesUnionImplTest method checkResultViaMemory.

@Test
public void checkResultViaMemory() {
    // empty gadget
    final DoublesUnion union = DoublesUnion.builder().build();
    // memory too small
    WritableMemory mem = WritableMemory.allocate(1);
    try {
        union.getResult(mem);
        fail();
    } catch (final SketchesArgumentException e) {
    // expected
    }
    // sufficient memory
    mem = WritableMemory.allocate(8);
    DoublesSketch result = union.getResult(mem);
    assertTrue(result.isEmpty());
    final int k = 128;
    final int n = 1392;
    mem = WritableMemory.allocate(DoublesSketch.getUpdatableStorageBytes(k, n));
    final DoublesSketch qs = buildAndLoadQS(k, n);
    union.update(qs);
    result = union.getResult(mem);
    DoublesSketchTest.testSketchEquality(result, qs);
}
Also used : SketchesArgumentException(com.yahoo.sketches.SketchesArgumentException) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 13 with SketchesArgumentException

use of com.yahoo.sketches.SketchesArgumentException in project sketches-core by DataSketches.

the class SetOperation method wrap.

/**
   * Wrap takes the SetOperation image in Memory and refers to it directly.
   * There is no data copying onto the java heap.
   * Only "Direct" SetOperations that have been explicity stored as direct can be wrapped.
   * @param srcMem an image of a SetOperation where the hash of the given seed matches the image seed hash.
   * <a href="{@docRoot}/resources/dictionary.html#mem">See Memory</a>
   * @param seed <a href="{@docRoot}/resources/dictionary.html#seed">See Update Hash Seed</a>.
   * @return a SetOperation backed by the given Memory
   */
public static SetOperation wrap(final Memory srcMem, final long seed) {
    final byte famID = srcMem.getByte(FAMILY_BYTE);
    final Family family = idToFamily(famID);
    final int serVer = srcMem.getByte(SER_VER_BYTE);
    if (serVer != 3) {
        throw new SketchesArgumentException("SerVer must be 3: " + serVer);
    }
    switch(family) {
        case UNION:
            {
                return UnionImpl.wrapInstance(srcMem, seed);
            }
        case INTERSECTION:
            {
                return IntersectionImplR.wrapInstance(srcMem, seed);
            }
        default:
            throw new SketchesArgumentException("SetOperation cannot wrap family: " + family.toString());
    }
}
Also used : SketchesArgumentException(com.yahoo.sketches.SketchesArgumentException) Family(com.yahoo.sketches.Family) Family.idToFamily(com.yahoo.sketches.Family.idToFamily)

Example 14 with SketchesArgumentException

use of com.yahoo.sketches.SketchesArgumentException in project sketches-core by DataSketches.

the class SetOperation method wrap.

/**
   * Wrap takes the SetOperation image in Memory and refers to it directly.
   * There is no data copying onto the java heap.
   * Only "Direct" SetOperations that have been explicity stored as direct can be wrapped.
   * @param srcMem an image of a SetOperation where the hash of the given seed matches the image seed hash.
   * <a href="{@docRoot}/resources/dictionary.html#mem">See Memory</a>
   * @param seed <a href="{@docRoot}/resources/dictionary.html#seed">See Update Hash Seed</a>.
   * @return a SetOperation backed by the given Memory
   */
public static SetOperation wrap(final WritableMemory srcMem, final long seed) {
    final byte famID = srcMem.getByte(FAMILY_BYTE);
    final Family family = idToFamily(famID);
    final int serVer = srcMem.getByte(SER_VER_BYTE);
    if (serVer != 3) {
        throw new SketchesArgumentException("SerVer must be 3: " + serVer);
    }
    switch(family) {
        case UNION:
            {
                return UnionImpl.wrapInstance(srcMem, seed);
            }
        case INTERSECTION:
            {
                return IntersectionImpl.wrapInstance(srcMem, seed);
            }
        default:
            throw new SketchesArgumentException("SetOperation cannot wrap family: " + family.toString());
    }
}
Also used : SketchesArgumentException(com.yahoo.sketches.SketchesArgumentException) Family(com.yahoo.sketches.Family) Family.idToFamily(com.yahoo.sketches.Family.idToFamily)

Example 15 with SketchesArgumentException

use of com.yahoo.sketches.SketchesArgumentException in project sketches-core by DataSketches.

the class VarOptItemsUnion method heapify.

/**
   * Instantiates a Union from Memory
   *
   * @param <T> The type of item this sketch contains
   * @param srcMem Memory object containing a serialized union
   * @param serDe An instance of ArrayOfItemsSerDe
   * @return A VarOptItemsUnion created from the provided Memory
   */
public static <T> VarOptItemsUnion<T> heapify(final Memory srcMem, final ArrayOfItemsSerDe<T> serDe) {
    Family.VAROPT_UNION.checkFamilyID(srcMem.getByte(FAMILY_BYTE));
    long n = 0;
    double outerTauNum = 0.0;
    long outerTauDenom = 0;
    // If we have read-only memory on heap (aka not-direct) then the backing array exists but is
    // not available to us, so srcMem.array() will fail. In that case, we can use the (slower)
    // Memory interface methods to read values directly.
    final int numPreLongs = extractPreLongs(srcMem);
    final int serVer = extractSerVer(srcMem);
    final boolean isEmpty = (extractFlags(srcMem) & EMPTY_FLAG_MASK) != 0;
    final int maxK = extractMaxK(srcMem);
    if (!isEmpty) {
        n = extractN(srcMem);
        outerTauNum = extractOuterTauNumerator(srcMem);
        outerTauDenom = extractOuterTauDenominator(srcMem);
    }
    if (serVer != SER_VER) {
        throw new SketchesArgumentException("Possible Corruption: Ser Ver must be " + SER_VER + ": " + serVer);
    }
    final boolean preLongsEqMin = (numPreLongs == Family.VAROPT_UNION.getMinPreLongs());
    final boolean preLongsEqMax = (numPreLongs == Family.VAROPT_UNION.getMaxPreLongs());
    if (!preLongsEqMin && !preLongsEqMax) {
        throw new SketchesArgumentException("Possible corruption: Non-empty union with only " + Family.VAROPT_UNION.getMinPreLongs() + "preLongs");
    }
    final VarOptItemsUnion<T> viu = new VarOptItemsUnion<>(maxK);
    if (isEmpty) {
        viu.gadget_ = VarOptItemsSketch.newInstanceAsGadget(maxK);
    } else {
        viu.n_ = n;
        viu.outerTauNumer = outerTauNum;
        viu.outerTauDenom = outerTauDenom;
        final int preLongBytes = numPreLongs << 3;
        final Memory sketchMem = srcMem.region(preLongBytes, srcMem.getCapacity() - preLongBytes);
        viu.gadget_ = VarOptItemsSketch.heapify(sketchMem, serDe);
    }
    return viu;
}
Also used : SketchesArgumentException(com.yahoo.sketches.SketchesArgumentException) Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory)

Aggregations

SketchesArgumentException (com.yahoo.sketches.SketchesArgumentException)41 WritableMemory (com.yahoo.memory.WritableMemory)22 Test (org.testng.annotations.Test)12 Family (com.yahoo.sketches.Family)11 Memory (com.yahoo.memory.Memory)10 ResizeFactor (com.yahoo.sketches.ResizeFactor)6 Family.idToFamily (com.yahoo.sketches.Family.idToFamily)5 ArrayOfLongsSerDe (com.yahoo.sketches.ArrayOfLongsSerDe)4 PreambleUtil.extractResizeFactor (com.yahoo.sketches.sampling.PreambleUtil.extractResizeFactor)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ArrayList (java.util.ArrayList)2 ArrayOfBooleansSerDe (com.yahoo.sketches.ArrayOfBooleansSerDe)1 ArrayOfNumbersSerDe (com.yahoo.sketches.ArrayOfNumbersSerDe)1 ArrayOfStringsSerDe (com.yahoo.sketches.ArrayOfStringsSerDe)1 Util.checkIsCompactMemory (com.yahoo.sketches.quantiles.Util.checkIsCompactMemory)1 Method (java.lang.reflect.Method)1 BigDecimal (java.math.BigDecimal)1