• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

apache / datasketches-java / #306

30 Apr 2024 10:01PM UTC coverage: 97.645% (-0.5%) from 98.139%
#306

push

web-flow
Merge pull request #555 from apache/fix_pom_xml_header

Fix pom xml header

26865 of 27513 relevant lines covered (97.64%)

0.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.35
/src/main/java/org/apache/datasketches/hll/DirectCouponList.java
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements.  See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership.  The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19

20
package org.apache.datasketches.hll;
21

22
import static org.apache.datasketches.hll.HllUtil.EMPTY;
23
import static org.apache.datasketches.hll.HllUtil.LG_INIT_LIST_SIZE;
24
import static org.apache.datasketches.hll.HllUtil.LG_INIT_SET_SIZE;
25
import static org.apache.datasketches.hll.HllUtil.noWriteAccess;
26
import static org.apache.datasketches.hll.PreambleUtil.EMPTY_FLAG_MASK;
27
import static org.apache.datasketches.hll.PreambleUtil.HASH_SET_PREINTS;
28
import static org.apache.datasketches.hll.PreambleUtil.HLL_PREINTS;
29
import static org.apache.datasketches.hll.PreambleUtil.LIST_INT_ARR_START;
30
import static org.apache.datasketches.hll.PreambleUtil.LIST_PREINTS;
31
import static org.apache.datasketches.hll.PreambleUtil.computeLgArr;
32
import static org.apache.datasketches.hll.PreambleUtil.extractCompactFlag;
33
import static org.apache.datasketches.hll.PreambleUtil.extractInt;
34
import static org.apache.datasketches.hll.PreambleUtil.extractLgArr;
35
import static org.apache.datasketches.hll.PreambleUtil.extractListCount;
36
import static org.apache.datasketches.hll.PreambleUtil.insertCurMin;
37
import static org.apache.datasketches.hll.PreambleUtil.insertCurMode;
38
import static org.apache.datasketches.hll.PreambleUtil.insertEmptyFlag;
39
import static org.apache.datasketches.hll.PreambleUtil.insertFamilyId;
40
import static org.apache.datasketches.hll.PreambleUtil.insertFlags;
41
import static org.apache.datasketches.hll.PreambleUtil.insertInt;
42
import static org.apache.datasketches.hll.PreambleUtil.insertKxQ0;
43
import static org.apache.datasketches.hll.PreambleUtil.insertLgArr;
44
import static org.apache.datasketches.hll.PreambleUtil.insertLgK;
45
import static org.apache.datasketches.hll.PreambleUtil.insertListCount;
46
import static org.apache.datasketches.hll.PreambleUtil.insertModes;
47
import static org.apache.datasketches.hll.PreambleUtil.insertNumAtCurMin;
48
import static org.apache.datasketches.hll.PreambleUtil.insertPreInts;
49
import static org.apache.datasketches.hll.PreambleUtil.insertSerVer;
50

51
import org.apache.datasketches.common.SketchesArgumentException;
52
import org.apache.datasketches.common.SketchesStateException;
53
import org.apache.datasketches.memory.Memory;
54
import org.apache.datasketches.memory.WritableMemory;
55

56
/**
57
 * @author Lee Rhodes
58
 */
