• 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/KllHeapFloatsSketch.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.putFloatLE;
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.FLOATS_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 floats 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 KllHeapFloatsSketch extends KllFloatsSketch {
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 float minFloatItem;
54
  private float maxFloatItem;
55
  private float[] floatItems;
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
  KllHeapFloatsSketch(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.minFloatItem = Float.NaN;
1✔
78
    this.maxFloatItem = Float.NaN;
1✔
79
    this.floatItems = new float[k];
1✔
80
  }
1✔
81

82
  /**
83
   * Used for creating a temporary sketch for use with weighted updates.
84
   */
85
  KllHeapFloatsSketch(final int k, final int m, final float 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.minFloatItem = item;
1✔
97
    this.maxFloatItem = item;
1✔
98
    this.floatItems = KllFloatsHelper.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 KllHeapFloatsSketch(
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
      minFloatItem = Float.NaN;
1✔
120
      maxFloatItem = Float.NaN;
1✔
121
      floatItems = new float[k];
1✔
122
    }
123
    else if (memStructure == COMPACT_SINGLE) {
1✔
124
      final float item = srcMem.getFloat(DATA_START_ADR_SINGLE_ITEM);
1✔
125
      minFloatItem = maxFloatItem = item;
1✔
126
      floatItems = new float[k];
1✔
127
      floatItems[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
      minFloatItem = srcMem.getFloat(offsetBytes);
1✔
133
      offsetBytes += Float.BYTES;
1✔
134
      maxFloatItem = srcMem.getFloat(offsetBytes);
1✔
135
      offsetBytes += Float.BYTES;
1✔
136
      final int capacityItems = levelsArr[getNumLevels()];
1✔
137
      final int freeSpace = levelsArr[0];
1✔
138
      final int retainedItems = capacityItems - freeSpace;
1✔
139
      floatItems = new float[capacityItems];
1✔
140
      srcMem.getFloatArray(offsetBytes, floatItems, 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
      minFloatItem = srcMem.getFloat(offsetBytes);
1✔
146
      offsetBytes += Float.BYTES;
1✔
147
      maxFloatItem = srcMem.getFloat(offsetBytes);
1✔
148
      offsetBytes += Float.BYTES;
1✔
149
      final int capacityItems = levelsArr[getNumLevels()];
1✔
150
      floatItems = new float[capacityItems];
1✔
151
      srcMem.getFloatArray(offsetBytes, floatItems, 0, capacityItems);
1✔
152
    }
153
  }
1✔
154

155
  static KllHeapFloatsSketch heapifyImpl(final Memory srcMem) {
156
    Objects.requireNonNull(srcMem, "Parameter 'srcMem' must not be null");
1✔
157
    final KllMemoryValidate memVal = new KllMemoryValidate(srcMem, FLOATS_SKETCH);
1✔
158
    return new KllHeapFloatsSketch(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 Float.toString(floatItems[index]);
1✔
167
  }
168

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

172
  //MinMax Methods
173

174
  @Override
175
 float getMaxItemInternal() { return maxFloatItem; }
1✔
176

177
  @Override
178
  public float getMaxItem() {
179
    if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); }
1✔
180
    return maxFloatItem;
1✔
181
  }
182

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

188
  @Override
189
  float getMinItemInternal() { return minFloatItem; }
1✔
190

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

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

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

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

213
  @Override
214
  void setMinItem(final float item) { this.minFloatItem = 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
  float[] getFloatItemsArray() { return floatItems; }
1✔
225

226
  @Override
227
  float getFloatSingleItem() {
228
    if (n != 1L) { throw new SketchesArgumentException(NOT_SINGLE_ITEM_MSG); }
1✔
229
    return floatItems[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[Float.BYTES];
×
247
      putFloatLE(bytesOut, 0, getFloatSingleItem());
×
248
      return bytesOut;
×
249
    }
250
    final int retained = getNumRetained();
1✔
251
    final int bytes = retained * Float.BYTES;
1✔
252
    bytesOut = new byte[bytes];
1✔
253
    final WritableMemory wmem = WritableMemory.writableWrap(bytesOut);
1✔
254
    wmem.putFloatArray(0, floatItems, levelsArr[0], retained);
1✔
255
    return bytesOut;
1✔
256
  }
257

258
  @Override
259
  byte[] getTotalItemsByteArr() {
260
    final byte[] byteArr = new byte[floatItems.length * Float.BYTES];
1✔
261
    final WritableMemory wmem = WritableMemory.writableWrap(byteArr);
1✔
262
    wmem.putFloatArray(0, floatItems, 0, floatItems.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 setFloatItemsArray(final float[] floatItems) { this.floatItems = floatItems; }
1✔
284

285
  @Override
286
  void setFloatItemsArrayAt(final int index, final float item) { this.floatItems[index] = item; }
1✔
287

288
  @Override
289
  void setFloatItemsArrayAt(final int dstIndex, final float[] srcItems, final int srcOffset, final int length) {
290
    System.arraycopy(srcItems, srcOffset, floatItems, 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
  float[] getFloatRetainedItemsArray() {
309
    return Arrays.copyOfRange(floatItems, 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