Exemplo n.º 1
0
 @Override
 protected void execute(
     final @NonNull KernelHttpRequest request, final @NonNull KernelHttpResponse response) {
   if (!request.isPrivileged()) throw new AdminAccessException();
   final String queryType = request.getParameter("queryType");
   final PrintWriter writer = response.getWriter();
   writer.print("[");
   try {
     final ValidatingKernel kernel = getKernel();
     final BeginTransactionResult btr = kernel.beginTransaction();
     final NodesTransaction txn = btr.getTxn();
     boolean commit = false;
     try {
       final Processor processor = processors.get(queryType);
       if (processor == null) writer.println("Query type '" + queryType + "' not found");
       else processor.process(txn, request, writer);
       commit = true;
     } finally {
       txn.finish(commit);
     }
     writer.print("]");
   } finally {
     writer.close();
   }
 }
Exemplo n.º 2
0
 @Override
 public @NonNull NodeId insertNode(
     final @NonNull NodesTransaction txn,
     final @NonNull URL url,
     final @NonNull NodeIdType nodeIdType) {
   final String string = url.toString();
   if (string == null) throw new NullPointerException("@URL.toString() returned null");
   final NodeId nodeId = txn.insertNode(new StringDatum(string), nodeIdType);
   txn.assignNature(nodeId, getNodeId());
   txn.dereference(nodeId);
   return nodeId;
 }
Exemplo n.º 3
0
 @Override
 public void execute() {
   final BeginTransactionResult btr = kernel.beginTransaction();
   final NodesTransaction txn = btr.getTxn();
   boolean commit = false;
   try {
     txn.deleteLink(linkNodeId, wordParentMeaningNodeId);
     txn.deleteLink(linkNodeId, wordMeaningNodeId);
     txn.deleteNodeRecursively(linkNodeId);
     commit = true;
   } finally {
     txn.finish(commit);
   }
 }
 @Override
 public void insertLink(
     final @NonNull NodesTransaction txn,
     final @NonNull NodeId sourceNodeId,
     final @NonNull NodeId targetNodeId,
     final boolean overwrite) {
   txn.insertLink(sourceNodeId, targetNodeId, getNodeId(), overwrite);
 }
Exemplo n.º 5
0
 @Override
 public void endElement(final String uriString, final String localName, final String qName) {
   // save text, if any
   if (level.sb.length() > 0) {
     final String string = level.sb.toString();
     if (string != null) txn.updateNode(level.nodeId, new StringDatum(string));
   }
   level = level.parent;
 }
Exemplo n.º 6
0
 private @NonNull JsonEntity getNodeIdJson(
     final @NonNull NodesTransaction txn,
     final @NonNull NodeId nodeId,
     final @NonNull Set<NodeId> seen) {
   if (seen.contains(nodeId)) return new JsonString("seen");
   seen.add(nodeId);
   final NodeIdJsonTranslatorRegistry nitjr = nitjrReactor.getInstance();
   final NodeIdJsonTranslator nitj = nitjr.get(nodeId.getType());
   if (nitj == null) return new JsonString("error");
   final JsonEntity je = nitj.translate(nodeId);
   if (!(je instanceof JsonObject)) return je;
   final JsonObject jo = (JsonObject) je;
   final JsonObjectEditor joe = jo.createEditor();
   {
     final JsonArrayEditor jae = new JsonArrayEditor();
     final Set<NodeId> natureNodeIds = txn.getDeclaredNatureNodeIds(nodeId);
     for (final NodeId natureNodeId : natureNodeIds) {
       if (natureNodeId != null) {
         jae.add(getNodeIdJson(txn, natureNodeId));
       }
     }
     joe.set("natures", jae);
   }
   {
     final JsonArrayEditor jae = new JsonArrayEditor();
     Cursor<NodeKey> cursor = null;
     try {
       cursor = txn.fetchKeys(nodeId);
       for (final NodeKey nodeKey : cursor) {
         if (nodeKey != null) {
           jae.add(nodeKey.toString());
         }
       }
     } finally {
       if (cursor != null) cursor.close();
     }
     joe.set("keys", jae);
   }
   return joe.create();
 }
