use of uk.gov.gchq.gaffer.time.RBMBackedTimestampSet 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 uk.gov.gchq.gaffer.time.RBMBackedTimestampSet 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 uk.gov.gchq.gaffer.time.RBMBackedTimestampSet in project Gaffer by gchq.
the class RBMBackedTimestampSetAggregatorTest method testAggregate.
@Test
public void testAggregate() {
// Given
final RBMBackedTimestampSet rbmBackedTimestampSet1 = new RBMBackedTimestampSet(TimeBucket.SECOND);
rbmBackedTimestampSet1.add(Instant.ofEpochMilli(1000L));
rbmBackedTimestampSet1.add(Instant.ofEpochMilli(1000000L));
final RBMBackedTimestampSet rbmBackedTimestampSet2 = new RBMBackedTimestampSet(TimeBucket.SECOND);
rbmBackedTimestampSet2.add(Instant.ofEpochMilli(1000L));
rbmBackedTimestampSet2.add(Instant.ofEpochMilli(2000000L));
// When
final RBMBackedTimestampSet aggregated = RBM_BACKED_TIMESTAMP_SET_AGGREGATOR._apply(rbmBackedTimestampSet1, rbmBackedTimestampSet2);
final RBMBackedTimestampSet expected = new RBMBackedTimestampSet(TimeBucket.SECOND);
expected.add(Instant.ofEpochMilli(1000L));
expected.add(Instant.ofEpochMilli(1000000L));
expected.add(Instant.ofEpochMilli(2000000L));
// Then
assertEquals(3, aggregated.getNumberOfTimestamps());
assertEquals(expected, aggregated);
}
use of uk.gov.gchq.gaffer.time.RBMBackedTimestampSet in project Gaffer by gchq.
the class RBMBackedTimestampSetInRange method test.
@Override
public boolean test(final RBMBackedTimestampSet rbmBackedTimestampSet) {
if (rbmBackedTimestampSet == null) {
throw new IllegalArgumentException("TimestampSet cannot be null");
}
if (rbmBackedTimestampSet.getNumberOfTimestamps() == 0) {
throw new IllegalArgumentException("TimestampSet must contain at least one value");
}
Long startEpoch = startTime != null ? startTime.longValue() : null;
Long endEpoch = endTime != null ? endTime.longValue() : null;
RBMBackedTimestampSet masked = new MaskTimestampSetByTimeRange(startEpoch, endEpoch, timeUnit).apply(rbmBackedTimestampSet);
if (includeAllTimestamps) {
return masked.equals(rbmBackedTimestampSet);
} else {
return masked.getNumberOfTimestamps() > 0L;
}
}
Aggregations