Search in sources :

Example 26 with SketchesArgumentException

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

the class HllUtil method checkPreamble.

static CurMode checkPreamble(final Memory mem) {
    final int preInts = extractPreInts(mem);
    final int serVer = extractSerVer(mem);
    final int famId = extractFamilyId(mem);
    final CurMode curMode = extractCurMode(mem);
    if ((famId != Family.HLL.getID()) || (serVer != 1) || ((preInts != LIST_PREINTS) && (preInts != HASH_SET_PREINTS) && (preInts != HLL_PREINTS)) || ((curMode == CurMode.LIST) && (preInts != LIST_PREINTS)) || ((curMode == CurMode.SET) && (preInts != HASH_SET_PREINTS)) || ((curMode == CurMode.HLL) && (preInts != HLL_PREINTS))) {
        throw new SketchesArgumentException("Possible Corruption, Invalid Preamble:" + PreambleUtil.toString(mem));
    }
    return curMode;
}
Also used : PreambleUtil.extractCurMode(org.apache.datasketches.hll.PreambleUtil.extractCurMode) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException)

Example 27 with SketchesArgumentException

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

the class DirectUpdateDoublesSketch method growCombinedMemBuffer.

// Direct supporting methods
private WritableMemory growCombinedMemBuffer(final int itemSpaceNeeded) {
    final long memBytes = mem_.getCapacity();
    // + preamble + min & max
    final int needBytes = (itemSpaceNeeded << 3) + COMBINED_BUFFER;
    assert needBytes > memBytes;
    memReqSvr = (memReqSvr == null) ? mem_.getMemoryRequestServer() : memReqSvr;
    if (memReqSvr == null) {
        throw new SketchesArgumentException("A request for more memory has been denied, " + "or a default MemoryRequestServer has not been provided. Must abort. ");
    }
    final WritableMemory newMem = memReqSvr.request(mem_, needBytes);
    mem_.copyTo(0, newMem, 0, memBytes);
    memReqSvr.requestClose(mem_, newMem);
    return newMem;
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) WritableMemory(org.apache.datasketches.memory.WritableMemory)

Example 28 with SketchesArgumentException

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

the class ReservoirItemsSketchTest method checkBadMemory.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBadMemory() {
    byte[] bytes = new byte[4];
    Memory mem = Memory.wrap(bytes);
    try {
        PreambleUtil.getAndCheckPreLongs(mem);
        fail();
    } catch (final SketchesArgumentException e) {
    // expected
    }
    bytes = new byte[8];
    // only 1 preLong worth of items in bytearray
    bytes[0] = 2;
    mem = Memory.wrap(bytes);
    PreambleUtil.getAndCheckPreLongs(mem);
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) Memory(org.apache.datasketches.memory.Memory) WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 29 with SketchesArgumentException

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

the class ReservoirItemsSketchTest method checkArrayOfNumbersSerDeErrors.

@Test
public void checkArrayOfNumbersSerDeErrors() {
    // Highly debatable whether this belongs here vs a stand-alone test class
    final ReservoirItemsSketch<Number> ris = ReservoirItemsSketch.newInstance(6);
    assertNull(ris.getSamples());
    assertNull(ris.getSamples(Number.class));
    // using mixed types, but BigDecimal not supported by serde class
    ris.update(1);
    ris.update(new BigDecimal(2));
    // this should work since BigDecimal is an instance of Number
    final Number[] data = ris.getSamples(Number.class);
    assertNotNull(data);
    assertEquals(data.length, 2);
    // toByteArray() should fail
    final ArrayOfNumbersSerDe serDe = new ArrayOfNumbersSerDe();
    try {
        ris.toByteArray(serDe, Number.class);
        fail();
    } catch (final SketchesArgumentException e) {
    // expected
    }
    // force entry to a supported type
    data[1] = 3.0;
    final byte[] bytes = serDe.serializeToByteArray(data);
    // change first element to indicate something unsupported
    bytes[0] = 'q';
    try {
        serDe.deserializeFromMemory(Memory.wrap(bytes), 2);
        fail();
    } catch (final SketchesArgumentException e) {
    // expected
    }
}
Also used : ArrayOfNumbersSerDe(org.apache.datasketches.ArrayOfNumbersSerDe) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 30 with SketchesArgumentException

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

the class VarOptItemsSketchTest method checkBadMemory.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBadMemory() {
    byte[] bytes = new byte[4];
    Memory mem = Memory.wrap(bytes);
    try {
        PreambleUtil.getAndCheckPreLongs(mem);
        fail();
    } catch (final SketchesArgumentException e) {
    // expected
    }
    bytes = new byte[8];
    // only 1 preLong worth of items in bytearray
    bytes[0] = 2;
    mem = Memory.wrap(bytes);
    PreambleUtil.getAndCheckPreLongs(mem);
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) Memory(org.apache.datasketches.memory.Memory) 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