Skip to content

Commit cea3f74

Browse files
author
lrhodes
committed
restructured coupon toByteArray to consolidate code.
1 parent 11aad90 commit cea3f74

9 files changed

Lines changed: 335 additions & 243 deletions

File tree

src/main/java/com/yahoo/sketches/hll/AbstractCoupons.java

Lines changed: 26 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,18 @@
55

66
package com.yahoo.sketches.hll;
77

8+
import static com.yahoo.sketches.Util.ceilingPowerOf2;
89
import static com.yahoo.sketches.hll.HllUtil.COUPON_RSE;
910
import static com.yahoo.sketches.hll.HllUtil.EMPTY;
1011
import 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;
2417
import static java.lang.Math.max;
2518

26-
import com.yahoo.memory.WritableMemory;
19+
import com.yahoo.memory.Memory;
2720
import 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
}

src/main/java/com/yahoo/sketches/hll/CouponList.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,16 @@ int getCompactSerializationBytes() {
128128
return getMemDataStart() + (couponCount << 2);
129129
}
130130

131-
@Override //get coupons from internal int[] to dstMem
132-
//Called by CouponList.insertList
133-
//Called by CouponList.insertSet
134-
void getCouponsToMemoryInts(final WritableMemory dstWmem, final int lenInts) {
135-
dstWmem.putIntArray(getMemDataStart(), couponIntArr, 0, lenInts);
136-
}
137-
138131
@Override
139132
int getCouponCount() {
140133
return couponCount;
141134
}
142135

136+
@Override
137+
int[] getCouponIntArr() {
138+
return couponIntArr;
139+
}
140+
143141
@Override
144142
PairIterator getIterator() {
145143
return new IntArrayPairIterator(couponIntArr, lgConfigK);
@@ -155,6 +153,11 @@ int getMemDataStart() {
155153
return LIST_INT_ARR_START;
156154
}
157155

156+
@Override
157+
Memory getMemory() {
158+
return null;
159+
}
160+
158161
@Override
159162
int getPreInts() {
160163
return LIST_PREINTS;
@@ -195,16 +198,6 @@ void putOutOfOrderFlag(final boolean oooFlag) {
195198
this.oooFlag = oooFlag;
196199
}
197200

198-
@Override
199-
byte[] toCompactByteArray() {
200-
return toByteArray(this, true);
201-
}
202-
203-
@Override
204-
byte[] toUpdatableByteArray() {
205-
return toByteArray(this, false);
206-
}
207-
208201
@Override
209202
CouponList reset() {
210203
return new CouponList(lgConfigK, tgtHllType, CurMode.LIST);

src/main/java/com/yahoo/sketches/hll/DirectCouponHashSet.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ HllSketchImpl couponUpdate(final int coupon) {
6464
return this; //found duplicate, ignore
6565
}
6666
insertInt(memObj, memAdd, HASH_SET_INT_ARR_START + (~index << 2), coupon);
67-
incCouponCount();
67+
insertHashSetCount(memObj, memAdd, getCouponCount() + 1);
6868
final boolean promote = checkGrowOrPromote();
6969
if (!promote) { return this; }
7070
return promoteListOrSetToHll(this);
@@ -85,12 +85,6 @@ int getPreInts() {
8585
return HASH_SET_PREINTS;
8686
}
8787

88-
void incCouponCount() {
89-
assert wmem != null;
90-
int count = extractHashSetCount(memObj, memAdd);
91-
insertHashSetCount(memObj, memAdd, ++count);
92-
}
93-
9488
private boolean checkGrowOrPromote() {
9589
int lgCouponArrInts = getLgCouponArrInts();
9690
if ((RESIZE_DENOM * getCouponCount()) > (RESIZE_NUMER * (1 << lgCouponArrInts))) {

src/main/java/com/yahoo/sketches/hll/DirectCouponList.java

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -139,19 +139,16 @@ int getCompactSerializationBytes() {
139139
return getMemDataStart() + (getCouponCount() << 2);
140140
}
141141

142-
@Override //get Coupons from internal Mem to dstMem
143-
//Called by CouponList.insertList()
144-
//Called by CouponList.insertSet()
145-
void getCouponsToMemoryInts(final WritableMemory dstWmem, final int lenInts) {
146-
final int memDataStart = getMemDataStart();
147-
mem.copyTo(memDataStart, dstWmem, memDataStart, lenInts << 2);
148-
}
149-
150142
@Override
151143
int getCouponCount() {
152144
return extractListCount(memObj, memAdd);
153145
}
154146

147+
@Override
148+
int[] getCouponIntArr() { //here only to satisfy the abstract, should not be used
149+
return null;
150+
}
151+
155152
@Override
156153
PairIterator getIterator() {
157154
final long dataStart = getMemDataStart();
@@ -161,14 +158,20 @@ PairIterator getIterator() {
161158

162159
@Override
163160
int getLgCouponArrInts() {
164-
return extractLgArr(memObj, memAdd);
161+
final int lgArr = extractLgArr(memObj, memAdd);
162+
return getLgCouponArrInts(this, lgArr);
165163
}
166164

167165
@Override
168166
int getMemDataStart() {
169167
return LIST_INT_ARR_START;
170168
}
171169

170+
@Override
171+
Memory getMemory() {
172+
return mem;
173+
}
174+
172175
@Override
173176
int getPreInts() {
174177
return LIST_PREINTS;
@@ -220,34 +223,6 @@ DirectCouponList reset() {
220223
return DirectCouponList.newInstance(lgConfigK, tgtHllType, wmem);
221224
}
222225

223-
@Override
224-
byte[] toCompactByteArray() {
225-
final boolean memIsCompact = extractCompactFlag(memObj, memAdd);
226-
final int totBytes = getCompactSerializationBytes();
227-
final byte[] byteArr = new byte[totBytes];
228-
final WritableMemory memOut = WritableMemory.wrap(byteArr);
229-
if (memIsCompact) {
230-
mem.copyTo(0, memOut, 0, totBytes);
231-
return byteArr;
232-
} else {
233-
return toByteArray(this, true);
234-
}
235-
}
236-
237-
@Override
238-
byte[] toUpdatableByteArray() {
239-
final boolean memIsCompact = extractCompactFlag(memObj, memAdd);
240-
final int totBytes = getUpdatableSerializationBytes();
241-
final byte[] byteArr = new byte[totBytes];
242-
final WritableMemory memOut = WritableMemory.wrap(byteArr);
243-
if (!memIsCompact) {
244-
mem.copyTo(0, memOut, 0, totBytes);
245-
return byteArr;
246-
} else {
247-
return toByteArray(this, true);
248-
}
249-
}
250-
251226
//Called by DirectCouponList.couponUpdate()
252227
static final DirectCouponHashSet promoteListToSet(final DirectCouponList src) {
253228
final WritableMemory wmem = src.wmem;
@@ -339,4 +314,6 @@ static final DirectHllArray promoteListOrSetToHll(final DirectCouponList src) {
339314
return dirHllArr;
340315
}
341316

317+
318+
342319
}

0 commit comments

Comments
 (0)