Search in sources :

Example 21 with SketchesArgumentException

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

the class DoublesSketch method putMemory.

/**
 * Puts the current sketch into the given Memory if there is sufficient space, otherwise,
 * throws an error.
 *
 * @param dstMem the given memory.
 * @param compact if true, compacts and sorts the base buffer, which optimizes merge
 *                performance at the cost of slightly increased serialization time.
 */
public void putMemory(final WritableMemory dstMem, final boolean compact) {
    if (isDirect() && isCompact() == compact) {
        final Memory srcMem = getMemory();
        srcMem.copyTo(0, dstMem, 0, getStorageBytes());
    } else {
        final byte[] byteArr = toByteArray(compact);
        final int arrLen = byteArr.length;
        final long memCap = dstMem.getCapacity();
        if (memCap < arrLen) {
            throw new SketchesArgumentException("Destination Memory not large enough: " + memCap + " < " + arrLen);
        }
        dstMem.putByteArray(0, byteArr, 0, arrLen);
    }
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) Util.checkIsCompactMemory(org.apache.datasketches.quantiles.Util.checkIsCompactMemory) Memory(org.apache.datasketches.memory.Memory) WritableMemory(org.apache.datasketches.memory.WritableMemory)

Example 22 with SketchesArgumentException

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

the class ItemsSketch method getInstance.

/**
 * Heapifies the given srcMem, which must be a Memory image of a ItemsSketch
 * @param <T> type of item
 * @param srcMem a Memory image of a sketch.
 * <a href="{@docRoot}/resources/dictionary.html#mem">See Memory</a>
 * @param comparator to compare items
 * @param serDe an instance of ArrayOfItemsSerDe
 * @return a ItemsSketch on the Java heap.
 */
public static <T> ItemsSketch<T> getInstance(final Memory srcMem, final Comparator<? super T> comparator, final ArrayOfItemsSerDe<T> serDe) {
    final long memCapBytes = srcMem.getCapacity();
    if (memCapBytes < 8) {
        throw new SketchesArgumentException("Memory too small: " + memCapBytes);
    }
    final int preambleLongs = extractPreLongs(srcMem);
    final int serVer = extractSerVer(srcMem);
    final int familyID = extractFamilyID(srcMem);
    final int flags = extractFlags(srcMem);
    final int k = extractK(srcMem);
    ItemsUtil.checkItemsSerVer(serVer);
    if (serVer == 3 && (flags & COMPACT_FLAG_MASK) == 0) {
        throw new SketchesArgumentException("Non-compact Memory images are not supported.");
    }
    final boolean empty = Util.checkPreLongsFlagsCap(preambleLongs, flags, memCapBytes);
    Util.checkFamilyID(familyID);
    // checks k
    final ItemsSketch<T> qs = getInstance(k, comparator);
    if (empty) {
        return qs;
    }
    // Not empty, must have valid preamble + min, max
    final long n = extractN(srcMem);
    // can't check memory capacity here, not enough information
    // for min, max
    final int extra = 2;
    final int numMemItems = Util.computeRetainedItems(k, n) + extra;
    // set class members
    qs.n_ = n;
    qs.combinedBufferItemCapacity_ = Util.computeCombinedBufferItemCapacity(k, n);
    qs.baseBufferCount_ = computeBaseBufferItems(k, n);
    qs.bitPattern_ = computeBitPattern(k, n);
    qs.combinedBuffer_ = new Object[qs.combinedBufferItemCapacity_];
    final int srcMemItemsOffsetBytes = preambleLongs * Long.BYTES;
    final Memory mReg = srcMem.region(srcMemItemsOffsetBytes, srcMem.getCapacity() - srcMemItemsOffsetBytes);
    final T[] itemsArray = serDe.deserializeFromMemory(mReg, numMemItems);
    qs.itemsArrayToCombinedBuffer(itemsArray);
    return qs;
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) Memory(org.apache.datasketches.memory.Memory) WritableMemory(org.apache.datasketches.memory.WritableMemory)

