• 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.7
/src/main/java/org/apache/datasketches/kll/KllDirectDoublesSketch.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.copyBytes;
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.KllPreambleUtil.getMemoryK;
26
import static org.apache.datasketches.kll.KllPreambleUtil.getMemoryLevelZeroSortedFlag;
27
import static org.apache.datasketches.kll.KllPreambleUtil.getMemoryM;
28
import static org.apache.datasketches.kll.KllPreambleUtil.getMemoryMinK;
29
import static org.apache.datasketches.kll.KllPreambleUtil.getMemoryN;
30
import static org.apache.datasketches.kll.KllPreambleUtil.getMemoryNumLevels;
31
import static org.apache.datasketches.kll.KllPreambleUtil.setMemoryFamilyID;
32
import static org.apache.datasketches.kll.KllPreambleUtil.setMemoryK;
33
import static org.apache.datasketches.kll.KllPreambleUtil.setMemoryLevelZeroSortedFlag;
34
import static org.apache.datasketches.kll.KllPreambleUtil.setMemoryM;
35
import static org.apache.datasketches.kll.KllPreambleUtil.setMemoryMinK;
36
import static org.apache.datasketches.kll.KllPreambleUtil.setMemoryN;
37
import static org.apache.datasketches.kll.KllPreambleUtil.setMemoryNumLevels;
38
import static org.apache.datasketches.kll.KllPreambleUtil.setMemoryPreInts;
39
import static org.apache.datasketches.kll.KllPreambleUtil.setMemorySerVer;
40
import static org.apache.datasketches.kll.KllSketch.SketchStructure.COMPACT_EMPTY;
41
import static org.apache.datasketches.kll.KllSketch.SketchStructure.COMPACT_FULL;
42
import static org.apache.datasketches.kll.KllSketch.SketchStructure.COMPACT_SINGLE;
43
import static org.apache.datasketches.kll.KllSketch.SketchStructure.UPDATABLE;
44
import static org.apache.datasketches.kll.KllSketch.SketchType.DOUBLES_SKETCH;
45

46
import org.apache.datasketches.common.ByteArrayUtil;
47
import org.apache.datasketches.common.Family;
48
import org.apache.datasketches.common.SketchesArgumentException;
49
import org.apache.datasketches.memory.Memory;
50
import org.apache.datasketches.memory.MemoryRequestServer;
51
import org.apache.datasketches.memory.WritableMemory;
52

53
/**
54
 * This class implements an off-heap, updatable KllDoublesSketch using WritableMemory.
55
 *
56
 * <p>Please refer to the documentation in the package-info:<br>
57
 * {@link org.apache.datasketches.kll}</p>
58
 *
59
 * @author Lee Rhodes, Kevin Lang
60
 */
