Search in sources :

Example 21 with HyperLogLogPlus

use of com.clearspring.analytics.stream.cardinality.HyperLogLogPlus in project Gaffer by gchq.

the class HyperLogLogPlusJsonDeserialiser method deserialize.

@Override
public HyperLogLogPlus deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    TreeNode treeNode = jsonParser.getCodec().readTree(jsonParser);
    final TreeNode nestedHllp = treeNode.get("hyperLogLogPlus");
    if (nonNull(nestedHllp)) {
        treeNode = nestedHllp;
    }
    final HyperLogLogPlus hllp;
    final TextNode jsonNodes = (TextNode) treeNode.get(HyperLogLogPlusJsonConstants.HYPER_LOG_LOG_PLUS_SKETCH_BYTES_FIELD);
    if (isNull(jsonNodes)) {
        final IntNode pNode = (IntNode) treeNode.get("p");
        final IntNode spNode = (IntNode) treeNode.get("sp");
        final int p = nonNull(pNode) ? pNode.asInt(DEFAULT_P) : DEFAULT_P;
        final int sp = nonNull(spNode) ? spNode.asInt(DEFAULT_SP) : DEFAULT_SP;
        hllp = new HyperLogLogPlus(p, sp);
    } else {
        hllp = HyperLogLogPlus.Builder.build(jsonNodes.binaryValue());
    }
    final ArrayNode offers = (ArrayNode) treeNode.get("offers");
    if (nonNull(offers)) {
        for (final JsonNode offer : offers) {
            if (nonNull(offer)) {
                hllp.offer(offer.asText());
            }
        }
    }
    return hllp;
}
Also used : IntNode(com.fasterxml.jackson.databind.node.IntNode) HyperLogLogPlus(com.clearspring.analytics.stream.cardinality.HyperLogLogPlus) TreeNode(com.fasterxml.jackson.core.TreeNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 22 with HyperLogLogPlus

use of com.clearspring.analytics.stream.cardinality.HyperLogLogPlus in project Gaffer by gchq.

the class HyperLogLogPlusEntityGeneratorTest method shouldCreateSimpleEntitiesWithProperties.

@Test
public void shouldCreateSimpleEntitiesWithProperties() {
    // Given
    HyperLogLogPlusEntityGenerator hyperLogLogPlusEntityGenerator = new HyperLogLogPlusEntityGenerator();
    hyperLogLogPlusEntityGenerator.propertyToCopy(PROP1);
    Edge edge = new Edge.Builder().group(TestGroups.ENTITY).source(A).dest(B).property(PROP1, VALUE1).build();
    List<? extends Element> edges = Arrays.asList(edge);
    // When
    Iterable<? extends Element> elements = hyperLogLogPlusEntityGenerator.apply(edges);
    // Then
    Iterator<? extends Element> elementIterator = elements.iterator();
    Edge edgeResult = (Edge) elementIterator.next();
    Entity entityResultA = (Entity) elementIterator.next();
    Entity entityResultB = (Entity) elementIterator.next();
    assertThat(elementIterator.hasNext()).isFalse();
    assertThat(edgeResult).isEqualTo(edge);
    assertThat(entityResultA.getGroup()).isEqualTo(DEFAULT_ENTITY_GROUP);
    assertThat(entityResultA.getVertex()).isEqualTo(A);
    HyperLogLogPlus entityCardinalityA = (HyperLogLogPlus) entityResultA.getProperty(DEFAULT_PROPERTY_NAME);
    assertThat(entityCardinalityA.cardinality()).isEqualTo(1);
    assertThat(entityResultA.getProperty(PROP1)).isEqualTo(VALUE1);
    assertThat(entityResultB.getGroup()).isEqualTo(DEFAULT_ENTITY_GROUP);
    assertThat(entityResultB.getVertex()).isEqualTo(B);
    HyperLogLogPlus entityCardinalityB = (HyperLogLogPlus) entityResultB.getProperty(DEFAULT_PROPERTY_NAME);
    assertThat(entityCardinalityB.cardinality()).isEqualTo(1);
    assertThat(entityResultB.getProperty(PROP1)).isEqualTo(VALUE1);
}
Also used : Entity(uk.gov.gchq.gaffer.data.element.Entity) HyperLogLogPlus(com.clearspring.analytics.stream.cardinality.HyperLogLogPlus) Edge(uk.gov.gchq.gaffer.data.element.Edge) Test(org.junit.jupiter.api.Test)

Example 23 with HyperLogLogPlus

use of com.clearspring.analytics.stream.cardinality.HyperLogLogPlus in project Gaffer by gchq.

the class IterableToHyperLogLogPlusTest method shouldCreateEmptyWhenNull.

@Test
public void shouldCreateEmptyWhenNull() {
    // Given
    IterableToHyperLogLogPlus iterableToHyperLogLogPlus = new IterableToHyperLogLogPlus();
    // When
    HyperLogLogPlus result = iterableToHyperLogLogPlus.apply(null);
    // Then
    assertThat(result.cardinality()).isEqualTo(0);
}
Also used : HyperLogLogPlus(com.clearspring.analytics.stream.cardinality.HyperLogLogPlus) Test(org.junit.jupiter.api.Test) FunctionTest(uk.gov.gchq.koryphe.function.FunctionTest)

Example 24 with HyperLogLogPlus

use of com.clearspring.analytics.stream.cardinality.HyperLogLogPlus in project Gaffer by gchq.

the class IterableToHyperLogLogPlusTest method shouldCreateHllSketch.

@Test
public void shouldCreateHllSketch() {
    // Given
    IterableToHyperLogLogPlus iterableToHyperLogLogPlus = new IterableToHyperLogLogPlus();
    List<Object> input = Arrays.asList("one", "two", "three", "four", "five");
    // When
    HyperLogLogPlus result = iterableToHyperLogLogPlus.apply(input);
    // Then
    assertThat(result.cardinality()).isEqualTo(5);
}
Also used : HyperLogLogPlus(com.clearspring.analytics.stream.cardinality.HyperLogLogPlus) Test(org.junit.jupiter.api.Test) FunctionTest(uk.gov.gchq.koryphe.function.FunctionTest)

Example 25 with HyperLogLogPlus

use of com.clearspring.analytics.stream.cardinality.HyperLogLogPlus in project Gaffer by gchq.

the class GetDataFrameOfElementsHandlerTest method getElementsWithNonStandardProperties.

private static List<Element> getElementsWithNonStandardProperties() {
    final List<Element> elements = new ArrayList<>();
    final FreqMap freqMap = new FreqMap();
    freqMap.put("W", 10L);
    freqMap.put("X", 100L);
    final HyperLogLogPlus hllpp = new HyperLogLogPlus(5, 5);
    hllpp.offer("AAA");
    final Entity entity = new Entity.Builder().group(TestGroups.ENTITY).vertex("A").property("freqMap", freqMap).property("hllpp", hllpp).build();
    elements.add(entity);
    final Edge edge = new Edge.Builder().group(TestGroups.EDGE).source("B").dest("C").directed(true).build();
    final FreqMap freqMap2 = new FreqMap();
    freqMap2.put("Y", 1000L);
    freqMap2.put("Z", 10000L);
    edge.putProperty("freqMap", freqMap2);
    final HyperLogLogPlus hllpp2 = new HyperLogLogPlus(5, 5);
    hllpp2.offer("AAA");
    hllpp2.offer("BBB");
    edge.putProperty("hllpp", hllpp2);
    elements.add(edge);
    return elements;
}
Also used : Entity(uk.gov.gchq.gaffer.data.element.Entity) FreqMap(uk.gov.gchq.gaffer.types.FreqMap) HyperLogLogPlus(com.clearspring.analytics.stream.cardinality.HyperLogLogPlus) Element(uk.gov.gchq.gaffer.data.element.Element) ArrayList(java.util.ArrayList) Edge(uk.gov.gchq.gaffer.data.element.Edge)

Aggregations

HyperLogLogPlus (com.clearspring.analytics.stream.cardinality.HyperLogLogPlus)58 Test (org.junit.jupiter.api.Test)19 Test (org.junit.Test)14 Entity (uk.gov.gchq.gaffer.data.element.Entity)8 AggregateFunctionTest (uk.gov.gchq.gaffer.function.AggregateFunctionTest)6 User (uk.gov.gchq.gaffer.user.User)6 Edge (uk.gov.gchq.gaffer.data.element.Edge)5 Element (uk.gov.gchq.gaffer.data.element.Element)5 Graph (uk.gov.gchq.gaffer.graph.Graph)5 FunctionTest (uk.gov.gchq.koryphe.function.FunctionTest)5 ArrayList (java.util.ArrayList)4 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)4 HashSet (java.util.HashSet)3 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)3 SerialisationException (uk.gov.gchq.gaffer.exception.SerialisationException)3 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)3 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)3 GetAllElements (uk.gov.gchq.gaffer.operation.impl.get.GetAllElements)3 CardinalityMergeException (com.clearspring.analytics.stream.cardinality.CardinalityMergeException)2 TreeNode (com.fasterxml.jackson.core.TreeNode)2