Skip to content

Commit 5e7174f

Browse files
authored
Merge pull request #503 from apache/ebpps
use partial item flag in serialization
2 parents a8231cb + 2d5769a commit 5e7174f

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/main/java/org/apache/datasketches/sampling/EbppsItemsSketch.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import static org.apache.datasketches.sampling.PreambleUtil.EBPPS_SER_VER;
2323
import static org.apache.datasketches.sampling.PreambleUtil.EMPTY_FLAG_MASK;
24+
import static org.apache.datasketches.sampling.PreambleUtil.HAS_PARTIAL_ITEM_MASK;
2425

2526
import java.util.ArrayList;
2627
import java.util.Arrays;
@@ -121,6 +122,7 @@ public static <T> EbppsItemsSketch<T> heapify(final Memory srcMem,
121122
final int familyId = PreambleUtil.extractFamilyID(srcMem);
122123
final int flags = PreambleUtil.extractFlags(srcMem);
123124
final boolean isEmpty = (flags & EMPTY_FLAG_MASK) != 0;
125+
final boolean hasPartialItem = (flags & HAS_PARTIAL_ITEM_MASK) != 0;
124126

125127
// Check values
126128
if (isEmpty) {
@@ -189,8 +191,11 @@ public static <T> EbppsItemsSketch<T> heapify(final Memory srcMem,
189191
final List<T> itemsList = Arrays.asList(rawItems);
190192
final ArrayList<T> data;
191193
final T partialItem;
192-
if (numFullItems < numTotalItems) {
193-
data = new ArrayList<>(itemsList.subList(0, numFullItems));
194+
if (hasPartialItem) {
195+
if (numFullItems >= numTotalItems)
196+
throw new SketchesArgumentException("Possible Corruption: Expected partial item but none found");
197+
198+
data = new ArrayList<>(itemsList.subList(0, numFullItems));
194199
partialItem = itemsList.get(numFullItems); // 0-based, so last item
195200
} else {
196201
data = new ArrayList<>(itemsList);
@@ -487,7 +492,7 @@ public byte[] toByteArray(final ArrayOfItemsSerDe<? super T> serDe, final Class<
487492
if (empty) {
488493
PreambleUtil.insertFlags(mem, EMPTY_FLAG_MASK); // Byte 3
489494
} else {
490-
PreambleUtil.insertFlags(mem, 0);
495+
PreambleUtil.insertFlags(mem, sample_.hasPartialItem() ? HAS_PARTIAL_ITEM_MASK : 0);
491496
}
492497
PreambleUtil.insertK(mem, k_); // Bytes 4-7
493498

src/main/java/org/apache/datasketches/sampling/PreambleUtil.java

+1
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ private PreambleUtil() {}
209209
//static final int BIG_ENDIAN_FLAG_MASK = 1;
210210
//static final int READ_ONLY_FLAG_MASK = 2;
211211
static final int EMPTY_FLAG_MASK = 4;
212+
static final int HAS_PARTIAL_ITEM_MASK = 8; // EBPPS only
212213
static final int GADGET_FLAG_MASK = 128;
213214

214215
//Other constants

0 commit comments

Comments
 (0)