1919
2020package org .apache .datasketches .frequencies ;
2121
22+ import static java .lang .foreign .ValueLayout .JAVA_BYTE ;
23+ import static java .lang .foreign .ValueLayout .JAVA_LONG_UNALIGNED ;
2224import static org .apache .datasketches .common .Util .LS ;
2325import static org .apache .datasketches .common .Util .checkBounds ;
2426import static org .apache .datasketches .common .Util .exactLog2OfInt ;
4244import static org .apache .datasketches .frequencies .Util .LG_MIN_MAP_SIZE ;
4345import static org .apache .datasketches .frequencies .Util .SAMPLE_SIZE ;
4446
47+ import java .lang .foreign .MemorySegment ;
4548import java .lang .reflect .Array ;
4649import java .util .ArrayList ;
4750import java .util .Comparator ;
4851import java .util .Objects ;
4952
50- import org .apache .datasketches .common .ArrayOfItemsSerDe ;
53+ import org .apache .datasketches .common .ArrayOfItemsSerDe2 ;
5154import org .apache .datasketches .common .Family ;
5255import org .apache .datasketches .common .SketchesArgumentException ;
5356import org .apache .datasketches .common .SketchesStateException ;
54- import org .apache .datasketches .memory .Memory ;
55- import org .apache .datasketches .memory .WritableMemory ;
5657
5758/**
5859 * This sketch is useful for tracking approximate frequencies of items of type <i><T></i>
@@ -209,7 +210,7 @@ public ItemsSketch(final int maxMapSize) {
209210 this .lgMaxMapSize = Math .max (lgMaxMapSize , LG_MIN_MAP_SIZE );
210211 final int lgCurMapSz = Math .max (lgCurMapSize , LG_MIN_MAP_SIZE );
211212 hashMap = new ReversePurgeItemHashMap <>(1 << lgCurMapSz );
212- this . curMapCap = hashMap .getCapacity ();
213+ curMapCap = hashMap .getCapacity ();
213214 final int maxMapCap =
214215 (int ) ((1 << lgMaxMapSize ) * ReversePurgeItemHashMap .getLoadFactor ());
215216 offset = 0 ;
@@ -218,20 +219,19 @@ public ItemsSketch(final int maxMapSize) {
218219
219220 /**
220221 * Returns a sketch instance of this class from the given srcMem,
221- * which must be a Memory representation of this sketch class.
222+ * which must be a MemorySegment representation of this sketch class.
222223 *
223224 * @param <T> The type of item that this sketch will track
224- * @param srcMem a Memory representation of a sketch of this class.
225- * <a href="{@docRoot}/resources/dictionary.html#mem">See Memory</a>
225+ * @param srcSeg a MemorySegment representation of a sketch of this class.
226226 * @param serDe an instance of ArrayOfItemsSerDe
227227 * @return a sketch instance of this class.
228228 */
229- public static <T > ItemsSketch <T > getInstance (final Memory srcMem ,
230- final ArrayOfItemsSerDe <T > serDe ) {
231- Objects .requireNonNull (srcMem , "srcMem must not be null." );
229+ public static <T > ItemsSketch <T > getInstance (final MemorySegment srcSeg ,
230+ final ArrayOfItemsSerDe2 <T > serDe ) {
231+ Objects .requireNonNull (srcSeg , "srcMem must not be null." );
232232 Objects .requireNonNull (serDe , "serDe must not be null." );
233233
234- final long pre0 = PreambleUtil .checkPreambleSize (srcMem ); //make sure preamble will fit
234+ final long pre0 = PreambleUtil .checkPreambleSize (srcSeg ); //make sure preamble will fit
235235 final int maxPreLongs = Family .FREQUENCY .getMaxPreLongs ();
236236
237237 final int preLongs = extractPreLongs (pre0 ); //Byte 0
@@ -267,7 +267,7 @@ public static <T> ItemsSketch<T> getInstance(final Memory srcMem,
267267 }
268268 //get full preamble
269269 final long [] preArr = new long [preLongs ];
270- srcMem . getLongArray ( 0 , preArr , 0 , preLongs );
270+ MemorySegment . copy ( srcSeg , JAVA_LONG_UNALIGNED , 0 , preArr , 0 , preLongs );
271271
272272 final ItemsSketch <T > fis = new ItemsSketch <>(lgMaxMapSize , lgCurMapSize );
273273 fis .streamWeight = 0 ; //update after
@@ -278,14 +278,13 @@ public static <T> ItemsSketch<T> getInstance(final Memory srcMem,
278278
279279 //Get countArray
280280 final long [] countArray = new long [activeItems ];
281- final int reqBytes = preBytes + activeItems * Long .BYTES ; //count Arr only
282- checkBounds (0 , reqBytes , srcMem . getCapacity ()); //check Memory capacity
283- srcMem . getLongArray ( preBytes , countArray , 0 , activeItems );
281+ final int reqBytes = preBytes + ( activeItems * Long .BYTES ) ; //count Arr only
282+ checkBounds (0 , reqBytes , srcSeg . byteSize ()); //check MemorySegment capacity
283+ MemorySegment . copy ( srcSeg , JAVA_LONG_UNALIGNED , preBytes , countArray , 0 , activeItems );
284284
285285 //Get itemArray
286286 final int itemsOffset = preBytes + (Long .BYTES * activeItems );
287- final T [] itemArray = serDe .deserializeFromMemory (
288- srcMem .region (itemsOffset , srcMem .getCapacity () - itemsOffset ), 0 , activeItems );
287+ final T [] itemArray = serDe .deserializeFromMemorySegment (srcSeg .asSlice (itemsOffset ), 0 , activeItems );
289288 //update the sketch
290289 for (int i = 0 ; i < activeItems ; i ++) {
291290 fis .update (itemArray [i ], countArray [i ]);
@@ -311,7 +310,7 @@ public static double getAprioriError(final int maxMapSize, final long estimatedT
311310 * @return the current number of counters the sketch is configured to support.
312311 */
313312 public int getCurrentMapCapacity () {
314- return this . curMapCap ;
313+ return curMapCap ;
315314 }
316315
317316 /**
@@ -429,7 +428,7 @@ public int getNumActiveItems() {
429428 * @return the sum of the frequencies in the stream seen so far by the sketch
430429 */
431430 public long getStreamLength () {
432- return this . streamWeight ;
431+ return streamWeight ;
433432 }
434433
435434 /**
@@ -462,17 +461,16 @@ public boolean isEmpty() {
462461 * largest error tolerance of the two merged sketches.
463462 */
464463 public ItemsSketch <T > merge (final ItemsSketch <T > other ) {
465- if (other == null ) { return this ; }
466- if (other .isEmpty ()) { return this ; }
464+ if ((other == null ) || other .isEmpty ()) { return this ; }
467465
468- final long streamLen = this . streamWeight + other .streamWeight ; //capture before merge
466+ final long streamLen = streamWeight + other .streamWeight ; //capture before merge
469467
470468 final ReversePurgeItemHashMap .Iterator <T > iter = other .hashMap .iterator ();
471469 while (iter .next ()) { //this may add to offset during rebuilds
472470 this .update (iter .getKey (), iter .getValue ());
473471 }
474- this . offset += other .offset ;
475- this . streamWeight = streamLen ; //corrected streamWeight
472+ offset += other .offset ;
473+ streamWeight = streamLen ; //corrected streamWeight
476474 return this ;
477475 }
478476
@@ -481,9 +479,9 @@ public ItemsSketch<T> merge(final ItemsSketch<T> other) {
481479 */
482480 public void reset () {
483481 hashMap = new ReversePurgeItemHashMap <>(1 << LG_MIN_MAP_SIZE );
484- this . curMapCap = hashMap .getCapacity ();
485- this . offset = 0 ;
486- this . streamWeight = 0 ;
482+ curMapCap = hashMap .getCapacity ();
483+ offset = 0 ;
484+ streamWeight = 0 ;
487485 }
488486
489487 //Serialization
@@ -493,7 +491,7 @@ public void reset() {
493491 * @param serDe an instance of ArrayOfItemsSerDe
494492 * @return a byte array representation of this sketch
495493 */
496- public byte [] toByteArray (final ArrayOfItemsSerDe <T > serDe ) {
494+ public byte [] toByteArray (final ArrayOfItemsSerDe2 <T > serDe ) {
497495 final int preLongs ;
498496 final int outBytes ;
499497 final boolean empty = isEmpty ();
@@ -508,7 +506,7 @@ public byte[] toByteArray(final ArrayOfItemsSerDe<T> serDe) {
508506 outBytes = ((preLongs + activeItems ) << 3 ) + bytes .length ;
509507 }
510508 final byte [] outArr = new byte [outBytes ];
511- final WritableMemory mem = WritableMemory . writableWrap (outArr );
509+ final MemorySegment seg = MemorySegment . ofArray (outArr );
512510
513511 // build first preLong empty or not
514512 long pre0 = 0L ;
@@ -520,18 +518,19 @@ public byte[] toByteArray(final ArrayOfItemsSerDe<T> serDe) {
520518 pre0 = empty ? insertFlags (EMPTY_FLAG_MASK , pre0 ) : insertFlags (0 , pre0 ); //Byte 5
521519
522520 if (empty ) {
523- mem . putLong ( 0 , pre0 );
521+ seg . set ( JAVA_LONG_UNALIGNED , 0 , pre0 );
524522 } else {
525523 final long pre = 0 ;
526524 final long [] preArr = new long [preLongs ];
527525 preArr [0 ] = pre0 ;
528526 preArr [1 ] = insertActiveItems (activeItems , pre );
529- preArr [2 ] = this .streamWeight ;
530- preArr [3 ] = this .offset ;
531- mem .putLongArray (0 , preArr , 0 , preLongs );
527+ preArr [2 ] = streamWeight ;
528+ preArr [3 ] = offset ;
529+ MemorySegment .copy (preArr , 0 , seg , JAVA_LONG_UNALIGNED , 0 , preLongs );
530+
532531 final int preBytes = preLongs << 3 ;
533- mem . putLongArray ( preBytes , hashMap .getActiveValues (), 0 , activeItems );
534- mem . putByteArray ( preBytes + (this .getNumActiveItems () << 3 ), bytes , 0 , bytes .length );
532+ MemorySegment . copy ( hashMap .getActiveValues (), 0 , seg , JAVA_LONG_UNALIGNED , preBytes , activeItems );
533+ MemorySegment . copy ( bytes , 0 , seg , JAVA_BYTE , preBytes + (this .getNumActiveItems () << 3 ), bytes .length );
535534 }
536535 return outArr ;
537536 }
@@ -556,16 +555,16 @@ public String toString() {
556555 * @return a human readable string of the preamble of a byte array image of a ItemsSketch.
557556 */
558557 public static String toString (final byte [] byteArr ) {
559- return toString (Memory . wrap (byteArr ));
558+ return toString (MemorySegment . ofArray (byteArr ));
560559 }
561560
562561 /**
563- * Returns a human readable string of the preamble of a Memory image of a ItemsSketch.
564- * @param mem the given Memory object
565- * @return a human readable string of the preamble of a Memory image of a ItemsSketch.
562+ * Returns a human readable string of the preamble of a MemorySegment image of a ItemsSketch.
563+ * @param seg the given MemorySegment object
564+ * @return a human readable string of the preamble of a MemorySegment image of a ItemsSketch.
566565 */
567- public static String toString (final Memory mem ) {
568- return PreambleUtil .preambleToString (mem );
566+ public static String toString (final MemorySegment seg ) {
567+ return PreambleUtil .preambleToString (seg );
569568 }
570569
571570 /**
@@ -590,7 +589,7 @@ public void update(final T item, final long count) {
590589 if (count < 0 ) {
591590 throw new SketchesArgumentException ("Count may not be negative" );
592591 }
593- this . streamWeight += count ;
592+ streamWeight += count ;
594593 hashMap .adjustOrPutValue (item , count );
595594
596595 if (getNumActiveItems () > curMapCap ) { //over the threshold, we need to do something
@@ -620,7 +619,7 @@ public static class Row<T> implements Comparable<Row<T>> {
620619
621620 Row (final T item , final long estimate , final long ub , final long lb ) {
622621 this .item = item ;
623- this . est = estimate ;
622+ est = estimate ;
624623 this .ub = ub ;
625624 this .lb = lb ;
626625 }
@@ -672,7 +671,7 @@ public String toString() {
672671 */
673672 @ Override
674673 public int compareTo (final Row <T > that ) {
675- return (this . est < that .est ) ? -1 : (this . est > that .est ) ? 1 : 0 ;
674+ return (est < that .est ) ? -1 : (est > that .est ) ? 1 : 0 ;
676675 }
677676
678677 /**
@@ -685,9 +684,8 @@ public int compareTo(final Row<T> that) {
685684 @ Override
686685 public int hashCode () {
687686 final int prime = 31 ;
688- int result = 1 ;
689- result = (prime * result ) + (int ) (est ^ (est >>> 32 ));
690- return result ;
687+ final int result = 1 ;
688+ return (prime * result ) + (int ) (est ^ (est >>> 32 ));
691689 }
692690
693691 /**
@@ -701,15 +699,15 @@ public int hashCode() {
701699 @ Override
702700 public boolean equals (final Object obj ) {
703701 if (this == obj ) { return true ; }
704- if (obj == null ) { return false ; }
705- if ( !(obj instanceof Row )) { return false ; }
702+ if ( (obj == null ) || !(obj instanceof Row )) { return false ; }
706703 final Row <T > that = (Row <T >) obj ;
707704 if (est != that .est ) { return false ; }
708705 return true ;
709706 }
710707
711708 } //End of class Row<T>
712709
710+ @ SuppressWarnings ("unchecked" )
713711 Row <T >[] sortItems (final long threshold , final ErrorType errorType ) {
714712 final ArrayList <Row <T >> rowList = new ArrayList <>();
715713 final ReversePurgeItemHashMap .Iterator <T > iter = hashMap .iterator ();
@@ -743,10 +741,7 @@ public int compare(final Row<T> r1, final Row<T> r2) {
743741 }
744742 });
745743
746- @ SuppressWarnings ("unchecked" )
747- final Row <T >[] rowsArr =
748- rowList .toArray ((Row <T >[]) Array .newInstance (Row .class , rowList .size ()));
749- return rowsArr ;
744+ return rowList .toArray ((Row <T >[]) Array .newInstance (Row .class , rowList .size ()));
750745 }
751746
752747}
0 commit comments