• 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.78
/src/main/java/org/apache/datasketches/theta/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.theta;
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

27
import org.apache.datasketches.common.Family;
28
import org.apache.datasketches.common.ResizeFactor;
29
import org.apache.datasketches.common.SketchesArgumentException;
30
import org.apache.datasketches.common.Util;
31
import org.apache.datasketches.memory.Memory;
32
import org.apache.datasketches.memory.WritableMemory;
33
import org.apache.datasketches.thetacommon.ThetaUtil;
34

35
//@formatter:off
36

37
/**
38
 * This class defines the preamble data structure and provides basic utilities for some of the key
39
 * fields.
40
 * <p>The intent of the design of this class was to isolate the detailed knowledge of the bit and
41
 * byte layout of the serialized form of the sketches derived from the Sketch class into one place.
42
 * This allows the possibility of the introduction of different serialization
43
 * schemes with minimal impact on the rest of the library.</p>
44
 *
45
 * <p>
46
 * MAP: Low significance bytes of this <i>long</i> data structure are on the right. However, the
47
 * multi-byte integers (<i>int</i> and <i>long</i>) are stored in native byte order. The
48
 * <i>byte</i> values are treated as unsigned.</p>
49
 *
50
 * <p>An empty CompactSketch only requires 8 bytes.
51
 * Flags: notSI, Ordered*, Compact, Empty*, ReadOnly, LE.
52
 * (*) Earlier versions did not set these.</p>
53
 *
54
 * <pre>
55
 * Long || Start Byte Adr:
56
 * Adr:
57
 *      ||    7   |    6   |    5   |    4   |    3   |    2   |    1   |     0              |
58
 *  0   ||    Seed Hash    | Flags  |        |        | FamID  | SerVer |     PreLongs = 1   |
59
 * </pre>
60
 *
61
 * <p>A SingleItemSketch (extends CompactSketch) requires an 8 byte preamble plus a single
62
 * hash item of 8 bytes. Flags: SingleItem*, Ordered, Compact, notEmpty, ReadOnly, LE.
63
 * (*) Earlier versions did not set these.</p>
64
 *
65
 * <pre>
66
 * Long || Start Byte Adr:
67
 * Adr:
68
 *      ||    7   |    6   |    5   |    4   |    3   |    2   |    1   |     0              |
69
 *  0   ||    Seed Hash    | Flags  |        |        | FamID  | SerVer |     PreLongs = 1   |
70
 *
71
 *      ||   15   |   14   |   13   |   12   |   11   |   10   |    9   |     8              |
72
 *  1   ||---------------------------Single long hash----------------------------------------|
73
 * </pre>
74
 *
75
 * <p>An exact (non-estimating) CompactSketch requires 16 bytes of preamble plus a compact array of
76
 * longs.</p>
77
 *
78
 * <pre>
79
 * Long || Start Byte Adr:
80
 * Adr:
81
 *      ||    7   |    6   |    5   |    4   |    3   |    2   |    1   |     0              |
82
 *  0   ||    Seed Hash    | Flags  |        |        | FamID  | SerVer |     PreLongs = 2   |
83
 *
84
 *      ||   15   |   14   |   13   |   12   |   11   |   10   |    9   |     8              |
85
 *  1   ||-----------------p-----------------|----------Retained Entries Count---------------|
86
 *
87
 *      ||   23   |   22   |   21    |  20   |   19   |   18   |   17   |    16              |
88
 *  2   ||----------------------Start of Compact Long Array----------------------------------|
89
 * </pre>
90
 *
91
 * <p>An estimating CompactSketch requires 24 bytes of preamble plus a compact array of longs.</p>
92
 *
93
 * <pre>
94
 * Long || Start Byte Adr:
95
 * Adr:
96
 *      ||    7   |    6   |    5   |    4   |    3   |    2   |    1   |     0              |
97
 *  0   ||    Seed Hash    | Flags  |        |        | FamID  | SerVer |     PreLongs = 3   |
98
 *
99
 *      ||   15   |   14   |   13   |   12   |   11   |   10   |    9   |     8              |
100
 *  1   ||-----------------p-----------------|----------Retained Entries Count---------------|
101
 *
102
 *      ||   23   |   22   |   21    |  20   |   19   |   18   |   17   |    16              |
103
 *  2   ||------------------------------THETA_LONG-------------------------------------------|
104
 *
105
 *      ||   31   |   30   |   29   |   28   |   27   |   26   |   25   |    24              |
106
 *  3   ||----------------------Start of Compact Long Array----------------------------------|
107
 *  </pre>
108
 *
109
 * <p>The UpdateSketch and AlphaSketch require 24 bytes of preamble followed by a non-compact
110
 * array of longs representing a hash table.</p>
111
 *
112
 * <p>The following table applies to both the Theta UpdateSketch and the Alpha Sketch</p>
113
 * <pre>
114
 * Long || Start Byte Adr:
115
 * Adr:
116
 *      ||    7   |    6   |    5   |    4   |    3   |    2   |    1   |     0              |
117
 *  0   ||    Seed Hash    | Flags  |  LgArr |  lgNom | FamID  | SerVer | RF, PreLongs = 3   |
118
 *
119
 *      ||   15   |   14   |   13   |   12   |   11   |   10   |    9   |     8              |
120
 *  1   ||-----------------p-----------------|----------Retained Entries Count---------------|
121
 *
122
 *      ||   23   |   22   |   21    |  20   |   19   |   18   |   17   |    16              |
123
 *  2   ||------------------------------THETA_LONG-------------------------------------------|
124
 *
125
 *      ||   31   |   30   |   29   |   28   |   27   |   26   |   25   |    24              |
126
 *  3   ||----------------------Start of Hash Table of longs---------------------------------|
127
 *  </pre>
128
 *
129
 * <p> Union objects require 32 bytes of preamble plus a non-compact array of longs representing a
130
 * hash table.</p>
131
 *
132
 * <pre>
133
 * Long || Start Byte Adr:
134
 * Adr:
135
 *      ||    7   |    6   |    5   |    4   |    3   |    2   |    1   |     0              |
136
 *  0   ||    Seed Hash    | Flags  |  LgArr |  lgNom | FamID  | SerVer | RF, PreLongs = 4   |
137
 *
138
 *      ||   15   |   14   |   13   |   12   |   11   |   10   |    9   |     8              |
139
 *  1   ||-----------------p-----------------|----------Retained Entries Count---------------|
140
 *
141
 *      ||   23   |   22   |   21    |  20   |   19   |   18   |   17   |    16              |
142
 *  2   ||------------------------------THETA_LONG-------------------------------------------|
143
 *
144
 *      ||   31   |   30   |   29   |   28   |   27   |   26   |   25   |    24              |
145
 *  3   ||---------------------------UNION THETA LONG----------------------------------------|
146
 *
147
 *      ||   39   |   38   |   37   |   36   |   35   |   34   |   33   |    32              |
148
 *  4   ||----------------------Start of Hash Table of longs---------------------------------|
149
 *
150
 *  </pre>
151
 *
152
 *  @author Lee Rhodes
153
 */
