/** * 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()); } }
public FTIndexWorker(FTIndex index, DBBroker broker) throws DatabaseConfigurationException { this.index = index; try { this.engine = new NativeTextEngine(broker, index.getBFile(), broker.getConfiguration()); } catch (DBException e) { throw new DatabaseConfigurationException(e.getMessage(), e); } }
private AnalysisXQueryContext(DBBroker broker, AccessContext accessCtx) { super(broker.getBrokerPool(), accessCtx); }
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()); } }
@Override public void execute(DBBroker broker) throws EXistException { final Agent agentInstance = AgentFactory.getInstance(); final BrokerPool brokerPool = broker.getBrokerPool(); final TaskStatus endStatus = new TaskStatus(TaskStatus.Status.STOPPED_OK); agentInstance.changeStatus(brokerPool, new TaskStatus(TaskStatus.Status.INIT)); if (paused) { LOG.info("Consistency check is paused."); agentInstance.changeStatus(brokerPool, new TaskStatus(TaskStatus.Status.PAUSED)); return; } brokerPool.getProcessMonitor().startJob(ProcessMonitor.ACTION_BACKUP, null, monitor); PrintWriter report = null; try { boolean doBackup = createBackup; // TODO: don't use the direct access feature for now. needs more testing List<ErrorReport> errors = null; if (!incremental || incrementalCheck) { LOG.info("Starting consistency check..."); report = openLog(); final CheckCallback cb = new CheckCallback(report); final ConsistencyCheck check = new ConsistencyCheck(broker, false, checkDocs); agentInstance.changeStatus(brokerPool, new TaskStatus(TaskStatus.Status.RUNNING_CHECK)); errors = check.checkAll(cb); if (!errors.isEmpty()) { endStatus.setStatus(TaskStatus.Status.STOPPED_ERROR); endStatus.setReason(errors); LOG.error("Errors found: " + errors.size()); doBackup = true; if (fatalErrorsFound(errors)) { LOG.error("Fatal errors were found: pausing the consistency check task."); paused = true; } } LOG.info("Finished consistency check"); } if (doBackup) { LOG.info("Starting backup..."); final SystemExport sysexport = new SystemExport(broker, logCallback, monitor, false); lastExportedBackup = sysexport.export(exportDir, incremental, maxInc, createZip, errors); agentInstance.changeStatus(brokerPool, new TaskStatus(TaskStatus.Status.RUNNING_BACKUP)); if (lastExportedBackup != null) { LOG.info("Created backup to file: " + lastExportedBackup.getAbsolutePath()); } LOG.info("Finished backup"); } } catch (final TerminatedException e) { throw new EXistException(e.getMessage(), e); } catch (final PermissionDeniedException e) { // TODO should maybe throw PermissionDeniedException instead! throw new EXistException(e.getMessage(), e); } finally { if (report != null) { report.close(); } agentInstance.changeStatus(brokerPool, endStatus); brokerPool.getProcessMonitor().endJob(); } }