Changeset 101:7268c3ca025b in AnnotationManagerN4J for src


Ignore:
Timestamp:
Feb 13, 2015, 5:10:11 PM (9 years ago)
Author:
casties
Branch:
default
Message:

make admin ui view of all annotations scale better.

Location:
src/main/java/de/mpiwg/itgroup/annotations
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java

    r98 r101  
    6262public class AnnotationStore {
    6363
    64     protected static Logger logger = Logger.getLogger("de.mpiwg.itgroup.annotations.neo4j.AnnotationStore");
     64    protected static Logger logger = Logger.getLogger(AnnotationStore.class.getCanonicalName());
    6565
    6666    protected GraphDatabaseService graphDb;
     
    257257
    258258    /**
     259     * Returns the total number of Annotations.
     260     *
     261     * @return number of annotations
     262     */
     263    public int getAnnotationCount() {
     264        int num = -1;
     265        Index<Node> idx = getNodeIndex(NodeTypes.ANNOTATION);
     266        try (Transaction tx = graphDb.beginTx()) {
     267            IndexHits<Node> annotNodes = idx.query("id", "*");
     268            num = annotNodes.size();
     269            tx.success();
     270        }
     271        return num;
     272    }
     273   
     274    /**
    259275     * Returns List of Annotations. Key has to be indexed.
    260276     *
    261277     * @param key
    262278     * @param query
    263      * @return
    264      */
    265     public List<Annotation> getAnnotations(String key, String query) {
     279     * @param limit
     280     * @param offset
     281     * @return
     282     */
     283    public List<Annotation> getAnnotations(String key, String query, int limit, int offset) {
     284        long ts = System.currentTimeMillis();
    266285        ArrayList<Annotation> annotations = new ArrayList<Annotation>();
    267286        Index<Node> idx = getNodeIndex(NodeTypes.ANNOTATION);
     
    272291        try (Transaction tx = graphDb.beginTx()) {
    273292            IndexHits<Node> annotNodes = idx.query(key, query);
     293            int cnt = 0;
     294            int max = offset + limit;
    274295            for (Node annotNode : annotNodes) {
     296                cnt += 1;
     297                if (cnt < offset) continue;
    275298                Annotation annotation = createAnnotationFromNode(annotNode);
    276299                annotations.add(annotation);
    277             }
    278             tx.success();
    279         }
     300                if (limit > 0 && cnt >= max) {
     301                    annotNodes.close();
     302                    break;
     303                }
     304            }
     305            tx.success();
     306        }
     307        logger.finer("got "+annotations.size()+" annotations in "+(System.currentTimeMillis()-ts)+"ms");
    280308        return annotations;
    281309    }
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java

    r91 r101  
    8585            // no id -- send all annotations
    8686            Form form = getRequest().getResourceRef().getQueryAsForm();
    87             int limit = getInt(form.getFirstValue("limit"));
    88             int offset = getInt(form.getFirstValue("offset"));
     87            int limit = getInt(form.getFirstValue("limit", "1000"));
     88            int offset = getInt(form.getFirstValue("offset", "0"));
    8989            String sortBy = form.getFirstValue("sortBy");
    9090            return getAllAnnotations(authUser, limit, offset, sortBy);
     
    113113
    114114        // read all annotations
    115         List<Annotation> annotations = store.getAnnotations(null, null);
     115        List<Annotation> annotations = store.getAnnotations(null, null, 0, 0);
    116116        for (Annotation annotation : annotations) {
    117117            // check permission
     
    131131        JSONArray rows = new JSONArray();
    132132        int cnt = 0;
     133        int max = limit + offset;
    133134        for (JSONObject result : results) {
    134135            cnt += 1;
    135             if (offset > 0 && cnt < offset)
    136                 continue;
     136            if (cnt < offset) continue;
    137137            rows.put(result);
    138             if (limit > 0 && cnt >= limit)
    139                 break;
     138            if (limit > 0 && cnt >= max) break;
    140139        }
    141140
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationsResource.java

    r99 r101  
    2929import java.util.logging.Logger;
    3030
     31import org.restlet.data.Form;
    3132import org.restlet.data.MediaType;
    3233import org.restlet.data.Reference;
     
    3940import de.mpiwg.itgroup.annotations.Annotation;
    4041import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
     42import de.mpiwg.itgroup.annotations.restlet.AnnotatorResourceImpl;
    4143import de.mpiwg.itgroup.annotations.restlet.BaseRestlet;
    4244
     
    7072    @Get("html")
    7173    public Representation doGetHTML(Representation entity) {
    72         String result = null;
     74        Form form = getRequest().getResourceRef().getQueryAsForm();
     75        int limit = AnnotatorResourceImpl.getInt(form.getFirstValue("limit", "100"));
     76        int offset = AnnotatorResourceImpl.getInt(form.getFirstValue("offset", "0"));
     77        int max = offset + limit;
     78        int numannots = store.getAnnotationCount();
     79        String baseUrl = this.getReference().getHierarchicalPart();
     80        StringBuilder result = null;
    7381        // list all annotations
    74         result = "<html><body>\n<h1>Annotations</h1>\n<table>";
    75         result += "<tr><th>uri</th><th>text</th><th>target</th><th>fragment</th><th>creator</th></tr>";
    76         List<Annotation> annotations = store.getAnnotations("id", "*");
     82        result = new StringBuilder("<html><body>\n<h1>Annotations</h1>\n");
     83        result.append("<p>");
     84        if (offset > 0) {
     85            result.append(String.format(" <a href=\"%s?offset=%s&limit=%s\">prev</a> ", baseUrl, Math.max(offset - limit, 0), limit));
     86        }
     87        result.append(String.format("| %s - %s of %s annotations |", offset, (offset + limit), numannots));
     88        if (max < numannots) {
     89            result.append(String.format(" <a href=\"%s?offset=%s&limit=%s\">next</a>", baseUrl, Math.min(max, numannots), limit));
     90        }
     91        result.append("</p>\n");
     92        result.append("<table>");
     93        result.append("<tr><th>uri</th><th>text</th><th>target</th><th>fragment</th><th>creator</th></tr>");
     94        List<Annotation> annotations = store.getAnnotations("id", "*", limit, offset);
    7795        for (Annotation annotation : annotations) {
    7896            Reference url = this.getReference().clone();
    7997            url.addSegment(annotation.getUrlId());
    80             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,
     98            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,
    8199                    annotation.getUri(), annotation.getBodyText(), annotation.getTargetBaseUri(), annotation.getTargetBaseUri(),
    82                     annotation.getTargetFragment(), annotation.getCreatorName());
     100                    annotation.getTargetFragment(), annotation.getCreatorName()));
    83101        }
    84         result += "</table>\n";
    85         result += "</body>\n</html>";
     102        result.append("</table>\n");
     103        result.append("</body>\n</html>");
    86104        return new StringRepresentation(result, MediaType.TEXT_HTML);
    87105    }
Note: See TracChangeset for help on using the changeset viewer.