Exemplo n.º 7
0
 @Override
 public void startElement(
     final String uriString,
     final String localName,
     final String qName,
     final Attributes saxAttrs)
     throws SAXException {
   checkCancelled();
   final Level parentLevel = level;
   level = new Level(parentLevel);
   // uriString + localName = global meaning
   // if no uriString then localName = meaning to parent
   final NodeId meaningNodeId = constructMeaningNodeId(parentLevel, uriString, localName);
   // link through an intermediate node in case there are duplicates
   NodeId targetNodeId = txn.getTarget(parentLevel.nodeId, meaningNodeId);
   if (targetNodeId == null) {
     targetNodeId = i2nit.create(parentLevel.nodeId, parentLevel.index++);
     txn.insertLink(parentLevel.nodeId, targetNodeId, meaningNodeId, false);
   }
   txn.insertLink(level.nodeId, targetNodeId, parentMeaning.getNodeId(), false);
   // create nodes for attributes
   for (int i = 0; i < saxAttrs.getLength(); i++) {
     final String attrLocalName = saxAttrs.getLocalName(i);
     final String attrUriString = saxAttrs.getURI(i);
     final String attrValue = saxAttrs.getValue(i);
     if (attrValue != null) {
       final NodeId attrNodeId = txn.insertNode(new StringDatum(attrValue));
       final NodeId attrMeaningNodeId =
           constructMeaningNodeId(level, attrUriString, attrLocalName);
       // TODO what if an attribute name collides with an element name?
       txn.insertLink(level.nodeId, attrNodeId, attrMeaningNodeId, false);
       txn.dereference(attrNodeId);
     }
   }
 }
Exemplo n.º 8
0
 @Override
 public @NonNull URL getUrl(final @NonNull NodesTransaction txn, final @NonNull NodeId nodeId) {
   final Datum datum = txn.getDatum(nodeId);
   if (!(datum instanceof StringDatum))
     throw new DataTypeException(nodeId, datum.getType(), StringDatumType.INSTANCE, getNodeId());
   final StringDatum stringDatum = (StringDatum) datum;
   final String string = stringDatum.getValue();
   try {
     return new URL(string);
   } catch (final MalformedURLException e) {
     throw new RuntimeException(e);
   }
 }
Exemplo n.º 9
0
 @Override
 public void execute() {
   final WordIndexContextProvider wicp = wicpReactor.getInstance();
   // add new link and possibly word
   final NodeId contextNodeId = wicp.getWordIndexContext();
   final NodeKey nodeKey = new NamedNodeKey(contextNodeId, word);
   //
   final Coordinator coordinator = coordReactor.getInstance();
   final Reservation reservation =
       coordinator.reserve(
           CoordinatorUtil.makeSetOfRules(new BaseKeyRule(nodeKey)),
           CoordinatorUtil.makeSetOfIntents(new BaseKeyIntent(nodeKey)));
   try {
     final BeginTransactionResult btr = kernel.beginTransaction();
     final NodesTransaction txn = btr.getTxn();
     boolean commit = false;
     try {
       NodeId wordNodeId = txn.getNode(nodeKey);
       if (wordNodeId == null) {
         // TODO use unique datum?
         wordNodeId = txn.insertNode(new StringDatum(word));
         txn.insertKey(nodeKey, wordNodeId, false);
         txn.dereference(wordNodeId);
       }
       final NodeId linkNodeId = txn.insertNode();
       txn.insertLink(linkNodeId, nodeBeingIndexed, wordParentMeaningNodeId, false);
       txn.insertLink(linkNodeId, wordNodeId, wordMeaningNodeId, false);
       txn.dereference(linkNodeId);
       commit = true;
     } finally {
       txn.finish(commit);
     }
   } finally {
     reservation.release();
   }
 }
Exemplo n.º 10
0
 @Override
 public void purgeIndex(
     final @NonNull NodesTransaction txn, final @NonNull NodeId nodeId, final boolean deferred) {
   final WordParentMeaning wordParentMeaning = wordParentMeaningReactor.getInstance();
   final Set<NodeId> oldNodesToDelete =
       txn.fetchSourceNodes(nodeId, wordParentMeaning.getNodeId()).toSet();
   final ValidatingKernel kernel = k1Reactor.getInstance();
   final WordMeaning wordMeaning = wordMeaningReactor.getInstance();
   final Queue queue = queueReactor.getInstance();
   // enqueue deletes for all links
   for (final NodeId linkNodeId : oldNodesToDelete) {
     if (linkNodeId != null) {
       final QueueOperation op =
           new DeleteIndexLinkOperation(
               kernel, nodeId, linkNodeId, wordParentMeaning.getNodeId(), wordMeaning.getNodeId());
       if (deferred) {
         queue.add(op, 0);
         queue.start();
       } else op.execute();
     }
   }
 }
