Search in sources :

Example 1 with TimeBucket

use of uk.gov.gchq.gaffer.time.CommonTimeUtil.TimeBucket in project Gaffer by gchq.

the class DeltaLongTimeSeriesSerialiser method deserialise.

@Override
public LongTimeSeries 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 numEntries = (int) CompactRawSerialisationUtils.read(dis);
    final LongTimeSeries timeSeries = new LongTimeSeries(bucket);
    try {
        final boolean deltaMode = dis.readBoolean();
        if (deltaMode) {
            deltaDeserialise(timeSeries, numEntries, dis);
        } else {
            defaultDeserialise(timeSeries, numEntries, dis);
        }
    } catch (final IOException e) {
        throw new SerialisationException("IOException reading boolean", e);
    }
    return timeSeries;
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) ByteArrayInputStream(java.io.ByteArrayInputStream) LongTimeSeries(uk.gov.gchq.gaffer.time.LongTimeSeries) TimeBucket(uk.gov.gchq.gaffer.time.CommonTimeUtil.TimeBucket) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 2 with TimeBucket

use of uk.gov.gchq.gaffer.time.CommonTimeUtil.TimeBucket 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 3 with TimeBucket

use of uk.gov.gchq.gaffer.time.CommonTimeUtil.TimeBucket 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 4 with TimeBucket

use of uk.gov.gchq.gaffer.time.CommonTimeUtil.TimeBucket in project Gaffer by gchq.

the class LongTimeSeriesTest method testGetTimeBucket.

@Test
public void testGetTimeBucket() {
    // Given
    final LongTimeSeries timeSeries1 = new LongTimeSeries(TimeBucket.MINUTE);
    final LongTimeSeries timeSeries2 = new LongTimeSeries(TimeBucket.HOUR);
    // When
    final TimeBucket bucket1 = timeSeries1.getTimeBucket();
    final TimeBucket bucket2 = timeSeries2.getTimeBucket();
    // Then
    assertEquals(TimeBucket.MINUTE, bucket1);
    assertEquals(TimeBucket.HOUR, bucket2);
}
Also used : TimeBucket(uk.gov.gchq.gaffer.time.CommonTimeUtil.TimeBucket) JSONSerialisationTest(uk.gov.gchq.gaffer.JSONSerialisationTest) Test(org.junit.jupiter.api.Test)

Aggregations

TimeBucket (uk.gov.gchq.gaffer.time.CommonTimeUtil.TimeBucket)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 DataInputStream (java.io.DataInputStream)3 IOException (java.io.IOException)3 SerialisationException (uk.gov.gchq.gaffer.exception.SerialisationException)3 RoaringBitmap (org.roaringbitmap.RoaringBitmap)2 RBMBackedTimestampSet (uk.gov.gchq.gaffer.time.RBMBackedTimestampSet)2 ReservoirLongsUnion (com.yahoo.sketches.sampling.ReservoirLongsUnion)1 Test (org.junit.jupiter.api.Test)1 JSONSerialisationTest (uk.gov.gchq.gaffer.JSONSerialisationTest)1 BoundedTimestampSet (uk.gov.gchq.gaffer.time.BoundedTimestampSet)1 LongTimeSeries (uk.gov.gchq.gaffer.time.LongTimeSeries)1