Minor refactorings for the server node
Change-Id: Ibb05882b775bfb02a88f0a1599384da047738b3d
diff --git a/src/main/java/de/ids_mannheim/korap/response/Response.java b/src/main/java/de/ids_mannheim/korap/response/Response.java
index 674cd0b..d5846fb 100644
--- a/src/main/java/de/ids_mannheim/korap/response/Response.java
+++ b/src/main/java/de/ids_mannheim/korap/response/Response.java
@@ -49,6 +49,8 @@
private String benchmark;
private boolean timeExceeded = false;
+ private static final String KORAL_VERSION =
+ "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld";
/**
* Construct a new Response object.
@@ -451,6 +453,8 @@
// Get notifications json response
ObjectNode json = (ObjectNode) super.toJsonNode();
+ json.put("@context", KORAL_VERSION);
+
StringBuilder sb = new StringBuilder();
if (this.getName() != null) {
sb.append(this.getName());
diff --git a/src/main/java/de/ids_mannheim/korap/server/Resource.java b/src/main/java/de/ids_mannheim/korap/server/Resource.java
index 7f9e99b..bb047a5 100644
--- a/src/main/java/de/ids_mannheim/korap/server/Resource.java
+++ b/src/main/java/de/ids_mannheim/korap/server/Resource.java
@@ -76,20 +76,12 @@
@GET
@Produces(MediaType.APPLICATION_JSON)
public String info () {
- Response kresp = new Response();
- kresp.setNode(Node.getName());
- kresp.setListener(Node.getListener());
+ Response kresp = _initResponse();
- // Get index
- KrillIndex index = Node.getIndex();
- kresp.setName(index.getName());
- kresp.setVersion(index.getVersion());
- /*
- kresp.addMessage(
- "Number of documents in the index",
- String.parseLong(index.numberOf("documents"))
- );
- */
+ if (kresp.hasErrors())
+ return kresp.toJsonString();
+
+ // TODO: Name the number of documents in the index
kresp.addMessage(680, "Server is up and running!");
return kresp.toJsonString();
};
@@ -104,7 +96,7 @@
*/
/*
* Support GZip:
- * oR MAYBE IT'S ALREADY SUPPORTED ....
+ * Or maybe it's already supported ...
* http://stackoverflow.com/questions/19765582/how-to-make-jersey-use-gzip-compression-for-the-response-message-body
*/
@PUT
@@ -122,20 +114,13 @@
if (DEBUG)
log.trace("Added new document with unique identifier {}", uid);
+ Response kresp = _initResponse();
+ if (kresp.hasErrors())
+ return kresp.toJsonString();
+
// Get index
KrillIndex index = Node.getIndex();
- Response kresp = new Response();
- kresp.setNode(Node.getName());
-
- if (index == null) {
- kresp.addError(601, "Unable to find index");
- return kresp.toJsonString();
- };
-
- kresp.setVersion(index.getVersion());
- kresp.setName(index.getName());
-
FieldDocument fd = index.addDoc(uid, json);
if (fd == null) {
// Set HTTP to ???
@@ -165,18 +150,12 @@
@Produces(MediaType.APPLICATION_JSON)
public String commit () {
+ Response kresp = _initResponse();
+ if (kresp.hasErrors())
+ return kresp.toJsonString();
+
// Get index
KrillIndex index = Node.getIndex();
- Response kresp = new Response();
- kresp.setNode(Node.getName());
-
- if (index == null) {
- kresp.addError(601, "Unable to find index");
- return kresp.toJsonString();
- };
-
- kresp.setVersion(index.getVersion());
- kresp.setName(index.getName());
// There are documents to commit
try {
@@ -206,48 +185,37 @@
@Consumes(MediaType.APPLICATION_JSON)
public String find (String json, @Context UriInfo uri) {
- // Get index
- KrillIndex index = Node.getIndex();
+ Response kresp = _initResponse();
+ if (kresp.hasErrors())
+ return kresp.toJsonString();
// Search index
- if (index != null) {
- Krill ks = new Krill(json);
+ Krill ks = new Krill(json);
- // Get query parameters
- MultivaluedMap<String, String> qp = uri.getQueryParameters();
+ // Get query parameters
+ MultivaluedMap<String, String> qp = uri.getQueryParameters();
- if (qp.get("uid") != null) {
-
- // Build Collection based on a list of uids
- List<String> uids = qp.get("uid");
- KrillCollection kc = new KrillCollection();
- kc.filterUIDs(uids.toArray(new String[uids.size()]));
-
- // TODO: RESTRICT COLLECTION TO ONLY RESPECT SELF DOCS (REPLICATION)
-
- // Override old collection
- ks.setCollection(kc);
-
- // Only return the first match per text
- ks.getMeta().setItemsPerResource(1);
-
- return ks.apply(index).toJsonString();
- };
- Result kr = new Result();
- kr.setNode(Node.getName());
- kr.addError(610, "Missing request parameters",
- "No unique IDs were given");
- return kr.toJsonString();
+ if (qp.get("uid") == null) {
+ kresp.addError(610,
+ "Missing request parameters",
+ "No unique IDs were given");
+ return kresp.toJsonString();
};
- Response kresp = new Response();
- kresp.setNode(Node.getName());
- kresp.setName(index.getName());
- kresp.setVersion(index.getVersion());
+ // Build Collection based on a list of uids
+ List<String> uids = qp.get("uid");
+ KrillCollection kc = new KrillCollection();
+ kc.filterUIDs(uids.toArray(new String[uids.size()]));
- kresp.addError(601, "Unable to find index");
+ // TODO: RESTRICT COLLECTION TO ONLY RESPECT SELF DOCS (REPLICATION)
- return kresp.toJsonString();
+ // Override old collection
+ ks.setCollection(kc);
+
+ // Only return the first match per text
+ ks.getMeta().setItemsPerResource(1);
+
+ return ks.apply(Node.getIndex()).toJsonString();
};
@@ -264,16 +232,9 @@
public String collect (String json, @PathParam("resultID") String resultID,
@Context UriInfo uri) {
- // Get index
- KrillIndex index = Node.getIndex();
-
- // No index found
- if (index == null) {
- Response kresp = new Response();
- kresp.setNode(Node.getName());
- kresp.addError(601, "Unable to find index");
+ Response kresp = _initResponse();
+ if (kresp.hasErrors())
return kresp.toJsonString();
- };
// Get the database
try {
@@ -284,7 +245,8 @@
// TODO: Only search in self documents (REPLICATION FTW!)
Krill ks = new Krill(json);
- MatchCollector result = index.collect(ks, mc);
+ // TODO: Reuse response!
+ MatchCollector result = Node.getIndex().collect(ks, mc);
result.setNode(Node.getName());
return result.toJsonString();
@@ -293,11 +255,6 @@
log.error(e.getLocalizedMessage());
};
- Response kresp = new Response();
- kresp.setNode(Node.getName());
- kresp.setName(index.getName());
- kresp.setVersion(index.getVersion());
-
kresp.addError(604, "Unable to connect to database");
return kresp.toJsonString();
};
@@ -321,23 +278,14 @@
@Consumes(MediaType.APPLICATION_JSON)
public String search (String json) {
- // Get index
- KrillIndex index = Node.getIndex();
+ Response kresp = _initResponse();
+ if (kresp.hasErrors())
+ return kresp.toJsonString();
// Search index
- if (index != null) {
- Result kr = new Krill(json).apply(index);
- kr.setNode(Node.getName());
- return kr.toJsonString();
- };
-
- Response kresp = new Response();
- kresp.setNode(Node.getName());
- kresp.setName(index.getName());
- kresp.setVersion(index.getVersion());
-
- kresp.addError(601, "Unable to find index");
- return kresp.toJsonString();
+ // Reuse Response
+ Result kr = new Krill(json).apply(Node.getIndex());
+ return kr.toJsonString();
};
@@ -346,59 +294,55 @@
@Produces(MediaType.APPLICATION_JSON)
public String match (@PathParam("matchID") String id, @Context UriInfo uri) {
+ Response kresp = _initResponse();
+ if (kresp.hasErrors())
+ return kresp.toJsonString();
+
// Get index
KrillIndex index = Node.getIndex();
- // Search index
- if (index != null) {
- // Get query parameters
- MultivaluedMap<String, String> qp = uri.getQueryParameters();
+ // Get query parameters
+ MultivaluedMap<String, String> qp = uri.getQueryParameters();
- boolean includeSpans = false, includeHighlights = true, extendToSentence = false, info = false;
+ boolean includeSpans = false, includeHighlights = true, extendToSentence = false, info = false;
- // Optional query parameter "info" for more information on the match
- if (!_isNull(qp.getFirst("info")))
- info = true;
+ // Optional query parameter "info" for more information on the match
+ if (!_isNull(qp.getFirst("info")))
+ info = true;
- // Optional query parameter "spans" for span information inclusion
- if (!_isNull(qp.getFirst("spans"))) {
- includeSpans = true;
- info = true;
- };
-
- // Optional query parameter "highlights" for highlight information inclusion
- String highlights = qp.getFirst("highlights");
- if (highlights != null && _isNull(highlights))
- includeHighlights = false;
-
- // Optional query parameter "extended" for sentence expansion
- if (!_isNull(qp.getFirst("extended")))
- extendToSentence = true;
-
- List<String> foundries = qp.get("foundry");
- List<String> layers = qp.get("layer");
-
- try {
- // Get match info
- return index.getMatchInfo(id, "tokens", info, foundries,
- layers, includeSpans, includeHighlights,
- extendToSentence).toJsonString();
- }
-
- // Nothing found
- catch (QueryException qe) {
- // Todo: Make Match rely on Response!
- Match km = new Match();
- km.addError(qe.getErrorCode(), qe.getMessage());
- return km.toJsonString();
- }
+ // Optional query parameter "spans" for span information inclusion
+ if (!_isNull(qp.getFirst("spans"))) {
+ includeSpans = true;
+ info = true;
};
- // Response with error message
- Match km = new Match();
- km.addError(601, "Unable to find index");
- return km.toJsonString();
+ // Optional query parameter "highlights" for highlight information inclusion
+ String highlights = qp.getFirst("highlights");
+ if (highlights != null && _isNull(highlights))
+ includeHighlights = false;
+
+ // Optional query parameter "extended" for sentence expansion
+ if (!_isNull(qp.getFirst("extended")))
+ extendToSentence = true;
+
+ List<String> foundries = qp.get("foundry");
+ List<String> layers = qp.get("layer");
+
+ try {
+ // Get match info
+ return index.getMatchInfo(id, "tokens", info, foundries,
+ layers, includeSpans, includeHighlights,
+ extendToSentence).toJsonString();
+ }
+
+ // Nothing found
+ catch (QueryException qe) {
+ // Todo: Make Match rely on Response!
+ kresp.addError(qe.getErrorCode(), qe.getMessage());
+ };
+
+ return kresp.toJsonString();
};
@@ -455,6 +399,24 @@
};
+ private Response _initResponse () {
+ Response kresp = new Response();
+ kresp.setNode(Node.getName());
+ kresp.setListener(Node.getListener());
+
+ // Get index
+ KrillIndex index = Node.getIndex();
+
+ if (index == null) {
+ kresp.addError(601, "Unable to find index");
+ return kresp;
+ };
+
+ kresp.setVersion(index.getVersion());
+ kresp.setName(index.getName());
+ return kresp;
+ };
+
// Check if a string is meant to represent null
private static boolean _isNull (String value) {
if (value == null)
diff --git a/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionIndex.java b/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionIndex.java
index 932426a..2449452 100644
--- a/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionIndex.java
@@ -153,7 +153,6 @@
kcn.fromBuilder(cb.orGroup().with(cb.term("textClass", "kultur").not())
.with(cb.term("author", "Sebastian")));
assertEquals(1, kcn.docCount());
-
};
diff --git a/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionJSON.java b/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionJSON.java
index 2736722..6c2b2f9 100644
--- a/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionJSON.java
+++ b/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionJSON.java
@@ -95,6 +95,16 @@
@Test
+ public void collectionWithNegativeContainment () {
+ String query = _getJSONString("collection_containsnot.jsonld");
+ Krill ks = new Krill(query);
+ assertFalse(ks.hasErrors());
+ assertFalse(ks.hasWarnings());
+ assertFalse(ks.hasMessages());
+ assertEquals("-author:goethe", ks.getCollection().toString());
+ };
+
+ @Test
public void nocollectiontypegiven () {
String metaQuery = _getJSONString("multiterm_rewrite_collection.jsonld");
KrillCollection kc = new KrillCollection(metaQuery);
@@ -146,6 +156,9 @@
};
+
+
+ // Legacy collections reflect old tests, that were adopted to the new scheme
@Test
public void metaQuery1Legacy () {
String metaQuery = getString(getClass().getResource(
diff --git a/src/test/java/de/ids_mannheim/korap/response/TestResponse.java b/src/test/java/de/ids_mannheim/korap/response/TestResponse.java
index b93ddfb..99bc0a5 100644
--- a/src/test/java/de/ids_mannheim/korap/response/TestResponse.java
+++ b/src/test/java/de/ids_mannheim/korap/response/TestResponse.java
@@ -24,7 +24,11 @@
@Test
public void testResponse () throws IOException {
Response resp = new Response();
- assertEquals("{\"meta\":{}}", resp.toJsonString());
+ JsonNode respJson = mapper.readTree(resp.toJsonString());
+ assertEquals("http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
+ respJson.at("/@context").asText());
+ assertEquals("", respJson.at("/meta").asText());
+
resp.setVersion("0.24");
resp.setNode("Tanja");
assertEquals("0.24", resp.getVersion());
@@ -34,7 +38,7 @@
assertFalse(resp.hasMessages());
assertFalse(resp.hasErrors());
- JsonNode respJson = mapper.readTree(resp.toJsonString());
+ respJson = mapper.readTree(resp.toJsonString());
assertEquals("0.24", respJson.at("/meta/version").asText());
assertEquals("Tanja", respJson.at("/meta/node").asText());
@@ -54,7 +58,10 @@
@Test
public void testResponseNotifications () throws IOException {
Response resp = new Response();
- assertEquals("{\"meta\":{}}", resp.toJsonString());
+ JsonNode respJson = mapper.readTree(resp.toJsonString());
+ assertEquals("http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
+ respJson.at("/@context").asText());
+ assertEquals("", respJson.at("/meta").asText());
resp.setVersion("0.24");
resp.setNode("Tanja");
assertEquals("0.24", resp.getVersion());
@@ -64,7 +71,7 @@
assertFalse(resp.hasMessages());
assertFalse(resp.hasErrors());
- JsonNode respJson = mapper.readTree(resp.toJsonString());
+ respJson = mapper.readTree(resp.toJsonString());
assertEquals("0.24", respJson.at("/meta/version").asText());
assertEquals("Tanja", respJson.at("/meta/node").asText());
diff --git a/src/test/java/de/ids_mannheim/korap/server/TestResource.java b/src/test/java/de/ids_mannheim/korap/server/TestResource.java
index 635e897..643f081 100644
--- a/src/test/java/de/ids_mannheim/korap/server/TestResource.java
+++ b/src/test/java/de/ids_mannheim/korap/server/TestResource.java
@@ -82,6 +82,7 @@
};
+ // This tests the node info
@Test
public void testInfo () throws IOException {
String responseMsg = target.path("/").request().get(String.class);
@@ -90,7 +91,6 @@
assertEquals(680, res.at("/messages/0/0").asInt());
};
-
@Test
public void testResource () throws IOException {
String resp;
@@ -111,7 +111,7 @@
res = mapper.readTree(resp);
assertEquals("milena", res.at("/meta/node").asText());
- assertEquals(681, res.at("/messages/0/0").asInt());
+ // System.err.println(res.toString());
}
catch (Exception e) {
fail("Server response failed " + e.getMessage()
diff --git a/src/test/resources/queries/collections/collection_containsnot.jsonld b/src/test/resources/queries/collections/collection_containsnot.jsonld
new file mode 100644
index 0000000..0443116
--- /dev/null
+++ b/src/test/resources/queries/collections/collection_containsnot.jsonld
@@ -0,0 +1,26 @@
+{
+ "@context":"http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld",
+ "errors":[],
+ "warnings":[],
+ "messages":[],
+ "query":{
+ "@type":"koral:token",
+ "wrap":{
+ "@type":"koral:term",
+ "layer":"orth",
+ "key":"der",
+ "match":"match:eq",
+ "foundry":"opennlp"
+ }
+ },
+ "collection":{
+ "value":"Goethe",
+ "match":"match:containsnot",
+ "type":"type:string",
+ "key":"author",
+ "@type":"koral:doc"
+ },
+ "meta":{
+ "cutOff":null
+ }
+}