Exemplo n.º 11
0
 @Override
 public void updateIndex(
     final @NonNull NodesTransaction txn, final @NonNull NodeId nodeId, final boolean deferred) {
   final ValidatingKernel kernel = k1Reactor.getInstance();
   final Queue queue = queueReactor.getInstance();
   final WordMeaning wordMeaning = wordMeaningReactor.getInstance();
   final WordParentMeaning wordParentMeaning = wordParentMeaningReactor.getInstance();
   final MimeTypeMeaning mimeTypeMeaning = mimeTypeMeaningReactor.getInstance();
   final LocaleMeaning localeMeaning = localeMeaningReactor.getInstance();
   final Coordinator coordinator = coordReactor.getInstance();
   final Reservation reservation =
       coordinator.reserve(
           CoordinatorUtil.makeSetOfRules(new BaseNodeRule(nodeId)),
           CoordinatorUtil.makeSetOfIntents(new BaseNodeIntent(nodeId)));
   try {
     final Datum datum = txn.getDatum(nodeId);
     final MimeType mimeType = mimeTypeMeaning.getMimeType(txn, nodeId);
     final Locale locale = localeMeaning.getLocale(txn, nodeId);
     final List<Stemmer> stemmers = StemmerUtil.getStemmers(bundleContext, locale);
     WordParser parser;
     if (mimeType != null) {
       parser = getParser(mimeType.toString());
     } else if (datum instanceof StringDatum) {
       parser = getParser("text/plain");
     } else {
       parser = null;
     }
     if (parser == null) return;
     Cursor<NodeId> cursor = null;
     final Map<String, NodeId> oldIndexEntries = new HashMap<String, NodeId>();
     final Set<NodeId> oldNodesToDelete = new HashSet<NodeId>();
     try {
       cursor = txn.fetchSourceNodes(nodeId, wordParentMeaning.getNodeId());
       for (final NodeId linkNodeId : cursor) {
         if (linkNodeId != null) {
           final NodeId wordNodeId = txn.getTarget(linkNodeId, wordMeaning.getNodeId());
           if (wordNodeId != null) {
             final Datum wordDatum = txn.getDatum(wordNodeId);
             if (wordDatum instanceof StringDatum) {
               final String indexEntry = ((StringDatum) wordDatum).getValue();
               final NodeId oldLinkNodeId = oldIndexEntries.get(indexEntry);
               if (oldLinkNodeId != null) {
                 // get rid of redundant link
                 oldNodesToDelete.add(linkNodeId);
               } else {
                 oldIndexEntries.put(indexEntry, linkNodeId);
               }
             }
           }
         }
       }
     } finally {
       if (cursor != null) cursor.close();
     }
     // enqueue deletes for all redundant links
     for (final NodeId linkNodeId : oldNodesToDelete) {
       if (linkNodeId != null) {
         final QueueOperation op =
             new DeleteIndexLinkOperation(
                 kernel,
                 nodeId,
                 linkNodeId,
                 wordParentMeaning.getNodeId(),
                 wordMeaning.getNodeId());
         if (deferred) {
           queue.add(op, 0);
           queue.start();
         } else op.execute();
       }
     }
     Reader reader;
     try {
       reader = new InputStreamReader(datum.getInputStream());
     } catch (final IOException e) {
       // TODO log something
       reader = null;
     }
     if (reader == null) return;
     final Set<String> newStemmedWords = new HashSet<String>();
     Set<String> words = new HashSet<String>();
     for (final String word : parser.parse(reader)) {
       if (word != null) {
         words.add(word);
       }
     }
     for (final Stemmer stemmer : stemmers) {
       if (stemmer != null) {
         words = stemmer.stem(words);
       }
     }
     newStemmedWords.addAll(words);
     for (final String word : newStemmedWords) {
       if (word != null) {
         final NodeId oldLinkNodeId = oldIndexEntries.get(word);
         if (oldLinkNodeId != null) {
           oldIndexEntries.remove(word);
         } else {
           final QueueOperation op =
               new IndexOperation(
                   kernel,
                   wicpReactor,
                   coordReactor,
                   word,
                   nodeId,
                   wordParentMeaning.getNodeId(),
                   wordMeaning.getNodeId());
           queue.add(op, 0);
         }
       }
     }
     // enqueue deletes for all old word links that no longer apply
     for (final NodeId linkNodeId : oldIndexEntries.values()) {
       if (linkNodeId != null) {
         final QueueOperation op =
             new DeleteIndexLinkOperation(
                 kernel,
                 nodeId,
                 linkNodeId,
                 wordParentMeaning.getNodeId(),
                 wordMeaning.getNodeId());
         if (deferred) queue.add(op, 0);
         else op.execute();
       }
     }
   } finally {
     reservation.release();
   }
 }