m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2019-12-30 11:46:07 +0100
committerMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2019-12-30 13:18:02 +0100
commitc6f148b33422f0d1660a4289f413193011736475 (patch)
tree9a322a93a6786002a7324a1e17e7da6c0d493eb4
parent0e9ed16b0a048266b56d1238bd7a8a31e59fdac5 (diff)
Test multiple queries in Qurnik
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java5
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java18
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/model/AttributesUtil.java21
-rw-r--r--src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/QurnikTest.java125
4 files changed, 148 insertions, 21 deletions
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java
index 3864aba..9b7268a 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java
@@ -15,6 +15,7 @@ import pl.edu.mimuw.cloudatlas.interpreter.InterpreterException;
import pl.edu.mimuw.cloudatlas.interpreter.QueryResult;
import pl.edu.mimuw.cloudatlas.model.Attribute;
import pl.edu.mimuw.cloudatlas.model.AttributesMap;
+import pl.edu.mimuw.cloudatlas.model.AttributesUtil;
import pl.edu.mimuw.cloudatlas.model.PathName;
import pl.edu.mimuw.cloudatlas.model.TypePrimitive;
import pl.edu.mimuw.cloudatlas.model.Value;
@@ -92,9 +93,11 @@ public class Qurnik extends Module {
if (!currentPath.toString().equals("/")) {
newAttributes.add("name", new ValueString(currentPath.getSingletonName()));
}
- long currentTime = System.currentTimeMillis() / 1000;
+ long currentTime = System.currentTimeMillis();
newAttributes.add("timestamp", new ValueTime(currentTime));
+ AttributesUtil.transferAttributes(newAttributes, zmi.getAttributes());
+
UpdateAttributesMessage message = new UpdateAttributesMessage("", currentTime, currentPath.toString(), newAttributes);
sendMessage(message);
}
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 e8721b3..4694219 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
@@ -12,6 +12,7 @@ import pl.edu.mimuw.cloudatlas.agent.messages.UpdateAttributesMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.UpdateQueriesMessage;
import pl.edu.mimuw.cloudatlas.model.Attribute;
import pl.edu.mimuw.cloudatlas.model.AttributesMap;
+import pl.edu.mimuw.cloudatlas.model.AttributesUtil;
import pl.edu.mimuw.cloudatlas.model.PathName;
import pl.edu.mimuw.cloudatlas.model.Type;
import pl.edu.mimuw.cloudatlas.model.TypePrimitive;
@@ -67,7 +68,7 @@ public class Stanik extends Module {
ZMI zone = hierarchy.findDescendant(message.getPathName());
AttributesMap attributes = zone.getAttributes();
if (valueLower(attributes.get("timestamp"), message.getAttributes().get("timestamp"))) {
- transferAttributes(message.getAttributes(), attributes);
+ AttributesUtil.transferAttributes(message.getAttributes(), attributes);
} else {
System.out.println("DEBUG: not applying update with older attributes");
}
@@ -127,21 +128,6 @@ public class Stanik extends Module {
return value != null && !value.isNull() && value.getType().isCompatible(type);
}
- private void transferAttributes(AttributesMap fromAttributes, AttributesMap toAttributes) {
- Iterator<Entry<Attribute, Value>> iterator = toAttributes.iterator();
- while (iterator.hasNext()) {
- Entry<Attribute, Value> entry = iterator.next();
- Attribute attribute = entry.getKey();
- Value newValue = fromAttributes.getOrNull(attribute);
- if (newValue == null) {
- iterator.remove();
- }
- }
- for (Entry<Attribute, Value> entry : fromAttributes) {
- toAttributes.addOrChange(entry.getKey(), entry.getValue());
- }
- }
-
private void addMissingZones(PathName path) {
try {
if (!hierarchy.descendantExists(path)) {
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/AttributesUtil.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/AttributesUtil.java
new file mode 100644
index 0000000..cd2ae91
--- /dev/null
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/AttributesUtil.java
@@ -0,0 +1,21 @@
+package pl.edu.mimuw.cloudatlas.model;
+
+import java.util.Iterator;
+import java.util.Map.Entry;
+
+public class AttributesUtil {
+ public static void transferAttributes(AttributesMap fromAttributes, AttributesMap toAttributes) {
+ Iterator<Entry<Attribute, Value>> iterator = toAttributes.iterator();
+ while (iterator.hasNext()) {
+ Entry<Attribute, Value> entry = iterator.next();
+ Attribute attribute = entry.getKey();
+ Value newValue = fromAttributes.getOrNull(attribute);
+ if (newValue == null) {
+ iterator.remove();
+ }
+ }
+ for (Entry<Attribute, Value> entry : fromAttributes) {
+ toAttributes.addOrChange(entry.getKey(), entry.getValue());
+ }
+ }
+}
diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/QurnikTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/QurnikTest.java
index 28f316c..0c10092 100644
--- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/QurnikTest.java
+++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/QurnikTest.java
@@ -63,9 +63,9 @@ public class QurnikTest {
)
);
StateMessage message = new StateMessage("", ModuleType.QUERY, 0, 0, root, queries);
- long timeBefore = System.currentTimeMillis() / 1000;
+ long timeBefore = System.currentTimeMillis();
qurnik.handleTyped(message);
- long timeAfter = System.currentTimeMillis() / 1000;
+ long timeAfter = System.currentTimeMillis();
assertEquals(1, executor.messagesToPass.size());
AgentMessage receivedMessage = (AgentMessage) executor.messagesToPass.take();
@@ -77,10 +77,127 @@ public class QurnikTest {
AttributesMap updatedAttributes = updateAttributesMessage.getAttributes();
assertEquals(2, TestUtil.iterableSize(updatedAttributes));
assertEquals(new ValueInt(1l), updatedAttributes.getOrNull("one"));
- System.out.println(timeBefore);
- System.out.println(updatedAttributes.getOrNull("timestamp"));
long timestamp = ((ValueTime) updatedAttributes.getOrNull("timestamp")).getValue();
assertTrue(timeBefore <= timestamp);
assertTrue(timestamp <= timeAfter);
}
+
+ private ZMI setupSampleHierarchy() {
+ ZMI root = new ZMI();
+
+ ZMI uw = new ZMI(root);
+ root.addSon(uw);
+ ZMI pw = new ZMI(root);
+ root.addSon(pw);
+
+ ZMI uw1 = new ZMI(uw);
+ uw.addSon(uw1);
+ ZMI uw2 = new ZMI(uw);
+ uw.addSon(uw2);
+
+ AttributesMap uwAttributes = uw.getAttributes();
+ uwAttributes.add("name", new ValueString("uw"));
+
+ AttributesMap pwAttributes = pw.getAttributes();
+ pwAttributes.add("name", new ValueString("pw"));
+ pwAttributes.add("x", new ValueInt(42l));
+ pwAttributes.add("y", new ValueInt(250l));
+ pwAttributes.add("z", new ValueInt(5l));
+
+ AttributesMap uw1Attributes = uw1.getAttributes();
+ uw1Attributes.add("name", new ValueString("uw1"));
+ uw1Attributes.add("x", new ValueInt(12l));
+ uw1Attributes.add("y", new ValueInt(100l));
+ uw1Attributes.add("a", new ValueInt(123l));
+
+ AttributesMap uw2Attributes = uw2.getAttributes();
+ uw2Attributes.add("name", new ValueString("uw2"));
+ uw2Attributes.add("x", new ValueInt(13l));
+ uw2Attributes.add("a", new ValueInt(134l));
+ uw2Attributes.add("b", new ValueInt(777l));
+
+ return root;
+ }
+
+ public Map<Attribute, Entry<ValueQuery, ValueTime>> setupSampleQueries() throws Exception {
+ Map<Attribute, Entry<ValueQuery, ValueTime>> queries = new HashMap();
+
+ queries.put(
+ new Attribute("&query1"),
+ new SimpleImmutableEntry(
+ new ValueQuery("SELECT sum(x) AS x"),
+ new ValueTime(0l)
+ )
+ );
+ queries.put(
+ new Attribute("&query2"),
+ new SimpleImmutableEntry(
+ new ValueQuery("SELECT min(y) AS y"),
+ new ValueTime(0l)
+ )
+ );
+ queries.put(
+ new Attribute("&query3"),
+ new SimpleImmutableEntry(
+ new ValueQuery("SELECT max(z) AS z"),
+ new ValueTime(0l)
+ )
+ );
+ queries.put(
+ new Attribute("&query4"),
+ new SimpleImmutableEntry(
+ new ValueQuery("SELECT sum(a + 1) AS a"),
+ new ValueTime(0l)
+ )
+ );
+ queries.put(
+ new Attribute("&query5"),
+ new SimpleImmutableEntry(
+ new ValueQuery("SELECT sum(2 * b) AS b"),
+ new ValueTime(0l)
+ )
+ );
+
+ return queries;
+ }
+
+ @Test
+ public void multipleQueries() throws Exception {
+ ZMI root = setupSampleHierarchy();
+
+ Map<Attribute, Entry<ValueQuery, ValueTime>> queries = setupSampleQueries();
+ StateMessage message = new StateMessage("", ModuleType.QUERY, 0, 0, root, queries);
+ long timeBefore = System.currentTimeMillis();
+ qurnik.handleTyped(message);
+ long timeAfter = System.currentTimeMillis();
+
+ assertEquals(2, executor.messagesToPass.size());
+
+ UpdateAttributesMessage message1 = (UpdateAttributesMessage) executor.messagesToPass.take();
+ assertEquals("/uw", message1.getPathName());
+ AttributesMap attributes1 = message1.getAttributes();
+ assertEquals(6, TestUtil.iterableSize(attributes1));
+ assertEquals(new ValueString("uw"), attributes1.getOrNull("name"));
+ assertEquals(new ValueInt(25l), attributes1.getOrNull("x"));
+ assertEquals(new ValueInt(100l), attributes1.getOrNull("y"));
+ assertEquals(new ValueInt(259l), attributes1.getOrNull("a"));
+ assertEquals(new ValueInt(1554l), attributes1.getOrNull("b"));
+ long timestamp1 = ((ValueTime) attributes1.getOrNull("timestamp")).getValue();
+ assertTrue(timeBefore <= timestamp1);
+ assertTrue(timestamp1 <= timeAfter);
+
+ UpdateAttributesMessage message2 = (UpdateAttributesMessage) executor.messagesToPass.take();
+ assertEquals("/", message2.getPathName());
+ AttributesMap attributes2 = message2.getAttributes();
+ System.out.println("got attributes " + attributes2.toString());
+ assertEquals(6, TestUtil.iterableSize(attributes2));
+ assertEquals(new ValueInt(67l), attributes2.getOrNull("x"));
+ assertEquals(new ValueInt(100l), attributes2.getOrNull("y"));
+ assertEquals(new ValueInt(5l), attributes2.getOrNull("z"));
+ assertEquals(new ValueInt(260l), attributes2.getOrNull("a"));
+ assertEquals(new ValueInt(3108l), attributes2.getOrNull("b"));
+ long timestamp2 = ((ValueTime) attributes2.getOrNull("timestamp")).getValue();
+ assertTrue(timeBefore <= timestamp2);
+ assertTrue(timestamp2 <= timeAfter);
+ }
}