61
class KllDirectDoublesSketch extends KllDoublesSketch {
62
  private WritableMemory wmem;
63
  private MemoryRequestServer memReqSvr;
64

65
  /**
66
   * Constructs from Memory or WritableMemory already initialized with a sketch image and validated.
67
   * @param wmem the current WritableMemory
68
   * @param memReqSvr the given MemoryRequestServer to request a larger WritableMemory
69
   * @param memVal the MemoryValadate object
70
   */
71
  KllDirectDoublesSketch(
72
      final SketchStructure sketchStructure,
73
      final WritableMemory wmem,
74
      final MemoryRequestServer memReqSvr,
75
      final KllMemoryValidate memVal) {
76
    super(sketchStructure);
1✔
77
    this.wmem = wmem;
1✔
78
    this.memReqSvr = memReqSvr;
1✔
79
    readOnly = (wmem != null && wmem.isReadOnly()) || sketchStructure != UPDATABLE;
1✔
80
    levelsArr = memVal.levelsArr; //always converted to writable form.
1✔
81
  }
1✔
82

83
  /**
84
   * Create a new updatable, direct instance of this sketch.
85
   * @param k parameter that controls size of the sketch and accuracy of estimates
86
   * @param m parameter that controls the minimum level width in items.
87
   * @param dstMem the given destination WritableMemory object for use by the sketch
88
   * @param memReqSvr the given MemoryRequestServer to request a larger WritableMemory
89
   * @return a new instance of this sketch
90
   */
91
  static KllDirectDoublesSketch newDirectUpdatableInstance(
92
      final int k,
93
      final int m,
94
      final WritableMemory dstMem,
95
      final MemoryRequestServer memReqSvr) {
96
    setMemoryPreInts(dstMem, UPDATABLE.getPreInts());
1✔
97
    setMemorySerVer(dstMem, UPDATABLE.getSerVer());
1✔
98
    setMemoryFamilyID(dstMem, Family.KLL.getID());
1✔
99
    setMemoryK(dstMem, k);
1✔
100
    setMemoryM(dstMem, m);
1✔
101
    setMemoryN(dstMem, 0);
1✔
102
    setMemoryMinK(dstMem, k);
1✔
103
    setMemoryNumLevels(dstMem, 1);
1✔
104
    int offset = DATA_START_ADR;
1✔
105
    //new Levels array
106
    dstMem.putIntArray(offset, new int[] {k, k}, 0, 2);
1✔
107
    offset += 2 * Integer.BYTES;
1✔
108
    //new min/max array
109
    dstMem.putDoubleArray(offset, new double[] {Double.NaN, Double.NaN}, 0, 2);
1✔
110
    offset += 2 * ITEM_BYTES;
1✔
111
    //new empty items array
112
    dstMem.putDoubleArray(offset, new double[k], 0, k);
1✔
113

114
    final KllMemoryValidate memVal = new KllMemoryValidate(dstMem, DOUBLES_SKETCH, null);
1✔
115
    final WritableMemory wMem = dstMem;
1✔
116
    return new KllDirectDoublesSketch(UPDATABLE, wMem, memReqSvr, memVal);
1✔
117
  }
118

119
  //End of constructors
120

121
  @Override
122
  String getItemAsString(final int index) {
123
    if (isEmpty()) { return "NaN"; }
1✔
124
    return Double.toString(getDoubleItemsArray()[index]);
1✔
125
  }
126

127
  @Override
128
  public int getK() {
129
    return getMemoryK(wmem);
1✔
130
  }
131

132
  //MinMax Methods
133

134
  @Override
135
  public double getMaxItem() {
136
    if (sketchStructure == COMPACT_EMPTY || isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); }
1✔
137
    if (sketchStructure == COMPACT_SINGLE) { return getDoubleSingleItem(); }
1✔
138
    //either compact-full or updatable
139
    final int offset = DATA_START_ADR + getLevelsArrSizeBytes(sketchStructure) + ITEM_BYTES;
1✔
140
    return wmem.getDouble(offset);
1✔
141
  }
142

143
  @Override
144
  double getMaxItemInternal() {
145
    if (sketchStructure == COMPACT_EMPTY || isEmpty()) { return Double.NaN; }
1✔
146
    if (sketchStructure == COMPACT_SINGLE) { return getDoubleSingleItem(); }
1✔
147
    //either compact-full or updatable
148
    final int offset = DATA_START_ADR + getLevelsArrSizeBytes(sketchStructure) + ITEM_BYTES;
1✔
149
    return wmem.getDouble(offset);
1✔
150
  }
151

152
  @Override
153
  String getMaxItemAsString() {
154
    final double maxItem = getMaxItemInternal();
1✔
155
    return Double.toString(maxItem);
1✔
156
  }
157

158
  @Override
159
  public double getMinItem() {
160
    if (sketchStructure == COMPACT_EMPTY || isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); }
1✔
161
    if (sketchStructure == COMPACT_SINGLE) { return getDoubleSingleItem(); }
1✔
162
    //either compact-full or updatable
163
    final int offset = DATA_START_ADR + getLevelsArrSizeBytes(sketchStructure);
1✔
164
    return wmem.getDouble(offset);
1✔
165
  }
166

