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;
}
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);
}
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);
}
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);
}
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;
}
Aggregations