Search in sources :

Example 6 with ResizeFactor

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

the class HeapQuickSelectSketch method heapifyInstance.

/**
 * Heapify a sketch from a Memory UpdateSketch or Union object
 * containing sketch data.
 * @param srcMem The source Memory object.
 * <a href="{@docRoot}/resources/dictionary.html#mem">See Memory</a>
 * @param seed <a href="{@docRoot}/resources/dictionary.html#seed">See seed</a>
 * @return instance of this sketch
 */
static HeapQuickSelectSketch heapifyInstance(final Memory srcMem, final long seed) {
    // byte 0
    final int preambleLongs = extractPreLongs(srcMem);
    // byte 3
    final int lgNomLongs = extractLgNomLongs(srcMem);
    // byte 4
    final int lgArrLongs = extractLgArrLongs(srcMem);
    checkUnionQuickSelectFamily(srcMem, preambleLongs, lgNomLongs);
    checkMemIntegrity(srcMem, seed, preambleLongs, lgNomLongs, lgArrLongs);
    // bytes 12-15
    final float p = extractP(srcMem);
    // byte 0
    final int memlgRF = extractLgResizeFactor(srcMem);
    ResizeFactor memRF = ResizeFactor.getRF(memlgRF);
    final int familyID = extractFamilyID(srcMem);
    final Family family = Family.idToFamily(familyID);
    if (isResizeFactorIncorrect(srcMem, lgNomLongs, lgArrLongs)) {
        // X2 always works.
        memRF = ResizeFactor.X2;
    }
    final HeapQuickSelectSketch hqss = new HeapQuickSelectSketch(lgNomLongs, seed, p, memRF, preambleLongs, family);
    hqss.lgArrLongs_ = lgArrLongs;
    hqss.hashTableThreshold_ = setHashTableThreshold(lgNomLongs, lgArrLongs);
    hqss.curCount_ = extractCurCount(srcMem);
    hqss.thetaLong_ = extractThetaLong(srcMem);
    hqss.empty_ = PreambleUtil.isEmptyFlag(srcMem);
    hqss.cache_ = new long[1 << lgArrLongs];
    // read in as hash table
    srcMem.getLongArray(preambleLongs << 3, hqss.cache_, 0, 1 << lgArrLongs);
    return hqss;
}
Also used : Family(org.apache.datasketches.Family) PreambleUtil.extractLgResizeFactor(org.apache.datasketches.theta.PreambleUtil.extractLgResizeFactor) ResizeFactor(org.apache.datasketches.ResizeFactor)

Example 7 with ResizeFactor

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

the class HeapQuickSelectSketch method reset.

@Override
public void reset() {
    final ResizeFactor rf = getResizeFactor();
    final int lgArrLongsSM = startingSubMultiple(lgNomLongs_ + 1, rf.lg(), MIN_LG_ARR_LONGS);
    if (lgArrLongsSM == lgArrLongs_) {
        final int arrLongs = cache_.length;
        assert (1 << lgArrLongs_) == arrLongs;
        java.util.Arrays.fill(cache_, 0L);
    } else {
        cache_ = new long[1 << lgArrLongsSM];
        lgArrLongs_ = lgArrLongsSM;
    }
    hashTableThreshold_ = setHashTableThreshold(lgNomLongs_, lgArrLongs_);
    empty_ = true;
    curCount_ = 0;
    thetaLong_ = (long) (getP() * LONG_MAX_VALUE_AS_DOUBLE);
}
Also used : PreambleUtil.extractLgResizeFactor(org.apache.datasketches.theta.PreambleUtil.extractLgResizeFactor) ResizeFactor(org.apache.datasketches.ResizeFactor)

Example 8 with ResizeFactor

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

the class HeapQuickSelectSketch method resizeCache.

// Must resize. Changes lgArrLongs_, cache_, hashTableThreshold;
// theta and count don't change.
// Used by hashUpdate()
private final void resizeCache() {
    final ResizeFactor rf = getResizeFactor();
    final int lgMaxArrLongs = lgNomLongs_ + 1;
    final int lgDeltaLongs = lgMaxArrLongs - lgArrLongs_;
    // rf_.lg() could be 0
    final int lgResizeFactor = max(min(rf.lg(), lgDeltaLongs), 1);
    // new arr size
    lgArrLongs_ += lgResizeFactor;
    final long[] tgtArr = new long[1 << lgArrLongs_];
    final int newCount = HashOperations.hashArrayInsert(cache_, tgtArr, lgArrLongs_, thetaLong_);
    // Assumes no dirty values.
    assert newCount == curCount_;
    curCount_ = newCount;
    cache_ = tgtArr;
    hashTableThreshold_ = setHashTableThreshold(lgNomLongs_, lgArrLongs_);
}
Also used : PreambleUtil.extractLgResizeFactor(org.apache.datasketches.theta.PreambleUtil.extractLgResizeFactor) ResizeFactor(org.apache.datasketches.ResizeFactor)