167
  @Override
168
  double getMinItemInternal() {
169
    if (sketchStructure == COMPACT_EMPTY || isEmpty()) { return Double.NaN; }
1✔
170
    if (sketchStructure == COMPACT_SINGLE) { return getDoubleSingleItem(); }
1✔
171
    //either compact-full or updatable
172
    final int offset = DATA_START_ADR + getLevelsArrSizeBytes(sketchStructure);
1✔
173
    return wmem.getDouble(offset);
1✔
174
  }
175

176
  @Override
177
  String getMinItemAsString() {
178
    final double minItem = getMinItemInternal();
1✔
179
    return Double.toString(minItem);
1✔
180
  }
181

182
  @Override
183
  void setMaxItem(final double item) {
184
    if (readOnly) { throw new SketchesArgumentException(TGT_IS_READ_ONLY_MSG); }
1✔
185
    final int offset = DATA_START_ADR + getLevelsArrSizeBytes(sketchStructure) + ITEM_BYTES;
1✔
186
    wmem.putDouble(offset, item);
1✔
187
  }
1✔
188

189
  @Override
190
  void setMinItem(final double item) {
191
    if (readOnly) { throw new SketchesArgumentException(TGT_IS_READ_ONLY_MSG); }
1✔
192
    final int offset = DATA_START_ADR + getLevelsArrSizeBytes(sketchStructure);
1✔
193
    wmem.putDouble(offset, item);
1✔
194
  }
1✔
195

196
  //END MinMax Methods
197

198
  @Override
199
  public long getN() {
200
    if (sketchStructure == COMPACT_EMPTY) { return 0; }
1✔
201
    else if (sketchStructure == COMPACT_SINGLE) { return 1; }
1✔
202
    else { return getMemoryN(wmem); }
1✔
203
  }
204

205
  //other restricted
206

207
  @Override //returns updatable, expanded array including free space at bottom
208
  double[] getDoubleItemsArray() {
209
    final int k = getK();
1✔
210
    if (sketchStructure == COMPACT_EMPTY) { return new double[k]; }
1✔
211
    if (sketchStructure == COMPACT_SINGLE) {
1✔
212
      final double[] itemsArr = new double[k];
1✔
213
      itemsArr[k - 1] = getDoubleSingleItem();
1✔
214
      return itemsArr;
1✔
215
    }
216
    final int capacityItems = KllHelper.computeTotalItemCapacity(k, getM(), getNumLevels());
1✔
217
    final double[] doubleItemsArr = new double[capacityItems];
1✔
218
    final int offset = DATA_START_ADR + getLevelsArrSizeBytes(sketchStructure) + 2 * ITEM_BYTES;
1✔
219
    final int shift = (sketchStructure == COMPACT_FULL) ? levelsArr[0] : 0;
1✔
220
    final int numItems = (sketchStructure == COMPACT_FULL) ? getNumRetained() : capacityItems;
1✔
221
    wmem.getDoubleArray(offset, doubleItemsArr, shift, numItems);
1✔
222
    return doubleItemsArr;
1✔
223
  }
224

225
  @Override //returns compact items array of retained items, no free space.
226
  double[] getDoubleRetainedItemsArray() {
227
    if (sketchStructure == COMPACT_EMPTY) { return new double[0]; }
1✔
228
    if (sketchStructure == COMPACT_SINGLE) { return new double[] { getDoubleSingleItem() }; }
1✔
229
    final int numRetained = getNumRetained();
1✔
230
    final double[] doubleItemsArr = new double[numRetained];
1✔
231
    final int offset = DATA_START_ADR + getLevelsArrSizeBytes(sketchStructure) + 2 * ITEM_BYTES
1✔
232
        + (sketchStructure == COMPACT_FULL ? 0 : levelsArr[0] * ITEM_BYTES);
233
    wmem.getDoubleArray(offset, doubleItemsArr, 0, numRetained);
1✔
234
    return doubleItemsArr;
1✔
235
  }
236

237
  @Override
