• 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.0
/src/main/java/org/apache/datasketches/kll/KllHeapDoublesSketch.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.kll;
21

22
import static org.apache.datasketches.common.ByteArrayUtil.putDoubleLE;
23
import static org.apache.datasketches.kll.KllPreambleUtil.DATA_START_ADR;
24
import static org.apache.datasketches.kll.KllPreambleUtil.DATA_START_ADR_SINGLE_ITEM;
25
import static org.apache.datasketches.kll.KllSketch.SketchStructure.COMPACT_EMPTY;
26
import static org.apache.datasketches.kll.KllSketch.SketchStructure.COMPACT_FULL;
27
import static org.apache.datasketches.kll.KllSketch.SketchStructure.COMPACT_SINGLE;
28
import static org.apache.datasketches.kll.KllSketch.SketchStructure.UPDATABLE;
29
import static org.apache.datasketches.kll.KllSketch.SketchType.DOUBLES_SKETCH;
30

31
import java.util.Arrays;
32
import java.util.Objects;
33

34
import org.apache.datasketches.common.SketchesArgumentException;
35
import org.apache.datasketches.memory.Memory;
36
import org.apache.datasketches.memory.MemoryRequestServer;
37
import org.apache.datasketches.memory.WritableMemory;
38

39
/**
40
 * This class implements an on-heap doubles KllSketch.
41
 *
42
 * <p>Please refer to the documentation in the package-info:<br>
43
 * {@link org.apache.datasketches.kll}</p>
44
 *
45
 * @author Lee Rhodes, Kevin Lang
46
 */
