Search in sources :

Example 21 with Memory

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

the class ArrayOfStringsSummaryTest method checkInBytes.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkInBytes() {
    Memory mem = Memory.wrap(new byte[100]);
    ArrayOfStringsSummary.checkInBytes(mem, 200);
}
Also used : Memory(org.apache.datasketches.memory.Memory) Test(org.testng.annotations.Test)

Example 22 with Memory

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

the class Sketch method checkSketchAndMemoryFlags.

/**
 * Checks Ordered and Compact flags for integrity between sketch and Memory
 * @param sketch the given sketch
 */
static final void checkSketchAndMemoryFlags(final Sketch sketch) {
    final Memory mem = sketch.getMemory();
    if (mem == null) {
        return;
    }
    final int flags = PreambleUtil.extractFlags(mem);
    if ((flags & COMPACT_FLAG_MASK) > 0 ^ sketch.isCompact()) {
        throw new SketchesArgumentException("Possible corruption: " + "Memory Compact Flag inconsistent with Sketch");
    }
    if ((flags & ORDERED_FLAG_MASK) > 0 ^ sketch.isOrdered()) {
        throw new SketchesArgumentException("Possible corruption: " + "Memory Ordered Flag inconsistent with Sketch");
    }
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) Memory(org.apache.datasketches.memory.Memory) WritableMemory(org.apache.datasketches.memory.WritableMemory)

Example 23 with Memory

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

the class ReservoirItemsUnion 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 ReservoirItemsUnion created from the provided Memory
 */
public static <T> ReservoirItemsUnion<T> heapify(final Memory srcMem, final ArrayOfItemsSerDe<T> serDe) {
    Family.RESERVOIR_UNION.checkFamilyID(srcMem.getByte(FAMILY_BYTE));
    final int numPreLongs = extractPreLongs(srcMem);
    final int serVer = extractSerVer(srcMem);
    final boolean isEmpty = (extractFlags(srcMem) & EMPTY_FLAG_MASK) != 0;
    int maxK = extractMaxK(srcMem);
    final boolean preLongsEqMin = (numPreLongs == Family.RESERVOIR_UNION.getMinPreLongs());
    final boolean preLongsEqMax = (numPreLongs == Family.RESERVOIR_UNION.getMaxPreLongs());
    if (!preLongsEqMin & !preLongsEqMax) {
        throw new SketchesArgumentException("Possible corruption: Non-empty union with only " + Family.RESERVOIR_UNION.getMinPreLongs() + "preLongs");
    }
    if (serVer != SER_VER) {
        if (serVer == 1) {
            final short encMaxK = extractEncodedReservoirSize(srcMem);
            maxK = ReservoirSize.decodeValue(encMaxK);
        } else {
            throw new SketchesArgumentException("Possible Corruption: Ser Ver must be " + SER_VER + ": " + serVer);
        }
    }
    final ReservoirItemsUnion<T> riu = new ReservoirItemsUnion<>(maxK);
    if (!isEmpty) {
        final int preLongBytes = numPreLongs << 3;
        final Memory sketchMem = srcMem.region(preLongBytes, srcMem.getCapacity() - preLongBytes);
        riu.update(sketchMem, serDe);
    }
    return riu;
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) Memory(org.apache.datasketches.memory.Memory) WritableMemory(org.apache.datasketches.memory.WritableMemory)

Example 24 with Memory

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

the class ReservoirLongsUnion method heapify.

/**
 * Instantiates a Union from Memory
 *
 * @param srcMem Memory object containing a serialized union
 * @return A ReservoirLongsUnion created from the provided Memory
 */
public static ReservoirLongsUnion heapify(final Memory srcMem) {
    Family.RESERVOIR_UNION.checkFamilyID(srcMem.getByte(FAMILY_BYTE));
    final int numPreLongs = extractPreLongs(srcMem);
    final int serVer = extractSerVer(srcMem);
    final boolean isEmpty = (extractFlags(srcMem) & EMPTY_FLAG_MASK) != 0;
    int maxK = extractMaxK(srcMem);
    final boolean preLongsEqMin = (numPreLongs == Family.RESERVOIR_UNION.getMinPreLongs());
    final boolean preLongsEqMax = (numPreLongs == Family.RESERVOIR_UNION.getMaxPreLongs());
    if (!preLongsEqMin & !preLongsEqMax) {
        throw new SketchesArgumentException("Possible corruption: Non-empty union with only " + Family.RESERVOIR_UNION.getMinPreLongs() + "preLongs");
    }
    if (serVer != SER_VER) {
        if (serVer == 1) {
            final short encMaxK = extractEncodedReservoirSize(srcMem);
            maxK = ReservoirSize.decodeValue(encMaxK);
        } else {
            throw new SketchesArgumentException("Possible Corruption: Ser Ver must be " + SER_VER + ": " + serVer);
        }
    }
    final ReservoirLongsUnion rlu = new ReservoirLongsUnion(maxK);
    if (!isEmpty) {
        final int preLongBytes = numPreLongs << 3;
        final Memory sketchMem = srcMem.region(preLongBytes, srcMem.getCapacity() - preLongBytes);
        rlu.update(sketchMem);
    }
    return rlu;
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) Memory(org.apache.datasketches.memory.Memory) WritableMemory(org.apache.datasketches.memory.WritableMemory)

Example 25 with Memory

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

Aggregations

Memory (org.apache.datasketches.memory.Memory)213 Test (org.testng.annotations.Test)180 WritableMemory (org.apache.datasketches.memory.WritableMemory)164 SketchesArgumentException (org.apache.datasketches.SketchesArgumentException)17 ArrayOfLongsSerDe (org.apache.datasketches.ArrayOfLongsSerDe)14 SketchesReadOnlyException (org.apache.datasketches.SketchesReadOnlyException)11 File (java.io.File)10 ArrayOfStringsSerDe (org.apache.datasketches.ArrayOfStringsSerDe)10 Util.getResourceFile (org.apache.datasketches.Util.getResourceFile)10 MapHandle (org.apache.datasketches.memory.MapHandle)10 WritableHandle (org.apache.datasketches.memory.WritableHandle)6 SharedLocal (org.apache.datasketches.theta.ConcurrentHeapQuickSelectSketchTest.SharedLocal)3 ArrayOfNumbersSerDe (org.apache.datasketches.ArrayOfNumbersSerDe)2 CloseableIterator (org.apache.druid.java.util.common.parsers.CloseableIterator)2 IntIterator (it.unimi.dsi.fastutil.ints.IntIterator)1 HashSet (java.util.HashSet)1 Family (org.apache.datasketches.Family)1 SketchesStateException (org.apache.datasketches.SketchesStateException)1 CompressedState.importFromMemory (org.apache.datasketches.cpc.CompressedState.importFromMemory)1 DefaultMemoryRequestServer (org.apache.datasketches.memory.DefaultMemoryRequestServer)1