238
  double getDoubleSingleItem() {
239
    if (!isSingleItem()) { throw new SketchesArgumentException(NOT_SINGLE_ITEM_MSG); }
1✔
240
    if (sketchStructure == COMPACT_SINGLE) {
1✔
241
      return wmem.getDouble(DATA_START_ADR_SINGLE_ITEM);
1✔
242
    }
243
    final int offset;
244
    if (sketchStructure == COMPACT_FULL) {
1✔
245
      offset = DATA_START_ADR + getLevelsArrSizeBytes(sketchStructure) + 2 * ITEM_BYTES;
×
246
    } else { //sketchStructure == UPDATABLE
247
      offset = DATA_START_ADR + getLevelsArrSizeBytes(sketchStructure) + (2 + getK() - 1) * ITEM_BYTES;
1✔
248
    }
249
    return wmem.getDouble(offset);
1✔
250
  }
251

252
  @Override
253
  int getM() {
254
    return getMemoryM(wmem);
1✔
255
  }
256

257
  @Override
258
  MemoryRequestServer getMemoryRequestServer() { return memReqSvr; }
1✔
259

260
  @Override
261
  int getMinK() {
262
    if (sketchStructure == COMPACT_FULL || sketchStructure == UPDATABLE) { return getMemoryMinK(wmem); }
1✔
263
    return getK();
1✔
264
  }
265

266
  @Override
267
  byte[] getMinMaxByteArr() {
268
    final byte[] bytesOut = new byte[2 * ITEM_BYTES];
1✔
269
    if (sketchStructure == COMPACT_EMPTY) {
1✔
270
      ByteArrayUtil.putDoubleLE(bytesOut, 0, Double.NaN);
×
271
      ByteArrayUtil.putDoubleLE(bytesOut, ITEM_BYTES, Double.NaN);
×
272
      return bytesOut;
×
273
    }
274
    final int offset;
275
    if (sketchStructure == COMPACT_SINGLE) {
1✔
276
      offset = DATA_START_ADR_SINGLE_ITEM;
×
277
      wmem.getByteArray(offset, bytesOut, 0, ITEM_BYTES);
×
278
      copyBytes(bytesOut, 0, bytesOut, ITEM_BYTES, ITEM_BYTES);
×
279
      return bytesOut;
×
280
    }
281
    //sketchStructure == UPDATABLE OR COMPACT_FULL
282
    offset = DATA_START_ADR + getLevelsArrSizeBytes(sketchStructure);
1✔
283
    wmem.getByteArray(offset, bytesOut, 0, ITEM_BYTES);
1✔
284
    wmem.getByteArray(offset + ITEM_BYTES, bytesOut, ITEM_BYTES, ITEM_BYTES);
1✔
285
    return bytesOut;
1✔
286
  }
287

288
  @Override
289
  byte[] getRetainedItemsByteArr() {
290
    if (sketchStructure == COMPACT_EMPTY) { return new byte[0]; }
1✔
291
    final double[] dblArr = getDoubleRetainedItemsArray();
1✔
292
    final byte[] dblByteArr = new byte[dblArr.length * ITEM_BYTES];
1✔
293
    final WritableMemory wmem2 = WritableMemory.writableWrap(dblByteArr);
1✔
294
    wmem2.putDoubleArray(0, dblArr, 0, dblArr.length);
1✔
295
    return dblByteArr;
1✔
296
  }
297

298
  @Override
299
  byte[] getTotalItemsByteArr() {
300
    final double[] dblArr = getDoubleItemsArray();
1✔
301
    final byte[] dblByteArr = new byte[dblArr.length * ITEM_BYTES];
1✔
302
    final WritableMemory wmem2 = WritableMemory.writableWrap(dblByteArr);
1✔
303
    wmem2.putDoubleArray(0, dblArr, 0, dblArr.length);
1✔
304
    return dblByteArr;
1✔
305
  }
306

307
  @Override
308
  WritableMemory getWritableMemory() {
309
    return wmem;
1✔
310
  }
