Skip to content

Commit 8841d5a

Browse files
committed
This fixes the "getResultAndReset" issue flagged by Copilot.
1 parent da3123d commit 8841d5a

12 files changed

Lines changed: 142 additions & 123 deletions

src/main/java/org/apache/datasketches/quantiles/DirectUpdateDoublesSketch.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ static DirectUpdateDoublesSketch newInstance(final int k, final MemorySegment ds
100100
return new DirectUpdateDoublesSketch(k, dstSeg, mSegReq);
101101
}
102102

103-
static DirectUpdateDoublesSketch(final DirectUpdateDoublesSketch skIn) {
104-
return null;
103+
static HeapUpdateDoublesSketch heapify(final DirectUpdateDoublesSketch skIn) {
104+
final MemorySegment segIn = skIn.getMemorySegment();
105+
return HeapUpdateDoublesSketch.heapifyInstance(segIn);
105106
}
106107

107108
/**
@@ -214,6 +215,23 @@ public void reset() {
214215
}
215216

216217
//Restricted overrides
218+
219+
@Override
220+
HeapUpdateDoublesSketch getSketchAndReset() {
221+
final HeapUpdateDoublesSketch skCopy = heapify(this);
222+
reset();
223+
return skCopy;
224+
}
225+
226+
@Override
227+
double[] growCombinedBuffer(final int curCombBufItemCap, final int itemSpaceNeeded) {
228+
seg_ = growCombinedSegBuffer(itemSpaceNeeded);
229+
// copy out any data that was there
230+
final double[] newCombBuf = new double[itemSpaceNeeded];
231+
MemorySegment.copy(seg_, JAVA_DOUBLE_UNALIGNED, COMBINED_BUFFER, newCombBuf, 0, curCombBufItemCap);
232+
return newCombBuf;
233+
}
234+
217235
//Puts
218236

219237
@Override
@@ -249,15 +267,6 @@ void putBitPattern(final long bitPattern) {
249267
//intentionally a no-op, not kept on-heap, always derived.
250268
}
251269

252-
@Override
253-
double[] growCombinedBuffer(final int curCombBufItemCap, final int itemSpaceNeeded) {
254-
seg_ = growCombinedSegBuffer(itemSpaceNeeded);
255-
// copy out any data that was there
256-
final double[] newCombBuf = new double[itemSpaceNeeded];
257-
MemorySegment.copy(seg_, JAVA_DOUBLE_UNALIGNED, COMBINED_BUFFER, newCombBuf, 0, curCombBufItemCap);
258-
return newCombBuf;
259-
}
260-
261270
//Direct supporting methods
262271

263272
private MemorySegment growCombinedSegBuffer(final int itemSpaceNeeded) {

src/main/java/org/apache/datasketches/quantiles/DirectUpdateDoublesSketchR.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ int getCombinedBufferItemCapacity() {
160160
final double[] combinedBuffer = new double[itemCap];
161161
MemorySegment.copy(seg_, JAVA_DOUBLE_UNALIGNED, COMBINED_BUFFER, combinedBuffer, 0, itemCap);
162162

163-
164163
return combinedBuffer;
165164
}
166165

@@ -176,41 +175,46 @@ MemorySegment getMemorySegment() {
176175
return seg_;
177176
}
178177

178+
@Override
179+
UpdateDoublesSketch getSketchAndReset() {
180+
throw new SketchesReadOnlyException("Call to getResultAndReset() on read-only sketch");
181+
}
182+
179183
//Puts
180184

181185
@Override
182186
void putMinItem(final double minQuantile) {
183-
throw new SketchesReadOnlyException("Call to putMinQuantile() on read-only buffer");
187+
throw new SketchesReadOnlyException("Call to putMinQuantile() on read-only sketch");
184188
}
185189

186190
@Override
187191
void putMaxItem(final double maxQuantile) {
188-
throw new SketchesReadOnlyException("Call to putMaxQuantile() on read-only buffer");
192+
throw new SketchesReadOnlyException("Call to putMaxQuantile() on read-only sketch");
189193
}
190194

191195
@Override
192196
void putN(final long n) {
193-
throw new SketchesReadOnlyException("Call to putN() on read-only buffer");
197+
throw new SketchesReadOnlyException("Call to putN() on read-only sketch");
194198
}
195199

196200
@Override
197201
void putCombinedBuffer(final double[] combinedBuffer) {
198-
throw new SketchesReadOnlyException("Call to putCombinedBuffer() on read-only buffer");
202+
throw new SketchesReadOnlyException("Call to putCombinedBuffer() on read-only sketch");
199203
}
200204

201205
@Override
202206
void putBaseBufferCount(final int baseBufferCount) {
203-
throw new SketchesReadOnlyException("Call to putBaseBufferCount() on read-only buffer");
207+
throw new SketchesReadOnlyException("Call to putBaseBufferCount() on read-only sketch");
204208
}
205209

206210
@Override
207211
void putBitPattern(final long bitPattern) {
208-
throw new SketchesReadOnlyException("Call to putBaseBufferCount() on read-only buffer");
212+
throw new SketchesReadOnlyException("Call to putBaseBufferCount() on read-only sketch");
209213
}
210214

211215
@Override
212216
double[] growCombinedBuffer(final int curCombBufItemCap, final int itemSpaceNeeded) {
213-
throw new SketchesReadOnlyException("Call to growCombinedBuffer() on read-only buffer");
217+
throw new SketchesReadOnlyException("Call to growCombinedBuffer() on read-only sketch");
214218
}
215219

216220
//Checks

src/main/java/org/apache/datasketches/quantiles/DoublesUnionImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public void update(final double quantile) {
143143
@Override
144144
public UpdateDoublesSketch getResultAndReset() {
145145
if (gadget_ == null) { return null; } //Intentionally return null here for speed.
146-
final UpdateDoublesSketch ds = gadget_;
146+
final UpdateDoublesSketch ds = gadget_.getSketchAndReset();
147147
gadget_ = null;
148148
return ds;
149149
}

src/main/java/org/apache/datasketches/quantiles/HeapUpdateDoublesSketch.java

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,13 @@ static HeapUpdateDoublesSketch copy(final HeapUpdateDoublesSketch skIn) {
141141
/**
142142
* Heapifies the given srcSeg, which must be a MemorySegment image of a DoublesSketch and may have data.
143143
*
144-
* @param srcSeg a MemorySegment image of a sketch, which may be in compact or not compact form.
144+
* @param srcSeg a MemorySegment image of a sketch, which may be in compact or updatable form.
145145
* @return a DoublesSketch on the Java heap.
146146
*/
147147
static HeapUpdateDoublesSketch heapifyInstance(final MemorySegment srcSeg) {
148148
final long segCapBytes = srcSeg.byteSize();
149-
if (segCapBytes < 8) {
150-
throw new SketchesArgumentException("Source MemorySegment too small: " + segCapBytes + " < 8");
149+
if ((segCapBytes < 8) || (segCapBytes > Integer.MAX_VALUE)) {
150+
throw new SketchesArgumentException("Source MemorySegment byteSize must be >= 8 and <= Integer.MAX_VALUE: " + segCapBytes);
151151
}
152152

153153
final int preLongs = extractPreLongs(srcSeg);
@@ -203,13 +203,6 @@ public long getN() {
203203
return n_;
204204
}
205205

206-
@Override
207-
HeapUpdateDoublesSketch getResultAndReset() {
208-
final HeapUpdateDoublesSketch skCopy = HeapUpdateDoublesSketch.copy(this);
209-
reset();
210-
return skCopy;
211-
}
212-
213206
@Override
214207
public boolean hasMemorySegment() {
215208
return false;
@@ -361,8 +354,8 @@ int getBaseBufferCount() {
361354
}
362355

363356
@Override
364-
int getCombinedBufferItemCapacity() {
365-
return combinedBuffer_.length;
357+
long getBitPattern() {
358+
return bitPattern_;
366359
}
367360

368361
@Override
@@ -371,30 +364,38 @@ int getCombinedBufferItemCapacity() {
371364
}
372365

373366
@Override
374-
long getBitPattern() {
375-
return bitPattern_;
367+
int getCombinedBufferItemCapacity() {
368+
return combinedBuffer_.length;
376369
}
377370

378371
@Override
379372
MemorySegment getMemorySegment() {
380373
return null;
381374
}
382375

383-
//Puts
384-
385376
@Override
386-
void putMinItem(final double minItem) {
387-
minItem_ = minItem;
377+
HeapUpdateDoublesSketch getSketchAndReset() {
378+
final HeapUpdateDoublesSketch skCopy = HeapUpdateDoublesSketch.copy(this);
379+
reset();
380+
return skCopy;
381+
}
382+
383+
@Override //the returned array is not always used
384+
double[] growCombinedBuffer(final int currentSpace, final int spaceNeeded) {
385+
combinedBuffer_ = Arrays.copyOf(combinedBuffer_, spaceNeeded);
386+
return combinedBuffer_;
388387
}
389388

389+
//Puts
390+
390391
@Override
391-
void putMaxItem(final double maxItem) {
392-
maxItem_ = maxItem;
392+
void putBaseBufferCount(final int baseBufferCount) {
393+
baseBufferCount_ = baseBufferCount;
393394
}
394395

395396
@Override
396-
void putN(final long n) {
397-
n_ = n;
397+
void putBitPattern(final long bitPattern) {
398+
bitPattern_ = bitPattern;
398399
}
399400

400401
@Override
@@ -403,19 +404,18 @@ void putCombinedBuffer(final double[] combinedBuffer) {
403404
}
404405

405406
@Override
406-
void putBaseBufferCount(final int baseBufferCount) {
407-
baseBufferCount_ = baseBufferCount;
407+
void putMaxItem(final double maxItem) {
408+
maxItem_ = maxItem;
408409
}
409410

410411
@Override
411-
void putBitPattern(final long bitPattern) {
412-
bitPattern_ = bitPattern;
412+
void putMinItem(final double minItem) {
413+
minItem_ = minItem;
413414
}
414415

415-
@Override //the returned array is not always used
416-
double[] growCombinedBuffer(final int currentSpace, final int spaceNeeded) {
417-
combinedBuffer_ = Arrays.copyOf(combinedBuffer_, spaceNeeded);
418-
return combinedBuffer_;
416+
@Override
417+
void putN(final long n) {
418+
n_ = n;
419419
}
420420

421421
/**

src/main/java/org/apache/datasketches/quantiles/ItemsSketch.java

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
*
7373
* @see QuantilesAPI
7474
*
75-
* @param <T> The sketch data type
75+
* @param <T> The sketch item data type.
7676
*/
7777
public final class ItemsSketch<T> implements QuantilesGenericAPI<T> {
7878
final Class<T> clazz;
@@ -124,6 +124,8 @@ public final class ItemsSketch<T> implements QuantilesGenericAPI<T> {
124124
*/
125125
public static final Random rand = new Random();
126126

127+
//**Constructors**
128+
127129
private ItemsSketch(
128130
final int k,
129131
final Class<T> clazz,
@@ -138,9 +140,9 @@ private ItemsSketch(
138140

139141
/**
140142
* Obtains a new instance of an ItemsSketch using the DEFAULT_K.
141-
* @param <T> The sketch data type
142-
* @param clazz the given class of T
143-
* @param comparator to compare items
143+
* @param <T> The sketch item data type.
144+
* @param clazz the given class of T.
145+
* @param comparator to compare items.
144146
* @return an ItemSketch&lt;T&gt;.
145147
*/
146148
public static <T> ItemsSketch<T> getInstance(
@@ -150,12 +152,12 @@ public static <T> ItemsSketch<T> getInstance(
150152
}
151153

152154
/**
153-
* Obtains a new instance of an ItemsSketch.
154-
* @param <T> The sketch data type
155-
* @param clazz the given class of T
155+
* Obtains a new instance of an ItemsSketch using the given <i>k</i>.
156+
* @param <T> The sketch item data type.
157+
* @param clazz the given class of T.
156158
* @param k Parameter that controls space usage of sketch and accuracy of estimates.
157159
* Must be greater than 2 and less than 65536 and a power of 2.
158-
* @param comparator to compare items
160+
* @param comparator to compare items.
159161
* @return an ItemSketch&lt;T&gt;.
160162
*/
161163
public static <T> ItemsSketch<T> getInstance(
@@ -175,15 +177,15 @@ public static <T> ItemsSketch<T> getInstance(
175177
}
176178

177179
/**
178-
* Heapifies the given srcSeg, which must be a MemorySegment image of a ItemsSketch
179-
* @param clazz the given class of T
180-
* @param srcSeg a MemorySegment image of a sketch.
181-
* @param comparator to compare items
182-
* @param serDe an instance of ArrayOfItemsSerDe
183-
* @param <T> The sketch data type
184-
* @return a ItemSketch&lt;T&gt; on the Java heap.
180+
* Heapifies the given srcSeg, which must be a MemorySegment image of a ItemsSketch.
181+
* @param <T> The sketch item data type.
182+
* @param clazz the given class of T.
183+
* @param srcSeg a MemorySegment image of a sketch generated from this class.
184+
* @param comparator to compare items.
185+
* @param serDe an instance of ArrayOfItemsSerDe.
186+
* @return a ItemsSketch&lt;T&gt; on the Java heap.
185187
*/
186-
public static <T> ItemsSketch<T> getInstance(
188+
public static <T> ItemsSketch<T> heapify(
187189
final Class<T> clazz,
188190
final MemorySegment srcSeg,
189191
final Comparator<? super T> comparator,
@@ -192,7 +194,6 @@ public static <T> ItemsSketch<T> getInstance(
192194
if (segCapBytes < 8) {
193195
throw new SketchesArgumentException("MemorySegment too small: " + segCapBytes);
194196
}
195-
196197
final int preambleLongs = extractPreLongs(srcSeg);
197198
final int serVer = extractSerVer(srcSeg);
198199
final int familyID = extractFamilyID(srcSeg);
@@ -233,10 +234,10 @@ public static <T> ItemsSketch<T> getInstance(
233234
}
234235

235236
/**
236-
* Returns a copy of the given sketch
237-
* @param <T> The sketch data type
238-
* @param sketch the given sketch
239-
* @return a copy of the given sketch
237+
* Returns a copy of the given sketch.
238+
* @param <T> The sketch item data type.
239+
* @param sketch the given sketch.
240+
* @return a copy of the given sketch.
240241
*/
241242
static <T> ItemsSketch<T> copy(final ItemsSketch<T> sketch) {
242243
final ItemsSketch<T> qsCopy = ItemsSketch.getInstance(sketch.clazz, sketch.k_, sketch.comparator_);
@@ -251,7 +252,7 @@ static <T> ItemsSketch<T> copy(final ItemsSketch<T> sketch) {
251252
return qsCopy;
252253
}
253254

254-
//END of Constructors
255+
//**END of Constructors**
255256

256257
@Override
257258
public double[] getCDF(final T[] splitPoints, final QuantileSearchCriteria searchCrit) {
@@ -581,6 +582,16 @@ Object[] getCombinedBuffer() {
581582
return combinedBuffer_;
582583
}
583584

585+
/**
586+
* Returns a copy of this sketch and then resets.
587+
* @return a copy of this sketch and then resets.
588+
*/
589+
ItemsSketch<T> getSketchAndReset() {
590+
final ItemsSketch<T> skCopy = copy(this);
591+
reset();
592+
return skCopy;
593+
}
594+
584595
/**
585596
* Loads the Combined Buffer, min and max from the given items array.
586597
* The Combined Buffer is always in non-compact form and must be pre-allocated.

0 commit comments

Comments
 (0)