• 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

94.96
/src/main/java/org/apache/datasketches/sampling/PreambleUtil.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.sampling;
21

22
import static org.apache.datasketches.common.Util.LS;
23
import static org.apache.datasketches.common.Util.zeroPad;
24

25
import java.nio.ByteOrder;
26
import java.util.Locale;
27

28
import org.apache.datasketches.common.Family;
29
import org.apache.datasketches.common.ResizeFactor;
30
import org.apache.datasketches.common.SketchesArgumentException;
31
import org.apache.datasketches.memory.Memory;
32
import org.apache.datasketches.memory.WritableMemory;
33

34
//@formatter:off
35

36
/**
37
 * This class defines the preamble items structure and provides basic utilities for some of the key
38
 * fields. Fields are presented in Little Endian format, but multi-byte values (int, long, double)
39
 * are stored in native byte order. All <tt>byte</tt> values are treated as unsigned.
40
 *
41
 * <h1>Reservoir Sampling</h1>
42
 *
43
 * <p><strong>Sketch:</strong> The count of items seen is limited to 48 bits (~256 trillion) even
44
 * though there are adjacent unused preamble bits. The acceptance probability for an item is a
45
 * double in the range [0,1), limiting us to 53 bits of randomness due to details of the IEEE
46
 * floating point format. To ensure meaningful probabilities as the items seen count approaches
47
 * capacity, we intentionally use slightly fewer bits.</p>
48
 *
49
 * <p>An empty reservoir sampling sketch only requires 8 bytes. A non-empty sampling sketch
50
 * requires 16 bytes of preamble.</p>
51
 *
52
 * <pre>
53
 * Long || Start Byte Adr:
54
 * Adr:
55
 *      ||       0        |    1   |    2   |    3   |    4   |    5   |    6   |    7   |
56
 *  0   || Preamble_Longs | SerVer | FamID  |  Flags |---------Max Res. Size (K)---------|
57
 *
58
 *      ||       8        |    9   |   10   |   11   |   12   |   13   |   14   |   15   |
59
 *  1   ||----------------------------Items Seen Count (N)-------------------------------|
60
 *  </pre>
61
 *
62
 * <p><strong>Union:</strong> The reservoir union has fewer internal parameters to track and uses
63
 * a slightly different preamble structure. The maximum reservoir size intentionally occupies the
64
 * same byte range as the reservoir size in the sketch preamble, allowing the same methods to be
65
 * used for reading and writing the values. The varopt union takes advantage of the same format.
66
 * The items in the union are stored in a reservoir sketch-compatible format after the union
67
 * preamble.
68
 * </p>
69
 *
70
 * <p>An empty union only requires 8 bytes. A non-empty union requires 8 bytes of preamble.</p>
71
 *
72
 * <pre>
73
 * Long || Start Byte Adr:
74
 * Adr:
75
 *      ||       0        |    1   |    2   |    3   |    4   |    5   |    6   |    7   |
76
 *  0   || Preamble_Longs | SerVer | FamID  |  Flags |---------Max Res. Size (K)---------|
77
 * </pre>
78
 *
79
 * 
80
 * <h1>VarOpt Sampling</h1>
81
 * 
82
 * <p><strong>VarOpt:</strong> A VarOpt sketch has a more complex internal items structure and
83
 * requires a larger preamble. Values serving a similar purpose in both reservoir and varopt sampling
84
 * share the same byte ranges, allowing method re-use where practical.</p>
85
 *
86
 * <p>An empty varopt sample requires 8 bytes. A non-empty sketch requires 16 bytes of preamble
87
 * for an under-full sample and otherwise 32 bytes of preamble.</p>
88
 *
89
 * <pre>
90
 * Long || Start Byte Adr:
91
 * Adr:
92
 *      ||       0        |    1   |    2   |    3   |    4   |    5   |    6   |    7   |
93
 *  0   || Preamble_Longs | SerVer | FamID  |  Flags |---------Max Res. Size (K)---------|
94
 *
95
 *      ||       8        |    9   |   10   |   11   |   12   |   13   |   14   |   15   |
96
 *  1   ||----------------------------Items Seen Count (N)-------------------------------|
97
 *
98
 *      ||      16        |   17   |   18   |   19   |   20   |   21   |   22   |   23   |
99
 *  2   ||--------Item Count in R-----------|-----------Item Count in H------------------|
100
 *
101
 *      ||      24        |   25   |   26   |   27   |   28   |   29   |   30   |   31   |
102
 *  3   ||------------------------------Total Weight in R--------------------------------|
103
 *  </pre>
104
 *
105
 * <p><strong>VarOpt Union:</strong> VarOpt unions also store more information than a reservoir
106
 * sketch. As before, we keep values with similar to the same meaning in corresponding locations
107
 * actoss sketch and union formats. The items in the union are stored in a varopt sketch-compatible
108
 * format after the union preamble.</p>
109
 *
110
 * <p>An empty union only requires 8 bytes. A non-empty union requires 32 bytes of preamble.</p>
111
 *
112
 * <pre>
113
 * Long || Start Byte Adr:
114
 * Adr:
115
 *      ||       0        |    1   |    2   |    3   |    4   |    5   |    6   |    7   |
116
 *  0   || Preamble_Longs | SerVer | FamID  |  Flags |---------Max Res. Size (K)---------|
117
 *
118
 *      ||       8        |    9   |   10   |   11   |   12   |   13   |   14   |   15   |
119
 *  1   ||----------------------------Items Seen Count (N)-------------------------------|
120
 *
121
 *      ||      16        |   17   |   18   |   19   |   20   |   21   |   22   |   23   |
122
 *  2   ||-------------------------Outer Tau Numerator (double)--------------------------|
123
 *
124
 *      ||      24        |   25   |   26   |   27   |   28   |   29   |   30   |   31   |
125
 *  3   ||-------------------------Outer Tau Denominator (long)--------------------------|
126
 *  </pre>
127
 *
128
 * 
129
 * <h1>EPPS Sampling</h1>
130
 * 
131
 * <p>An empty sketch requires 8 bytes.
132
 *
133
 * <pre>
134
 * Long || Start Byte Adr:
135
 * Adr:
136
 *      ||       0        |    1   |    2   |    3   |    4   |    5   |    6   |    7   |
137
 *  0   || Preamble_Longs | SerVer | FamID  |  Flags |---------Max Res. Size (K)---------|
138
 * </pre>
139
 *
140
 * <p>A non-empty sketch requires 40 bytes of preamble. C looks like part of
141
 * the preamble but is treated as part of the sample state.
142
 *
143
 * <p>The count of items seen is not used but preserved as the value seems like a useful
144
 * count to track.
145
 *
146
 * <pre>
147
 * Long || Start Byte Adr:
148
 * Adr:
149
 *      ||       0        |    1   |    2   |    3   |    4   |    5   |    6   |    7   |
150
 *  0   || Preamble_Longs | SerVer | FamID  |  Flags |---------Max Res. Size (K)---------|
151
 *
152
 *      ||       8        |    9   |   10   |   11   |   12   |   13   |   14   |   15   |
153
 *  1   ||---------------------------Items Seen Count (N)--------------------------------|
154
 *
155
 *      ||      16        |   17   |   18   |   19   |   20   |   21   |   22   |   23   |
156
 *  2   ||----------------------------Cumulative Weight----------------------------------|
157
 *
158
 *      ||      24        |   25   |   26   |   27   |   28   |   29   |   30   |   31   |
159
 *  3   ||-----------------------------Max Item Weight-----------------------------------|
160
 *
161
 *      ||      32        |   33   |   34   |   35   |   36   |   37   |   38   |   39   |
162
 *  4   ||----------------------------------Rho------------------------------------------|
163
 *
164
 *      ||      40        |   41   |   42   |   43   |   44   |   45   |   46   |   47   |
165
 *  5   ||-----------------------------------C-------------------------------------------|
166
 *
167
 *      ||      40+                      |
168
 *  6+  ||  {Items Array}                |
169
 *      ||  {Optional Item (if needed)}  |
170
 * </pre>
171
 * 
172
 *  @author Jon Malkin
173
 *  @author Lee Rhodes
174
 */
