changeset 101:7268c3ca025b

make admin ui view of all annotations scale better.
author casties
date Fri, 13 Feb 2015 18:10:11 +0100
parents f9ee715ee9e9
children 9140017e8962
files src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationsResource.java
diffstat 3 files changed, 62 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java	Wed Feb 11 20:02:56 2015 +0100
+++ b/src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java	Fri Feb 13 18:10:11 2015 +0100
@@ -61,7 +61,7 @@
  */
 public class AnnotationStore {
 
-    protected static Logger logger = Logger.getLogger("de.mpiwg.itgroup.annotations.neo4j.AnnotationStore");
+    protected static Logger logger = Logger.getLogger(AnnotationStore.class.getCanonicalName());
 
     protected GraphDatabaseService graphDb;
 
@@ -256,13 +256,32 @@
     }
 
     /**
+     * Returns the total number of Annotations.
+     * 
+     * @return number of annotations
+     */
+    public int getAnnotationCount() {
+        int num = -1;
+        Index<Node> idx = getNodeIndex(NodeTypes.ANNOTATION);
+        try (Transaction tx = graphDb.beginTx()) {
+            IndexHits<Node> annotNodes = idx.query("id", "*");
+            num = annotNodes.size();
+            tx.success();
+        }
+        return num;
+    }
+    
+    /**
      * Returns List of Annotations. Key has to be indexed.
      * 
      * @param key
      * @param query
+     * @param limit 
+     * @param offset 
      * @return
      */
-    public List<Annotation> getAnnotations(String key, String query) {
+    public List<Annotation> getAnnotations(String key, String query, int limit, int offset) {
+        long ts = System.currentTimeMillis();
         ArrayList<Annotation> annotations = new ArrayList<Annotation>();
         Index<Node> idx = getNodeIndex(NodeTypes.ANNOTATION);
         if (key == null) {
@@ -271,12 +290,21 @@
         }
         try (Transaction tx = graphDb.beginTx()) {
             IndexHits<Node> annotNodes = idx.query(key, query);
+            int cnt = 0;
+            int max = offset + limit;
             for (Node annotNode : annotNodes) {
+                cnt += 1;
+                if (cnt < offset) continue;
                 Annotation annotation = createAnnotationFromNode(annotNode);
                 annotations.add(annotation);
+                if (limit > 0 && cnt >= max) {
+                    annotNodes.close();
+                    break;
+                }
             }
             tx.success();
         }
+        logger.finer("got "+annotations.size()+" annotations in "+(System.currentTimeMillis()-ts)+"ms");
         return annotations;
     }
 
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java	Wed Feb 11 20:02:56 2015 +0100
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java	Fri Feb 13 18:10:11 2015 +0100
@@ -84,8 +84,8 @@
         if (id == null) {
             // no id -- send all annotations
             Form form = getRequest().getResourceRef().getQueryAsForm();
-            int limit = getInt(form.getFirstValue("limit"));
-            int offset = getInt(form.getFirstValue("offset"));
+            int limit = getInt(form.getFirstValue("limit", "1000"));
+            int offset = getInt(form.getFirstValue("offset", "0"));
             String sortBy = form.getFirstValue("sortBy");
             return getAllAnnotations(authUser, limit, offset, sortBy);
         }
@@ -112,7 +112,7 @@
         ArrayList<JSONObject> results = new ArrayList<JSONObject>();
 
         // read all annotations
-        List<Annotation> annotations = store.getAnnotations(null, null);
+        List<Annotation> annotations = store.getAnnotations(null, null, 0, 0);
         for (Annotation annotation : annotations) {
             // check permission
             if (!annotation.isActionAllowed("read", authUser, store))
@@ -130,13 +130,12 @@
         // put in JSON list
         JSONArray rows = new JSONArray();
         int cnt = 0;
+        int max = limit + offset;
         for (JSONObject result : results) {
             cnt += 1;
-            if (offset > 0 && cnt < offset)
-                continue;
+            if (cnt < offset) continue;
             rows.put(result);
-            if (limit > 0 && cnt >= limit)
-                break;
+            if (limit > 0 && cnt >= max) break;
         }
 
         // assemble result object
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationsResource.java	Wed Feb 11 20:02:56 2015 +0100
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationsResource.java	Fri Feb 13 18:10:11 2015 +0100
@@ -28,6 +28,7 @@
 import java.util.List;
 import java.util.logging.Logger;
 
+import org.restlet.data.Form;
 import org.restlet.data.MediaType;
 import org.restlet.data.Reference;
 import org.restlet.representation.Representation;
@@ -38,6 +39,7 @@
 
 import de.mpiwg.itgroup.annotations.Annotation;
 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
+import de.mpiwg.itgroup.annotations.restlet.AnnotatorResourceImpl;
 import de.mpiwg.itgroup.annotations.restlet.BaseRestlet;
 
 /**
@@ -69,20 +71,36 @@
      */
     @Get("html")
     public Representation doGetHTML(Representation entity) {
-        String result = null;
+        Form form = getRequest().getResourceRef().getQueryAsForm();
+        int limit = AnnotatorResourceImpl.getInt(form.getFirstValue("limit", "100"));
+        int offset = AnnotatorResourceImpl.getInt(form.getFirstValue("offset", "0"));
+        int max = offset + limit;
+        int numannots = store.getAnnotationCount();
+        String baseUrl = this.getReference().getHierarchicalPart();
+        StringBuilder result = null;
         // list all annotations
-        result = "<html><body>\n<h1>Annotations</h1>\n<table>";
-        result += "<tr><th>uri</th><th>text</th><th>target</th><th>fragment</th><th>creator</th></tr>";
-        List<Annotation> annotations = store.getAnnotations("id", "*");
+        result = new StringBuilder("<html><body>\n<h1>Annotations</h1>\n");
+        result.append("<p>");
+        if (offset > 0) {
+            result.append(String.format(" <a href=\"%s?offset=%s&limit=%s\">prev</a> ", baseUrl, Math.max(offset - limit, 0), limit));
+        }
+        result.append(String.format("| %s - %s of %s annotations |", offset, (offset + limit), numannots));
+        if (max < numannots) {
+            result.append(String.format(" <a href=\"%s?offset=%s&limit=%s\">next</a>", baseUrl, Math.min(max, numannots), limit));
+        }
+        result.append("</p>\n");
+        result.append("<table>");
+        result.append("<tr><th>uri</th><th>text</th><th>target</th><th>fragment</th><th>creator</th></tr>");
+        List<Annotation> annotations = store.getAnnotations("id", "*", limit, offset);
         for (Annotation annotation : annotations) {
             Reference url = this.getReference().clone();
             url.addSegment(annotation.getUrlId());
-            result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", url,
+            result.append(String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", url,
                     annotation.getUri(), annotation.getBodyText(), annotation.getTargetBaseUri(), annotation.getTargetBaseUri(), 
-                    annotation.getTargetFragment(), annotation.getCreatorName());
+                    annotation.getTargetFragment(), annotation.getCreatorName()));
         }
-        result += "</table>\n";
-        result += "</body>\n</html>";
+        result.append("</table>\n");
+        result.append("</body>\n</html>");
         return new StringRepresentation(result, MediaType.TEXT_HTML);
     }