@@ -190,49 +190,53 @@ private static CompactSketch wrap(final MemorySegment srcSeg, final long seed, f
190190 }
191191 final short seedHash = Util .computeSeedHash (seed );
192192
193- if (serVer == 4 ) {
194- return DirectCompactCompressedSketch .wrapInstance (srcSeg ,
195- enforceSeed ? seedHash : (short ) extractSeedHash (srcSeg ));
196- }
197- else if (serVer == 3 ) {
198- if (PreambleUtil .isEmptyFlag (srcSeg )) {
199- return EmptyCompactSketch .getHeapInstance (srcSeg );
193+ switch (serVer ) {
194+ case 1 : {
195+ return ForwardCompatibility .heapify1to3 (srcSeg , seedHash );
200196 }
201- if (otherCheckForSingleItem (srcSeg )) {
202- return SingleItemSketch .heapify (srcSeg , enforceSeed ? seedHash : (short ) extractSeedHash (srcSeg ));
197+ case 2 : {
198+ return ForwardCompatibility .heapify2to3 (srcSeg ,
199+ enforceSeed ? seedHash : (short ) extractSeedHash (srcSeg ));
203200 }
204- //not empty & not singleItem
205- final int flags = extractFlags (srcSeg );
206- final boolean compactFlag = (flags & COMPACT_FLAG_MASK ) > 0 ;
207- if (!compactFlag ) {
208- throw new SketchesArgumentException (
209- "Corrupted: COMPACT family sketch image must have compact flag set" );
201+ case 3 : {
202+ if (PreambleUtil .isEmptyFlag (srcSeg )) {
203+ return EmptyCompactSketch .getHeapInstance (srcSeg );
204+ }
205+ if (otherCheckForSingleItem (srcSeg )) {
206+ return SingleItemSketch .heapify (srcSeg , enforceSeed ? seedHash : (short ) extractSeedHash (srcSeg ));
207+ }
208+ //not empty & not singleItem
209+ final int flags = extractFlags (srcSeg );
210+ final boolean compactFlag = (flags & COMPACT_FLAG_MASK ) > 0 ;
211+ if (!compactFlag ) {
212+ throw new SketchesArgumentException (
213+ "Corrupted: COMPACT family sketch image must have compact flag set" );
214+ }
215+ final boolean readOnly = (flags & READ_ONLY_FLAG_MASK ) > 0 ;
216+ if (!readOnly ) {
217+ throw new SketchesArgumentException (
218+ "Corrupted: COMPACT family sketch image must have Read-Only flag set" );
219+ }
220+ return DirectCompactSketch .wrapInstance (srcSeg ,
221+ enforceSeed ? seedHash : (short ) extractSeedHash (srcSeg ));
210222 }
211- final boolean readOnly = (flags & READ_ONLY_FLAG_MASK ) > 0 ;
212- if (!readOnly ) {
223+ case 4 : {
224+ return DirectCompactCompressedSketch .wrapInstance (srcSeg ,
225+ enforceSeed ? seedHash : (short ) extractSeedHash (srcSeg ));
226+ }
227+ default : {
213228 throw new SketchesArgumentException (
214- "Corrupted: COMPACT family sketch image must have Read-Only flag set " );
229+ "Corrupted: Serialization Version " + serVer + " not recognized. " );
215230 }
216- return DirectCompactSketch .wrapInstance (srcSeg ,
217- enforceSeed ? seedHash : (short ) extractSeedHash (srcSeg ));
218- } //end of serVer 3
219- else if (serVer == 1 ) {
220- return ForwardCompatibility .heapify1to3 (srcSeg , seedHash );
221- }
222- else if (serVer == 2 ) {
223- return ForwardCompatibility .heapify2to3 (srcSeg ,
224- enforceSeed ? seedHash : (short ) extractSeedHash (srcSeg ));
225231 }
226- throw new SketchesArgumentException (
227- "Corrupted: Serialization Version " + serVer + " not recognized." );
228232 }
229233
230234 /**
231235 * Wrap takes the sketch image in the given byte array and refers to it directly.
232236 * There is no data copying onto the java heap.
233237 * The wrap operation enables fast read-only merging and access to all the public read-only API.
234238 *
235- * <p>Only "Direct" Serialization Version 3 (i.e, OpenSource) sketches that have
239+ * <p>Only "Direct" Serialization Versions 3 and 4 (i.e, OpenSource) sketches that have
236240 * been explicitly stored as direct sketches can be wrapped.
237241 * Wrapping earlier serial version sketches will result in a heapify operation.
238242 * These early versions were never designed to "wrap".</p>
@@ -242,7 +246,7 @@ else if (serVer == 2) {
242246 * This is actually faster and consumes less overall space.</p>
243247 *
244248 * <p>This method checks if the DEFAULT_UPDATE_SEED was used to create the source byte array image.
245- * Note that SerialVersion 1 sketches cannot be checked as they don't have a seedHash field,
249+ * Note that SerialVersion 1 (pre-open-source) sketches cannot be checked as they don't have a seedHash field,
246250 * so the resulting heapified CompactSketch will be given the hash of DEFAULT_UPDATE_SEED.</p>
247251 *
248252 * @param bytes a byte array image of a Sketch that was created using the DEFAULT_UPDATE_SEED.
@@ -258,7 +262,7 @@ public static CompactSketch wrap(final byte[] bytes) {
258262 * There is no data copying onto the java heap.
259263 * The wrap operation enables fast read-only merging and access to all the public read-only API.
260264 *
261- * <p>Only "Direct" Serialization Version 3 (i.e, OpenSource) sketches that have
265+ * <p>Only "Direct" Serialization Versions 3 and 4 (i.e, OpenSource) sketches that have
262266 * been explicitly stored as direct sketches can be wrapped.
263267 * Wrapping earlier serial version sketches will result in a heapify operation.
264268 * These early versions were never designed to "wrap".</p>
@@ -288,38 +292,46 @@ private static CompactSketch wrap(final byte[] bytes, final long seed, final boo
288292 throw new SketchesArgumentException ("Corrupted: " + family + " is not Compact!" );
289293 }
290294 final short seedHash = Util .computeSeedHash (seed );
291- if (serVer == 4 ) {
292- return WrappedCompactCompressedSketch .wrapInstance (bytes , seedHash );
293- } else if (serVer == 3 ) {
294- final int flags = bytes [FLAGS_BYTE ];
295- if ((flags & EMPTY_FLAG_MASK ) > 0 ) {
296- return EmptyCompactSketch .getHeapInstance (MemorySegment .ofArray (bytes ));
295+
296+ switch (serVer ) {
297+ case 1 : {
298+ return ForwardCompatibility .heapify1to3 (MemorySegment .ofArray (bytes ), seedHash );
297299 }
298- final int preLongs = bytes [ PREAMBLE_LONGS_BYTE ];
299- if ( otherCheckForSingleItem ( preLongs , serVer , familyId , flags )) {
300- return SingleItemSketch . heapify ( MemorySegment . ofArray ( bytes ), enforceSeed ? seedHash : getShortLE (bytes , SEED_HASH_SHORT ));
300+ case 2 : {
301+ return ForwardCompatibility . heapify2to3 ( MemorySegment . ofArray ( bytes ),
302+ enforceSeed ? seedHash : getShortLE (bytes , SEED_HASH_SHORT ));
301303 }
302- //not empty & not singleItem
303- final boolean compactFlag = (flags & COMPACT_FLAG_MASK ) > 0 ;
304- if (!compactFlag ) {
305- throw new SketchesArgumentException (
306- "Corrupted: COMPACT family sketch image must have compact flag set" );
304+ case 3 : {
305+ final int flags = bytes [FLAGS_BYTE ];
306+ if ((flags & EMPTY_FLAG_MASK ) > 0 ) {
307+ return EmptyCompactSketch .getHeapInstance (MemorySegment .ofArray (bytes ));
308+ }
309+ final int preLongs = bytes [PREAMBLE_LONGS_BYTE ];
310+ if (otherCheckForSingleItem (preLongs , serVer , familyId , flags )) {
311+ return SingleItemSketch .heapify (MemorySegment .ofArray (bytes ), enforceSeed ? seedHash : getShortLE (bytes , SEED_HASH_SHORT ));
312+ }
313+ //not empty & not singleItem
314+ final boolean compactFlag = (flags & COMPACT_FLAG_MASK ) > 0 ;
315+ if (!compactFlag ) {
316+ throw new SketchesArgumentException (
317+ "Corrupted: COMPACT family sketch image must have compact flag set" );
318+ }
319+ final boolean readOnly = (flags & READ_ONLY_FLAG_MASK ) > 0 ;
320+ if (!readOnly ) {
321+ throw new SketchesArgumentException (
322+ "Corrupted: COMPACT family sketch image must have Read-Only flag set" );
323+ }
324+ return WrappedCompactSketch .wrapInstance (bytes ,
325+ enforceSeed ? seedHash : getShortLE (bytes , SEED_HASH_SHORT ));
326+ }
327+ case 4 : {
328+ return WrappedCompactCompressedSketch .wrapInstance (bytes , seedHash );
307329 }
308- final boolean readOnly = (flags & READ_ONLY_FLAG_MASK ) > 0 ;
309- if (!readOnly ) {
330+ default : {
310331 throw new SketchesArgumentException (
311- "Corrupted: COMPACT family sketch image must have Read-Only flag set " );
332+ "Corrupted: Serialization Version " + serVer + " not recognized. " );
312333 }
313- return WrappedCompactSketch .wrapInstance (bytes ,
314- enforceSeed ? seedHash : getShortLE (bytes , SEED_HASH_SHORT ));
315- } else if (serVer == 1 ) {
316- return ForwardCompatibility .heapify1to3 (MemorySegment .ofArray (bytes ), seedHash );
317- } else if (serVer == 2 ) {
318- return ForwardCompatibility .heapify2to3 (MemorySegment .ofArray (bytes ),
319- enforceSeed ? seedHash : getShortLE (bytes , SEED_HASH_SHORT ));
320334 }
321- throw new SketchesArgumentException (
322- "Corrupted: Serialization Version " + serVer + " not recognized." );
323335 }
324336
325337 //Sketch Overrides
0 commit comments