Search in sources :

Example 6 with BoundedTimestampSet

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

the class BoundedTimestampSetAggregatorTest method testAggregateWhenBothInNotFullState.

@Test
public void testAggregateWhenBothInNotFullState() {
    // Given
    final BoundedTimestampSet boundedTimestampSet1 = new BoundedTimestampSet(TimeBucket.SECOND, 10);
    boundedTimestampSet1.add(Instant.ofEpochMilli(1000L));
    boundedTimestampSet1.add(Instant.ofEpochMilli(1000000L));
    final BoundedTimestampSet boundedTimestampSet2 = new BoundedTimestampSet(TimeBucket.SECOND, 10);
    boundedTimestampSet2.add(Instant.ofEpochMilli(1000L));
    boundedTimestampSet2.add(Instant.ofEpochMilli(2000000L));
    // When
    final BoundedTimestampSet aggregated = BOUNDED_TIMESTAMP_SET_AGGREGATOR._apply(boundedTimestampSet1, boundedTimestampSet2);
    final BoundedTimestampSet expected = new BoundedTimestampSet(TimeBucket.SECOND, 10);
    expected.add(Instant.ofEpochMilli(1000L));
    expected.add(Instant.ofEpochMilli(1000000L));
    expected.add(Instant.ofEpochMilli(2000000L));
    // Then
    assertEquals(3, aggregated.getNumberOfTimestamps());
    assertEquals(BoundedTimestampSet.State.NOT_FULL, aggregated.getState());
    assertEquals(expected.getTimestamps(), aggregated.getTimestamps());
}
Also used : BoundedTimestampSet(uk.gov.gchq.gaffer.time.BoundedTimestampSet) Test(org.junit.jupiter.api.Test)

Example 7 with BoundedTimestampSet

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

the class BoundedTimestampSetSerialiserTest method getExampleValue.

private BoundedTimestampSet getExampleValue() {
    final BoundedTimestampSet boundedTimestampSet = new BoundedTimestampSet(TimeBucket.SECOND, 10);
    boundedTimestampSet.add(Instant.ofEpochMilli(1000L));
    boundedTimestampSet.add(Instant.ofEpochMilli(1000000L));
    return boundedTimestampSet;
}
Also used : BoundedTimestampSet(uk.gov.gchq.gaffer.time.BoundedTimestampSet)

Example 8 with BoundedTimestampSet

use of uk.gov.gchq.gaffer.time.BoundedTimestampSet 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 9 with BoundedTimestampSet

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

the class BoundedTimestampSetAggregatorTest method testCantMergeIfDifferentMaxSize.

@Test
public void testCantMergeIfDifferentMaxSize() {
    try {
        final BoundedTimestampSet boundedTimestampSet1 = new BoundedTimestampSet(TimeBucket.SECOND, 10);
        final BoundedTimestampSet boundedTimestampSet2 = new BoundedTimestampSet(TimeBucket.MINUTE, 11);
        BOUNDED_TIMESTAMP_SET_AGGREGATOR._apply(boundedTimestampSet1, boundedTimestampSet2);
    } catch (final RuntimeException e) {
    // Expected
    }
}
Also used : BoundedTimestampSet(uk.gov.gchq.gaffer.time.BoundedTimestampSet) Test(org.junit.jupiter.api.Test)

Example 10 with BoundedTimestampSet

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

the class BoundedTimestampSetSerialiserTest method testSerialiserWhenNotFull.

@Test
public void testSerialiserWhenNotFull() throws SerialisationException {
    // Given
    final BoundedTimestampSet boundedTimestampSet = getExampleValue();
    // When
    final byte[] serialised = serialiser.serialise(boundedTimestampSet);
    final BoundedTimestampSet deserialised = serialiser.deserialise(serialised);
    // Then
    assertEquals(boundedTimestampSet.getState(), deserialised.getState());
    assertEquals(boundedTimestampSet.getTimeBucket(), deserialised.getTimeBucket());
    assertEquals(boundedTimestampSet.getMaxSize(), deserialised.getMaxSize());
    assertEquals(boundedTimestampSet.getNumberOfTimestamps(), deserialised.getNumberOfTimestamps());
    assertEquals(boundedTimestampSet.getTimestamps(), deserialised.getTimestamps());
}
Also used : BoundedTimestampSet(uk.gov.gchq.gaffer.time.BoundedTimestampSet) Test(org.junit.jupiter.api.Test) ToBytesSerialisationTest(uk.gov.gchq.gaffer.serialisation.ToBytesSerialisationTest)

Aggregations

BoundedTimestampSet (uk.gov.gchq.gaffer.time.BoundedTimestampSet)11 Test (org.junit.jupiter.api.Test)8 Instant (java.time.Instant)4 HashSet (java.util.HashSet)4 ToBytesSerialisationTest (uk.gov.gchq.gaffer.serialisation.ToBytesSerialisationTest)2 ReservoirLongsUnion (com.yahoo.sketches.sampling.ReservoirLongsUnion)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 RoaringBitmap (org.roaringbitmap.RoaringBitmap)1 Edge (uk.gov.gchq.gaffer.data.element.Edge)1 Element (uk.gov.gchq.gaffer.data.element.Element)1 SerialisationException (uk.gov.gchq.gaffer.exception.SerialisationException)1 TimeBucket (uk.gov.gchq.gaffer.time.CommonTimeUtil.TimeBucket)1 RBMBackedTimestampSet (uk.gov.gchq.gaffer.time.RBMBackedTimestampSet)1 TimestampSet (uk.gov.gchq.gaffer.time.TimestampSet)1