m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin <marcin.j.chrzanowski@gmail.com>2019-11-19 12:53:14 +0100
committerGitHub <noreply@github.com>2019-11-19 12:53:14 +0100
commit11df9be19067a6567b499d37cba07e41d21b3c52 (patch)
treeb7b4887ce1f44a805f93b113939c29dcb6d39c69
parent3de1798d35334b7bad2a1c9d49ca4a50f2762bf4 (diff)
Run queries (#19)
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java42
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/NoopStream.java7
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Main.java22
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java4
-rw-r--r--src/test/java/pl/edu/mimuw/cloudatlas/agent/AgentIntegrationTest.java (renamed from src/test/java/pl/edu/mimuw/cloudatlas/agent/AgentTest.java)12
-rw-r--r--src/test/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementationTests.java145
6 files changed, 223 insertions, 9 deletions
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java
index 12682a6..4ac6f5c 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java
@@ -1,10 +1,18 @@
package pl.edu.mimuw.cloudatlas.agent;
+import java.io.PrintStream;
+
import java.rmi.RemoteException;
-import java.util.Set;
+import java.util.List;
import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import pl.edu.mimuw.cloudatlas.interpreter.Interpreter;
+import pl.edu.mimuw.cloudatlas.interpreter.InterpreterException;
+import pl.edu.mimuw.cloudatlas.interpreter.Main;
+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.PathName;
@@ -13,6 +21,7 @@ import pl.edu.mimuw.cloudatlas.model.Value;
import pl.edu.mimuw.cloudatlas.model.ValueQuery;
import pl.edu.mimuw.cloudatlas.model.ValueSet;
import pl.edu.mimuw.cloudatlas.model.ValueNull;
+import pl.edu.mimuw.cloudatlas.model.Type;
import pl.edu.mimuw.cloudatlas.model.TypePrimitive;
import pl.edu.mimuw.cloudatlas.model.ZMI;
import pl.edu.mimuw.cloudatlas.api.Api;
@@ -53,6 +62,7 @@ public class ApiImplementation implements Api {
ValueQuery query = new ValueQuery(queryCode);
Attribute attributeName = new Attribute(name);
installQueryInHierarchy(root, attributeName, query);
+ executeAllQueries(root);
} catch (Exception e) {
throw new RemoteException("Failed to install query", e);
}
@@ -84,11 +94,41 @@ public class ApiImplementation implements Api {
try {
ZMI zmi = root.findDescendant(new PathName(zoneName));
zmi.getAttributes().addOrChange(new Attribute(attributeName), value);
+ executeAllQueries(root);
} catch (ZMI.NoSuchZoneException e) {
throw new RemoteException("Zone not found", e);
}
}
+ private void executeAllQueries(ZMI zmi) {
+ if(!zmi.getSons().isEmpty()) {
+ for(ZMI son : zmi.getSons()) {
+ executeAllQueries(son);
+ }
+
+ Interpreter interpreter = new Interpreter(zmi);
+ for (ValueQuery query : getQueries(zmi)) {
+ try {
+ List<QueryResult> result = interpreter.interpretProgram(query.getQuery());
+ for(QueryResult r : result) {
+ zmi.getAttributes().addOrChange(r.getName(), r.getValue());
+ }
+ } catch(InterpreterException exception) {}
+ }
+ }
+ }
+
+ private Set<ValueQuery> getQueries(ZMI zmi) {
+ Set<ValueQuery> querySet = new HashSet<ValueQuery>();
+ for (Map.Entry<Attribute, Value> attribute : zmi.getAttributes()) {
+ if (attribute.getValue().getType().getPrimaryType() == Type.PrimaryType.QUERY) {
+ querySet.add((ValueQuery) attribute.getValue());
+ }
+ }
+
+ return querySet;
+ }
+
public void setFallbackContacts(Set<ValueContact> contacts) throws RemoteException {
this.contacts = contacts;
}
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/NoopStream.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/NoopStream.java
new file mode 100644
index 0000000..cbd4b91
--- /dev/null
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/NoopStream.java
@@ -0,0 +1,7 @@
+package pl.edu.mimuw.cloudatlas.agent;
+
+import java.io.OutputStream;
+
+class NoopOutputStream extends OutputStream {
+ public void write(int b) {}
+}
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Main.java b/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Main.java
index 979aa92..4d54bb3 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Main.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Main.java
@@ -35,6 +35,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
+import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.Program;
import pl.edu.mimuw.cloudatlas.interpreter.query.Yylex;
import pl.edu.mimuw.cloudatlas.interpreter.query.parser;
import pl.edu.mimuw.cloudatlas.model.PathName;
@@ -60,19 +61,26 @@ public class Main {
Scanner scanner = new Scanner(in);
scanner.useDelimiter("\\n");
while(scanner.hasNext()) {
- executeQueries(root, scanner.next(), out);
+ executeQueriesFromSource(root, scanner.next(), out);
}
scanner.close();
}
- private static void executeQueries(ZMI zmi, String query, PrintStream out) throws Exception {
+ private static void executeQueriesFromSource(ZMI zmi, String query, PrintStream out) throws Exception {
+ Yylex lex = new Yylex(new ByteArrayInputStream(query.getBytes()));
+ Program program = (new parser(lex)).pProgram();
+ executeQueries(zmi, program, out);
+ }
+
+ public static void executeQueries(ZMI zmi, Program program, PrintStream out) throws Exception {
if(!zmi.getSons().isEmpty()) {
- for(ZMI son : zmi.getSons())
- executeQueries(son, query, out);
+ for(ZMI son : zmi.getSons()) {
+ executeQueries(son, program, out);
+ }
+
Interpreter interpreter = new Interpreter(zmi);
- Yylex lex = new Yylex(new ByteArrayInputStream(query.getBytes()));
try {
- List<QueryResult> result = interpreter.interpretProgram((new parser(lex)).pProgram());
+ List<QueryResult> result = interpreter.interpretProgram(program);
PathName zone = zmi.getPathName();
for(QueryResult r : result) {
out.println(zone + ": " + r);
@@ -303,6 +311,7 @@ public class Main {
violet07.getAttributes().add("creation", new ValueTime("2011/11/09 20:8:13.123"));
violet07.getAttributes().add("cpu_usage", new ValueDouble(0.9));
violet07.getAttributes().add("num_cores", new ValueInt(3l));
+ violet07.getAttributes().add("num_processes", new ValueInt(131l));
violet07.getAttributes().add("has_ups", new ValueBoolean(null));
list = Arrays.asList(new Value[] {
new ValueString("tola"), new ValueString("tosia"),
@@ -334,6 +343,7 @@ public class Main {
khaki31.getAttributes().add("creation", new ValueTime("2011/11/09 20:12:13.123"));
khaki31.getAttributes().add("cpu_usage", new ValueDouble(null));
khaki31.getAttributes().add("num_cores", new ValueInt(3l));
+ khaki31.getAttributes().add("num_processes", new ValueInt(124l));
khaki31.getAttributes().add("has_ups", new ValueBoolean(false));
list = Arrays.asList(new Value[] {
new ValueString("agatka"), new ValueString("beatka"), new ValueString("celina"),
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java
index d9cbe4c..82e1602 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java
@@ -32,6 +32,10 @@ public class ValueQuery extends Value {
this.query = null;
}
+ public Program getQuery() {
+ return query;
+ }
+
@Override
public Type getType() {
return TypePrimitive.QUERY;
diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/AgentTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/AgentIntegrationTest.java
index 8c9c495..1d7a2c5 100644
--- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/AgentTest.java
+++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/AgentIntegrationTest.java
@@ -21,7 +21,6 @@ import java.util.Set;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
-import java.math.BigDecimal;
import pl.edu.mimuw.cloudatlas.api.Api;
import pl.edu.mimuw.cloudatlas.model.AttributesMap;
@@ -35,7 +34,7 @@ import pl.edu.mimuw.cloudatlas.model.ValueQuery;
import pl.edu.mimuw.cloudatlas.model.ValueString;
import pl.edu.mimuw.cloudatlas.model.ValueTime;
-public class AgentTest {
+public class AgentIntegrationTest {
private static Process registryProcess;
private static Process agentProcess;
@@ -116,6 +115,15 @@ public class AgentTest {
}
@Test
+ public void testInstallQueryRuns() throws Exception {
+ String name = "&query";
+ String queryCode = "SELECT 1 AS one";
+ api.installQuery(name, queryCode);
+ AttributesMap attributes = api.getZoneAttributeValues("/pjwstk");
+ assertEquals(new ValueInt(1l), attributes.get("one"));
+ }
+
+ @Test
public void testUninstallQuery() throws Exception {
String name = "&query";
String queryCode = "SELECT 1 AS one";
diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementationTests.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementationTests.java
new file mode 100644
index 0000000..d98377c
--- /dev/null
+++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementationTests.java
@@ -0,0 +1,145 @@
+package pl.edu.mimuw.cloudatlas.agent;
+
+import org.junit.Before;
+import org.junit.Test;
+import static org.junit.Assert.*;
+import static org.junit.Assert.assertThat;
+import static org.hamcrest.CoreMatchers.hasItems;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+import pl.edu.mimuw.cloudatlas.api.Api;
+import pl.edu.mimuw.cloudatlas.interpreter.Main;
+import pl.edu.mimuw.cloudatlas.model.AttributesMap;
+import pl.edu.mimuw.cloudatlas.model.TypePrimitive;
+import pl.edu.mimuw.cloudatlas.model.Value;
+import pl.edu.mimuw.cloudatlas.model.ValueDouble;
+import pl.edu.mimuw.cloudatlas.model.ValueInt;
+import pl.edu.mimuw.cloudatlas.model.ValueList;
+import pl.edu.mimuw.cloudatlas.model.ValueNull;
+import pl.edu.mimuw.cloudatlas.model.ValueQuery;
+import pl.edu.mimuw.cloudatlas.model.ValueString;
+import pl.edu.mimuw.cloudatlas.model.ValueTime;
+import pl.edu.mimuw.cloudatlas.model.ZMI;
+
+public class ApiImplementationTests {
+ private ApiImplementation api;
+
+ @Before
+ public void initializeApi() throws Exception {
+ ZMI root = Main.createTestHierarchy2();
+ api = new ApiImplementation(root);
+ }
+
+ @Test
+ public void testGetZoneSet() throws Exception {
+ Set<String> set = api.getZoneSet();
+ assertEquals(8, set.size());
+ assertThat(set, hasItems("/"));
+ assertThat(set, hasItems("/uw"));
+ assertThat(set, hasItems("/uw/violet07", "/uw/khaki31", "/uw/khaki13"));
+ assertThat(set, hasItems("/pjwstk"));
+ assertThat(set, hasItems("/pjwstk/whatever01", "/pjwstk/whatever02"));
+ }
+
+ @Test
+ public void testRootGetZoneAttributeValue() throws Exception {
+ AttributesMap rootAttributes = api.getZoneAttributeValues("/");
+ assertEquals(new ValueInt(0l), rootAttributes.get("level"));
+ assertEquals(ValueNull.getInstance(), rootAttributes.get("name"));
+ }
+
+ @Test
+ public void testIntermediateGetZoneAttributeValue() throws Exception {
+ AttributesMap attributes = api.getZoneAttributeValues("/uw");
+ assertEquals(new ValueInt(1l), attributes.get("level"));
+ assertEquals(new ValueString("uw"), attributes.get("name"));
+ }
+
+ @Test
+ public void testLeafGetZoneAttributeValue() throws Exception {
+ AttributesMap attributes = api.getZoneAttributeValues("/pjwstk/whatever01");
+ assertEquals(new ValueInt(2l), attributes.get("level"));
+ assertEquals(new ValueString("whatever01"), attributes.get("name"));
+ assertEquals(new ValueString("/pjwstk/whatever01"), attributes.get("owner"));
+ assertEquals(new ValueTime("2012/11/09 21:12:00.000"), attributes.get("timestamp"));
+ assertEquals(new ValueInt(1l), attributes.get("cardinality"));
+ assertEquals(new ValueTime("2012/10/18 07:03:00.000"), attributes.get("creation"));
+ assertEquals(new ValueDouble(0.1), attributes.get("cpu_usage"));
+ assertEquals(new ValueInt(7l), attributes.get("num_cores"));
+ assertEquals(new ValueInt(215l), attributes.get("num_processes"));
+
+ List<Value> phpModules = new ArrayList<Value>();
+ phpModules.add(new ValueString("rewrite"));
+ assertEquals(new ValueList(phpModules, TypePrimitive.STRING), attributes.get("php_modules"));
+ }
+
+ @Test
+ public void testInstallQuery() throws Exception {
+ String name = "&query";
+ String queryCode = "SELECT 1 AS one";
+ api.installQuery(name, queryCode);
+ assertAttributeInZmiEquals(name, new ValueQuery(queryCode), "/");
+ assertAttributeInZmiEquals(name, new ValueQuery(queryCode), "/uw");
+ assertAttributeInZmiEquals(name, new ValueQuery(queryCode), "/pjwstk");
+ }
+
+ @Test
+ public void testInstallQueryRuns() throws Exception {
+ api.installQuery("&query", "SELECT 1 AS one");
+ assertAttributeInZmiEquals("one", new ValueInt(1l), "/");
+ assertAttributeInZmiEquals("one", new ValueInt(1l), "/uw");
+ assertAttributeInZmiEquals("one", new ValueInt(1l), "/pjwstk");
+ }
+
+ @Test
+ public void testInstallQueryRuns2() throws Exception {
+ api.installQuery("&query", "SELECT sum(num_processes) AS num_processes");
+ assertAttributeInZmiEquals("num_processes", new ValueInt(362l), "/uw");
+ assertAttributeInZmiEquals("num_processes", new ValueInt(437l), "/pjwstk");
+ assertAttributeInZmiEquals("num_processes", new ValueInt(799l), "/");
+ }
+
+ public void assertAttributeInZmiEquals(String attribute, Value expected, String zmiPath) throws Exception {
+ AttributesMap attributes = api.getZoneAttributeValues(zmiPath);
+ assertEquals(expected, attributes.get(attribute));
+ }
+
+ @Test
+ public void testUninstallQuery() throws Exception {
+ String name = "&query";
+ String queryCode = "SELECT 1 AS one";
+ api.installQuery(name, queryCode);
+ api.uninstallQuery(name);
+ AttributesMap attributes = api.getZoneAttributeValues("/pjwstk");
+ assertNull(attributes.getOrNull(name));
+ }
+
+ @Test
+ public void testSetAttributeValueChange() throws Exception {
+ Value numProcesses = new ValueInt(42l);
+ api.setAttributeValue("/uw/khaki13", "num_processes", numProcesses);
+ AttributesMap attributes = api.getZoneAttributeValues("/uw/khaki13");
+ assertEquals(numProcesses, attributes.get("num_processes"));
+ }
+
+ @Test
+ public void testSetAttributeValueAdd() throws Exception {
+ Value numProcesses = new ValueInt(42l);
+ api.setAttributeValue("/uw/khaki13", "an_attribute", numProcesses);
+ AttributesMap attributes = api.getZoneAttributeValues("/uw/khaki13");
+ assertEquals(numProcesses, attributes.get("an_attribute"));
+ }
+
+ @Test
+ public void testSetAttributeValueRunsQueries() throws Exception {
+ api.installQuery("&query", "SELECT sum(num_processes) AS num_processes");
+ Value numProcesses = new ValueInt(42l);
+ api.setAttributeValue("/uw/khaki13", "num_processes", numProcesses);
+ assertAttributeInZmiEquals("num_processes", new ValueInt(297l), "/uw");
+ assertAttributeInZmiEquals("num_processes", new ValueInt(437l), "/pjwstk");
+ assertAttributeInZmiEquals("num_processes", new ValueInt(734l), "/");
+ }
+}