6060import java .lang .foreign .MemorySegment ;
6161
6262import org .apache .datasketches .common .Family ;
63+ import org .apache .datasketches .common .MemorySegmentRequest ;
6364import org .apache .datasketches .common .ResizeFactor ;
6465import org .apache .datasketches .common .SketchesArgumentException ;
6566import org .apache .datasketches .common .SuppressFBWarnings ;
8182class DirectQuickSelectSketch extends DirectQuickSelectSketchR {
8283 private static final double DQS_RESIZE_THRESHOLD = 15.0 / 16.0 ; //tuned for space
8384 int hashTableThreshold_ ; //computed and mutable, kept only on heap, never serialized.
85+ private final MemorySegmentRequest mSegReq ;
8486
8587 /**
86- * Construct this sketch as a result of a wrap operation where the given MemorySegment already has a sketch image.
87- * @param wseg the given MemorySegment that has a sketch image.
88+ * Construct this sketch as a result of a wrap operation where the given MemorySegment already has an updatable sketch image.
89+ * @param wseg the given MemorySegment that has an updatable sketch image.
90+ * @param mSegReq an implementation of the MemorySegmentRequest interface or null.
8891 * @param seed <a href="{@docRoot}/resources/dictionary.html#seed">See Update Hash Seed</a>.
8992 */
9093 private DirectQuickSelectSketch (
9194 final MemorySegment wseg ,
95+ final MemorySegmentRequest mSegReq ,
9296 final long seed ) {
97+ this .mSegReq = mSegReq == null ? MemorySegmentRequest .DEFAULT : mSegReq ;
9398 super (wseg , seed );
9499 }
95100
@@ -105,6 +110,7 @@ private DirectQuickSelectSketch(
105110 * <a href="{@docRoot}/resources/dictionary.html#resizeFactor">See Resize Factor</a>
106111 * @param dstSeg the given MemorySegment object destination. It cannot be null.
107112 * It will be cleared prior to use.
113+ * @param mSegReq an implementation of the MemorySegmentRequest interface or null.
108114 * @param unionGadget true if this sketch is implementing the Union gadget function.
109115 * Otherwise, it is behaving as a normal QuickSelectSketch.
110116 */
@@ -114,6 +120,7 @@ private DirectQuickSelectSketch(
114120 final float p ,
115121 final ResizeFactor rf ,
116122 final MemorySegment dstSeg ,
123+ final MemorySegmentRequest mSegReq ,
117124 final boolean unionGadget ) {
118125
119126 //Choose family, preambleLongs
@@ -129,7 +136,7 @@ private DirectQuickSelectSketch(
129136 final long curSegCapBytes = dstSeg .byteSize ();
130137 if (curSegCapBytes < minReqBytes ) {
131138 throw new SketchesArgumentException (
132- "MemorySegment capacity is too small : " + curSegCapBytes + " < " + minReqBytes );
139+ "MemorySegment capacity is less than minimum required : " + curSegCapBytes + " < " + minReqBytes );
133140 }
134141
135142 //@formatter:off
@@ -153,17 +160,22 @@ private DirectQuickSelectSketch(
153160 //clear hash table area
154161 dstSeg .asSlice (preambleLongs << 3 , Long .BYTES << lgArrLongs ).fill ((byte )0 );
155162 hashTableThreshold_ = getOffHeapHashTableThreshold (lgNomLongs , lgArrLongs );
163+ this .mSegReq = mSegReq == null ? MemorySegmentRequest .DEFAULT : mSegReq ;
156164 super (dstSeg , seed );
157165 }
158166
159167 /**
160- * Wrap a sketch around the given source MemorySegment containing sketch data that originated from
161- * this sketch.
168+ * Wrap a sketch around the given source MemorySegment containing sketch data that originated from this sketch.
162169 * @param srcSeg The given MemorySegment object must be in hash table form and not read only.
170+ * @param mSegReq an implementation of the MemorySegmentRequest interface or null.
163171 * @param seed <a href="{@docRoot}/resources/dictionary.html#seed">See Update Hash Seed</a>
164172 * @return instance of this sketch
165173 */
166- static DirectQuickSelectSketch writableWrap (final MemorySegment srcSeg , final long seed ) {
174+ //called from UnionImpl and UpdateSketch
175+ static DirectQuickSelectSketch writableWrap (
176+ final MemorySegment srcSeg ,
177+ final MemorySegmentRequest mSegReq ,
178+ final long seed ) {
167179 final int preambleLongs = extractPreLongs (srcSeg ); //byte 0
168180 final int lgNomLongs = extractLgNomLongs (srcSeg ); //byte 3
169181 final int lgArrLongs = extractLgArrLongs (srcSeg ); //byte 4
@@ -176,7 +188,7 @@ static DirectQuickSelectSketch writableWrap(final MemorySegment srcSeg, final lo
176188 insertLgResizeFactor (srcSeg , ResizeFactor .X2 .lg ());
177189 }
178190
179- final DirectQuickSelectSketch dqss = new DirectQuickSelectSketch (srcSeg , seed );
191+ final DirectQuickSelectSketch dqss = new DirectQuickSelectSketch (srcSeg , mSegReq , seed );
180192 dqss .hashTableThreshold_ = getOffHeapHashTableThreshold (lgNomLongs , lgArrLongs );
181193 return dqss ;
182194 }
@@ -185,14 +197,19 @@ static DirectQuickSelectSketch writableWrap(final MemorySegment srcSeg, final lo
185197 * Fast-wrap a sketch around the given source MemorySegment containing sketch data that originated from
186198 * this sketch. This does NO validity checking of the given MemorySegment.
187199 * @param srcSeg The given MemorySegment must be in hash table form and not read only.
200+ * @param mSegReq an implementation of the MemorySegmentRequest interface or null.
188201 * @param seed <a href="{@docRoot}/resources/dictionary.html#seed">See Update Hash Seed</a>
189202 * @return instance of this sketch
190203 */
191- static DirectQuickSelectSketch fastWritableWrap (final MemorySegment srcSeg , final long seed ) {
204+ //called from UnionImpl <- Union
205+ static DirectQuickSelectSketch fastWritableWrap (
206+ final MemorySegment srcSeg ,
207+ final MemorySegmentRequest mSegReq ,
208+ final long seed ) {
192209 final int lgNomLongs = extractLgNomLongs (srcSeg ); //byte 3
193210 final int lgArrLongs = extractLgArrLongs (srcSeg ); //byte 4
194211
195- final DirectQuickSelectSketch dqss = new DirectQuickSelectSketch (srcSeg , seed );
212+ final DirectQuickSelectSketch dqss = new DirectQuickSelectSketch (srcSeg , mSegReq , seed );
196213 dqss .hashTableThreshold_ = getOffHeapHashTableThreshold (lgNomLongs , lgArrLongs );
197214 return dqss ;
198215 }
@@ -205,7 +222,7 @@ static DirectQuickSelectSketch fastWritableWrap(final MemorySegment srcSeg, fina
205222 public UpdateSketch rebuild () {
206223 final int lgNomLongs = getLgNomLongs ();
207224 final int preambleLongs = wseg_ .get (JAVA_BYTE , PREAMBLE_LONGS_BYTE ) & 0X3F ;
208- if (getRetainedEntries (true ) > ( 1 << lgNomLongs ) ) {
225+ if (getRetainedEntries (true ) > 1 << lgNomLongs ) {
209226 quickSelectAndRebuild (wseg_ , preambleLongs , lgNomLongs );
210227 }
211228 return this ;
@@ -279,10 +296,13 @@ UpdateReturnState hashUpdate(final long hash) {
279296 tgtLgArrLongs = Math .min (lgArrLongs + lgRF , lgNomLongs + 1 );
280297 final int tgtArrBytes = 8 << tgtLgArrLongs ;
281298 final int reqBytes = tgtArrBytes + preBytes ;
282- final MemorySegment newDstSeg = MemorySegment .ofArray (new byte [reqBytes ]); //always on-heap //TODO ADD MemSegReq
299+
300+ final MemorySegment newDstSeg = mSegReq .request (reqBytes );
283301
284302 moveAndResize (wseg_ , preambleLongs , lgArrLongs , newDstSeg , tgtLgArrLongs , thetaLong );
303+ final MemorySegment oldSeg = wseg_ ;
285304 wseg_ = newDstSeg ;
305+ mSegReq .requestClose (oldSeg );
286306
287307 hashTableThreshold_ = getOffHeapHashTableThreshold (lgNomLongs , tgtLgArrLongs );
288308 return InsertedCountIncrementedResized ;
0 commit comments