Search in sources :

Example 71 with RoaringBitmap

use of org.roaringbitmap.RoaringBitmap in project Gaffer by gchq.

the class RoaringBitmapSerialiserTest method getExampleValue.

private RoaringBitmap getExampleValue() {
    RoaringBitmap testBitmap = new RoaringBitmap();
    testBitmap.add(2);
    testBitmap.add(3000);
    testBitmap.add(300000);
    return testBitmap;
}
Also used : RoaringBitmap(org.roaringbitmap.RoaringBitmap)

Example 72 with RoaringBitmap

use of org.roaringbitmap.RoaringBitmap in project Gaffer by gchq.

the class RoaringBitmapSerialiserTest method testCanSerialiseAndDeserialise.

@Test
public void testCanSerialiseAndDeserialise() throws SerialisationException {
    RoaringBitmap testBitmap = getExampleValue();
    for (int i = 400000; i < 500000; i += 2) {
        testBitmap.add(i);
    }
    byte[] b = SERIALISER.serialise(testBitmap);
    Object o = SERIALISER.deserialise(b);
    assertEquals(RoaringBitmap.class, o.getClass());
    assertEquals(testBitmap, o);
}
Also used : RoaringBitmap(org.roaringbitmap.RoaringBitmap) Test(org.junit.jupiter.api.Test) ToBytesSerialisationTest(uk.gov.gchq.gaffer.serialisation.ToBytesSerialisationTest)

Example 73 with RoaringBitmap

use of org.roaringbitmap.RoaringBitmap in project Gaffer by gchq.

the class BoundedTimestampSetSerialiser method deserialise.

@Override
public BoundedTimestampSet deserialise(final byte[] allBytes, final int offset, final int length) throws SerialisationException {
    if (allBytes.length == 0 || length == 0) {
        return null;
    }
    final ByteArrayInputStream bais = new ByteArrayInputStream(allBytes, offset, length);
    final DataInputStream dis = new DataInputStream(bais);
    final int bucketInt = (int) CompactRawSerialisationUtils.read(dis);
    final TimeBucket bucket = TimeBucket.values()[bucketInt];
    final int maxSize = (int) CompactRawSerialisationUtils.read(dis);
    final BoundedTimestampSet boundedTimestampSet = new BoundedTimestampSet(bucket, maxSize);
    try {
        final byte state = dis.readByte();
        if (NOT_FULL == state) {
            final RBMBackedTimestampSet rbmBackedTimestampSet = new RBMBackedTimestampSet(bucket);
            final RoaringBitmap rbm = new RoaringBitmap();
            final byte[] serialisedRBM = new byte[bais.available()];
            if (-1 == dis.read(serialisedRBM)) {
                throw new SerialisationException("Unexpected end of stream when reading serialised RoaringBitmap");
            }
            final byte[] convertedBytes = RoaringBitmapUtils.upConvertSerialisedForm(serialisedRBM, 0, serialisedRBM.length);
            final ByteArrayInputStream baisConvertedBytes = new ByteArrayInputStream(convertedBytes);
            final DataInputStream disConvertedBytes = new DataInputStream(baisConvertedBytes);
            rbm.deserialize(disConvertedBytes);
            rbmBackedTimestampSet.setRbm(rbm);
            boundedTimestampSet.setRbmBackedTimestampSet(rbmBackedTimestampSet);
        } else if (SAMPLE == state) {
            final byte[] serialisedRLU = new byte[dis.available()];
            if (-1 == dis.read(serialisedRLU)) {
                throw new SerialisationException("Unexpected end of stream when reading serialised ReservoirLongsUnion");
            }
            final ReservoirLongsUnion reservoirLongsUnion = ReservoirLongsUnion.heapify(WritableMemory.wrap(serialisedRLU));
            boundedTimestampSet.setReservoirLongsUnion(reservoirLongsUnion);
        } else {
            throw new SerialisationException("Unexpected byte indicating the state: expected " + NOT_FULL + " or " + SAMPLE + ", got " + state);
        }
    } catch (final IOException e) {
        throw new SerialisationException("IOException deserialising BoundedTimestampSet from byte array", e);
    }
    return boundedTimestampSet;
}
Also used : RBMBackedTimestampSet(uk.gov.gchq.gaffer.time.RBMBackedTimestampSet) SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) ByteArrayInputStream(java.io.ByteArrayInputStream) TimeBucket(uk.gov.gchq.gaffer.time.CommonTimeUtil.TimeBucket) BoundedTimestampSet(uk.gov.gchq.gaffer.time.BoundedTimestampSet) ReservoirLongsUnion(com.yahoo.sketches.sampling.ReservoirLongsUnion) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) RoaringBitmap(org.roaringbitmap.RoaringBitmap)

