# HG changeset patch # User casties # Date 1423847411 -3600 # Node ID 7268c3ca025ba8e2d709afab8679a7eeb3dd24f7 # Parent f9ee715ee9e9a7b1ef1472fb261197b9c394ea51 make admin ui view of all annotations scale better. diff -r f9ee715ee9e9 -r 7268c3ca025b src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java --- 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 idx = getNodeIndex(NodeTypes.ANNOTATION); + try (Transaction tx = graphDb.beginTx()) { + IndexHits 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 getAnnotations(String key, String query) { + public List getAnnotations(String key, String query, int limit, int offset) { + long ts = System.currentTimeMillis(); ArrayList annotations = new ArrayList(); Index idx = getNodeIndex(NodeTypes.ANNOTATION); if (key == null) { @@ -271,12 +290,21 @@ } try (Transaction tx = graphDb.beginTx()) { IndexHits 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; } diff -r f9ee715ee9e9 -r 7268c3ca025b src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java --- 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 results = new ArrayList(); // read all annotations - List annotations = store.getAnnotations(null, null); + List 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 diff -r f9ee715ee9e9 -r 7268c3ca025b src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationsResource.java --- 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 = "\n

Annotations

\n"; - result += ""; - List annotations = store.getAnnotations("id", "*"); + result = new StringBuilder("\n

Annotations

\n"); + result.append("

"); + if (offset > 0) { + result.append(String.format(" prev ", 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(" next", baseUrl, Math.min(max, numannots), limit)); + } + result.append("

\n"); + result.append("
uritexttargetfragmentcreator
"); + result.append(""); + List annotations = store.getAnnotations("id", "*", limit, offset); for (Annotation annotation : annotations) { Reference url = this.getReference().clone(); url.addSegment(annotation.getUrlId()); - result += String.format("\n", url, + result.append(String.format("\n", url, annotation.getUri(), annotation.getBodyText(), annotation.getTargetBaseUri(), annotation.getTargetBaseUri(), - annotation.getTargetFragment(), annotation.getCreatorName()); + annotation.getTargetFragment(), annotation.getCreatorName())); } - result += "
uritexttargetfragmentcreator
%s%s%s%s%s
%s%s%s%s%s
\n"; - result += "\n"; + result.append("\n"); + result.append("\n"); return new StringRepresentation(result, MediaType.TEXT_HTML); }