• 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.61
/src/main/java/org/apache/datasketches/quantiles/DirectUpdateDoublesSketchR.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.quantiles;
21

22
import static org.apache.datasketches.quantiles.PreambleUtil.COMBINED_BUFFER;
23
import static org.apache.datasketches.quantiles.PreambleUtil.COMPACT_FLAG_MASK;
24
import static org.apache.datasketches.quantiles.PreambleUtil.EMPTY_FLAG_MASK;
25
import static org.apache.datasketches.quantiles.PreambleUtil.MAX_DOUBLE;
26
import static org.apache.datasketches.quantiles.PreambleUtil.MIN_DOUBLE;
27
import static org.apache.datasketches.quantiles.PreambleUtil.N_LONG;
28
import static org.apache.datasketches.quantiles.PreambleUtil.ORDERED_FLAG_MASK;
29
import static org.apache.datasketches.quantiles.PreambleUtil.READ_ONLY_FLAG_MASK;
30
import static org.apache.datasketches.quantiles.PreambleUtil.extractFamilyID;
31
import static org.apache.datasketches.quantiles.PreambleUtil.extractFlags;
32
import static org.apache.datasketches.quantiles.PreambleUtil.extractK;
33
import static org.apache.datasketches.quantiles.PreambleUtil.extractN;
34
import static org.apache.datasketches.quantiles.PreambleUtil.extractPreLongs;
35
import static org.apache.datasketches.quantiles.PreambleUtil.extractSerVer;
36

37
import org.apache.datasketches.common.SketchesArgumentException;
38
import org.apache.datasketches.common.SketchesReadOnlyException;
39
import org.apache.datasketches.memory.Memory;
40
import org.apache.datasketches.memory.WritableMemory;
41
import org.apache.datasketches.quantilescommon.QuantilesAPI;
42

43
/**
44
 * Implements the DoublesSketch off-heap.
45
 *
46
 * @author Kevin Lang
47
 * @author Lee Rhodes
48
 *
49
 */