Example 74 with RoaringBitmap

use of org.roaringbitmap.RoaringBitmap in project Gaffer by gchq.

the class RBMBackedTimestampSetSerialiser method deserialise.

@Override
public RBMBackedTimestampSet deserialise(final byte[] allBytes, final int offset, final int length) throws SerialisationException {
    if (allBytes.length == 0 || length == 0) {
        return null;
    }
    final int bucketInt = (int) CompactRawSerialisationUtils.readLong(allBytes, offset);
    final int numBytesForInt = CompactRawSerialisationUtils.decodeVIntSize(allBytes[offset]);
    final TimeBucket bucket = TimeBucket.values()[bucketInt];
    final RBMBackedTimestampSet rbmBackedTimestampSet = new RBMBackedTimestampSet(bucket);
    final RoaringBitmap rbm = new RoaringBitmap();
    try {
        // Deal with different versions of RoaringBitmap
        final byte[] convertedBytes = RoaringBitmapUtils.upConvertSerialisedForm(allBytes, offset + numBytesForInt, length - numBytesForInt);
        final ByteArrayInputStream baisConvertedBytes = new ByteArrayInputStream(convertedBytes);
        final DataInputStream disConvertedBytes = new DataInputStream(baisConvertedBytes);
        rbm.deserialize(disConvertedBytes);
    } catch (final IOException e) {
        throw new SerialisationException("IOException deserialising RoaringBitmap from byte array", e);
    }
    rbmBackedTimestampSet.setRbm(rbm);
    return rbmBackedTimestampSet;
}
Also used : RBMBackedTimestampSet(uk.gov.gchq.gaffer.time.RBMBackedTimestampSet) SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) ByteArrayInputStream(java.io.ByteArrayInputStream) TimeBucket(uk.gov.gchq.gaffer.time.CommonTimeUtil.TimeBucket) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) RoaringBitmap(org.roaringbitmap.RoaringBitmap)

Example 75 with RoaringBitmap

use of org.roaringbitmap.RoaringBitmap in project Gaffer by gchq.

the class RBMBackedTimestampSet method applyTimeRangeMask.

/**
 * Applies a time range mask. Timestamps which fall outside the range are filtered.
 *
 * @param startMillis filter start time in milliseconds
 * @param endMillis   filter end time in milliseconds
 */
public void applyTimeRangeMask(final Long startMillis, final Long endMillis) {
    final int startTime;
    final int endTime;
    if (startMillis != null && endMillis != null && startMillis > endMillis) {
        throw new IllegalArgumentException("The start time should not be chronologically later than the end time");
    }
    if (startMillis == null) {
        startTime = toInt(getEarliest().toEpochMilli());
    } else {
        startTime = toInt(startMillis);
        if (startMillis > 0 && startTime < 0) {
            throw new RuntimeException("Failed to convert start time to " + timeBucket.name() + " as the resulting value was outside the range of Integer");
        }
    }
    if (endMillis == null) {
        endTime = toInt(getLatest().toEpochMilli());
    } else {
        endTime = toInt(endMillis);
        if (endMillis > 0 && endTime < 0) {
            throw new RuntimeException("Failed to convert end time to " + timeBucket.name() + " as the resulting value was outside the range of Integer");
        }
    }
    RoaringBitmap timeRange = new RoaringBitmap();
    // end date is exclusive
    timeRange.add(startTime, endTime + 1);
    rbm.and(timeRange);
}
Also used : RoaringBitmap(org.roaringbitmap.RoaringBitmap)

Aggregations

RoaringBitmap (org.roaringbitmap.RoaringBitmap)81 Benchmark (org.openjdk.jmh.annotations.Benchmark)14 Test (org.junit.jupiter.api.Test)10 Test (org.junit.Test)9 DataOutputStream (java.io.DataOutputStream)8 MutableRoaringBitmap (org.roaringbitmap.buffer.MutableRoaringBitmap)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 DataInputStream (java.io.DataInputStream)7 IOException (java.io.IOException)7 BenchmarkMode (org.openjdk.jmh.annotations.BenchmarkMode)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 OutputTimeUnit (org.openjdk.jmh.annotations.OutputTimeUnit)6 BitmapDataProvider (org.roaringbitmap.BitmapDataProvider)6 ByteString (com.google.protobuf.ByteString)5 ByteBuffer (java.nio.ByteBuffer)5 Setup (org.openjdk.jmh.annotations.Setup)5 SerialisationException (uk.gov.gchq.gaffer.exception.SerialisationException)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 FasterList (jcog.list.FasterList)4 Term (nars.term.Term)4