Example 9 with ResizeFactor

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

the class HeapAlphaSketch method heapifyInstance.

/**
 * Heapify a sketch from a Memory object containing sketch data.
 * @param srcMem The source Memory object.
 * <a href="{@docRoot}/resources/dictionary.html#mem">See Memory</a>
 * @param expectedSeed the seed used to validate the given Memory image.
 * <a href="{@docRoot}/resources/dictionary.html#seed">See seed</a>
 * @return instance of this sketch
 */
static HeapAlphaSketch heapifyInstance(final Memory srcMem, final long expectedSeed) {
    // byte 0
    final int preambleLongs = extractPreLongs(srcMem);
    // byte 3
    final int lgNomLongs = extractLgNomLongs(srcMem);
    // byte 4
    final int lgArrLongs = extractLgArrLongs(srcMem);
    checkAlphaFamily(srcMem, preambleLongs, lgNomLongs);
    checkMemIntegrity(srcMem, expectedSeed, preambleLongs, lgNomLongs, lgArrLongs);
    // bytes 12-15
    final float p = extractP(srcMem);
    // byte 0
    final int memlgRF = extractLgResizeFactor(srcMem);
    ResizeFactor memRF = ResizeFactor.getRF(memlgRF);
    final double nomLongs = (1L << lgNomLongs);
    final double alpha = nomLongs / (nomLongs + 1.0);
    final long split1 = (long) (((p * (alpha + 1.0)) / 2.0) * LONG_MAX_VALUE_AS_DOUBLE);
    if (isResizeFactorIncorrect(srcMem, lgNomLongs, lgArrLongs)) {
        // X2 always works.
        memRF = ResizeFactor.X2;
    }
    final HeapAlphaSketch has = new HeapAlphaSketch(lgNomLongs, expectedSeed, p, memRF, alpha, split1);
    has.lgArrLongs_ = lgArrLongs;
    has.hashTableThreshold_ = setHashTableThreshold(lgNomLongs, lgArrLongs);
    has.curCount_ = extractCurCount(srcMem);
    has.thetaLong_ = extractThetaLong(srcMem);
    has.empty_ = PreambleUtil.isEmptyFlag(srcMem);
    has.cache_ = new long[1 << lgArrLongs];
    // read in as hash table
    srcMem.getLongArray(preambleLongs << 3, has.cache_, 0, 1 << lgArrLongs);
    return has;
}
Also used : PreambleUtil.extractLgResizeFactor(org.apache.datasketches.theta.PreambleUtil.extractLgResizeFactor) ResizeFactor(org.apache.datasketches.ResizeFactor)

Example 10 with ResizeFactor

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

Aggregations

ResizeFactor (org.apache.datasketches.ResizeFactor)24 Test (org.testng.annotations.Test)13 PreambleUtil.insertLgResizeFactor (org.apache.datasketches.theta.PreambleUtil.insertLgResizeFactor)6 SketchesArgumentException (org.apache.datasketches.SketchesArgumentException)5 PreambleUtil.extractLgResizeFactor (org.apache.datasketches.theta.PreambleUtil.extractLgResizeFactor)5 Family (org.apache.datasketches.Family)4 ArrayList (java.util.ArrayList)3 WritableMemory (org.apache.datasketches.memory.WritableMemory)3 PreambleUtil.extractResizeFactor (org.apache.datasketches.sampling.PreambleUtil.extractResizeFactor)3 SketchesException (org.apache.datasketches.SketchesException)2 DefaultMemoryRequestServer (org.apache.datasketches.memory.DefaultMemoryRequestServer)2 MemoryRequestServer (org.apache.datasketches.memory.MemoryRequestServer)2 ArrayOfBooleansSerDe (org.apache.datasketches.ArrayOfBooleansSerDe)1