From cd247e8d64b2ef0aed7f0afdccd711008cd60fcd Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Sun, 12 Jan 2020 22:25:09 +0100 Subject: Add default queries for contacts and cardinality --- .../edu/mimuw/cloudatlas/agent/modules/Stanik.java | 22 ++++++++++++++++++++++ .../mimuw/cloudatlas/agent/modules/StanikTest.java | 9 +++++---- 2 files changed, 27 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java index a428232..6e7d4dc 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java @@ -2,6 +2,7 @@ package pl.edu.mimuw.cloudatlas.agent.modules; import java.nio.file.Path; import java.util.*; +import java.util.AbstractMap.SimpleImmutableEntry; import java.util.Map.Entry; import pl.edu.mimuw.cloudatlas.agent.messages.*; @@ -30,6 +31,24 @@ public class Stanik extends Module { this.freshnessPeriod = freshnessPeriod; this.contactsTimestamp = ValueUtils.currentTime(); this.contacts = new HashSet<>(); + setDefaultQueries(); + } + + private void setDefaultQueries() { + String cardinalityQuery = "SELECT sum(cardinality) AS cardinality"; + String contactsQuery = "SELECT random(5, unfold(contacts)) AS contacts"; + + setDefaultQuery("&cardinality", cardinalityQuery); + setDefaultQuery("&contacts", contactsQuery); + } + + private void setDefaultQuery(String name, String query) { + try { + ValueQuery queryValue = new ValueQuery(query); + queries.put(new Attribute(name), new SimpleImmutableEntry(queryValue, new ValueTime(0l))); + } catch (Exception e) { + System.out.println("ERROR: failed to compile default query"); + } } public Stanik(PathName ourPath) { @@ -90,6 +109,9 @@ public class Stanik extends Module { if (ValueUtils.isPrefix(zmi.getPathName(), ourPath)) { zmi.getAttributes().addOrChange("owner", new ValueString(ourPath.toString())); } + if (zmi.getPathName().equals(ourPath)) { + zmi.getAttributes().addOrChange("cardinality", new ValueInt(1l)); + } for (ZMI son : zmi.getSons()) { addValuesRecursive(son, level + 1); } diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java index d16d917..92ba051 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java @@ -61,7 +61,7 @@ public class StanikTest { assertEquals(new ValueInt(0l), zmi.getAttributes().getOrNull("level")); assertEquals(new ValueString("/new"), zmi.getAttributes().getOrNull("owner")); Map> queries = stateMessage.getQueries(); - assertEquals(0, TestUtil.iterableSize(queries.keySet())); + assertEquals(2, TestUtil.iterableSize(queries.keySet())); } @Test @@ -127,11 +127,12 @@ public class StanikTest { StateMessage newReceivedMessage = (StateMessage) executor.messagesToPass.poll(); AttributesMap actualAttributes = newReceivedMessage.getZMI().findDescendant("/new").getAttributes(); - assertEquals(6, TestUtil.iterableSize(actualAttributes)); + assertEquals(7, TestUtil.iterableSize(actualAttributes)); assertEquals(new ValueInt(1337l), actualAttributes.getOrNull("foo")); assertEquals(new ValueString("baz"), actualAttributes.getOrNull("bar")); assertEquals(new ValueString("new"), actualAttributes.getOrNull("name")); assertEquals(new ValueString("/new"), actualAttributes.getOrNull("owner")); + assertEquals(new ValueInt(1l), actualAttributes.getOrNull("cardinality")); assertEquals(testTime, actualAttributes.getOrNull("timestamp")); assertEquals(new ValueInt(1l), actualAttributes.getOrNull("level")); } @@ -223,7 +224,7 @@ public class StanikTest { stanik.handleTyped(message); HashMap> actualQueries = stanik.getQueries(); - assertEquals(1, TestUtil.iterableSize(actualQueries.keySet())); + assertEquals(3, TestUtil.iterableSize(actualQueries.keySet())); assertTrue(actualQueries.containsKey(new Attribute("&query"))); Entry timestampedQuery = actualQueries.get(new Attribute("&query")); assertEquals(new ValueTime(42l), timestampedQuery.getValue()); @@ -247,7 +248,7 @@ public class StanikTest { stanik.handleTyped(otherMessage); HashMap> actualQueries = stanik.getQueries(); - assertEquals(4, TestUtil.iterableSize(actualQueries.keySet())); + assertEquals(6, TestUtil.iterableSize(actualQueries.keySet())); assertTrue(actualQueries.containsKey(new Attribute("&query1"))); assertTrue(actualQueries.containsKey(new Attribute("&query2"))); assertTrue(actualQueries.containsKey(new Attribute("&query3"))); -- cgit v1.2.3