use of com.yahoo.sketches.ResizeFactor in project sketches-core by DataSketches.
the class HeapAlphaSketchTest method checkResetAndStartingSubMultiple.
@Test
public void checkResetAndStartingSubMultiple() {
int k = 1024;
int u = 4 * k;
UpdateSketch usk = UpdateSketch.builder().setFamily(fam_).setResizeFactor(X8).setNominalEntries(k).build();
//for internal checks
HeapAlphaSketch sk1 = (HeapAlphaSketch) usk;
assertTrue(usk.isEmpty());
for (int i = 0; i < u; i++) usk.update(i);
assertEquals(1 << sk1.getLgArrLongs(), 2 * k);
sk1.reset();
ResizeFactor rf = sk1.getResizeFactor();
//messy
int subMul = Util.startingSubMultiple(11, rf, 5);
assertEquals(sk1.getLgArrLongs(), subMul);
UpdateSketch usk2 = UpdateSketch.builder().setFamily(fam_).setResizeFactor(ResizeFactor.X1).setNominalEntries(k).build();
sk1 = (HeapAlphaSketch) usk2;
for (int i = 0; i < u; i++) usk2.update(i);
assertEquals(1 << sk1.getLgArrLongs(), 2 * k);
sk1.reset();
rf = sk1.getResizeFactor();
//messy
subMul = Util.startingSubMultiple(11, rf, 5);
assertEquals(sk1.getLgArrLongs(), subMul);
assertNull(sk1.getMemory());
assertFalse(sk1.isOrdered());
}
use of com.yahoo.sketches.ResizeFactor in project sketches-core by DataSketches.
the class HeapQuickSelectSketchTest method checkResetAndStartingSubMultiple.
@Test
public void checkResetAndStartingSubMultiple() {
int k = 1024;
int u = 4 * k;
UpdateSketch usk = UpdateSketch.builder().setFamily(fam_).setResizeFactor(X8).setNominalEntries(k).build();
//for internal checks
HeapQuickSelectSketch sk1 = (HeapQuickSelectSketch) usk;
assertTrue(usk.isEmpty());
for (int i = 0; i < u; i++) usk.update(i);
assertEquals(1 << sk1.getLgArrLongs(), 2 * k);
sk1.reset();
ResizeFactor rf = sk1.getResizeFactor();
//messy
int subMul = Util.startingSubMultiple(11, rf, 5);
assertEquals(sk1.getLgArrLongs(), subMul);
UpdateSketch usk2 = UpdateSketch.builder().setFamily(fam_).setResizeFactor(ResizeFactor.X1).setNominalEntries(k).build();
sk1 = (HeapQuickSelectSketch) usk2;
for (int i = 0; i < u; i++) usk2.update(i);
assertEquals(1 << sk1.getLgArrLongs(), 2 * k);
sk1.reset();
rf = sk1.getResizeFactor();
//messy
subMul = Util.startingSubMultiple(11, rf, 5);
assertEquals(sk1.getLgArrLongs(), subMul);
assertNull(sk1.getMemory());
assertFalse(sk1.isOrdered());
}
use of com.yahoo.sketches.ResizeFactor in project sketches-core by DataSketches.
the class SetOperationTest method checkBuilder2.
@Test
public void checkBuilder2() {
SetOperationBuilder bldr = SetOperation.builder();
long seed = 12345L;
bldr.setSeed(seed);
assertEquals(seed, bldr.getSeed());
float p = (float) 0.5;
bldr.setP(p);
assertEquals(p, bldr.getP());
ResizeFactor rf = ResizeFactor.X4;
bldr.setResizeFactor(rf);
assertEquals(rf, bldr.getResizeFactor());
int lgK = 10;
int k = 1 << lgK;
bldr.setNominalEntries(k);
assertEquals(lgK, bldr.getLgNominalEntries());
println(bldr.toString());
}
use of com.yahoo.sketches.ResizeFactor in project sketches-core by DataSketches.
the class SetOperationTest method checkBuilderBadSeedHashes.
@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBuilderBadSeedHashes() {
int k = 2048;
long seed = 1021;
UpdateSketch usk1 = UpdateSketch.builder().setSeed(seed).setNominalEntries(k).build();
UpdateSketch usk2 = UpdateSketch.builder().setNominalEntries(k).build();
for (int i = 0; i < (k / 2); i++) {
//256
usk1.update(i);
}
for (int i = k / 2; i < k; i++) {
//256 no overlap
usk2.update(i);
}
ResizeFactor rf = X4;
Union union = SetOperation.builder().setSeed(seed).setResizeFactor(rf).setNominalEntries(k).buildUnion();
union.update(usk1);
//throws seed exception here
union.update(usk2);
}
use of com.yahoo.sketches.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 = srcMem.getByte(PREAMBLE_LONGS_BYTE) & 0X3F;
final ResizeFactor myRF = ResizeFactor.getRF((//byte 0
srcMem.getByte(PREAMBLE_LONGS_BYTE) >>> LG_RESIZE_FACTOR_BIT));
//byte 1
final int serVer = srcMem.getByte(SER_VER_BYTE) & 0XFF;
//byte 2
final int familyID = srcMem.getByte(FAMILY_BYTE) & 0XFF;
//byte 3
final int lgNomLongs = srcMem.getByte(LG_NOM_LONGS_BYTE) & 0XFF;
//byte 4
final int lgArrLongs = srcMem.getByte(LG_ARR_LONGS_BYTE) & 0XFF;
//byte 5
final int flags = srcMem.getByte(FLAGS_BYTE) & 0XFF;
//byte 6,7
final short seedHash = srcMem.getShort(SEED_HASH_SHORT);
//bytes 8-11
final int curCount = srcMem.getInt(RETAINED_ENTRIES_INT);
//bytes 12-15
final float p = srcMem.getFloat(P_FLOAT);
//bytes 16-23
final long thetaLong = srcMem.getLong(THETA_LONG);
if (serVer != SER_VER) {
throw new SketchesArgumentException("Possible corruption: Invalid Serialization Version: " + serVer);
}
final Family family = Family.idToFamily(familyID);
if (family.equals(Family.UNION)) {
if (preambleLongs != Family.UNION.getMinPreLongs()) {
throw new SketchesArgumentException("Possible corruption: Invalid PreambleLongs value for UNION: " + preambleLongs);
}
} else if (family.equals(Family.QUICKSELECT)) {
if (preambleLongs != Family.QUICKSELECT.getMinPreLongs()) {
throw new SketchesArgumentException("Possible corruption: Invalid PreambleLongs value for QUICKSELECT: " + preambleLongs);
}
} else {
throw new SketchesArgumentException("Possible corruption: Invalid Family: " + family.toString());
}
if (lgNomLongs < MIN_LG_NOM_LONGS) {
throw new SketchesArgumentException("Possible corruption: Current Memory lgNomLongs < min required size: " + lgNomLongs + " < " + MIN_LG_NOM_LONGS);
}
final int flagsMask = ORDERED_FLAG_MASK | COMPACT_FLAG_MASK | READ_ONLY_FLAG_MASK | BIG_ENDIAN_FLAG_MASK;
if ((flags & flagsMask) > 0) {
throw new SketchesArgumentException("Possible corruption: Input srcMem cannot be: big-endian, compact, ordered, or read-only");
}
Util.checkSeedHashes(seedHash, Util.computeSeedHash(seed));
final long curCapBytes = srcMem.getCapacity();
final int minReqBytes = getMemBytes(lgArrLongs, preambleLongs);
if (curCapBytes < minReqBytes) {
throw new SketchesArgumentException("Possible corruption: Current Memory size < min required size: " + curCapBytes + " < " + minReqBytes);
}
final double theta = thetaLong / MAX_THETA_LONG_AS_DOUBLE;
if ((lgArrLongs <= lgNomLongs) && (theta < p)) {
throw new SketchesArgumentException("Possible corruption: Theta cannot be < p and lgArrLongs <= lgNomLongs. " + lgArrLongs + " <= " + lgNomLongs + ", Theta: " + theta + ", p: " + p);
}
final HeapQuickSelectSketch hqss = new HeapQuickSelectSketch(lgNomLongs, seed, p, myRF, preambleLongs, family);
hqss.lgArrLongs_ = lgArrLongs;
hqss.hashTableThreshold_ = setHashTableThreshold(lgNomLongs, lgArrLongs);
hqss.curCount_ = curCount;
hqss.thetaLong_ = thetaLong;
hqss.empty_ = (flags & EMPTY_FLAG_MASK) > 0;
hqss.cache_ = new long[1 << lgArrLongs];
//read in as hash table
srcMem.getLongArray(preambleLongs << 3, hqss.cache_, 0, 1 << lgArrLongs);
return hqss;
}
Aggregations