55
66package com .yahoo .sketches .hll ;
77
8+ import static com .yahoo .sketches .Util .ceilingPowerOf2 ;
89import static com .yahoo .sketches .hll .HllUtil .COUPON_RSE ;
910import static com .yahoo .sketches .hll .HllUtil .EMPTY ;
1011import static com .yahoo .sketches .hll .HllUtil .KEY_MASK_26 ;
11- import static com .yahoo .sketches .hll .PreambleUtil .HASH_SET_INT_ARR_START ;
12- import static com .yahoo .sketches .hll .PreambleUtil .insertCompactFlag ;
13- import static com .yahoo .sketches .hll .PreambleUtil .insertCurMode ;
14- import static com .yahoo .sketches .hll .PreambleUtil .insertEmptyFlag ;
15- import static com .yahoo .sketches .hll .PreambleUtil .insertFamilyId ;
16- import static com .yahoo .sketches .hll .PreambleUtil .insertHashSetCount ;
17- import static com .yahoo .sketches .hll .PreambleUtil .insertLgArr ;
18- import static com .yahoo .sketches .hll .PreambleUtil .insertLgK ;
19- import static com .yahoo .sketches .hll .PreambleUtil .insertListCount ;
20- import static com .yahoo .sketches .hll .PreambleUtil .insertOooFlag ;
21- import static com .yahoo .sketches .hll .PreambleUtil .insertPreInts ;
22- import static com .yahoo .sketches .hll .PreambleUtil .insertSerVer ;
23- import static com .yahoo .sketches .hll .PreambleUtil .insertTgtHllType ;
12+ import static com .yahoo .sketches .hll .HllUtil .LG_INIT_LIST_SIZE ;
13+ import static com .yahoo .sketches .hll .HllUtil .LG_INIT_SET_SIZE ;
14+ import static com .yahoo .sketches .hll .HllUtil .RESIZE_DENOM ;
15+ import static com .yahoo .sketches .hll .HllUtil .RESIZE_NUMER ;
16+ import static com .yahoo .sketches .hll .ToByteArrayImpl .toCouponByteArray ;
2417import static java .lang .Math .max ;
2518
26- import com .yahoo .memory .WritableMemory ;
19+ import com .yahoo .memory .Memory ;
2720import com .yahoo .sketches .SketchesArgumentException ;
2821
2922/**
@@ -42,7 +35,7 @@ abstract class AbstractCoupons extends HllSketchImpl {
4235
4336 abstract int getCouponCount ();
4437
45- abstract void getCouponsToMemoryInts ( WritableMemory dstWmem , int lenInts );
38+ abstract int [] getCouponIntArr ( );
4639
4740 /**
4841 * This is the estimator for the Coupon List mode and Coupon Hash Set mode.
@@ -75,6 +68,8 @@ abstract class AbstractCoupons extends HllSketchImpl {
7568 return max (tmp , couponCount );
7669 }
7770
71+ abstract Memory getMemory ();
72+
7873 @ Override
7974 double getUpperBound (final int numStdDev ) {
8075 HllUtil .checkNumStdDev (numStdDev );
@@ -95,58 +90,17 @@ boolean isEmpty() {
9590 return getCouponCount () == 0 ;
9691 }
9792
98- //To byte array for Heap and when direct memory is different from compact request
99- static final byte [] toByteArray (final AbstractCoupons impl , final boolean compact ) {
100- final byte [] byteArr ;
101- final int arrLenBytes ;
102- final int couponCount = impl .getCouponCount ();
103- final int couponArrInts = 1 << impl .getLgCouponArrInts ();
104- arrLenBytes = (compact )
105- ? couponCount << 2
106- : couponArrInts << 2 ;
107- byteArr = new byte [impl .getMemDataStart () + arrLenBytes ];
108- final WritableMemory tgtWmem = WritableMemory .wrap (byteArr );
109- final Object memObj = tgtWmem .getArray ();
110- final long memAdd = tgtWmem .getCumulativeOffset (0L );
111-
112- insertCompactFlag (memObj , memAdd , compact );
113- insertCommonListAndSet (impl , memObj , memAdd );
114-
115- if (impl .getCurMode () == CurMode .LIST ) {
116- final int lenInts = (compact ) ? couponCount : couponArrInts ;
117- insertListCount (memObj , memAdd , couponCount );
118- impl .getCouponsToMemoryInts (tgtWmem , lenInts );
119- }
120- else { //SET
121- insertHashSetCount (memObj , memAdd , couponCount );
122- if (compact ) {
123- final PairIterator itr = impl .getIterator ();
124- int cnt = 0 ;
125- while (itr .nextValid ()) {
126- tgtWmem .putInt (HASH_SET_INT_ARR_START + (cnt ++ << 2 ), itr .getPair ());
127- }
128- } else { //updatable
129- impl .getCouponsToMemoryInts (tgtWmem , couponArrInts );
130- }
131- }
132- return byteArr ;
93+ @ Override
94+ byte [] toCompactByteArray () {
95+ return toCouponByteArray (this , true );
13396 }
13497
135- private static final void insertCommonListAndSet (final AbstractCoupons impl ,
136- final Object memObj , final long memAdd ) {
137- insertPreInts (memObj , memAdd , impl .getPreInts ());
138- insertSerVer (memObj , memAdd );
139- insertFamilyId (memObj , memAdd );
140- insertLgK (memObj , memAdd , impl .getLgConfigK ());
141- insertLgArr (memObj , memAdd , impl .getLgCouponArrInts ());
142- insertEmptyFlag (memObj , memAdd , impl .isEmpty ());
143- insertOooFlag (memObj , memAdd , impl .isOutOfOrderFlag ());
144- insertCurMode (memObj , memAdd , impl .getCurMode ());
145- insertTgtHllType (memObj , memAdd , impl .getTgtHllType ());
98+ @ Override
99+ byte [] toUpdatableByteArray () {
100+ return toCouponByteArray (this , false );
146101 }
147102
148103 //FIND for Heap and Direct
149-
150104 //Searches the Coupon hash table for an empty slot or a duplicate depending on the context.
151105 //If entire entry is empty, returns one's complement of index = found empty.
152106 //If entry equals given coupon, returns its index = found duplicate coupon
@@ -173,4 +127,14 @@ else if (coupon == couponAtIdx) {
173127 throw new SketchesArgumentException ("Key not found and no empty slots!" );
174128 }
175129
130+ //just in case the LgArr is missing
131+ static int getLgCouponArrInts (final AbstractCoupons impl , final int lgArr ) {
132+ if (lgArr >= 2 ) { return lgArr ; } // < 2 is invalid
133+ final int coupons = impl .getCouponCount ();
134+ int ceilPwr2 = ceilingPowerOf2 (coupons );
135+ final int minLgArr = (impl .curMode == CurMode .LIST ) ? LG_INIT_LIST_SIZE : LG_INIT_SET_SIZE ;
136+ if ((RESIZE_DENOM * coupons ) > (RESIZE_NUMER * ceilPwr2 )) { ceilPwr2 <<= 1 ; }
137+ return Math .max (minLgArr , ceilPwr2 << 1 );
138+ }
139+
176140}
0 commit comments