175
final class PreambleUtil {
176

177
  private PreambleUtil() {}
178

179
  // ###### DO NOT MESS WITH THIS FROM HERE ...
180
  // Preamble byte Addresses
181
  static final int PREAMBLE_LONGS_BYTE   = 0; // Only low 6 bits used
182
  static final int LG_RESIZE_FACTOR_BIT  = 6; // upper 2 bits. Not used by compact or direct.
183
  static final int SER_VER_BYTE          = 1;
184
  static final int FAMILY_BYTE           = 2;
185
  static final int FLAGS_BYTE            = 3;
186
  static final int RESERVOIR_SIZE_SHORT  = 4; // used in ser_ver 1
187
  static final int RESERVOIR_SIZE_INT    = 4;
188
  static final int SERDE_ID_SHORT        = 6; // used in ser_ver 1
189
  static final int ITEMS_SEEN_LONG       = 8;
190

191
  static final int MAX_K_SIZE_INT        = 4; // used in Union only
192
  static final int OUTER_TAU_NUM_DOUBLE  = 16; // used in Varopt Union only
193
  static final int OUTER_TAU_DENOM_LONG  = 24; // used in Varopt Union only
194

195
  // constants and addresses used in varopt
196
  static final int ITEM_COUNT_H_INT      = 16;
197
  static final int ITEM_COUNT_R_INT      = 20;
198
  static final int TOTAL_WEIGHT_R_DOUBLE = 24;
199
  static final int VO_PRELONGS_EMPTY     = Family.VAROPT.getMinPreLongs();
1✔
200
  static final int VO_PRELONGS_WARMUP    = 3;   // Doesn't match min or max prelongs in Family
201
  static final int VO_PRELONGS_FULL      = Family.VAROPT.getMaxPreLongs();
1✔
202

203
  // constants and addresses used in EBPPS
204
  static final int EBPPS_CUM_WT_DOUBLE   = 16;
205
  static final int EBPPS_MAX_WT_DOUBLE   = 24;
206
  static final int EBPPS_RHO_DOUBLE      = 32;
207

208
  // flag bit masks
209
  //static final int BIG_ENDIAN_FLAG_MASK = 1;
210
  //static final int READ_ONLY_FLAG_MASK  = 2;
211
  static final int EMPTY_FLAG_MASK      = 4;
212
  static final int HAS_PARTIAL_ITEM_MASK = 8; // EBPPS only
213
  static final int GADGET_FLAG_MASK     = 128;
214

215
  //Other constants
216
  static final int RESERVOIR_SER_VER    = 2;
217
  static final int VAROPT_SER_VER       = 2;
218
  static final int EBPPS_SER_VER        = 1;
219

220
  static final boolean NATIVE_ORDER_IS_BIG_ENDIAN  =
1✔
221
      (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN);
1✔
222

223
  // STRINGS
224

225
  /**
226
   * Returns a human readable string summary of the preamble state of the given byte array.
227
   * Used primarily in testing.
228
   *
229
   * @param byteArr the given byte array.
230
   * @return the summary preamble string.
231
   */
232
  static String preambleToString(final byte[] byteArr) {
233
    final Memory mem = Memory.wrap(byteArr);
1✔
234
    return preambleToString(mem);
1✔
235
  }
236

237
  /**
238
   * Returns a human readable string summary of the preamble state of the given Memory.
239
   * Note: other than making sure that the given Memory size is large
240
   * enough for just the preamble, this does not do much value checking of the contents of the
241
   * preamble as this is primarily a tool for debugging the preamble visually.
242
   *
243
   * @param mem the given Memory.
244
   * @return the summary preamble string.
245
   */
246
  static String preambleToString(final Memory mem) {
247
    final int preLongs = getAndCheckPreLongs(mem);  // make sure we can get the assumed preamble
1✔
248

249
    final Family family = Family.idToFamily(mem.getByte(FAMILY_BYTE));
1✔
250

251
    switch (family) {
1✔
252
      case RESERVOIR:
253
      case VAROPT:
254
        return sketchPreambleToString(mem, family, preLongs);
1✔
255
      case RESERVOIR_UNION:
256
      case VAROPT_UNION:
257
        return unionPreambleToString(mem, family, preLongs);
1✔
258
      default:
259
        throw new SketchesArgumentException("Inspecting preamble with Sampling family's "
1✔
260
                + "PreambleUtil with object of family " + family.getFamilyName());
1✔
261
    }
262
  }
263

264
  private static String sketchPreambleToString(final Memory mem,
265
                                               final Family family,
266
                                               final int preLongs) {
267
    final ResizeFactor rf = ResizeFactor.getRF(extractResizeFactor(mem));
1✔
268
    final int serVer = extractSerVer(mem);
1✔
269

270
    // Flags
271
    final int flags = extractFlags(mem);
1✔
272
    final String flagsStr = zeroPad(Integer.toBinaryString(flags), 8) + ", " + (flags);
1✔
273
    //final boolean bigEndian = (flags & BIG_ENDIAN_FLAG_MASK) > 0;
274
    //final String nativeOrder = ByteOrder.nativeOrder().toString();
275
    //final boolean readOnly = (flags & READ_ONLY_FLAG_MASK) > 0;
276
    final boolean isEmpty = (flags & EMPTY_FLAG_MASK) > 0;
1✔
277
    final boolean isGadget = (flags & GADGET_FLAG_MASK) > 0;
1✔
278

279
    final int k;
280
    if (serVer == 1) {
1✔
281
      final short encK = extractEncodedReservoirSize(mem);
1✔
282
      k = ReservoirSize.decodeValue(encK);
1✔
283
    } else {
1✔
284
      k = extractK(mem);
1✔
285
    }
286

287
    long n = 0;
1✔
288
    if (!isEmpty) {
1✔
289
      n = extractN(mem);
1✔
290
    }
291
    final long dataBytes = mem.getCapacity() - (preLongs << 3);
1✔
292

293
    final StringBuilder sb = new StringBuilder();
1✔
294
    sb.append(LS)
1✔
295
      .append("### END ")
1✔
296
      .append(family.getFamilyName().toUpperCase(Locale.US))
1✔
297
      .append(" PREAMBLE SUMMARY").append(LS)
1✔
298
      .append("Byte  0: Preamble Longs       : ").append(preLongs).append(LS)
1✔
299
      .append("Byte  0: ResizeFactor         : ").append(rf.toString()).append(LS)
1✔
300
      .append("Byte  1: Serialization Version: ").append(serVer).append(LS)
1✔
301
      .append("Byte  2: Family               : ").append(family.toString()).append(LS)
1✔
302
      .append("Byte  3: Flags Field          : ").append(flagsStr).append(LS)
1✔
303
      //.append("  BIG_ENDIAN_STORAGE          : ").append(bigEndian).append(LS)
304
      //.append("  (Native Byte Order)         : ").append(nativeOrder).append(LS)
305
      //.append("  READ_ONLY                   : ").append(readOnly).append(LS)
306
      .append("  EMPTY                       : ").append(isEmpty).append(LS);
1✔
307
    if (family == Family.VAROPT) {
1✔
308
      sb.append("  GADGET                      : ").append(isGadget).append(LS);
1✔
309
    }
310
    sb.append("Bytes  4-7: Sketch Size (k)   : ").append(k).append(LS);
1✔
311
    if (!isEmpty) {
1✔
312
      sb.append("Bytes 8-15: Items Seen (n)    : ").append(n).append(LS);
1✔
313
    }
314
    if ((family == Family.VAROPT) && !isEmpty) {
1✔
315
      final int hCount = extractHRegionItemCount(mem);
×
316
      final int rCount = extractRRegionItemCount(mem);
×
317
      final double totalRWeight = extractTotalRWeight(mem);
×
318
      sb.append("Bytes 16-19: H region count   : ").append(hCount).append(LS)
×
319
        .append("Bytes 20-23: R region count   : ").append(rCount).append(LS);
×
320
      if (rCount > 0) {
×
321
        sb.append("Bytes 24-31: R region weight  : ").append(totalRWeight).append(LS);
×
322
      }
323
    }
324

325
    sb.append("TOTAL Sketch Bytes            : ").append(mem.getCapacity()).append(LS)
1✔
326
      .append("  Preamble Bytes              : ").append(preLongs << 3).append(LS)
1✔
327
      .append("  Data Bytes                  : ").append(dataBytes).append(LS)
1✔
328
      .append("### END ")
1✔
329
      .append(family.getFamilyName().toUpperCase(Locale.US))
1✔
330
      .append(" PREAMBLE SUMMARY").append(LS);
1✔
331
    return sb.toString();
1✔
332
  }
333

334
  private static String unionPreambleToString(final Memory mem,
335
                                              final Family family,
336
                                              final int preLongs) {
337
    final ResizeFactor rf = ResizeFactor.getRF(extractResizeFactor(mem));
1✔
338
    final int serVer = extractSerVer(mem);
1✔
339

340
    // Flags
341
    final int flags = extractFlags(mem);
1✔
342
    final String flagsStr = zeroPad(Integer.toBinaryString(flags), 8) + ", " + (flags);
1✔
343
    //final boolean bigEndian = (flags & BIG_ENDIAN_FLAG_MASK) > 0;
344
    //final String nativeOrder = ByteOrder.nativeOrder().toString();
345
    //final boolean readOnly = (flags & READ_ONLY_FLAG_MASK) > 0;
346
    final boolean isEmpty = (flags & EMPTY_FLAG_MASK) > 0;
1✔
347

348
    final int k;
349
    if (serVer == 1) {
1✔
350
      final short encK = extractEncodedReservoirSize(mem);
1✔
351
      k = ReservoirSize.decodeValue(encK);
1✔
352
    } else {
1✔
353
      k = extractK(mem);
1✔
354
    }
355

356
    final long dataBytes = mem.getCapacity() - (preLongs << 3);
1✔
357

358
    return LS
1✔
359
            + "### END " + family.getFamilyName().toUpperCase(Locale.US) + " PREAMBLE SUMMARY" + LS
1✔
360
            + "Byte  0: Preamble Longs           : " + preLongs + LS
361
            + "Byte  0: ResizeFactor             : " + rf.toString() + LS
1✔
362
            + "Byte  1: Serialization Version    : " + serVer + LS
363
            + "Byte  2: Family                   : " + family.toString() + LS
1✔
364
            + "Byte  3: Flags Field              : " + flagsStr + LS
365
            //+ "  BIG_ENDIAN_STORAGE              : " + bigEndian + LS
366
            //+ "  (Native Byte Order)             : " + nativeOrder + LS
367
            //+ "  READ_ONLY                       : " + readOnly + LS
368
            + "  EMPTY                           : " + isEmpty + LS
369
            + "Bytes  4-7: Max Sketch Size (maxK): " + k + LS
370
            + "TOTAL Sketch Bytes                : " + mem.getCapacity() + LS
1✔
371
            + "  Preamble Bytes                  : " + (preLongs << 3) + LS
372
            + "  Sketch Bytes                    : " + dataBytes + LS
373
            + "### END " + family.getFamilyName().toUpperCase(Locale.US) + " PREAMBLE SUMMARY" + LS;
1✔
374
  }
375

376
  // Extraction methods
377

378
  static int extractPreLongs(final Memory mem) {
379
    return mem.getByte(PREAMBLE_LONGS_BYTE) & 0x3F;
1✔
380
  }
381

382
  static int extractResizeFactor(final Memory mem) {
383
    return (mem.getByte(PREAMBLE_LONGS_BYTE) >>> LG_RESIZE_FACTOR_BIT) & 0x3;
1✔
384
  }
385

386
  static int extractSerVer(final Memory mem) {
387
    return mem.getByte(SER_VER_BYTE) & 0xFF;
1✔
388
  }
389

390
  static int extractFamilyID(final Memory mem) {
391
    return mem.getByte(FAMILY_BYTE) & 0xFF;
1✔
392
  }
393

394
  static int extractFlags(final Memory mem) {
395
    return mem.getByte(FLAGS_BYTE) & 0xFF;
1✔
396
  }
397

398
  static short extractEncodedReservoirSize(final Memory mem) {
399
    return mem.getShort(RESERVOIR_SIZE_SHORT);
1✔
400
  }
401

402
  static int extractK(final Memory mem) {
403
    return mem.getInt(RESERVOIR_SIZE_INT);
1✔
404
  }
405

406
  static int extractMaxK(final Memory mem) {
407
    return extractK(mem);
1✔
408
  }
409

410
  static long extractN(final Memory mem) {
411
    return mem.getLong(ITEMS_SEEN_LONG);
1✔
412
  }
413

414
  static int extractHRegionItemCount(final Memory mem) {
415
    return mem.getInt(ITEM_COUNT_H_INT);
1✔
416
  }
417

418
  static int extractRRegionItemCount(final Memory mem) {
419
    return mem.getInt(ITEM_COUNT_R_INT);
1✔
420
  }
421

422
  static double extractTotalRWeight(final Memory mem) {
423
    return mem.getDouble(TOTAL_WEIGHT_R_DOUBLE);
1✔
424
  }
425

426
  static double extractOuterTauNumerator(final Memory mem) {
427
    return mem.getDouble(OUTER_TAU_NUM_DOUBLE);
1✔
428
  }
429

430
  static long extractOuterTauDenominator(final Memory mem) {
431
    return mem.getLong(OUTER_TAU_DENOM_LONG);
1✔
432
  }
433

434
  static double extractEbppsCumulativeWeight(final Memory mem) {
435
    return mem.getDouble(EBPPS_CUM_WT_DOUBLE);
1✔
436
  }
437

438
  static double extractEbppsMaxWeight(final Memory mem) {
439
    return mem.getDouble(EBPPS_MAX_WT_DOUBLE);
1✔
440
  }
441

442
  static double extractEbppsRho(final Memory mem) {
443
    return mem.getDouble(EBPPS_RHO_DOUBLE);
1✔
444
  }
445

446
  // Insertion methods
447

448
  static void insertPreLongs(final WritableMemory wmem, final int preLongs) {
449
    final int curByte = wmem.getByte(PREAMBLE_LONGS_BYTE);
1✔
450
    final int mask = 0x3F;
1✔
451
    final byte newByte = (byte) ((preLongs & mask) | (~mask & curByte));
1✔
452
    wmem.putByte(PREAMBLE_LONGS_BYTE, newByte);
1✔
453
  }
1✔
454

455
  static void insertLgResizeFactor(final WritableMemory wmem, final int rf) {
456
    final int curByte = wmem.getByte(PREAMBLE_LONGS_BYTE);
1✔
457
    final int shift = LG_RESIZE_FACTOR_BIT; // shift in bits
1✔
458
    final int mask = 3;
1✔
459
    final byte newByte = (byte) (((rf & mask) << shift) | (~(mask << shift) & curByte));
1✔
460
    wmem.putByte(PREAMBLE_LONGS_BYTE, newByte);
1✔
461
  }
1✔
462

463
  static void insertSerVer(final WritableMemory wmem, final int serVer) {
464
    wmem.putByte(SER_VER_BYTE, (byte) serVer);
1✔
465
  }
1✔
466

467
  static void insertFamilyID(final WritableMemory wmem, final int famId) {
468
    wmem.putByte(FAMILY_BYTE, (byte) famId);
1✔
469
  }
1✔
470

471
  static void insertFlags(final WritableMemory wmem, final int flags) {
472
    wmem.putByte(FLAGS_BYTE,  (byte) flags);
1✔
473
  }
1✔
474

475
  static void insertK(final WritableMemory wmem, final int k) {
476
    wmem.putInt(RESERVOIR_SIZE_INT, k);
1✔
477
  }
1✔
478

479
  static void insertMaxK(final WritableMemory wmem, final int maxK) {
480
    insertK(wmem, maxK);
1✔
481
  }
1✔
482

483
  static void insertN(final WritableMemory wmem, final long totalSeen) {
484
    wmem.putLong(ITEMS_SEEN_LONG, totalSeen);
1✔
485
  }
1✔
486

487
  static void insertHRegionItemCount(final WritableMemory wmem, final int hCount) {
488
    wmem.putInt(ITEM_COUNT_H_INT, hCount);
1✔
489
  }
1✔
490

491
  static void insertRRegionItemCount(final WritableMemory wmem, final int rCount) {
492
    wmem.putInt(ITEM_COUNT_R_INT, rCount);
1✔
493
  }
1✔
494

495
  static void insertTotalRWeight(final WritableMemory wmem, final double weight) {
496
    wmem.putDouble(TOTAL_WEIGHT_R_DOUBLE, weight);
1✔
497
  }
1✔
498

499
  static void insertOuterTauNumerator(final WritableMemory wmem, final double numer) {
500
    wmem.putDouble(OUTER_TAU_NUM_DOUBLE, numer);
1✔
501
  }
1✔
502

503
  static void insertOuterTauDenominator(final WritableMemory wmem, final long denom) {
504
    wmem.putLong(OUTER_TAU_DENOM_LONG, denom);
1✔
505
  }
1✔
506

507
  static void insertEbppsCumulativeWeight(final WritableMemory wmem, final double cumWt) {
508
    wmem.putDouble(EBPPS_CUM_WT_DOUBLE, cumWt);
1✔
509
  }
1✔
510

511
  static void insertEbppsMaxWeight(final WritableMemory wmem, final double maxWt) {
512
    wmem.putDouble(EBPPS_MAX_WT_DOUBLE, maxWt);
1✔
513
  }
1✔
514

515
  static void insertEbppsRho(final WritableMemory wmem, final double rho) {
516
    wmem.putDouble(EBPPS_RHO_DOUBLE, rho);
1✔
517
  }
1✔
518

519
  /**
520
   * Checks Memory for capacity to hold the preamble and returns the extracted preLongs.
521
   * @param mem the given Memory
522
   * @return the extracted prelongs value.
523
   */
524
  static int getAndCheckPreLongs(final Memory mem) {
525
    final long cap = mem.getCapacity();
1✔
526
    if (cap < 8) { throwNotBigEnough(cap, 8); }
1✔
527
    final int preLongs = mem.getByte(0) & 0x3F;
1✔
528
    final int required = Math.max(preLongs << 3, 8);
1✔
529
    if (cap < required) { throwNotBigEnough(cap, required); }
1✔
530
    return preLongs;
1✔
531
  }
532

533
  private static void throwNotBigEnough(final long cap, final int required) {
534
    throw new SketchesArgumentException(
1✔
535
        "Possible Corruption: Size of byte array or Memory not large enough: Size: " + cap
536
        + ", Required: " + required);
537
  }
538
}
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