59
class DirectCouponList extends AbstractCoupons {
1✔
60
  WritableMemory wmem;
61
  Memory mem;
62
  final boolean compact;
63

64
  private static int checkMemCompactFlag(final WritableMemory wmem, final int lgConfigK) {
65
    assert !extractCompactFlag(wmem);
1✔
66
    return lgConfigK;
1✔
67
  }
68

69
  //called from newInstance, writableWrap and DirectCouponHashSet, must be compact
70
  DirectCouponList(final int lgConfigK, final TgtHllType tgtHllType, final CurMode curMode, final WritableMemory wmem) {
71
    super(checkMemCompactFlag(wmem, lgConfigK), tgtHllType, curMode);
1✔
72
    this.wmem = wmem;
1✔
73
    mem = wmem;
1✔
74
    compact = extractCompactFlag(wmem);
1✔
75
  }
1✔
76

77
  //called from HllSketch.wrap and from DirectCouponHashSet constructor, may or may not be compact
78
  DirectCouponList(final int lgConfigK, final TgtHllType tgtHllType, final CurMode curMode, final Memory mem) {
79
    super(lgConfigK, tgtHllType, curMode);
1✔
80
    wmem = null;
1✔
81
    this.mem = mem;
1✔
82
    compact = extractCompactFlag(mem);
1✔
83
  }
1✔
84

85
  /**
86
   * Standard factory for new DirectCouponList.
87
   * This initializes the given WritableMemory.
88
   * @param lgConfigK the configured Lg K
89
   * @param tgtHllType the configured HLL target
90
   * @param dstMem the destination memory for the sketch.
91
   * @return a new DirectCouponList
92
   */
93
  static DirectCouponList newInstance(final int lgConfigK, final TgtHllType tgtHllType,
94
      final WritableMemory dstMem) {
95
    insertPreInts(dstMem, LIST_PREINTS);
1✔
96
    insertSerVer(dstMem);
1✔
97
    insertFamilyId(dstMem);
1✔
98
    insertLgK(dstMem, lgConfigK);
1✔
99
    insertLgArr(dstMem, LG_INIT_LIST_SIZE);
1✔
100
    insertFlags(dstMem, EMPTY_FLAG_MASK); //empty and not compact
1✔
101
    insertListCount(dstMem, 0);
1✔
102
    insertModes(dstMem, tgtHllType, CurMode.LIST);
1✔
103
    return new DirectCouponList(lgConfigK, tgtHllType, CurMode.LIST, dstMem);
1✔
104
  }
105

106
  @Override //returns on-heap List
107
  CouponList copy() {
108
    return CouponList.heapifyList(mem);
1✔
109
  }
110

111
  @Override //returns on-heap List
112
  CouponList copyAs(final TgtHllType tgtHllType) {
113
    final CouponList clist = CouponList.heapifyList(mem);
1✔
114
    return new CouponList(clist, tgtHllType);
1✔
115
  }
116

117
  @Override
118
  HllSketchImpl couponUpdate(final int coupon) {
119
    if (wmem == null) { noWriteAccess(); }
1✔
120
    final int len = 1 << getLgCouponArrInts();
1✔
121
    for (int i = 0; i < len; i++) { //search for empty slot and duplicates
1✔
122
      final int couponAtIdx = extractInt(mem, LIST_INT_ARR_START + (i << 2));
1✔
123
      if (couponAtIdx == EMPTY) {
1✔
124
        insertInt(wmem, LIST_INT_ARR_START + (i << 2), coupon);
1✔
125
        int couponCount = extractListCount(mem);
1✔
126
        insertListCount(wmem, ++couponCount);
1✔
127
        insertEmptyFlag(wmem, false); //only first time
1✔
128
        if (couponCount >= len) { //array full
1✔
129
          if (lgConfigK < 8) {
1✔
130
            return promoteListOrSetToHll(this);//oooFlag = false
1✔
131
          }
132
          return promoteListToSet(this); //oooFlag = true
1✔
133
        }
134
        return this;
1✔
135
      }
136
      //cell not empty
137
      if (couponAtIdx == coupon) { return this; } //return if duplicate
1✔
138
      //cell not empty & not a duplicate, continue
139
    } //end for
140
    throw new SketchesStateException("Invalid State: no empties & no duplicates");
×
141
  }
142

143
  @Override
144
  int getCompactSerializationBytes() {
145
    return getMemDataStart() + (getCouponCount() << 2);
1✔
146
  }
147

148
  @Override //Overridden by DirectCouponHashSet
149
  int getCouponCount() {
150
    return extractListCount(mem);
1✔
151
  }
152

153
  @Override
154
  int[] getCouponIntArr() { //here only to satisfy the abstract, should not be used
155
    return null;
1✔
156
  }
157

158
  @Override
159
  int getLgCouponArrInts() {
160
    final int lgArr = extractLgArr(mem);
1✔
161
    if (lgArr >= LG_INIT_LIST_SIZE) { return lgArr; }
1✔
162
    //early versions of compact images did not use this lgArr field
163
    final int coupons = getCouponCount();
1✔
164
    return computeLgArr(mem, coupons, lgConfigK);
1✔
165
  }
166

167
  @Override
168
  int getMemDataStart() {
169
    return LIST_INT_ARR_START;
1✔
170
  }
171

172
  @Override
173
  Memory getMemory() {
174
    return mem;
1✔
175
  }
176

177
  @Override
178
  int getPreInts() {
179
    return LIST_PREINTS;
1✔
180
  }
181

182
  @Override
183
  WritableMemory getWritableMemory() {
184
    return wmem;
1✔
185
  }
186

187
  @Override
188
  boolean isCompact() {
189
    return compact;
1✔
190
  }
191

192
  @Override
193
  boolean isMemory() {
194
    return true;
1✔
195
  }
196

197
  @Override
198
  boolean isOffHeap() {
199
    return mem.isDirect();
1✔
200
  }
201

202
  @Override
203
  boolean isSameResource(final Memory mem) {
204
    return this.mem.isSameResource(mem);
1✔
205
  }
206

207
  @Override
208
  PairIterator iterator() {
209
    final long dataStart = getMemDataStart();
1✔
210
    final int lenInts = (compact) ? getCouponCount() : 1 << getLgCouponArrInts();
1✔
211
    return new IntMemoryPairIterator(mem, dataStart, lenInts, lgConfigK);
1✔
212
  }
213

214
  @Override
215
  void mergeTo(final HllSketch that) {
216
    final int lenInts = (compact) ? getCouponCount() : 1 << getLgCouponArrInts();
1✔
217
    final int dataStart = getMemDataStart();
1✔
218
    for (int i = 0; i < lenInts; i++) {
1✔
219
      final int pair = mem.getInt(dataStart + (i << 2));
1✔
220
      if (pair == 0) { continue; }
1✔
221
      that.couponUpdate(pair);
1✔
222
    }
223
  }
1✔
224

225
  @Override
226
  DirectCouponList reset() {
227
    if (wmem == null) {
1✔
228
      throw new SketchesArgumentException("Cannot reset a read-only sketch");
×
229
    }
230
    insertEmptyFlag(wmem, true);
1✔
231
    final int bytes = HllSketch.getMaxUpdatableSerializationBytes(lgConfigK, tgtHllType);
1✔
232
    wmem.clear(0, bytes);
1✔
233

234
    return DirectCouponList.newInstance(lgConfigK, tgtHllType, wmem);
1✔
235
  }
236

237
  //Called by DirectCouponList.couponUpdate()
238
  static final DirectCouponHashSet promoteListToSet(final DirectCouponList src) {
239
    final WritableMemory wmem = src.wmem;
1✔
240

241
    //get the data from the current memory
242
    HllUtil.checkPreamble(wmem); //sanity check
1✔
243
    final int lgConfigK = src.lgConfigK;
1✔
244
    final TgtHllType tgtHllType = src.tgtHllType;
1✔
245
    final int srcOffset = LIST_INT_ARR_START;
1✔
246
    final int couponArrInts = 1 << src.getLgCouponArrInts();
1✔
247
    final int[] couponArr = new int[couponArrInts]; //buffer
1✔
248
    wmem.getIntArray(srcOffset, couponArr, 0, couponArrInts);
1✔
249

250
    //rewrite the memory image as a SET:
251
    insertPreInts(wmem, HASH_SET_PREINTS);
1✔
252
    //SerVer, FamID, LgK  should be OK
253
    insertLgArr(wmem, LG_INIT_SET_SIZE);
1✔
254
    insertCurMin(wmem, 0); //was list count
1✔
255
    insertCurMode(wmem, CurMode.SET);
1✔
256
    //tgtHllType should already be ok
257
    final int maxBytes = HllSketch.getMaxUpdatableSerializationBytes(lgConfigK, tgtHllType);
1✔
258
    wmem.clear(LIST_INT_ARR_START, maxBytes - LIST_INT_ARR_START); //clear all past first 8
1✔
259

260
    //create the tgt
261
    final DirectCouponHashSet dchSet
1✔
262
        = new DirectCouponHashSet(src.lgConfigK, src.tgtHllType, src.wmem);
263

264
    //now reload the coupon data into the set
265
    for (int i = 0; i < couponArrInts; i++) {
1✔
266
      final int coupon = couponArr[i];
1✔
267
      if (coupon != EMPTY) {
1✔
268
        dchSet.couponUpdate(coupon);
1✔
269
      }
270
    }
271
    return dchSet;
1✔
272
  }
273

274
  static final DirectHllArray promoteListOrSetToHll(final DirectCouponList src) {
275
    final WritableMemory wmem = src.wmem;
1✔
276

277
    //get the data from the current list or set memory
278
    HllUtil.checkPreamble(wmem); //sanity check
1✔
279
    final int lgConfigK = src.lgConfigK;
1✔
280
    final TgtHllType tgtHllType = src.tgtHllType;
1✔
281
    final int srcMemDataStart = src.getMemDataStart();
1✔
282
    final double est = src.getEstimate();
1✔
283
    final int couponArrInts = 1 << src.getLgCouponArrInts();
1✔
284
    final int[] couponArr = new int[couponArrInts]; //buffer
1✔
285
    wmem.getIntArray(srcMemDataStart, couponArr, 0, couponArrInts);
1✔
286

287
    //rewrite the memory image as an HLL
288
    insertPreInts(wmem, HLL_PREINTS);
1✔
289
    //SerVer, FamID, LgK should be OK
290
    insertLgArr(wmem, 0); //no Aux possible yet
1✔
291
    insertFlags(wmem, 0); //clear all flags
1✔
292
    insertCurMin(wmem, 0);
1✔
293
    insertCurMode(wmem, CurMode.HLL);
1✔
294
    //tgtHllType should already be set
295
    //we update HipAccum at the end
296
    //clear KxQ0, KxQ1, NumAtCurMin, AuxCount, hllArray, auxArr
297
    final int maxBytes = HllSketch.getMaxUpdatableSerializationBytes(lgConfigK, tgtHllType);
1✔
298
    wmem.clear(LIST_INT_ARR_START, maxBytes - LIST_INT_ARR_START); //clear all past first 8
1✔
299
    insertNumAtCurMin(wmem, 1 << lgConfigK); //set numAtCurMin
1✔
300
    insertKxQ0(wmem, 1 << lgConfigK);
1✔
301

302
    //choose the tgt
303
    final DirectHllArray dirHllArr;
304
    if (tgtHllType == TgtHllType.HLL_4) {
1✔
305
      dirHllArr = new DirectHll4Array(lgConfigK, wmem);
1✔
306
    } else if (tgtHllType == TgtHllType.HLL_6) {
1✔
307
      dirHllArr = new DirectHll6Array(lgConfigK, wmem);
1✔
308
    } else { //Hll_8
309
      dirHllArr = new DirectHll8Array(lgConfigK, wmem);
1✔
310
    }
311

312
    //now load the coupon data into HLL
313
    for (int i = 0; i < couponArrInts; i++) {
1✔
314
      final int coupon = couponArr[i];
1✔
315
      if (coupon != EMPTY) {
1✔
316
        dirHllArr.couponUpdate(coupon);
1✔
317
      }
318
    }
319
    dirHllArr.putHipAccum(est);
1✔
320
    return dirHllArr;
1✔
321
  }
322

323
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc