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;
}
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);
}
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;
}
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;
}
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);
}
Aggregations