Search in sources :

Example 6 with RoaringBitmap

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);
}
Also used : Version(com.fasterxml.jackson.core.Version) RoaringBitmap(org.roaringbitmap.RoaringBitmap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) Test(org.junit.Test)

Example 7 with RoaringBitmap

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);
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ByteArrayBuilder(com.fasterxml.jackson.core.util.ByteArrayBuilder) RoaringBitmap(org.roaringbitmap.RoaringBitmap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 8 with RoaringBitmap

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);
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ByteArrayBuilder(com.fasterxml.jackson.core.util.ByteArrayBuilder) RoaringBitmap(org.roaringbitmap.RoaringBitmap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 9 with RoaringBitmap

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);
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ByteArrayBuilder(com.fasterxml.jackson.core.util.ByteArrayBuilder) RoaringBitmap(org.roaringbitmap.RoaringBitmap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 10 with RoaringBitmap

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();
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) RoaringBitmap(org.roaringbitmap.RoaringBitmap)

Aggregations

RoaringBitmap (org.roaringbitmap.RoaringBitmap)12 Test (org.junit.Test)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 JsonParser (com.fasterxml.jackson.core.JsonParser)3 ByteArrayBuilder (com.fasterxml.jackson.core.util.ByteArrayBuilder)3 IOException (java.io.IOException)2 SerialisationException (uk.gov.gchq.gaffer.exception.SerialisationException)2 TreeNode (com.fasterxml.jackson.core.TreeNode)1 Version (com.fasterxml.jackson.core.Version)1 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)1 TextNode (com.fasterxml.jackson.databind.node.TextNode)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1