Search in sources :

Example 51 with SketchesArgumentException

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

the class SingleItemSketchTest method checkSingleItemBadFlags.

@Test
public void checkSingleItemBadFlags() {
    final short defaultSeedHash = Util.computeSeedHash(DEFAULT_UPDATE_SEED);
    UpdateSketch sk1 = new UpdateSketchBuilder().build();
    sk1.update(1);
    WritableMemory wmem = WritableMemory.allocate(16);
    sk1.compact(true, wmem);
    // corrupt flags to zero
    wmem.putByte(5, (byte) 0);
    try {
        // fails due to corrupted flags bytes
        SingleItemSketch.heapify(wmem, defaultSeedHash);
        fail();
    } catch (SketchesArgumentException e) {
    }
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 52 with SketchesArgumentException

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

the class UpdateSketchTest method checkCorruption.

@Test
public void checkCorruption() {
    UpdateSketch sk = Sketches.updateSketchBuilder().build();
    sk.update(1);
    WritableMemory wmem = WritableMemory.writableWrap(sk.toByteArray());
    try {
        wmem.putByte(SER_VER_BYTE, (byte) 2);
        UpdateSketch.wrap(wmem, DEFAULT_UPDATE_SEED);
        fail();
    } catch (SketchesArgumentException e) {
    }
    try {
        wmem.putByte(SER_VER_BYTE, (byte) 3);
        wmem.putByte(PREAMBLE_LONGS_BYTE, (byte) 2);
        UpdateSketch.wrap(wmem, DEFAULT_UPDATE_SEED);
        fail();
    } catch (SketchesArgumentException e) {
    }
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 53 with SketchesArgumentException

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

the class HeapQuickSelectSketchTest method checkMemSerDeExceptions.

@Test
public void checkMemSerDeExceptions() {
    int k = 1024;
    UpdateSketch sk1 = UpdateSketch.builder().setFamily(QUICKSELECT).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, 1);
    // restore
    mem.putLong(0, pre0);
    // Corrupt READ_ONLY to true
    tryBadMem(mem, FLAGS_BYTE, 2);
    // restore
    mem.putLong(0, pre0);
    // Corrupt, Family to Union
    tryBadMem(mem, FAMILY_BYTE, 4);
    // 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);
        HeapQuickSelectSketch.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 {
        HeapQuickSelectSketch.heapifyInstance(mem2, DEFAULT_UPDATE_SEED);
        fail();
    } catch (SketchesArgumentException e) {
    // expected
    }
    // force ResizeFactor.X1, but allocated capacity too small
    insertLgResizeFactor(mem, ResizeFactor.X1.lg());
    UpdateSketch hqss = HeapQuickSelectSketch.heapifyInstance(mem, DEFAULT_UPDATE_SEED);
    // force-promote to X2
    assertEquals(hqss.getResizeFactor(), ResizeFactor.X2);
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 54 with SketchesArgumentException

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

the class SketchTest method checkCompactFlagsOnWrap.

@Test
public void checkCompactFlagsOnWrap() {
    WritableMemory wmem = createCompactSketchMemory(16, 32);
    Sketch sk = Sketch.wrap(wmem);
    assertTrue(sk instanceof CompactSketch);
    int flags = PreambleUtil.extractFlags(wmem);
    int flagsNoCompact = flags & ~COMPACT_FLAG_MASK;
    PreambleUtil.insertFlags(wmem, flagsNoCompact);
    try {
        sk = Sketch.wrap(wmem);
        fail();
    } catch (SketchesArgumentException e) {
    }
    int flagsNoReadOnly = flags & ~READ_ONLY_FLAG_MASK;
    PreambleUtil.insertFlags(wmem, flagsNoReadOnly);
    try {
        sk = Sketch.wrap(wmem);
        fail();
    } catch (SketchesArgumentException e) {
    }
    // repair to original
    PreambleUtil.insertFlags(wmem, flags);
    PreambleUtil.insertSerVer(wmem, 5);
    try {
        sk = Sketch.wrap(wmem);
        fail();
    } catch (SketchesArgumentException e) {
    }
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 55 with SketchesArgumentException

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

the class CompactSketch method wrap.

/**
 * Wrap takes the CompactSketch image in given Memory and refers to it directly.
 * There is no data copying onto the java heap.
 * The wrap operation enables fast read-only merging and access to all the public read-only API.
 *
 * <p>Only "Direct" Serialization Version 3 (i.e, OpenSource) sketches that have
 * been explicitly stored as direct sketches can be wrapped.
 * Wrapping earlier serial version sketches will result in a heapify operation.
 * These early versions were never designed to "wrap".</p>
 *
 * <p>Wrapping any subclass of this class that is empty or contains only a single item will
 * result in heapified forms of empty and single item sketch respectively.
 * This is actually faster and consumes less overall memory.</p>
 *
 * <p>This method assumes that the sketch image was created with the correct hash seed, so it is not checked.
 * However, Serial Version 1 sketch images do not have a seedHash field,
 * so the resulting on-heap CompactSketch will be given the hash of the DEFAULT_UPDATE_SEED.</p>
 *
 * @param srcMem an image of a Sketch.
 * <a href="{@docRoot}/resources/dictionary.html#mem">See Memory</a>.
 * @return a CompactSketch backed by the given Memory except as above.
 */
public static CompactSketch wrap(final Memory srcMem) {
    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.COMPACT) {
        throw new IllegalArgumentException("Corrupted: " + family + " is not Compact!");
    }
    if (serVer == 3) {
        if (PreambleUtil.isEmptyFlag(srcMem)) {
            return EmptyCompactSketch.getHeapInstance(srcMem);
        }
        final short memSeedHash = (short) extractSeedHash(srcMem);
        if (otherCheckForSingleItem(srcMem)) {
            // SINGLEITEM?
            return SingleItemSketch.heapify(srcMem, memSeedHash);
        }
        // not empty & not singleItem
        final int flags = srcMem.getByte(FLAGS_BYTE);
        final boolean compactFlag = (flags & COMPACT_FLAG_MASK) > 0;
        if (!compactFlag) {
            throw new SketchesArgumentException("Corrupted: COMPACT family sketch image must have compact flag set");
        }
        final boolean readOnly = (flags & READ_ONLY_FLAG_MASK) > 0;
        if (!readOnly) {
            throw new SketchesArgumentException("Corrupted: COMPACT family sketch image must have Read-Only flag set");
        }
        return DirectCompactSketch.wrapInstance(srcMem, memSeedHash);
    } else // end of serVer 3
    if (serVer == 1) {
        return ForwardCompatibility.heapify1to3(srcMem, defaultSeedHash);
    } else if (serVer == 2) {
        final short memSeedHash = (short) extractSeedHash(srcMem);
        return ForwardCompatibility.heapify2to3(srcMem, memSeedHash);
    }
    throw new SketchesArgumentException("Corrupted: Serialization Version " + serVer + " not recognized.");
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) Family.idToFamily(org.apache.datasketches.Family.idToFamily) 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