• 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

96.55
/src/main/java/org/apache/datasketches/hll/HllUtil.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 java.lang.Math.log;
23
import static java.lang.Math.sqrt;
24
import static org.apache.datasketches.common.Util.checkBounds;
25
import static org.apache.datasketches.hll.PreambleUtil.HASH_SET_PREINTS;
26
import static org.apache.datasketches.hll.PreambleUtil.HLL_PREINTS;
27
import static org.apache.datasketches.hll.PreambleUtil.LIST_PREINTS;
28
import static org.apache.datasketches.hll.PreambleUtil.extractCurMode;
29
import static org.apache.datasketches.hll.PreambleUtil.extractFamilyId;
30
import static org.apache.datasketches.hll.PreambleUtil.extractPreInts;
31
import static org.apache.datasketches.hll.PreambleUtil.extractSerVer;
32

33
import org.apache.datasketches.common.Family;
34
import org.apache.datasketches.common.SketchesArgumentException;
35
import org.apache.datasketches.common.SketchesReadOnlyException;
36
import org.apache.datasketches.memory.Memory;
37

38
/**
39
 * @author Lee Rhodes
40
 * @author Kevin Lang
41
 */
42
final class HllUtil {
×
43
  static final int KEY_BITS_26 = 26;
44
  static final int VAL_BITS_6 = 6;
45
  static final int KEY_MASK_26 = (1 << KEY_BITS_26) - 1;
46
  static final int VAL_MASK_6 = (1 << VAL_BITS_6) - 1;
47
  static final int EMPTY = 0;
48
  static final int MIN_LOG_K = 4;
49
  static final int MAX_LOG_K = 21;
50

51
  static final double HLL_HIP_RSE_FACTOR = sqrt(log(2.0)); //.8325546
1✔
52
  static final double HLL_NON_HIP_RSE_FACTOR = sqrt((3.0 * log(2.0)) - 1.0); //1.03896
1✔
53
  static final double COUPON_RSE_FACTOR = .409; //at transition point not the asymptote
54

55
  static final double COUPON_RSE = COUPON_RSE_FACTOR / (1 << 13);
56

57
  static final int LG_INIT_LIST_SIZE = 3;
58
  static final int LG_INIT_SET_SIZE = 5;
59
  static final int RESIZE_NUMER = 3;
60
  static final int RESIZE_DENOM = 4;
61

62
  static final int loNibbleMask = 0x0f;
63
  static final int hiNibbleMask = 0xf0;
64
  static final int AUX_TOKEN = 0xf;
65

66
  /**
67
   * Log2 table sizes for exceptions based on lgK from 0 to 26.
68
   * However, only lgK from 4 to 21 are used.
69
   */
70
  static final int[] LG_AUX_ARR_INTS = new int[] {
1✔
71
    0, 2, 2, 2, 2, 2, 2, 3, 3, 3,   //0 - 9
72
    4, 4, 5, 5, 6, 7, 8, 9, 10, 11, //10 - 19
73
    12, 13, 14, 15, 16, 17, 18      //20 - 26
74
  };
75

76
  //Checks
77
  static final int checkLgK(final int lgK) {
78
    if ((lgK >= MIN_LOG_K) && (lgK <= MAX_LOG_K)) { return lgK; }
1✔
79
    throw new SketchesArgumentException(
1✔
80
      "Log K must be between 4 and 21, inclusive: " + lgK);
81
  }
82

83
  static void checkMemSize(final long minBytes, final long capBytes) {
84
    if (capBytes < minBytes) {
1✔
85
      throw new SketchesArgumentException(
1✔
86
          "Given WritableMemory is not large enough: " + capBytes);
87
    }
88
  }
1✔
89

90
  static final void checkNumStdDev(final int numStdDev) {
91
    if ((numStdDev < 1) || (numStdDev > 3)) {
1✔
92
      throw new SketchesArgumentException(
1✔
93
          "NumStdDev may not be less than 1 or greater than 3.");
94
    }
95
  }
1✔
96

97
  static CurMode checkPreamble(final Memory mem) {
98
    checkBounds(0, 8, mem.getCapacity()); //need min 8 bytes
1✔
99
    final int preInts = extractPreInts(mem);
1✔
100
    checkBounds(0, (long)preInts * Integer.BYTES, mem.getCapacity());
1✔
101
    final int serVer = extractSerVer(mem);
1✔
102
    final int famId = extractFamilyId(mem);
1✔
103
    final CurMode curMode = extractCurMode(mem);
1✔
104
    if (
1✔
105
      (famId != Family.HLL.getID())
1✔
106
      || (serVer != 1)
107
      || ((preInts != LIST_PREINTS) && (preInts != HASH_SET_PREINTS) && (preInts != HLL_PREINTS))
108
      || ((curMode == CurMode.LIST) && (preInts != LIST_PREINTS))
109
      || ((curMode == CurMode.SET) && (preInts != HASH_SET_PREINTS))
110
      || ((curMode == CurMode.HLL) && (preInts != HLL_PREINTS))
111
    ) {
112
      throw new SketchesArgumentException("Possible Corruption, Invalid Preamble:"
1✔
113
          + PreambleUtil.toString(mem));
1✔
114
    }
115
    return curMode;
1✔
116
  }
117

118
  //Exceptions
119
  static final void noWriteAccess() {
120
    throw new SketchesReadOnlyException(
1✔
121
        "This sketch is compact or does not have write access to the underlying resource.");
122
  }
123

124
  //Used for thrown exceptions
125
  static String pairString(final int pair) {
126
    return "SlotNo: " + getPairLow26(pair) + ", Value: "
1✔
127
        + getPairValue(pair);
1✔
128
  }
129

130
  //Pairs
131
  static int pair(final int slotNo, final int value) {
132
    return (value << KEY_BITS_26) | (slotNo & KEY_MASK_26);
1✔
133
  }
134

135
  static final int getPairLow26(final int coupon) {
136
    return coupon & KEY_MASK_26;
1✔
137
  }
138

139
  static final int getPairValue(final int coupon) {
140
    return coupon >>> KEY_BITS_26;
1✔
141
  }
142

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