Skip to content

Commit

Permalink
[Bug] Java Version BitmapValue deserialized failed when only has 32-b…
Browse files Browse the repository at this point in the history
…it bitmap (apache#4884)
  • Loading branch information
wangbo authored Nov 16, 2020
1 parent e706a6b commit 2af4bc2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.roaringbitmap.BitmapDataProviderSupplier;
import org.roaringbitmap.IntConsumer;
import org.roaringbitmap.IntIterator;
import org.roaringbitmap.InvalidRoaringFormat;
import org.roaringbitmap.RoaringBitmap;
import org.roaringbitmap.RoaringBitmapSupplier;
import org.roaringbitmap.Util;
Expand Down Expand Up @@ -1345,16 +1346,22 @@ public void deserialize(DataInput in, int bitmapType) throws IOException {
this.clear();
highToBitmap = new TreeMap<>();

long nbHighs = 1;
if (bitmapType == BitmapValue.BITMAP64) {
nbHighs = Codec.decodeVarint64(in);
if (bitmapType == BitmapValue.BITMAP32) {
RoaringBitmap provider = new RoaringBitmap();
provider.deserialize(in);
highToBitmap.put(0, provider);
return;
}

if (bitmapType != BitmapValue.BITMAP64) {
throw new InvalidRoaringFormat("invalid bitmap type");
}

long nbHighs = Codec.decodeVarint64(in);
for (int i = 0; i < nbHighs; i++) {
int high = in.readInt();
RoaringBitmap provider = new RoaringBitmap();
provider.deserialize(in);

highToBitmap.put(high, provider);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,11 @@ public void testBitmapValueSerializeAndDeserialize() throws IOException {
Assert.assertTrue(serializeSingleValueBitmapValue.equals(deserializeSingleValueBitmapValue));

// bitmap
// case 1 : 32-bit bitmap
BitmapValue serializeBitmapBitmapValue = new BitmapValue();
for (int i = 0; i < 10; i++) {
serializeBitmapBitmapValue.add(i);
}
for (long i = Long.MAX_VALUE; i > Long.MAX_VALUE - 10; i--) {
serializeBitmapBitmapValue.add(i);
}
ByteArrayOutputStream bitmapOutputStream = new ByteArrayOutputStream();
DataOutput bitmapOutput = new DataOutputStream(bitmapOutputStream);
serializeBitmapBitmapValue.serialize(bitmapOutput);
Expand All @@ -340,6 +338,23 @@ public void testBitmapValueSerializeAndDeserialize() throws IOException {
deserializeBitmapBitmapValue.deserialize(bitmapInputStream);

Assert.assertTrue(serializeBitmapBitmapValue.equals(deserializeBitmapBitmapValue));


// bitmap
// case 2 : 64-bit bitmap
BitmapValue serializeBitmapBitmapValue64 = new BitmapValue();
for (long i = Long.MAX_VALUE; i > Long.MAX_VALUE - 10; i--) {
serializeBitmapBitmapValue64.add(i);
}
ByteArrayOutputStream bitmapOutputStream64 = new ByteArrayOutputStream();
DataOutput bitmapOutput64 = new DataOutputStream(bitmapOutputStream64);
serializeBitmapBitmapValue64.serialize(bitmapOutput64);

DataInputStream bitmapInputStream64 = new DataInputStream(new ByteArrayInputStream(bitmapOutputStream64.toByteArray()));
BitmapValue deserializeBitmapBitmapValue64 = new BitmapValue();
deserializeBitmapBitmapValue64.deserialize(bitmapInputStream64);

Assert.assertTrue(serializeBitmapBitmapValue64.equals(deserializeBitmapBitmapValue64));
}

@Test
Expand Down

0 comments on commit 2af4bc2

Please sign in to comment.