Beispiel #1
0
  /**
   * Statically analyze a query for various properties.
   *
   * @param query the query to analyze
   * @param params parameters for the query; if necessary parameters are left out they will be
   *     listed as required variables in the analysis
   * @return a query analysis facet
   */
  public QueryAnalysis analyze(String query, Object... params) {
    if (presub) query = presub(query, params);

    long t1 = System.currentTimeMillis(), t2 = 0, t3 = 0;
    DBBroker broker = null;
    try {
      broker = db.acquireBroker();
      prepareContext(broker);
      final org.exist.source.Source source = buildQuerySource(query, params, "analyze");
      final XQuery xquery = broker.getXQueryService();
      final XQueryPool pool = xquery.getXQueryPool();
      CompiledXQuery compiledQuery = pool.borrowCompiledXQuery(broker, source);
      try {
        AnalysisXQueryContext context;
        if (compiledQuery == null) {
          context = new AnalysisXQueryContext(broker, AccessContext.INTERNAL_PREFIX_LOOKUP);
          buildXQueryStaticContext(context, false);
          buildXQueryDynamicContext(context, params, null, false);
          t2 = System.currentTimeMillis();
          compiledQuery = xquery.compile(context, source);
          t3 = System.currentTimeMillis();
        } else {
          context = (AnalysisXQueryContext) compiledQuery.getContext();
          t2 = System.currentTimeMillis();
        }
        return new QueryAnalysis(
            compiledQuery,
            Collections.unmodifiableSet(context.requiredVariables),
            Collections.unmodifiableSet(context.requiredFunctions));
      } finally {
        if (compiledQuery != null) pool.returnCompiledXQuery(source, compiledQuery);
      }
    } catch (XPathException e) {
      LOG.warn(
          "query compilation failed --  "
              + query
              + "  -- "
              + (params == null ? "" : " with params " + Arrays.asList(params))
              + (bindings.isEmpty() ? "" : " and bindings " + bindings));
      throw new DatabaseException("failed to compile query", e);
    } catch (IOException e) {
      throw new DatabaseException("unexpected exception", e);
    } catch (PermissionDeniedException e) {
      throw new DatabaseException("permission denied", e);
    } finally {
      db.releaseBroker(broker);
      STATS.update(query, t1, t2, t3, 0, System.currentTimeMillis());
    }
  }
Beispiel #2
0
 ItemList executeQuery(String query, WrapperFactory wrapperFactory, Object[] params) {
   long t1 = System.currentTimeMillis(), t2 = 0, t3 = 0, t4 = 0;
   if (presub) query = presub(query, params);
   DBBroker broker = null;
   try {
     broker = db.acquireBroker();
     prepareContext(broker);
     if (overrideDocs != null) docs = overrideDocs;
     final org.exist.source.Source source = buildQuerySource(query, params, "execute");
     final XQuery xquery = broker.getXQueryService();
     final XQueryPool pool = xquery.getXQueryPool();
     CompiledXQuery compiledQuery = pool.borrowCompiledXQuery(broker, source);
     MutableDocumentSet docsToLock = new DefaultDocumentSet();
     if (docs != null) docsToLock.addAll(docs);
     if (base != null) docsToLock.addAll(base.getDocumentSet());
     try {
       XQueryContext context;
       if (compiledQuery == null) {
         context = xquery.newContext(AccessContext.INTERNAL_PREFIX_LOOKUP);
         buildXQueryStaticContext(context, true);
       } else {
         context = compiledQuery.getContext();
         // static context already set
       }
       buildXQueryDynamicContext(context, params, docsToLock, true);
       t2 = System.currentTimeMillis();
       if (compiledQuery == null) {
         compiledQuery = xquery.compile(context, source);
         t3 = System.currentTimeMillis();
       }
       docsToLock.lock(broker, false, false);
       try {
         return new ItemList(
             xquery.execute(wrap(compiledQuery, wrapperFactory, context), base),
             namespaceBindings.extend(),
             db);
       } finally {
         docsToLock.unlock(false);
         t4 = System.currentTimeMillis();
       }
     } finally {
       if (compiledQuery != null) pool.returnCompiledXQuery(source, compiledQuery);
     }
   } catch (XPathException e) {
     LOG.debug(
         "query execution failed --  "
             + query
             + "  -- "
             + (params == null ? "" : " with params " + Arrays.asList(params))
             + (bindings.isEmpty() ? "" : " and bindings " + bindings));
     throw new DatabaseException("failed to execute query", e);
   } catch (IOException e) {
     throw new DatabaseException("unexpected exception", e);
   } catch (LockException e) {
     throw new DatabaseException("deadlock", e);
   } catch (PermissionDeniedException e) {
     throw new DatabaseException("permission denied", e);
   } finally {
     db.releaseBroker(broker);
     STATS.update(query, t1, t2, t3, t4, System.currentTimeMillis());
   }
 }
Beispiel #3
0
 /*
  * (non-Javadoc)
  *
  * @see org.exist.xupdate.Modification#process(org.exist.dom.DocumentSet)
  */
 public long process(Txn transaction)
     throws PermissionDeniedException, LockException, EXistException, XPathException {
   NodeList children = content;
   if (children.getLength() == 0) return 0;
   int modifications = children.getLength();
   try {
     StoredNode ql[] = selectAndLock(transaction);
     IndexListener listener = new IndexListener(ql);
     NotificationService notifier = broker.getBrokerPool().getNotificationService();
     Node temp;
     TextImpl text;
     AttrImpl attribute;
     ElementImpl parent;
     for (int i = 0; i < ql.length; i++) {
       StoredNode node = ql[i];
       if (node == null) {
         LOG.warn("select " + selectStmt + " returned empty node");
         continue;
       }
       DocumentImpl doc = (DocumentImpl) node.getOwnerDocument();
       doc.getMetadata().setIndexListener(listener);
       if (!doc.getPermissions().validate(broker.getUser(), Permission.UPDATE))
         throw new PermissionDeniedException("permission to update document denied");
       switch (node.getNodeType()) {
         case Node.ELEMENT_NODE:
           if (modifications == 0) modifications = 1;
           ((ElementImpl) node).update(transaction, children);
           break;
         case Node.TEXT_NODE:
           parent = (ElementImpl) node.getParentNode();
           temp = children.item(0);
           text = new TextImpl(temp.getNodeValue());
           modifications = 1;
           text.setOwnerDocument(doc);
           parent.updateChild(transaction, node, text);
           break;
         case Node.ATTRIBUTE_NODE:
           parent = (ElementImpl) node.getParentNode();
           if (parent == null) {
             LOG.warn("parent node not found for " + node.getNodeId());
             break;
           }
           AttrImpl attr = (AttrImpl) node;
           temp = children.item(0);
           attribute = new AttrImpl(attr.getQName(), temp.getNodeValue());
           attribute.setOwnerDocument(doc);
           parent.updateChild(transaction, node, attribute);
           break;
         default:
           throw new EXistException("unsupported node-type");
       }
       doc.getMetadata().clearIndexListener();
       doc.getMetadata().setLastModified(System.currentTimeMillis());
       modifiedDocuments.add(doc);
       broker.storeXMLResource(transaction, doc);
       notifier.notifyUpdate(doc, UpdateListener.UPDATE);
     }
     checkFragmentation(transaction, modifiedDocuments);
   } finally {
     unlockDocuments(transaction);
   }
   return modifications;
 }