47
final class KllHeapDoublesSketch extends KllDoublesSketch {
48
  private final int k; // configured size of K.
49
  private final int m; // configured size of M.
50
  private long n;      // number of items input into this sketch.
51
  private int minK;    // dynamic minK for error estimation after merging with different k.
52
  private boolean isLevelZeroSorted;
53
  private double minDoubleItem;
54
  private double maxDoubleItem;
55
  private double[] doubleItems;
56

57
  /**
58
   * New instance heap constructor with a given parameters <em>k</em> and <em>m</em>.
59
   *
60
   * @param k parameter that controls size of the sketch and accuracy of estimates.
61
   * <em>k</em> can be between <em>m</em> and 65535, inclusive.
62
   * @param m parameter controls the minimum level width in items. It can be 2, 4, 6 or 8.
63
   * The DEFAULT_M, which is 8 is recommended. Other sizes of <em>m</em> should be considered
64
   * experimental as they have not been as well characterized.
65
   */
66
  KllHeapDoublesSketch(final int k, final int m) {
67
    super(UPDATABLE);
1✔
68
    KllHelper.checkM(m);
1✔
69
    KllHelper.checkK(k, m);
1✔
70
    this.levelsArr = new int[] {k, k};
1✔
71
    this.readOnly = false;
1✔
72
    this.k = k;
1✔
73
    this.m = m;
1✔
74
    this.n = 0;
1✔
75
    this.minK = k;
1✔
76
    this.isLevelZeroSorted = false;
1✔
77
    this.minDoubleItem = Double.NaN;
1✔
78
    this.maxDoubleItem = Double.NaN;
1✔
79
    this.doubleItems = new double[k];
1✔
80
  }
1✔
81

82
  /**
83
   * Used for creating a temporary sketch for use with weighted updates.
84
   */
85
  KllHeapDoublesSketch(final int k, final int m, final double item, final long weight) {
86
    super(UPDATABLE);
1✔
87
    KllHelper.checkM(m);
1✔
88
    KllHelper.checkK(k, m);
1✔
89
    this.levelsArr = KllHelper.createLevelsArray(weight);
1✔
90
    this.readOnly = false;
1✔
91
    this.k = k;
1✔
92
    this.m = m;
1✔
93
    this.n = weight;
1✔
94
    this.minK = k;
1✔
95
    this.isLevelZeroSorted = false;
1✔
96
    this.minDoubleItem = item;
1✔
97
    this.maxDoubleItem = item;
1✔
98
    this.doubleItems = KllDoublesHelper.createItemsArray(item, weight);
1✔
99
  }
1✔
100

101
  /**
102
   * Heapify constructor.
103
   * @param srcMem Memory object that contains data serialized by this sketch.
104
   * @param memValidate the MemoryValidate object
105
   */
106
  private KllHeapDoublesSketch(
107
      final Memory srcMem,
108
      final KllMemoryValidate memValidate) {
109
    super(UPDATABLE);
1✔
110
    final SketchStructure memStructure = memValidate.sketchStructure;
1✔
111
    this.k = memValidate.k;
1✔
112
    this.m = memValidate.m;
1✔
113
    this.n = memValidate.n;
1✔
114
    this.minK = memValidate.minK;
1✔
115
    this.levelsArr = memValidate.levelsArr; //normalized to full
1✔
116
    this.isLevelZeroSorted = memValidate.level0SortedFlag;
1✔
117

118
    if (memStructure == COMPACT_EMPTY) {
1✔
119
      minDoubleItem = Double.NaN;
1✔
120
      maxDoubleItem = Double.NaN;
1✔
121
      doubleItems = new double[k];
1✔
122
    }
123
    else if (memStructure == COMPACT_SINGLE) {
1✔
124
      final double item = srcMem.getDouble(DATA_START_ADR_SINGLE_ITEM);
1✔
125
      minDoubleItem = maxDoubleItem = item;
1✔
126
      doubleItems = new double[k];
1✔
127
      doubleItems[k - 1] = item;
1✔
128
    }
1✔
129
    else if (memStructure == COMPACT_FULL) {
1✔
130
      int offsetBytes = DATA_START_ADR;
1✔
131
      offsetBytes += (levelsArr.length - 1) * Integer.BYTES; //shortened levelsArr
1✔
132
      minDoubleItem = srcMem.getDouble(offsetBytes);
1✔
133
      offsetBytes += Double.BYTES;
1✔
134
      maxDoubleItem = srcMem.getDouble(offsetBytes);
1✔
135
      offsetBytes += Double.BYTES;
1✔
136
      final int capacityItems = levelsArr[getNumLevels()];
1✔
137
      final int freeSpace = levelsArr[0];
1✔
138
      final int retainedItems = capacityItems - freeSpace;
1✔
139
      doubleItems = new double[capacityItems];
1✔
140
      srcMem.getDoubleArray(offsetBytes, doubleItems, freeSpace, retainedItems);
1✔
141
    }
1✔
142
    else { //(memStructure == UPDATABLE)
143
      int offsetBytes = DATA_START_ADR;
1✔
144
      offsetBytes += levelsArr.length * Integer.BYTES; //full levelsArr
1✔
145
      minDoubleItem = srcMem.getDouble(offsetBytes);
1✔
146
      offsetBytes += Double.BYTES;
1✔
147
      maxDoubleItem = srcMem.getDouble(offsetBytes);
1✔
148
      offsetBytes += Double.BYTES;
1✔
149
      final int capacityItems = levelsArr[getNumLevels()];
1✔
150
      doubleItems = new double[capacityItems];
1✔
151
      srcMem.getDoubleArray(offsetBytes, doubleItems, 0, capacityItems);
1✔
152
    }
153
  }
1✔
154

155
  static KllHeapDoublesSketch heapifyImpl(final Memory srcMem) {
156
    Objects.requireNonNull(srcMem, "Parameter 'srcMem' must not be null");
1✔
157
    final KllMemoryValidate memVal = new KllMemoryValidate(srcMem, DOUBLES_SKETCH);
1✔
158
    return new KllHeapDoublesSketch(srcMem, memVal);
1✔
159
  }
160

161
  //End of constructors
162

163
  @Override
164
  String getItemAsString(final int index) {
165
    if (isEmpty()) { return "NaN"; }
1✔
166
    return Double.toString(doubleItems[index]);
1✔
167
  }
168

169
  @Override
170
  public int getK() { return k; }
1✔
171

172
  //MinMax Methods
173

174
  @Override
175
 double getMaxItemInternal() { return maxDoubleItem; }
1✔
176

177
  @Override
178
  public double getMaxItem() {
179
    if (isEmpty() || Double.isNaN(maxDoubleItem)) { throw new SketchesArgumentException(EMPTY_MSG); }
1✔
180
    return maxDoubleItem;
1✔
181
  }
182

183
  @Override
184
  String getMaxItemAsString() {
185
    return Double.toString(maxDoubleItem);
1✔
186
  }
187

188
  @Override
189
  double getMinItemInternal() { return minDoubleItem; }
1✔
190

191
  @Override
192
  public double getMinItem() {
193
    if (isEmpty() || Double.isNaN(minDoubleItem)) { throw new SketchesArgumentException(EMPTY_MSG); }
1✔
194
    return minDoubleItem;
1✔
195
  }
196

197
  @Override
198
  String getMinItemAsString() {
199
    return Double.toString(minDoubleItem);
1✔
200
  }
201

202
  @Override
203
  byte[] getMinMaxByteArr() {
204
    final byte[] bytesOut = new byte[2 * Double.BYTES];
1✔
205
    putDoubleLE(bytesOut, 0, minDoubleItem);
1✔
206
    putDoubleLE(bytesOut, Double.BYTES, maxDoubleItem);
1✔
207
    return bytesOut;
1✔
208
  }
209

210
  @Override
211
  void setMaxItem(final double item) { this.maxDoubleItem = item; }
1✔
212

213
  @Override
214
  void setMinItem(final double item) { this.minDoubleItem = item; }
1✔
215

216
  //END MinMax Methods
217

218
  @Override
219
  public long getN() { return n; }
1✔
220

221
  //other restricted
222

223
  @Override
224
  double[] getDoubleItemsArray() { return doubleItems; }
1✔
225

226
  @Override
227
  double getDoubleSingleItem() {
228
    if (n != 1L) { throw new SketchesArgumentException(NOT_SINGLE_ITEM_MSG); }
1✔
229
    return doubleItems[k - 1];
1✔
230
  }
231

232
  @Override
233
  int getM() { return m; }
1✔
234

235
  @Override
236
  MemoryRequestServer getMemoryRequestServer() { return null; }
×
237

238
  @Override
239
  int getMinK() { return minK; }
1✔
240

241
  @Override
242
  byte[] getRetainedItemsByteArr() {
243
    if (isEmpty()) { return new byte[0]; }
1✔
244
    final byte[] bytesOut;
245
    if (isSingleItem()) {
1✔
246
      bytesOut = new byte[Double.BYTES];
×
247
      putDoubleLE(bytesOut, 0, getDoubleSingleItem());
×
248
      return bytesOut;
×
249
    }
250
    final int retained = getNumRetained();
1✔
251
    final int bytes = retained * Double.BYTES;
1✔
252
    bytesOut = new byte[bytes];
1✔
253
    final WritableMemory wmem = WritableMemory.writableWrap(bytesOut);
1✔
254
    wmem.putDoubleArray(0, doubleItems, levelsArr[0], retained);
1✔
255
    return bytesOut;
1✔
256
  }
257

258
  @Override
259
  byte[] getTotalItemsByteArr() {
260
    final byte[] byteArr = new byte[doubleItems.length * Double.BYTES];
1✔
261
    final WritableMemory wmem = WritableMemory.writableWrap(byteArr);
1✔
262
    wmem.putDoubleArray(0, doubleItems, 0, doubleItems.length);
1✔
263
    return byteArr;
1✔
264
  }
265

266
  @Override
267
  WritableMemory getWritableMemory() {
268
    return null;
1✔
269
  }
270

271
  @Override
272
  void incN(final int increment) { n += increment; }
1✔
273

274
  @Override
275
  void incNumLevels() {
276
    //the heap sketch computes num levels from the array itself, so this is not used on-heap
277
  }
1✔
278

279
  @Override
280
  boolean isLevelZeroSorted() { return this.isLevelZeroSorted; }
1✔
281

282
  @Override
283
  void setDoubleItemsArray(final double[] doubleItems) { this.doubleItems = doubleItems; }
1✔
284

285
  @Override
286
  void setDoubleItemsArrayAt(final int index, final double item) { this.doubleItems[index] = item; }
1✔
287

288
  @Override
289
  void setDoubleItemsArrayAt(final int dstIndex, final double[] srcItems, final int srcOffset, final int length) {
290
    System.arraycopy(srcItems, srcOffset, doubleItems, dstIndex, length);
1✔
291
  }
1✔
292

293
  @Override
294
  void setLevelZeroSorted(final boolean sorted) { this.isLevelZeroSorted = sorted; }
1✔
295

296
  @Override
297
  void setMinK(final int minK) { this.minK = minK; }
1✔
298

299
  @Override
300
  void setN(final long n) { this.n = n; }
1✔
301

302
  @Override
303
  void setNumLevels(final int numLevels) {
304
    //the heap sketch computes num levels from the array itself, so this is not used on-heap
305
  }
1✔
306

307
  @Override
308
  double[] getDoubleRetainedItemsArray() {
309
    return Arrays.copyOfRange(doubleItems, levelsArr[0], levelsArr[getNumLevels()]);
1✔
310
  }
311

312
  @Override
313
  void setWritableMemory(final WritableMemory wmem) { }
×
314

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

© 2026 Coveralls, Inc