311

312
  @Override
313
  void incN(final int increment) {
314
    if (readOnly) { throw new SketchesArgumentException(TGT_IS_READ_ONLY_MSG); }
1✔
315
    setMemoryN(wmem, getMemoryN(wmem) + increment);
1✔
316
  }
1✔
317

318
  @Override
319
  void incNumLevels() {
320
    if (readOnly) { throw new SketchesArgumentException(TGT_IS_READ_ONLY_MSG); }
1✔
321
    int numLevels = getMemoryNumLevels(wmem);
1✔
322
    setMemoryNumLevels(wmem, ++numLevels);
1✔
323
  }
1✔
324

325
  @Override
326
  boolean isLevelZeroSorted() {
327
    return getMemoryLevelZeroSortedFlag(wmem);
1✔
328
  }
329

330
  @Override
331
  void setDoubleItemsArray(final double[] doubleItems) {
332
    if (readOnly) { throw new SketchesArgumentException(TGT_IS_READ_ONLY_MSG); }
1✔
333
    final int offset = DATA_START_ADR + getLevelsArrSizeBytes(sketchStructure) + 2 * ITEM_BYTES;
1✔
334
    wmem.putDoubleArray(offset, doubleItems, 0, doubleItems.length);
1✔
335
  }
1✔
336

337
  @Override
338
  void setDoubleItemsArrayAt(final int index, final double item) {
339
    if (readOnly) { throw new SketchesArgumentException(TGT_IS_READ_ONLY_MSG); }
1✔
340
    final int offset =
1✔
341
        DATA_START_ADR + getLevelsArrSizeBytes(sketchStructure) + (index + 2) * ITEM_BYTES;
1✔
342
    wmem.putDouble(offset, item);
1✔
343
  }
1✔
344

345
  @Override
346
  void setDoubleItemsArrayAt(final int index, final double[] items, final int srcOffset, final int length) {
347
    if (readOnly) { throw new SketchesArgumentException(TGT_IS_READ_ONLY_MSG); }
1✔
348
    final int offset = DATA_START_ADR + getLevelsArrSizeBytes(sketchStructure) + (index + 2) * ITEM_BYTES;
1✔
349
    wmem.putDoubleArray(offset, items, srcOffset, length);
1✔
350
  }
1✔
351

352
  @Override
353
  void setLevelZeroSorted(final boolean sorted) {
354
    if (readOnly) { throw new SketchesArgumentException(TGT_IS_READ_ONLY_MSG); }
1✔
355
    setMemoryLevelZeroSortedFlag(wmem, sorted);
1✔
356
  }
1✔
357

358
  @Override
359
  void setMinK(final int minK) {
360
    if (readOnly) { throw new SketchesArgumentException(TGT_IS_READ_ONLY_MSG); }
1✔
361
    setMemoryMinK(wmem, minK);
1✔
362
  }
1✔
363

364
  @Override
365
  void setN(final long n) {
366
    if (readOnly) { throw new SketchesArgumentException(TGT_IS_READ_ONLY_MSG); }
1✔
367
    setMemoryN(wmem, n);
1✔
368
  }
1✔
369

370
  @Override
371
  void setNumLevels(final int numLevels) {
372
    if (readOnly) { throw new SketchesArgumentException(TGT_IS_READ_ONLY_MSG); }
1✔
373
    setMemoryNumLevels(wmem, numLevels);
1✔
374
  }
1✔
375

376
  @Override
377
  void setWritableMemory(final WritableMemory wmem) {
378
    this.wmem = wmem;
1✔
379
  }
1✔
380

381
  final static class KllDirectCompactDoublesSketch extends KllDirectDoublesSketch {
382

383
    KllDirectCompactDoublesSketch(
384
        final SketchStructure sketchStructure,
385
        final Memory srcMem,
386
        final KllMemoryValidate memVal) {
387
      super(sketchStructure, (WritableMemory) srcMem, null, memVal);
1✔
388
    }
1✔
389
  }
390

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