154
final class PreambleUtil {
155

156
  private PreambleUtil() {}
157

158
  // ###### DO NOT MESS WITH THIS FROM HERE ...
159
  // Preamble byte Addresses
160
  static final int PREAMBLE_LONGS_BYTE        = 0; //lower 6 bits in byte.
161
  static final int LG_RESIZE_FACTOR_BIT       = 6; //upper 2 bits in byte. Not used by compact, direct
162
  static final int SER_VER_BYTE               = 1;
163
  static final int FAMILY_BYTE                = 2; //SerVer1,2 was SKETCH_TYPE_BYTE
164
  static final int LG_NOM_LONGS_BYTE          = 3; //not used by compact
165
  static final int LG_ARR_LONGS_BYTE          = 4; //not used by compact
166
  static final int FLAGS_BYTE                 = 5;
167
  static final int SEED_HASH_SHORT            = 6;  //byte 6,7
168
  static final int RETAINED_ENTRIES_INT       = 8;  //8 byte aligned
169
  static final int P_FLOAT                    = 12; //4 byte aligned, not used by compact
170
  static final int THETA_LONG                 = 16; //8-byte aligned
171
  static final int UNION_THETA_LONG           = 24; //8-byte aligned, only used by Union
172

173
  // flag bit masks
174
  static final int BIG_ENDIAN_FLAG_MASK = 1; //SerVer 1, 2, 3
175
  static final int READ_ONLY_FLAG_MASK  = 2; //Set but not read. Reserved. SerVer 1, 2, 3
176
  static final int EMPTY_FLAG_MASK      = 4; //SerVer 2, 3
177
  static final int COMPACT_FLAG_MASK    = 8; //SerVer 2 was NO_REBUILD_FLAG_MASK, 3
178
  static final int ORDERED_FLAG_MASK    = 16;//SerVer 2 was UNORDERED_FLAG_MASK, 3
179
  static final int SINGLEITEM_FLAG_MASK = 32;//SerVer 3
180
  //The last 2 bits of the flags byte are reserved and assumed to be zero, for now.
181

182
  //Backward compatibility: SerVer1 preamble always 3 longs, SerVer2 preamble: 1, 2, 3 longs
183
  //               SKETCH_TYPE_BYTE             2  //SerVer1, SerVer2
184
  //  V1, V2 types:  Alpha = 1, QuickSelect = 2, SetSketch = 3; V3 only: Buffered QS = 4
185
  static final int LG_RESIZE_RATIO_BYTE_V1    = 5; //used by SerVer 1
186
  static final int FLAGS_BYTE_V1              = 6; //used by SerVer 1
187

188
  //Other constants
189
  static final int SER_VER                    = 3;
190

191
  // serial version 4 compressed ordered sketch, not empty, not single item
192
  static final int ENTRY_BITS_BYTE_V4   = 3; // number of bits packed in deltas between hashes
193
  static final int NUM_ENTRIES_BYTES_BYTE_V4 = 4; // number of bytes used for the number of entries
194
  static final int THETA_LONG_V4             = 8; //8-byte aligned
195

196
  static final boolean NATIVE_ORDER_IS_BIG_ENDIAN  =
1✔
197
      (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN);
1✔
198

199
  /**
200
   * Computes the number of bytes required for a non-full sized sketch in hash-table form.
201
   * This can be used to compute current storage size for heap sketches, or current off-heap memory
202
   * required for off-heap (direct) sketches. This does not apply for compact sketches.
203
   * @param lgArrLongs log2(current hash-table size)
204
   * @param preambleLongs current preamble size
205
   * @return the size in bytes
206
   */
207
  static final int getMemBytes(final int lgArrLongs, final int preambleLongs) {
208
    return (8 << lgArrLongs) + (preambleLongs << 3);
1✔
209
  }
210

211
  // STRINGS
212

213
  /**
214
   * Returns a human readable string summary of the preamble state of the given byte array.
215
   * Used primarily in testing.
216
   *
217
   * @param byteArr the given byte array.
218
   * @return the summary preamble string.
219
   */
220
  static String preambleToString(final byte[] byteArr) {
221
    final Memory mem = Memory.wrap(byteArr);
1✔
222
    return preambleToString(mem);
1✔
223
  }
224

225
  /**
226
   * Returns a human readable string summary of the preamble state of the given Memory.
227
   * Note: other than making sure that the given Memory size is large
228
   * enough for just the preamble, this does not do much value checking of the contents of the
229
   * preamble as this is primarily a tool for debugging the preamble visually.
230
   *
231
   * @param mem the given Memory.
232
   * @return the summary preamble string.
233
   */
234
  static String preambleToString(final Memory mem) {
235
    final int preLongs = getAndCheckPreLongs(mem);
1✔
236
    final int rfId = extractLgResizeFactor(mem);
1✔
237
    final ResizeFactor rf = ResizeFactor.getRF(rfId);
1✔
238
    final int serVer = extractSerVer(mem);
1✔
239
    final int familyId = extractFamilyID(mem);
1✔
240
    final Family family = Family.idToFamily(familyId);
1✔
241
    final int lgNomLongs = extractLgNomLongs(mem);
1✔
242
    final int lgArrLongs = extractLgArrLongs(mem);
1✔
243

244
    //Flags
245
    final int flags = extractFlags(mem);
1✔
246
    final String flagsStr = (flags) + ", 0x" + (Integer.toHexString(flags)) + ", "
1✔
247
        + zeroPad(Integer.toBinaryString(flags), 8);
1✔
248
    final String nativeOrder = ByteOrder.nativeOrder().toString();
1✔
249
    final boolean bigEndian = (flags & BIG_ENDIAN_FLAG_MASK) > 0;
1✔
250
    final boolean readOnly = (flags & READ_ONLY_FLAG_MASK) > 0;
1✔
251
    final boolean empty = (flags & EMPTY_FLAG_MASK) > 0;
1✔
252
    final boolean compact = (flags & COMPACT_FLAG_MASK) > 0;
1✔
253
    final boolean ordered = (flags & ORDERED_FLAG_MASK) > 0;
1✔
254
    final boolean singleItem = (flags & SINGLEITEM_FLAG_MASK) > 0; //!empty && (preLongs == 1);
1✔
255

256
    final int seedHash = extractSeedHash(mem);
1✔
257

258
    //assumes preLongs == 1; empty or singleItem
259
    int curCount = singleItem ? 1 : 0;
1✔
260
    float p = (float) 1.0;            //preLongs 1 or 2
1✔
261
    long thetaLong = Long.MAX_VALUE;  //preLongs 1 or 2
1✔
262
    long thetaULong = thetaLong;      //preLongs 1, 2 or 3
1✔
263

264
    if (preLongs == 2) { //exact (non-estimating) CompactSketch
1✔
265
      curCount = extractCurCount(mem);
1✔
266
      p = extractP(mem);
1✔
267
    }
268
    else if (preLongs == 3) { //Update Sketch
1✔
269
      curCount = extractCurCount(mem);
1✔
270
      p = extractP(mem);
1✔
271
      thetaLong = extractThetaLong(mem);
1✔
272
      thetaULong = thetaLong;
1✔
273
    }
274
    else if (preLongs == 4) { //Union
1✔
275
      curCount = extractCurCount(mem);
1✔
276
      p = extractP(mem);
1✔
277
      thetaLong = extractThetaLong(mem);
1✔
278
      thetaULong = extractUnionThetaLong(mem);
1✔
279
    }
280
    //else the same as an empty sketch or singleItem
281

282
    final double thetaDbl = thetaLong / Util.LONG_MAX_VALUE_AS_DOUBLE;
1✔
283
    final String thetaHex = zeroPad(Long.toHexString(thetaLong), 16);
1✔
284
    final double thetaUDbl = thetaULong / Util.LONG_MAX_VALUE_AS_DOUBLE;
1✔
285
    final String thetaUHex = zeroPad(Long.toHexString(thetaULong), 16);
1✔
286

287
    final StringBuilder sb = new StringBuilder();
1✔
288
    sb.append(LS);
1✔
289
    sb.append("### SKETCH PREAMBLE SUMMARY:").append(LS);
1✔
290
    sb.append("Native Byte Order             : ").append(nativeOrder).append(LS);
1✔
291
    sb.append("Byte  0: Preamble Longs       : ").append(preLongs).append(LS);
1✔
292
    sb.append("Byte  0: ResizeFactor         : ").append(rfId + ", " + rf.toString()).append(LS);
1✔
293
    sb.append("Byte  1: Serialization Version: ").append(serVer).append(LS);
1✔
294
    sb.append("Byte  2: Family               : ").append(familyId + ", " + family.toString()).append(LS);
1✔
295
    sb.append("Byte  3: LgNomLongs           : ").append(lgNomLongs).append(LS);
1✔
296
    sb.append("Byte  4: LgArrLongs           : ").append(lgArrLongs).append(LS);
1✔
297
    sb.append("Byte  5: Flags Field          : ").append(flagsStr).append(LS);
1✔
298
    sb.append("  Bit Flag Name               : State:").append(LS);
1✔
299
    sb.append("    0 BIG_ENDIAN_STORAGE      : ").append(bigEndian).append(LS);
1✔
300
    sb.append("    1 READ_ONLY               : ").append(readOnly).append(LS);
1✔
301
    sb.append("    2 EMPTY                   : ").append(empty).append(LS);
1✔
302
    sb.append("    3 COMPACT                 : ").append(compact).append(LS);
1✔
303
    sb.append("    4 ORDERED                 : ").append(ordered).append(LS);
1✔
304
    sb.append("    5 SINGLE_ITEM             : ").append(singleItem).append(LS);
1✔
305
    sb.append("Bytes 6-7  : Seed Hash Hex    : ").append(Integer.toHexString(seedHash)).append(LS);
1✔
306
    if (preLongs == 1) {
1✔
307
      sb.append(" --ABSENT FIELDS, ASSUMED:").append(LS);
1✔
308
      sb.append("Bytes 8-11 : CurrentCount     : ").append(curCount).append(LS);
1✔
309
      sb.append("Bytes 12-15: P                : ").append(p).append(LS);
1✔
310
      sb.append("Bytes 16-23: Theta (double)   : ").append(thetaDbl).append(LS);
1✔
311
      sb.append("             Theta (long)     : ").append(thetaLong).append(LS);
1✔
312
      sb.append("             Theta (long,hex) : ").append(thetaHex).append(LS);
1✔
313
    }
314
    else if (preLongs == 2) {
1✔
315
      sb.append("Bytes 8-11 : CurrentCount     : ").append(curCount).append(LS);
1✔
316
      sb.append("Bytes 12-15: P                : ").append(p).append(LS);
1✔
317
      sb.append(" --ABSENT, ASSUMED:").append(LS);
1✔
318
      sb.append("Bytes 16-23: Theta (double)   : ").append(thetaDbl).append(LS);
1✔
319
      sb.append("             Theta (long)     : ").append(thetaLong).append(LS);
1✔
320
      sb.append("             Theta (long,hex) : ").append(thetaHex).append(LS);
1✔
321
    }
322
    else if (preLongs == 3) {
1✔
323
      sb.append("Bytes 8-11 : CurrentCount     : ").append(curCount).append(LS);
1✔
324
      sb.append("Bytes 12-15: P                : ").append(p).append(LS);
1✔
325
      sb.append("Bytes 16-23: Theta (double)   : ").append(thetaDbl).append(LS);
1✔
326
      sb.append("             Theta (long)     : ").append(thetaLong).append(LS);
1✔
327
      sb.append("             Theta (long,hex) : ").append(thetaHex).append(LS);
1✔
328
    }
329
    else { //preLongs == 4
330
      sb.append("Bytes 8-11 : CurrentCount     : ").append(curCount).append(LS);
1✔
331
      sb.append("Bytes 12-15: P                : ").append(p).append(LS);
1✔
332
      sb.append("Bytes 16-23: Theta (double)   : ").append(thetaDbl).append(LS);
1✔
333
      sb.append("             Theta (long)     : ").append(thetaLong).append(LS);
1✔
334
      sb.append("             Theta (long,hex) : ").append(thetaHex).append(LS);
1✔
335
      sb.append("Bytes 25-31: ThetaU (double)  : ").append(thetaUDbl).append(LS);
1✔
336
      sb.append("             ThetaU (long)    : ").append(thetaULong).append(LS);
1✔
337
      sb.append("             ThetaU (long,hex): ").append(thetaUHex).append(LS);
1✔
338
    }
339
    sb.append(  "Preamble Bytes                : ").append(preLongs * 8).append(LS);
1✔
340
    sb.append(  "Data Bytes                    : ").append(curCount * 8).append(LS);
1✔
341
    sb.append(  "TOTAL Sketch Bytes            : ").append((preLongs + curCount) * 8).append(LS);
1✔
342
    sb.append(  "TOTAL Capacity Bytes          : ").append(mem.getCapacity()).append(LS);
1✔
343
    sb.append("### END SKETCH PREAMBLE SUMMARY").append(LS);
1✔
344
    return sb.toString();
1✔
345
  }
346

347
  //@formatter:on
348

349
  static int extractPreLongs(final Memory mem) {
350
    return mem.getByte(PREAMBLE_LONGS_BYTE) & 0X3F;
1✔
351
  }
352

353
  static int extractLgResizeFactor(final Memory mem) {
354
    return (mem.getByte(PREAMBLE_LONGS_BYTE) >>> LG_RESIZE_FACTOR_BIT) & 0X3;
1✔
355
  }
356

357
  static int extractLgResizeRatioV1(final Memory mem) {
358
    return mem.getByte(LG_RESIZE_RATIO_BYTE_V1) & 0X3;
1✔
359
  }
360

361
  static int extractSerVer(final Memory mem) {
362
    return mem.getByte(SER_VER_BYTE) & 0XFF;
1✔
363
  }
364

365
  static int extractFamilyID(final Memory mem) {
366
    return mem.getByte(FAMILY_BYTE) & 0XFF;
1✔
367
  }
368

369
  static int extractLgNomLongs(final Memory mem) {
370
    return mem.getByte(LG_NOM_LONGS_BYTE) & 0XFF;
1✔
371
  }
372

373
  static int extractLgArrLongs(final Memory mem) {
374
    return mem.getByte(LG_ARR_LONGS_BYTE) & 0XFF;
1✔
375
  }
376

377
  static int extractFlags(final Memory mem) {
378
    return mem.getByte(FLAGS_BYTE) & 0XFF;
1✔
379
  }
380

381
  static int extractFlagsV1(final Memory mem) {
382
    return mem.getByte(FLAGS_BYTE_V1) & 0XFF;
1✔
383
  }
384

385
  static int extractSeedHash(final Memory mem) {
386
    return mem.getShort(SEED_HASH_SHORT) & 0XFFFF;
1✔
387
  }
388

389
  static int extractCurCount(final Memory mem) {
390
    return mem.getInt(RETAINED_ENTRIES_INT);
1✔
391
  }
392

393
  static float extractP(final Memory mem) {
394
    return mem.getFloat(P_FLOAT);
1✔
395
  }
396

397
  static long extractThetaLong(final Memory mem) {
398
    return mem.getLong(THETA_LONG);
1✔
399
  }
400

401
  static long extractUnionThetaLong(final Memory mem) {
402
    return mem.getLong(UNION_THETA_LONG);
1✔
403
  }
404

405
  static int extractEntryBitsV4(final Memory mem) {
406
    return mem.getByte(ENTRY_BITS_BYTE_V4) & 0XFF;
1✔
407
  }
408

409
  static int extractNumEntriesBytesV4(final Memory mem) {
410
    return mem.getByte(NUM_ENTRIES_BYTES_BYTE_V4) & 0XFF;
1✔
411
  }
412

413
  static long extractThetaLongV4(final Memory mem) {
414
    return mem.getLong(THETA_LONG_V4);
1✔
415
  }
416

417
  /**
418
   * Sets PreLongs in the low 6 bits and sets LgRF in the upper 2 bits = 0.
419
   * @param wmem the target WritableMemory
420
   * @param preLongs the given number of preamble longs
421
   */
422
  static void insertPreLongs(final WritableMemory wmem, final int preLongs) {
423
    wmem.putByte(PREAMBLE_LONGS_BYTE, (byte) (preLongs & 0X3F));
1✔
424
  }
1✔
425

426
  /**
427
   * Sets the top 2 lgRF bits and does not affect the lower 6 bits (PreLongs).
428
   * To work properly, this should be called after insertPreLongs().
429
   * @param wmem the target WritableMemory
430
   * @param rf the given lgRF bits
431
   */
432
  static void insertLgResizeFactor(final WritableMemory wmem, final int rf) {
433
    final int curByte = wmem.getByte(PREAMBLE_LONGS_BYTE) & 0xFF;
1✔
434
    final int shift = LG_RESIZE_FACTOR_BIT; // shift in bits
1✔
435
    final int mask = 3;
1✔
436
    final byte newByte = (byte) (((rf & mask) << shift) | (~(mask << shift) & curByte));
1✔
437
    wmem.putByte(PREAMBLE_LONGS_BYTE, newByte);
1✔
438
  }
1✔
439

440
  static void insertSerVer(final WritableMemory wmem, final int serVer) {
441
    wmem.putByte(SER_VER_BYTE, (byte) serVer);
1✔
442
  }
1✔
443

444
  static void insertFamilyID(final WritableMemory wmem, final int famId) {
445
    wmem.putByte(FAMILY_BYTE, (byte) famId);
1✔
446
  }
1✔
447

448
  static void insertLgNomLongs(final WritableMemory wmem, final int lgNomLongs) {
449
    wmem.putByte(LG_NOM_LONGS_BYTE, (byte) lgNomLongs);
1✔
450
  }
1✔
451

452
  static void insertLgArrLongs(final WritableMemory wmem, final int lgArrLongs) {
453
    wmem.putByte(LG_ARR_LONGS_BYTE, (byte) lgArrLongs);
1✔
454
  }
1✔
455

456
  static void insertFlags(final WritableMemory wmem, final int flags) {
457
    wmem.putByte(FLAGS_BYTE, (byte) flags);
1✔
458
  }
1✔
459

460
  static void insertSeedHash(final WritableMemory wmem, final int seedHash) {
461
    wmem.putShort(SEED_HASH_SHORT, (short) seedHash);
1✔
462
  }
1✔
463

464
  static void insertCurCount(final WritableMemory wmem, final int curCount) {
465
    wmem.putInt(RETAINED_ENTRIES_INT, curCount);
1✔
466
  }
1✔
467

468
  static void insertP(final WritableMemory wmem, final float p) {
469
    wmem.putFloat(P_FLOAT, p);
1✔
470
  }
1✔
471

472
  static void insertThetaLong(final WritableMemory wmem, final long thetaLong) {
473
    wmem.putLong(THETA_LONG, thetaLong);
1✔
474
  }
1✔
475

476
  static void insertUnionThetaLong(final WritableMemory wmem, final long unionThetaLong) {
477
    wmem.putLong(UNION_THETA_LONG, unionThetaLong);
1✔
478
  }
1✔
479

480
  static void setEmpty(final WritableMemory wmem) {
481
    int flags = wmem.getByte(FLAGS_BYTE) & 0XFF;
1✔
482
    flags |= EMPTY_FLAG_MASK;
1✔
483
    wmem.putByte(FLAGS_BYTE, (byte) flags);
1✔
484
  }
1✔
485

486
  static void clearEmpty(final WritableMemory wmem) {
487
    int flags = wmem.getByte(FLAGS_BYTE) & 0XFF;
1✔
488
    flags &= ~EMPTY_FLAG_MASK;
1✔
489
    wmem.putByte(FLAGS_BYTE, (byte) flags);
1✔
490
  }
1✔
491

492
  static boolean isEmptyFlag(final Memory mem) {
493
    return ((extractFlags(mem) & EMPTY_FLAG_MASK) > 0);
1✔
494
  }
495

496
  /**
497
   * Checks Memory for capacity to hold the preamble and returns the extracted preLongs.
498
   * @param mem the given Memory
499
   * @return the extracted prelongs value.
500
   */
501
  static int getAndCheckPreLongs(final Memory mem) {
502
    final long cap = mem.getCapacity();
1✔
503
    if (cap < 8) {
1✔
504
      throwNotBigEnough(cap, 8);
×
505
    }
506
    final int preLongs = extractPreLongs(mem);
1✔
507
    final int required = Math.max(preLongs << 3, 8);
1✔
508
    if (cap < required) {
1✔
509
      throwNotBigEnough(cap, required);
×
510
    }
511
    return preLongs;
1✔
512
  }
513

514
  static final short checkMemorySeedHash(final Memory mem, final long seed) {
515
    final short seedHashMem = (short) extractSeedHash(mem);
1✔
516
    ThetaUtil.checkSeedHashes(seedHashMem, ThetaUtil.computeSeedHash(seed)); //throws if bad seedHash
1✔
517
    return seedHashMem;
1✔
518
  }
519

520
  private static void throwNotBigEnough(final long cap, final int required) {
521
    throw new SketchesArgumentException(
1✔
522
        "Possible Corruption: Size of byte array or Memory not large enough: Size: " + cap
523
        + ", Required: " + required);
524
  }
525

526
}
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