use of org.apache.datasketches.ResizeFactor in project sketches-core by DataSketches.
the class SketchTest method checkBuilder.
@Test
public void checkBuilder() {
int k = 2048;
int lgK = Integer.numberOfTrailingZeros(k);
long seed = 1021;
float p = (float) 0.5;
ResizeFactor rf = X4;
Family fam = Family.ALPHA;
UpdateSketch sk1 = UpdateSketch.builder().setSeed(seed).setP(p).setResizeFactor(rf).setFamily(fam).setNominalEntries(k).build();
String nameS1 = sk1.getClass().getSimpleName();
assertEquals(nameS1, "HeapAlphaSketch");
assertEquals(sk1.getLgNomLongs(), lgK);
assertEquals(sk1.getSeed(), seed);
assertEquals(sk1.getP(), p);
// check reset of defaults
sk1 = UpdateSketch.builder().build();
nameS1 = sk1.getClass().getSimpleName();
assertEquals(nameS1, "HeapQuickSelectSketch");
assertEquals(sk1.getLgNomLongs(), Integer.numberOfTrailingZeros(DEFAULT_NOMINAL_ENTRIES));
assertEquals(sk1.getSeed(), DEFAULT_UPDATE_SEED);
assertEquals(sk1.getP(), (float) 1.0);
assertEquals(sk1.getResizeFactor(), ResizeFactor.X8);
}
use of org.apache.datasketches.ResizeFactor in project sketches-core by DataSketches.
the class SketchTest method checkBuilderResizeFactor.
@Test
public void checkBuilderResizeFactor() {
ResizeFactor rf;
rf = X1;
assertEquals(rf.getValue(), 1);
assertEquals(rf.lg(), 0);
assertEquals(ResizeFactor.getRF(0), X1);
rf = X2;
assertEquals(rf.getValue(), 2);
assertEquals(rf.lg(), 1);
assertEquals(ResizeFactor.getRF(1), X2);
rf = X4;
assertEquals(rf.getValue(), 4);
assertEquals(rf.lg(), 2);
assertEquals(ResizeFactor.getRF(2), X4);
rf = X8;
assertEquals(rf.getValue(), 8);
assertEquals(rf.lg(), 3);
assertEquals(ResizeFactor.getRF(3), X8);
}
use of org.apache.datasketches.ResizeFactor in project sketches-core by DataSketches.
the class HeapAlphaSketch method resizeClean.
// curCount > hashTableThreshold
// Checks for rare lockup condition
// Used by hashUpdate()
private final void resizeClean() {
// must resize, but are we at tgt size?
final int lgTgtLongs = lgNomLongs_ + 1;
if (lgTgtLongs > lgArrLongs_) {
// not yet at tgt size
final ResizeFactor rf = getResizeFactor();
// must be > 0
final int lgDeltaLongs = lgTgtLongs - lgArrLongs_;
// rf_.lg() could be 0
final int lgResizeFactor = max(min(rf.lg(), lgDeltaLongs), 1);
forceResizeCleanCache(lgResizeFactor);
} else {
// at tgt size or larger, no dirty values, must take drastic measures, very rare.
forceResizeCleanCache(1);
}
}
use of org.apache.datasketches.ResizeFactor in project sketches-core by DataSketches.
the class PreambleUtil method preambleToString.
/**
* Returns a human readable string summary of the preamble state of the given Memory.
* Note: other than making sure that the given Memory size is large
* enough for just the preamble, this does not do much value checking of the contents of the
* preamble as this is primarily a tool for debugging the preamble visually.
*
* @param mem the given Memory.
* @return the summary preamble string.
*/
static String preambleToString(final Memory mem) {
final int preLongs = getAndCheckPreLongs(mem);
final int rfId = extractLgResizeFactor(mem);
final ResizeFactor rf = ResizeFactor.getRF(rfId);
final int serVer = extractSerVer(mem);
final int familyId = extractFamilyID(mem);
final Family family = Family.idToFamily(familyId);
final int lgNomLongs = extractLgNomLongs(mem);
final int lgArrLongs = extractLgArrLongs(mem);
// Flags
final int flags = extractFlags(mem);
final String flagsStr = (flags) + ", 0x" + (Integer.toHexString(flags)) + ", " + zeroPad(Integer.toBinaryString(flags), 8);
final String nativeOrder = ByteOrder.nativeOrder().toString();
final boolean bigEndian = (flags & BIG_ENDIAN_FLAG_MASK) > 0;
final boolean readOnly = (flags & READ_ONLY_FLAG_MASK) > 0;
final boolean empty = (flags & EMPTY_FLAG_MASK) > 0;
final boolean compact = (flags & COMPACT_FLAG_MASK) > 0;
final boolean ordered = (flags & ORDERED_FLAG_MASK) > 0;
// !empty && (preLongs == 1);
final boolean singleItem = (flags & SINGLEITEM_FLAG_MASK) > 0;
final int seedHash = extractSeedHash(mem);
// assumes preLongs == 1; empty or singleItem
int curCount = singleItem ? 1 : 0;
// preLongs 1 or 2
float p = (float) 1.0;
// preLongs 1 or 2
long thetaLong = Long.MAX_VALUE;
// preLongs 1, 2 or 3
long thetaULong = thetaLong;
if (preLongs == 2) {
// exact (non-estimating) CompactSketch
curCount = extractCurCount(mem);
p = extractP(mem);
} else if (preLongs == 3) {
// Update Sketch
curCount = extractCurCount(mem);
p = extractP(mem);
thetaLong = extractThetaLong(mem);
thetaULong = thetaLong;
} else if (preLongs == 4) {
// Union
curCount = extractCurCount(mem);
p = extractP(mem);
thetaLong = extractThetaLong(mem);
thetaULong = extractUnionThetaLong(mem);
}
// else the same as an empty sketch or singleItem
final double thetaDbl = thetaLong / Util.LONG_MAX_VALUE_AS_DOUBLE;
final String thetaHex = zeroPad(Long.toHexString(thetaLong), 16);
final double thetaUDbl = thetaULong / Util.LONG_MAX_VALUE_AS_DOUBLE;
final String thetaUHex = zeroPad(Long.toHexString(thetaULong), 16);
final StringBuilder sb = new StringBuilder();
sb.append(LS);
sb.append("### SKETCH PREAMBLE SUMMARY:").append(LS);
sb.append("Native Byte Order : ").append(nativeOrder).append(LS);
sb.append("Byte 0: Preamble Longs : ").append(preLongs).append(LS);
sb.append("Byte 0: ResizeFactor : ").append(rfId + ", " + rf.toString()).append(LS);
sb.append("Byte 1: Serialization Version: ").append(serVer).append(LS);
sb.append("Byte 2: Family : ").append(familyId + ", " + family.toString()).append(LS);
sb.append("Byte 3: LgNomLongs : ").append(lgNomLongs).append(LS);
sb.append("Byte 4: LgArrLongs : ").append(lgArrLongs).append(LS);
sb.append("Byte 5: Flags Field : ").append(flagsStr).append(LS);
sb.append(" Bit Flag Name : State:").append(LS);
sb.append(" 0 BIG_ENDIAN_STORAGE : ").append(bigEndian).append(LS);
sb.append(" 1 READ_ONLY : ").append(readOnly).append(LS);
sb.append(" 2 EMPTY : ").append(empty).append(LS);
sb.append(" 3 COMPACT : ").append(compact).append(LS);
sb.append(" 4 ORDERED : ").append(ordered).append(LS);
sb.append(" 5 SINGLE_ITEM : ").append(singleItem).append(LS);
sb.append("Bytes 6-7 : Seed Hash Hex : ").append(Integer.toHexString(seedHash)).append(LS);
if (preLongs == 1) {
sb.append(" --ABSENT FIELDS, ASSUMED:").append(LS);
sb.append("Bytes 8-11 : CurrentCount : ").append(curCount).append(LS);
sb.append("Bytes 12-15: P : ").append(p).append(LS);
sb.append("Bytes 16-23: Theta (double) : ").append(thetaDbl).append(LS);
sb.append(" Theta (long) : ").append(thetaLong).append(LS);
sb.append(" Theta (long,hex) : ").append(thetaHex).append(LS);
} else if (preLongs == 2) {
sb.append("Bytes 8-11 : CurrentCount : ").append(curCount).append(LS);
sb.append("Bytes 12-15: P : ").append(p).append(LS);
sb.append(" --ABSENT, ASSUMED:").append(LS);
sb.append("Bytes 16-23: Theta (double) : ").append(thetaDbl).append(LS);
sb.append(" Theta (long) : ").append(thetaLong).append(LS);
sb.append(" Theta (long,hex) : ").append(thetaHex).append(LS);
} else if (preLongs == 3) {
sb.append("Bytes 8-11 : CurrentCount : ").append(curCount).append(LS);
sb.append("Bytes 12-15: P : ").append(p).append(LS);
sb.append("Bytes 16-23: Theta (double) : ").append(thetaDbl).append(LS);
sb.append(" Theta (long) : ").append(thetaLong).append(LS);
sb.append(" Theta (long,hex) : ").append(thetaHex).append(LS);
} else {
// preLongs == 4
sb.append("Bytes 8-11 : CurrentCount : ").append(curCount).append(LS);
sb.append("Bytes 12-15: P : ").append(p).append(LS);
sb.append("Bytes 16-23: Theta (double) : ").append(thetaDbl).append(LS);
sb.append(" Theta (long) : ").append(thetaLong).append(LS);
sb.append(" Theta (long,hex) : ").append(thetaHex).append(LS);
sb.append("Bytes 25-31: ThetaU (double) : ").append(thetaUDbl).append(LS);
sb.append(" ThetaU (long) : ").append(thetaULong).append(LS);
sb.append(" ThetaU (long,hex): ").append(thetaUHex).append(LS);
}
sb.append("Preamble Bytes : ").append(preLongs * 8).append(LS);
sb.append("Data Bytes : ").append(curCount * 8).append(LS);
sb.append("TOTAL Sketch Bytes : ").append((preLongs + curCount) * 8).append(LS);
sb.append("TOTAL Capacity Bytes : ").append(mem.getCapacity()).append(LS);
sb.append("### END SKETCH PREAMBLE SUMMARY").append(LS);
return sb.toString();
}
Aggregations