50
class DirectUpdateDoublesSketchR extends UpdateDoublesSketch {
51
  static final int MIN_DIRECT_DOUBLES_SER_VER = 3;
52
  WritableMemory mem_;
53

54
  //**CONSTRUCTORS**********************************************************
55
  DirectUpdateDoublesSketchR(final int k) {
56
    super(k); //Checks k
1✔
57
  }
1✔
58

59
  /**
60
   * Wrap this sketch around the given non-compact Memory image of a DoublesSketch.
61
   *
62
   * @param srcMem the given non-compact Memory image of a DoublesSketch that may have data
63
   * @return a sketch that wraps the given srcMem
64
   */
65
  static DirectUpdateDoublesSketchR wrapInstance(final Memory srcMem) {
66
    final long memCap = srcMem.getCapacity();
1✔
67

68
    final int preLongs = extractPreLongs(srcMem);
1✔
69
    final int serVer = extractSerVer(srcMem);
1✔
70
    final int familyID = extractFamilyID(srcMem);
1✔
71
    final int flags = extractFlags(srcMem);
1✔
72
    final int k = extractK(srcMem);
1✔
73

74
    final boolean empty = (flags & EMPTY_FLAG_MASK) > 0; //Preamble flags empty state
1✔
75
    final long n = empty ? 0 : extractN(srcMem);
1✔
76

77
    //VALIDITY CHECKS
78
    checkPreLongs(preLongs);
1✔
79
    ClassicUtil.checkFamilyID(familyID);
1✔
80
    DoublesUtil.checkDoublesSerVer(serVer, MIN_DIRECT_DOUBLES_SER_VER);
1✔
81
    checkDirectFlags(flags); //Cannot be compact
1✔
82
    ClassicUtil.checkK(k);
1✔
83
    checkCompact(serVer, flags);
1✔
84
    checkDirectMemCapacity(k, n, memCap);
1✔
85
    checkEmptyAndN(empty, n);
1✔
86

87
    final DirectUpdateDoublesSketchR dds = new DirectUpdateDoublesSketchR(k);
1✔
88
    dds.mem_ = (WritableMemory) srcMem;
1✔
89
    return dds;
1✔
90
  }
91

92
  @Override
93
  public double getMaxItem() {
94
    if (isEmpty()) { throw new IllegalArgumentException(QuantilesAPI.EMPTY_MSG); }
1✔
95
    return mem_.getDouble(MAX_DOUBLE);
1✔
96
  }
97

98
  @Override
99
  public double getMinItem() {
100
    if (isEmpty()) { throw new IllegalArgumentException(QuantilesAPI.EMPTY_MSG); }
1✔
101
    return mem_.getDouble(MIN_DOUBLE);
1✔
102
  }
103

104
  @Override
105
  public long getN() {
106
    return (mem_.getCapacity() < COMBINED_BUFFER) ? 0 : mem_.getLong(N_LONG);
1✔
107
  }
108

109
  @Override
110
  public boolean hasMemory() {
111
    return (mem_ != null);
1✔
112
  }
113

114
  @Override
115
  public boolean isDirect() {
116
    return (mem_ != null) ? mem_.isDirect() : false;
1✔
117
  }
118

119
  @Override
120
  public boolean isReadOnly() {
121
    return true;
×
122
  }
123

124
  @Override
125
  public boolean isSameResource(final Memory that) {
126
    return mem_.isSameResource(that);
1✔
127
  }
128

129
  @Override
130
  public void reset() {
131
    throw new SketchesReadOnlyException("Call to reset() on read-only buffer");
1✔
132
  }
133

134
  @Override
135
  public void update(final double dataItem) {
136
    throw new SketchesReadOnlyException("Call to update() on read-only buffer");
1✔
137
  }
138

139
  //Restricted overrides
140
  //Gets
141

142
  @Override
143
  int getBaseBufferCount() {
144
    return ClassicUtil.computeBaseBufferItems(getK(), getN());
1✔
145
  }
146

147
  @Override
148
  int getCombinedBufferItemCapacity() {
149
    return ((int)mem_.getCapacity() - COMBINED_BUFFER) / 8;
1✔
150
  }
151

152
  @Override
153
  double[] getCombinedBuffer() {
154
    final int k = getK();
1✔
155
    if (isEmpty()) { return new double[k << 1]; } //2K
1✔
156
    final long n = getN();
1✔
157
    final int itemCap = ClassicUtil.computeCombinedBufferItemCapacity(k, n);
1✔
158
    final double[] combinedBuffer = new double[itemCap];
1✔
159
    mem_.getDoubleArray(COMBINED_BUFFER, combinedBuffer, 0, itemCap);
1✔
160
    return combinedBuffer;
1✔
161
  }
162

163
  @Override
164
  long getBitPattern() {
165
    final int k = getK();
1✔
166
    final long n = getN();
1✔
167
    return ClassicUtil.computeBitPattern(k, n);
1✔
168
  }
169

170
  @Override
171
  WritableMemory getMemory() {
172
    return mem_;
1✔
173
  }
174

175
  //Puts
176

177
  @Override
178
  void putMinItem(final double minQuantile) {
179
    throw new SketchesReadOnlyException("Call to putMinQuantile() on read-only buffer");
1✔
180
  }
181

182
  @Override
183
  void putMaxItem(final double maxQuantile) {
184
    throw new SketchesReadOnlyException("Call to putMaxQuantile() on read-only buffer");
1✔
185
  }
186

187
  @Override
188
  void putN(final long n) {
189
    throw new SketchesReadOnlyException("Call to putN() on read-only buffer");
1✔
190
  }
191

192
  @Override
193
  void putCombinedBuffer(final double[] combinedBuffer) {
194
    throw new SketchesReadOnlyException("Call to putCombinedBuffer() on read-only buffer");
1✔
195
  }
196

197
  @Override
198
  void putBaseBufferCount(final int baseBufferCount) {
199
    throw new SketchesReadOnlyException("Call to putBaseBufferCount() on read-only buffer");
1✔
200
  }
201

202
  @Override
203
  void putBitPattern(final long bitPattern) {
204
    throw new SketchesReadOnlyException("Call to putBaseBufferCount() on read-only buffer");
1✔
205
  }
206

207
  @Override
208
  double[] growCombinedBuffer(final int curCombBufItemCap, final int itemSpaceNeeded) {
209
    throw new SketchesReadOnlyException("Call to growCombinedBuffer() on read-only buffer");
1✔
210
  }
211

212
  //Checks
213

214
  /**
215
   * Checks the validity of the direct memory capacity assuming n, k.
216
   * @param k the given k
217
   * @param n the given n
218
   * @param memCapBytes the current memory capacity in bytes
219
   */
220
  static void checkDirectMemCapacity(final int k, final long n, final long memCapBytes) {
221
    final int reqBufBytes = getUpdatableStorageBytes(k, n);
1✔
222

223
    if (memCapBytes < reqBufBytes) {
1✔
224
      throw new SketchesArgumentException("Possible corruption: Memory capacity too small: "
1✔
225
          + memCapBytes + " < " + reqBufBytes);
226
    }
227
  }
1✔
228

229
  static void checkCompact(final int serVer, final int flags) {
230
    final boolean compact = (serVer == 2) | ((flags & COMPACT_FLAG_MASK) > 0);
1✔
231
    if (compact) {
1✔
232
      throw new SketchesArgumentException("Compact Memory is not supported for Wrap Instance.");
1✔
233
    }
234
  }
1✔
235

236
  static void checkPreLongs(final int preLongs) {
237
    if ((preLongs < 1) || (preLongs > 2)) {
1✔
238
      throw new SketchesArgumentException(
1✔
239
          "Possible corruption: PreLongs must be 1 or 2: " + preLongs);
240
    }
241
  }
1✔
242

243
  static void checkDirectFlags(final int flags) {
244
    final int allowedFlags = //Cannot be compact!
1✔
245
        READ_ONLY_FLAG_MASK | EMPTY_FLAG_MASK | ORDERED_FLAG_MASK;
246
    final int flagsMask = ~allowedFlags;
1✔
247
    if ((flags & flagsMask) > 0) {
1✔
248
      throw new SketchesArgumentException(
1✔
249
         "Possible corruption: Invalid flags field: Cannot be compact! "
250
             + Integer.toBinaryString(flags));
1✔
251
    }
252
  }
1✔
253

254
  static void checkEmptyAndN(final boolean empty, final long n) {
255
    if (empty && (n > 0)) {
1✔
256
      throw new SketchesArgumentException(
1✔
257
          "Possible corruption: Empty Flag = true and N > 0: " + n);
258
    }
259
  }
1✔
260
}
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