use of org.roaringbitmap.RoaringBitmap in project Gaffer by gchq.
the class RoaringBitmapJsonSerialisationTest method testCanSerialiseWithCustomObjectMapper.
@Test
public void testCanSerialiseWithCustomObjectMapper() throws IOException {
//Bitmap of (2,3000,300000) serialised in 0.5.11 Roaring Bitmap base 64 encoded
String serialisedComparisonBitmap = "{\"roaringBitmap\":{\"value\":\"OjAAAAIAAAAAAAEABAAAABgAAAAcAAAAAgC4C+CT\"}}";
RoaringBitmap comparisonBitmap = new RoaringBitmap();
comparisonBitmap.add(2);
comparisonBitmap.add(3000);
comparisonBitmap.add(300000);
final ObjectMapper mapper = JSONSerialiser.createDefaultMapper();
final SimpleModule bitmapModule = new SimpleModule(RoaringBitmapConstants.BITMAP_MODULE_NAME, new Version(1, 0, 9, null, null, null));
bitmapModule.addSerializer(RoaringBitmap.class, new RoaringBitmapJsonSerialiser());
bitmapModule.addDeserializer(RoaringBitmap.class, new RoaringBitmapJsonDeserialiser());
mapper.registerModule(bitmapModule);
RoaringBitmap testBitmap = mapper.readValue(serialisedComparisonBitmap, RoaringBitmap.class);
assertEquals(comparisonBitmap, testBitmap);
String serialisedBitmap = mapper.writeValueAsString(testBitmap);
assertEquals(serialisedBitmap, serialisedComparisonBitmap);
}
use of org.roaringbitmap.RoaringBitmap in project Gaffer by gchq.
the class RoaringBitmapJsonSerialisationTest method testCanSerialiseAndDeserialise.
@Test
public void testCanSerialiseAndDeserialise() throws IOException {
final ByteArrayBuilder byteArrayBuilder = new ByteArrayBuilder();
JsonGenerator jsonGenerator = JSON_FACTORY.createGenerator(byteArrayBuilder);
RoaringBitmap testBitmap = new RoaringBitmap();
testBitmap.add(2);
testBitmap.add(3000);
testBitmap.add(300000);
for (int i = 400000; i < 500000; i += 2) {
testBitmap.add(i);
}
SERIALISER.serialize(testBitmap, jsonGenerator, null);
jsonGenerator.flush();
byte[] serialisedBitmap = byteArrayBuilder.toByteArray();
JsonParser parser = JSON_FACTORY.createParser(serialisedBitmap);
parser.setCodec(new ObjectMapper());
Object o = DESERIALISER.deserialize(parser, null);
assertEquals(RoaringBitmap.class, o.getClass());
assertEquals(testBitmap, o);
}
use of org.roaringbitmap.RoaringBitmap in project Gaffer by gchq.
the class RoaringBitmapJsonSerialisationTest method testCanDeserialiseVersionZeroPointThreePointFourBitmap.
@Test
public void testCanDeserialiseVersionZeroPointThreePointFourBitmap() throws IOException {
//Bitmap of (2,3000,300000) serialised in 0.3.4 Roaring Bitmap base 64 encoded
String serialisedBitmap = "{\"roaringBitmap\":{\"value\":\"OTAAAAIAAAAAAAEABAAAAAIAuAvgkw==\"}}";
RoaringBitmap comparisonBitmap = new RoaringBitmap();
comparisonBitmap.add(2);
comparisonBitmap.add(3000);
comparisonBitmap.add(300000);
JsonParser parser = JSON_FACTORY.createParser(serialisedBitmap);
parser.setCodec(new ObjectMapper());
Object o = DESERIALISER.deserialize(parser, null);
assertEquals(RoaringBitmap.class, o.getClass());
assertEquals(comparisonBitmap, o);
ByteArrayBuilder byteArrayBuilder = new ByteArrayBuilder();
JsonGenerator jsonGenerator = JSON_FACTORY.createGenerator(byteArrayBuilder);
SERIALISER.serialize((RoaringBitmap) o, jsonGenerator, null);
jsonGenerator.flush();
byte[] bytes = byteArrayBuilder.toByteArray();
String reSerialisedBitmap = new String(bytes);
byteArrayBuilder = new ByteArrayBuilder();
jsonGenerator = JSON_FACTORY.createGenerator(byteArrayBuilder);
SERIALISER.serialize(comparisonBitmap, jsonGenerator, null);
jsonGenerator.flush();
String serialisedComparisonBitmap = new String(byteArrayBuilder.toByteArray());
assertNotEquals(reSerialisedBitmap, serialisedBitmap);
assertEquals(reSerialisedBitmap, serialisedComparisonBitmap);
}
use of org.roaringbitmap.RoaringBitmap in project Gaffer by gchq.
the class RoaringBitmapJsonSerialisationTest method testCanSerialiseAndDeserialiseWithRuns.
@Test
public void testCanSerialiseAndDeserialiseWithRuns() throws IOException {
final ByteArrayBuilder byteArrayBuilder = new ByteArrayBuilder();
JsonGenerator jsonGenerator = JSON_FACTORY.createGenerator(byteArrayBuilder);
RoaringBitmap testBitmap = new RoaringBitmap();
testBitmap.add(2);
testBitmap.add(3000);
testBitmap.add(300000);
for (int i = 400000; i < 500000; i += 2) {
testBitmap.add(i);
}
testBitmap.runOptimize();
SERIALISER.serialize(testBitmap, jsonGenerator, null);
jsonGenerator.flush();
byte[] serialisedBitmap = byteArrayBuilder.toByteArray();
JsonParser parser = JSON_FACTORY.createParser(serialisedBitmap);
parser.setCodec(new ObjectMapper());
Object o = DESERIALISER.deserialize(parser, null);
assertEquals(RoaringBitmap.class, o.getClass());
assertEquals(testBitmap, o);
}
use of org.roaringbitmap.RoaringBitmap in project Gaffer by gchq.
the class RoaringBitmapSerialiser method serialise.
@Override
public byte[] serialise(final Object object) throws SerialisationException {
RoaringBitmap value = (RoaringBitmap) object;
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(byteOut);
try {
value.serialize(out);
} catch (final IOException e) {
throw new SerialisationException(e.getMessage(), e);
}
return byteOut.toByteArray();
}
Aggregations