Skip to content

Commit 8b98b74

Browse files
committed
In version 0.9.36, RoaringBitmap fixed compatibility issues with CRoaring
1 parent 5cf13b2 commit 8b98b74

File tree

1 file changed

+37
-21
lines changed

1 file changed

+37
-21
lines changed

clickhouse-client/src/main/java/com/clickhouse/client/data/ClickHouseBitmap.java

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,16 @@ public void serialize(ByteBuffer buffer) {
177177
}
178178

179179
byte[] bytes = bas.toByteArray();
180-
for (int i = 4; i > 0; i--) {
181-
buffer.put(bytes[i]);
180+
if (Roaring64NavigableMap.SERIALIZATION_MODE == Roaring64NavigableMap.SERIALIZATION_MODE_PORTABLE) {
181+
// In Roaring64NavigableMap SERIALIZATION_MODE_PORTABLE mode, binary serialization data consistent with the CRoaring. No other special processing is required
182+
buffer.put(bytes, 0, size);
183+
} else {
184+
for (int i = 4; i > 0; i--) {
185+
buffer.put(bytes[i]);
186+
}
187+
buffer.putInt(0);
188+
buffer.put(bytes, 5, size - 5);
182189
}
183-
buffer.putInt(0);
184-
buffer.put(bytes, 5, size - 5);
185190
} catch (IOException e) {
186191
throw new IllegalStateException("Failed to serialize given bitmap", e);
187192
}
@@ -362,18 +367,23 @@ public static ClickHouseBitmap deserialize(DataInputStream in, ClickHouseDataTyp
362367
b.deserialize(flip(newBuffer(len).put(bytes)));
363368
rb = ClickHouseBitmap.wrap(b, innerType);
364369
} else {
365-
// TODO implement a wrapper of DataInput to get rid of byte array here
366-
bytes[0] = (byte) 0; // always unsigned
367-
// read map size in big-endian byte order
368-
for (int i = 4; i > 0; i--) {
369-
bytes[i] = in.readByte();
370+
if (Roaring64NavigableMap.SERIALIZATION_MODE == Roaring64NavigableMap.SERIALIZATION_MODE_PORTABLE) {
371+
// In Roaring64NavigableMap SERIALIZATION_MODE_PORTABLE mode, binary serialization data consistent with the CRoaring
372+
in.readFully(bytes, 0 ,len);
373+
} else {
374+
// TODO implement a wrapper of DataInput to get rid of byte array here
375+
bytes[0] = (byte) 0; // always unsigned
376+
// read map size in big-endian byte order
377+
for (int i = 4; i > 0; i--) {
378+
bytes[i] = in.readByte();
379+
}
380+
if (in.readByte() != 0 || in.readByte() != 0 || in.readByte() != 0 || in.readByte() != 0) {
381+
throw new IllegalStateException(
382+
"Not able to deserialize ClickHouseBitmap for too many bitmaps(>" + 0xFFFFFFFFL + ")!");
383+
}
384+
// read the rest
385+
in.readFully(bytes, 5, len - 8);
370386
}
371-
if (in.readByte() != 0 || in.readByte() != 0 || in.readByte() != 0 || in.readByte() != 0) { // NOSONAR
372-
throw new IllegalStateException(
373-
"Not able to deserialize ClickHouseBitmap for too many bitmaps(>" + 0xFFFFFFFFL + ")!");
374-
}
375-
// read the rest
376-
in.readFully(bytes, 5, len - 8);
377387
Roaring64NavigableMap b = new Roaring64NavigableMap();
378388
b.deserialize(new DataInputStream(new ByteArrayInputStream(bytes)));
379389
rb = ClickHouseBitmap.wrap(b, innerType);
@@ -593,12 +603,18 @@ public ByteBuffer toByteBuffer() {
593603
BinaryStreamUtils.writeVarInt(buf, size);
594604
serialize(buf);
595605
} else { // 64
596-
// 1) deduct one to exclude the leading byte - boolean flag, see below:
597-
// https://github.com/RoaringBitmap/RoaringBitmap/blob/0.9.9/RoaringBitmap/src/main/java/org/roaringbitmap/longlong/Roaring64NavigableMap.java#L1107
598-
// 2) add 4 bytes because CRoaring uses long to store count of 32-bit bitmaps,
599-
// while Java uses int - see
600-
// https://github.com/RoaringBitmap/CRoaring/blob/v0.2.66/cpp/roaring64map.hh#L597
601-
long size = serializedSizeInBytesAsLong() - 1 + 4;
606+
long size = 0;
607+
if (Roaring64NavigableMap.SERIALIZATION_MODE == Roaring64NavigableMap.SERIALIZATION_MODE_PORTABLE) {
608+
// In Roaring64NavigableMap SERIALIZATION_MODE_PORTABLE mode, binary serialization data consistent with the CRoaring
609+
size = serializedSizeInBytesAsLong();
610+
} else {
611+
// 1) deduct one to exclude the leading byte - boolean flag, see below:
612+
// https://github.com/RoaringBitmap/RoaringBitmap/blob/0.9.9/RoaringBitmap/src/main/java/org/roaringbitmap/longlong/Roaring64NavigableMap.java#L1107
613+
// 2) add 4 bytes because CRoaring uses long to store count of 32-bit bitmaps,
614+
// while Java uses int - see
615+
// https://github.com/RoaringBitmap/CRoaring/blob/v0.2.66/cpp/roaring64map.hh#L597
616+
size = serializedSizeInBytesAsLong() - 1 + 4;
617+
}
602618
int varIntSize = BinaryStreamUtils.getVarLongSize(size);
603619
// TODO add serialize(DataOutput) to handle more
604620
int intSize = (int) size;

0 commit comments

Comments
 (0)