Search in sources :

Example 1 with ReservoirLongsSketch

use of com.yahoo.sketches.sampling.ReservoirLongsSketch in project Gaffer by gchq.

the class ReservoirLongsUnionSerialiserTest method testSerialiser.

private void testSerialiser(final ReservoirLongsUnion union) {
    final ReservoirLongsSketch result = union.getResult();
    final boolean resultIsNull = result == null;
    long[] sample = new long[] {};
    if (!resultIsNull) {
        sample = union.getResult().getSamples();
    }
    final byte[] unionSerialised;
    try {
        unionSerialised = SERIALISER.serialise(union);
    } catch (final SerialisationException exception) {
        fail("A SerialisationException occurred");
        return;
    }
    final ReservoirLongsUnion unionDeserialised;
    try {
        unionDeserialised = SERIALISER.deserialise(unionSerialised);
    } catch (final SerialisationException exception) {
        fail("A SerialisationException occurred");
        return;
    }
    final ReservoirLongsSketch deserialisedResult = unionDeserialised.getResult();
    if (deserialisedResult == null) {
        assertTrue(resultIsNull);
    } else {
        assertArrayEquals(sample, unionDeserialised.getResult().getSamples());
    }
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) ReservoirLongsUnion(com.yahoo.sketches.sampling.ReservoirLongsUnion) ReservoirLongsSketch(com.yahoo.sketches.sampling.ReservoirLongsSketch)

Example 2 with ReservoirLongsSketch

use of com.yahoo.sketches.sampling.ReservoirLongsSketch in project Gaffer by gchq.

the class ReservoirLongUnionAggregatorTest method testAggregate.

@Test
public void testAggregate() {
    final ReservoirLongsUnionAggregator unionAggregator = new ReservoirLongsUnionAggregator();
    unionAggregator.init();
    unionAggregator._aggregate(union1);
    ReservoirLongsSketch currentState = unionAggregator._state().getResult();
    assertEquals(3L, currentState.getN());
    assertEquals(3, currentState.getNumSamples());
    // As less items have been added than the capacity, the sample should exactly match what was added.
    Set<Long> samples = new HashSet<>(Arrays.asList(ArrayUtils.toObject(currentState.getSamples())));
    Set<Long> expectedSamples = new HashSet<>();
    expectedSamples.add(1L);
    expectedSamples.add(2L);
    expectedSamples.add(3L);
    assertEquals(expectedSamples, samples);
    unionAggregator._aggregate(union2);
    currentState = unionAggregator._state().getResult();
    assertEquals(99L, currentState.getN());
    assertEquals(20L, currentState.getNumSamples());
    // As more items have been added than the capacity, we can't know exactly what items will be present
    // in the sample but we can check that they are all from the set of things we added.
    samples = new HashSet<>(Arrays.asList(ArrayUtils.toObject(currentState.getSamples())));
    for (long l = 4L; l < 100; l++) {
        expectedSamples.add(l);
    }
    assertTrue(expectedSamples.containsAll(samples));
}
Also used : ReservoirLongsSketch(com.yahoo.sketches.sampling.ReservoirLongsSketch) HashSet(java.util.HashSet) Test(org.junit.Test) AggregateFunctionTest(uk.gov.gchq.gaffer.function.AggregateFunctionTest)

Aggregations

ReservoirLongsSketch (com.yahoo.sketches.sampling.ReservoirLongsSketch)2 ReservoirLongsUnion (com.yahoo.sketches.sampling.ReservoirLongsUnion)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1 SerialisationException (uk.gov.gchq.gaffer.exception.SerialisationException)1 AggregateFunctionTest (uk.gov.gchq.gaffer.function.AggregateFunctionTest)1