Example 23 with SketchesArgumentException

use of org.apache.datasketches.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;
    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(org.apache.datasketches.SketchesArgumentException) Memory(org.apache.datasketches.memory.Memory) WritableMemory(org.apache.datasketches.memory.WritableMemory)

Example 24 with SketchesArgumentException

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

the class HeapAlphaSketchTest method checkMemDeSerExceptions.

@Test
public void checkMemDeSerExceptions() {
    int k = 1024;
    UpdateSketch sk1 = UpdateSketch.builder().setFamily(ALPHA).setNominalEntries(k).build();
    // forces preLongs to 3
    sk1.update(1L);
    byte[] bytearray1 = sk1.toByteArray();
    WritableMemory mem = WritableMemory.writableWrap(bytearray1);
    long pre0 = mem.getLong(0);
    // Corrupt PreLongs
    tryBadMem(mem, PREAMBLE_LONGS_BYTE, 2);
    // restore
    mem.putLong(0, pre0);
    // Corrupt SerVer
    tryBadMem(mem, SER_VER_BYTE, 2);
    // restore
    mem.putLong(0, pre0);
    // Corrupt Family
    tryBadMem(mem, FAMILY_BYTE, 2);
    // restore
    mem.putLong(0, pre0);
    // Corrupt READ_ONLY to true
    tryBadMem(mem, FLAGS_BYTE, 2);
    // restore
    mem.putLong(0, pre0);
    final long origThetaLong = mem.getLong(THETA_LONG);
    try {
        // Corrupt the theta value
        mem.putLong(THETA_LONG, Long.MAX_VALUE / 2);
        HeapAlphaSketch.heapifyInstance(mem, DEFAULT_UPDATE_SEED);
        fail();
    } catch (SketchesArgumentException e) {
    // expected
    }
    // restore theta
    mem.putLong(THETA_LONG, origThetaLong);
    byte[] byteArray2 = new byte[bytearray1.length - 1];
    WritableMemory mem2 = WritableMemory.writableWrap(byteArray2);
    mem.copyTo(0, mem2, 0, mem2.getCapacity());
    try {
        HeapAlphaSketch.heapifyInstance(mem2, DEFAULT_UPDATE_SEED);
        fail();
    } catch (SketchesArgumentException e) {
    // expected
    }
    // force ResizeFactor.X1, and allocated capacity too small
    insertLgResizeFactor(mem, ResizeFactor.X1.lg());
    UpdateSketch usk = HeapAlphaSketch.heapifyInstance(mem, DEFAULT_UPDATE_SEED);
    ResizeFactor rf = usk.getResizeFactor();
    // ResizeFactor recovered to X2, which always works.
    assertEquals(rf, ResizeFactor.X2);
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) WritableMemory(org.apache.datasketches.memory.WritableMemory) ResizeFactor(org.apache.datasketches.ResizeFactor) PreambleUtil.insertLgResizeFactor(org.apache.datasketches.theta.PreambleUtil.insertLgResizeFactor) Test(org.testng.annotations.Test)

Example 25 with SketchesArgumentException

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

the class SketchTest method checkCompactSizeAndFlagsOnHeapify.

@Test
public void checkCompactSizeAndFlagsOnHeapify() {
    WritableMemory wmem = createCompactSketchMemory(16, 32);
    Sketch sk = Sketch.heapify(wmem);
    assertTrue(sk instanceof CompactSketch);
    int flags = PreambleUtil.extractFlags(wmem);
    int flagsNoCompact = flags & ~READ_ONLY_FLAG_MASK;
    PreambleUtil.insertFlags(wmem, flagsNoCompact);
    try {
        sk = Sketch.heapify(wmem);
        fail();
    } catch (SketchesArgumentException e) {
    }
    wmem = WritableMemory.allocate(7);
    PreambleUtil.insertSerVer(wmem, 3);
    // PreambleUtil.insertFamilyID(wmem, 3);
    try {
        sk = Sketch.heapify(wmem);
        fail();
    } catch (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