Skip to content

Commit 0115a7e

Browse files
authored
Merge pull request #682 from apache/Fix_getResultAndReset
Fix get result and reset
2 parents 316d691 + 2a6774f commit 0115a7e

13 files changed

Lines changed: 197 additions & 141 deletions

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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
final class DirectUpdateDoublesSketch extends DirectUpdateDoublesSketchR {
6363
private MemorySegmentRequest mSegReq = null;
6464

65+
//**CONSTRUCTORS**
6566
private DirectUpdateDoublesSketch(final int k, final MemorySegment seg, final MemorySegmentRequest mSegReq) {
6667
super(k, seg); //Checks k
6768
this.mSegReq = mSegReq;
@@ -99,6 +100,11 @@ static DirectUpdateDoublesSketch newInstance(final int k, final MemorySegment ds
99100
return new DirectUpdateDoublesSketch(k, dstSeg, mSegReq);
100101
}
101102

103+
static HeapUpdateDoublesSketch heapify(final DirectUpdateDoublesSketch skIn) {
104+
final MemorySegment segIn = skIn.getMemorySegment();
105+
return HeapUpdateDoublesSketch.heapifyInstance(segIn);
106+
}
107+
102108
/**
103109
* Wrap this sketch around the given non-compact MemorySegment image of a DoublesSketch.
104110
*
@@ -130,6 +136,8 @@ static DirectUpdateDoublesSketch wrapInstance(final MemorySegment srcSeg, final
130136
return new DirectUpdateDoublesSketch(k, srcSeg, mSegReq);
131137
}
132138

139+
//**END CONSTRUCTORS**
140+
133141
@Override
134142
public boolean isReadOnly() {
135143
return false;
@@ -207,6 +215,23 @@ public void reset() {
207215
}
208216

209217
//Restricted overrides
218+
219+
@Override
220+
UpdateDoublesSketch 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+
210235
//Puts
211236

212237
@Override
@@ -242,15 +267,6 @@ void putBitPattern(final long bitPattern) {
242267
//intentionally a no-op, not kept on-heap, always derived.
243268
}
244269

245-
@Override
246-
double[] growCombinedBuffer(final int curCombBufItemCap, final int itemSpaceNeeded) {
247-
seg_ = growCombinedSegBuffer(itemSpaceNeeded);
248-
// copy out any data that was there
249-
final double[] newCombBuf = new double[itemSpaceNeeded];
250-
MemorySegment.copy(seg_, JAVA_DOUBLE_UNALIGNED, COMBINED_BUFFER, newCombBuf, 0, curCombBufItemCap);
251-
return newCombBuf;
252-
}
253-
254270
//Direct supporting methods
255271

256272
private MemorySegment growCombinedSegBuffer(final int itemSpaceNeeded) {

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

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,12 @@ public boolean isSameResource(final MemorySegment that) {
130130

131131
@Override
132132
public void reset() {
133-
throw new SketchesReadOnlyException("Call to reset() on read-only buffer");
133+
throw new SketchesReadOnlyException("Call to reset() on read-only sketch");
134134
}
135135

136136
@Override
137137
public void update(final double dataItem) {
138-
throw new SketchesReadOnlyException("Call to update() on read-only buffer");
138+
throw new SketchesReadOnlyException("Call to update() on read-only sketch");
139139
}
140140

141141
//Restricted overrides
@@ -147,8 +147,10 @@ int getBaseBufferCount() {
147147
}
148148

149149
@Override
150-
int getCombinedBufferItemCapacity() {
151-
return Math.max(0, (int)seg_.byteSize() - COMBINED_BUFFER) / 8;
150+
long getBitPattern() {
151+
final int k = getK();
152+
final long n = getN();
153+
return ClassicUtil.computeBitPattern(k, n);
152154
}
153155

154156
@Override
@@ -159,58 +161,59 @@ int getCombinedBufferItemCapacity() {
159161
final int itemCap = ClassicUtil.computeCombinedBufferItemCapacity(k, n);
160162
final double[] combinedBuffer = new double[itemCap];
161163
MemorySegment.copy(seg_, JAVA_DOUBLE_UNALIGNED, COMBINED_BUFFER, combinedBuffer, 0, itemCap);
162-
163-
164164
return combinedBuffer;
165165
}
166166

167167
@Override
168-
long getBitPattern() {
169-
final int k = getK();
170-
final long n = getN();
171-
return ClassicUtil.computeBitPattern(k, n);
168+
int getCombinedBufferItemCapacity() {
169+
return Math.max(0, (int)seg_.byteSize() - COMBINED_BUFFER) / 8;
172170
}
173171

174172
@Override
175173
MemorySegment getMemorySegment() {
176174
return seg_;
177175
}
178176

177+
@Override
178+
UpdateDoublesSketch getSketchAndReset() {
179+
throw new SketchesReadOnlyException("Call to getSketchAndReset() on read-only sketch");
180+
}
181+
182+
@Override
183+
double[] growCombinedBuffer(final int curCombBufItemCap, final int itemSpaceNeeded) {
184+
throw new SketchesReadOnlyException("Call to growCombinedBuffer() on read-only sketch");
185+
}
186+
179187
//Puts
180188

181189
@Override
182190
void putMinItem(final double minQuantile) {
183-
throw new SketchesReadOnlyException("Call to putMinQuantile() on read-only buffer");
191+
throw new SketchesReadOnlyException("Call to putMinItem() on read-only sketch");
184192
}
185193

186194
@Override
187195
void putMaxItem(final double maxQuantile) {
188-
throw new SketchesReadOnlyException("Call to putMaxQuantile() on read-only buffer");
196+
throw new SketchesReadOnlyException("Call to putMaxItem() on read-only sketch");
189197
}
190198

191199
@Override
192200
void putN(final long n) {
193-
throw new SketchesReadOnlyException("Call to putN() on read-only buffer");
201+
throw new SketchesReadOnlyException("Call to putN() on read-only sketch");
194202
}
195203

196204
@Override
197205
void putCombinedBuffer(final double[] combinedBuffer) {
198-
throw new SketchesReadOnlyException("Call to putCombinedBuffer() on read-only buffer");
206+
throw new SketchesReadOnlyException("Call to putCombinedBuffer() on read-only sketch");
199207
}
200208

201209
@Override
202210
void putBaseBufferCount(final int baseBufferCount) {
203-
throw new SketchesReadOnlyException("Call to putBaseBufferCount() on read-only buffer");
211+
throw new SketchesReadOnlyException("Call to putBaseBufferCount() on read-only sketch");
204212
}
205213

206214
@Override
207215
void putBitPattern(final long bitPattern) {
208-
throw new SketchesReadOnlyException("Call to putBaseBufferCount() on read-only buffer");
209-
}
210-
211-
@Override
212-
double[] growCombinedBuffer(final int curCombBufItemCap, final int itemSpaceNeeded) {
213-
throw new SketchesReadOnlyException("Call to growCombinedBuffer() on read-only buffer");
216+
throw new SketchesReadOnlyException("Call to putBitPattern() on read-only sketch");
214217
}
215218

216219
//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: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ final class HeapUpdateDoublesSketch extends UpdateDoublesSketch {
9696
*/
9797
private double[] combinedBuffer_;
9898

99-
//**CONSTRUCTORS**********************************************************
99+
//**CONSTRUCTORS**
100100
private HeapUpdateDoublesSketch(final int k) {
101101
super(k); //Checks k
102102
}
@@ -120,16 +120,34 @@ static HeapUpdateDoublesSketch newInstance(final int k) {
120120
return huds;
121121
}
122122

123+
/**
124+
* Returns a copy of the given sketch
125+
* @param skIn the given sketch
126+
* @return a copy of the given sketch
127+
*/
128+
static HeapUpdateDoublesSketch copy(final HeapUpdateDoublesSketch skIn) {
129+
final HeapUpdateDoublesSketch skCopy = HeapUpdateDoublesSketch.newInstance(skIn.k_);
130+
if (skIn.n_ > 0) {
131+
skCopy.n_ = skIn.n_;
132+
skCopy.combinedBuffer_ = skIn.combinedBuffer_.clone();
133+
skCopy.baseBufferCount_ = skIn.baseBufferCount_;
134+
skCopy.bitPattern_ = skIn.bitPattern_;
135+
skCopy.minItem_ = skIn.minItem_;
136+
skCopy.maxItem_ = skIn.maxItem_;
137+
}
138+
return skCopy;
139+
}
140+
123141
/**
124142
* Heapifies the given srcSeg, which must be a MemorySegment image of a DoublesSketch and may have data.
125143
*
126-
* @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.
127145
* @return a DoublesSketch on the Java heap.
128146
*/
129147
static HeapUpdateDoublesSketch heapifyInstance(final MemorySegment srcSeg) {
130148
final long segCapBytes = srcSeg.byteSize();
131-
if (segCapBytes < 8) {
132-
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);
133151
}
134152

135153
final int preLongs = extractPreLongs(srcSeg);
@@ -166,6 +184,8 @@ static HeapUpdateDoublesSketch heapifyInstance(final MemorySegment srcSeg) {
166184
return huds;
167185
}
168186

187+
//**END CONSTRUCTORS**
188+
169189
@Override
170190
public double getMaxItem() {
171191
if (isEmpty()) { throw new IllegalArgumentException(QuantilesAPI.EMPTY_MSG); }
@@ -334,8 +354,8 @@ int getBaseBufferCount() {
334354
}
335355

336356
@Override
337-
int getCombinedBufferItemCapacity() {
338-
return combinedBuffer_.length;
357+
long getBitPattern() {
358+
return bitPattern_;
339359
}
340360

341361
@Override
@@ -344,30 +364,38 @@ int getCombinedBufferItemCapacity() {
344364
}
345365

346366
@Override
347-
long getBitPattern() {
348-
return bitPattern_;
367+
int getCombinedBufferItemCapacity() {
368+
return combinedBuffer_.length;
349369
}
350370

351371
@Override
352372
MemorySegment getMemorySegment() {
353373
return null;
354374
}
355375

356-
//Puts
357-
358376
@Override
359-
void putMinItem(final double minItem) {
360-
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_;
361387
}
362388

389+
//Puts
390+
363391
@Override
364-
void putMaxItem(final double maxItem) {
365-
maxItem_ = maxItem;
392+
void putBaseBufferCount(final int baseBufferCount) {
393+
baseBufferCount_ = baseBufferCount;
366394
}
367395

368396
@Override
369-
void putN(final long n) {
370-
n_ = n;
397+
void putBitPattern(final long bitPattern) {
398+
bitPattern_ = bitPattern;
371399
}
372400

373401
@Override
@@ -376,19 +404,18 @@ void putCombinedBuffer(final double[] combinedBuffer) {
376404
}
377405

378406
@Override
379-
void putBaseBufferCount(final int baseBufferCount) {
380-
baseBufferCount_ = baseBufferCount;
407+
void putMaxItem(final double maxItem) {
408+
maxItem_ = maxItem;
381409
}
382410

383411
@Override
384-
void putBitPattern(final long bitPattern) {
385-
bitPattern_ = bitPattern;
412+
void putMinItem(final double minItem) {
413+
minItem_ = minItem;
386414
}
387415

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

394421
/**

0 commit comments

Comments
 (0)