Search in sources :

Example 91 with EXistException

use of org.exist.EXistException in project exist by eXist-db.

the class SortIndexWorker method getId.

private short getId(final String name) throws EXistException, LockException {
    final byte[] key = new byte[1 + UTF8.encoded(name)];
    key[0] = 1;
    UTF8.encode(name, key, 1);
    try (final ManagedLock<ReentrantLock> btreeLock = lockManager.acquireBtreeReadLock(index.btree.getLockName())) {
        return (short) index.btree.findValue(new Value(key));
    } catch (final BTreeException | IOException e) {
        throw new EXistException("Exception caught while reading sort index: " + e.getMessage(), e);
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) BTreeException(org.exist.storage.btree.BTreeException) Value(org.exist.storage.btree.Value) IOException(java.io.IOException) EXistException(org.exist.EXistException)

Example 92 with EXistException

use of org.exist.EXistException in project exist by eXist-db.

the class SortIndexWorker method getOrRegisterId.

/**
 * Register the given index name and return a short id for it.
 *
 * @param name the name of the index
 *
 * @return a unique id to be used for the index entries
 *
 * @throws EXistException if an error occurs with the database
 * @throws LockException if a locking error occurs
 */
private short getOrRegisterId(final String name) throws EXistException, LockException {
    short id = getId(name);
    if (id < 0) {
        final byte[] fromKey = { 1 };
        final byte[] endKey = { 2 };
        final IndexQuery query = new IndexQuery(IndexQuery.RANGE, new Value(fromKey), new Value(endKey));
        try (final ManagedLock<ReentrantLock> btreeLock = lockManager.acquireBtreeWriteLock(index.btree.getLockName())) {
            final FindIdCallback callback = new FindIdCallback(false);
            index.btree.query(query, callback);
            id = (short) (callback.max + 1);
            registerId(id, name);
        } catch (final IOException | TerminatedException | BTreeException e) {
            throw new EXistException("Exception caught while reading sort index: " + e.getMessage(), e);
        }
    }
    return id;
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) BTreeException(org.exist.storage.btree.BTreeException) IndexQuery(org.exist.storage.btree.IndexQuery) Value(org.exist.storage.btree.Value) IOException(java.io.IOException) EXistException(org.exist.EXistException) TerminatedException(org.exist.xquery.TerminatedException)

Example 93 with EXistException

use of org.exist.EXistException in project exist by eXist-db.

the class RemoveIndex method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    final SortIndexWorker index = (SortIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(SortIndex.ID);
    final String id = args[0].getStringValue();
    try {
        if (getArgumentCount() == 2) {
            final NodeValue nv = (NodeValue) args[1].itemAt(0);
            if (nv.getImplementationType() == NodeValue.IN_MEMORY_NODE)
                throw new XPathException(this, "Second argument to remove should be a persistent node, not " + "an in-memory node.");
            final NodeProxy proxy = (NodeProxy) nv;
            index.remove(id, proxy.getOwnerDocument());
        } else {
            index.remove(id);
        }
    } catch (final EXistException e) {
        throw new XPathException(this, e.getMessage(), e);
    } catch (final LockException e) {
        throw new XPathException(this, "Caught lock error while removing index. Giving up.", e);
    }
    return Sequence.EMPTY_SEQUENCE;
}
Also used : SortIndexWorker(org.exist.indexing.sort.SortIndexWorker) LockException(org.exist.util.LockException) EXistException(org.exist.EXistException) NodeProxy(org.exist.dom.persistent.NodeProxy)

Example 94 with EXistException

use of org.exist.EXistException in project exist by eXist-db.

the class GetIndex method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    if (args[1].isEmpty())
        return Sequence.EMPTY_SEQUENCE;
    final String id = args[0].getStringValue();
    final NodeProxy node = (NodeProxy) args[1].itemAt(0);
    final SortIndexWorker index = (SortIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(SortIndex.ID);
    long pos = 0;
    try {
        pos = index.getIndex(id, node);
    } catch (final EXistException e) {
        throw new XPathException(this, e.getMessage(), e);
    } catch (final LockException e) {
        throw new XPathException(this, "Caught lock error while searching index. Giving up.", e);
    }
    return pos < 0 ? Sequence.EMPTY_SEQUENCE : new IntegerValue(pos, Type.LONG);
}
Also used : SortIndexWorker(org.exist.indexing.sort.SortIndexWorker) LockException(org.exist.util.LockException) EXistException(org.exist.EXistException) NodeProxy(org.exist.dom.persistent.NodeProxy)

Example 95 with EXistException

use of org.exist.EXistException in project exist by eXist-db.

the class RewriteConfig method configure.

private void configure(final String controllerConfig) throws ServletException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Loading XQueryURLRewrite configuration from {}", controllerConfig);
    }
    if (controllerConfig.startsWith(XmldbURI.XMLDB_URI_PREFIX)) {
        try (final DBBroker broker = urlRewrite.getBrokerPool().get(Optional.ofNullable(urlRewrite.getDefaultUser()))) {
            try (final LockedDocument lockedDocument = broker.getXMLResource(XmldbURI.create(controllerConfig), LockMode.READ_LOCK)) {
                final DocumentImpl doc = lockedDocument == null ? null : lockedDocument.getDocument();
                if (doc != null) {
                    parse(doc);
                }
            }
        } catch (final EXistException | PermissionDeniedException e) {
            throw new ServletException("Failed to parse controller.xml: " + e.getMessage(), e);
        }
    } else {
        try {
            final Path d = Paths.get(urlRewrite.getConfig().getServletContext().getRealPath("/")).normalize();
            final Path configFile = d.resolve(controllerConfig);
            if (Files.isReadable(configFile)) {
                final Document doc = parseConfig(configFile);
                parse(doc);
            }
        } catch (final ParserConfigurationException | IOException | SAXException e) {
            throw new ServletException("Failed to parse controller.xml: " + e.getMessage(), e);
        }
    }
    urlRewrite.clearCaches();
}
Also used : Path(java.nio.file.Path) EXistException(org.exist.EXistException) IOException(java.io.IOException) Document(org.w3c.dom.Document) LockedDocument(org.exist.dom.persistent.LockedDocument) DocumentImpl(org.exist.dom.persistent.DocumentImpl) SAXException(org.xml.sax.SAXException) ServletException(javax.servlet.ServletException) DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) PermissionDeniedException(org.exist.security.PermissionDeniedException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

EXistException (org.exist.EXistException)218 PermissionDeniedException (org.exist.security.PermissionDeniedException)80 IOException (java.io.IOException)63 DBBroker (org.exist.storage.DBBroker)58 Txn (org.exist.storage.txn.Txn)46 XmldbURI (org.exist.xmldb.XmldbURI)42 Collection (org.exist.collections.Collection)41 SAXException (org.xml.sax.SAXException)32 LockException (org.exist.util.LockException)31 DocumentImpl (org.exist.dom.persistent.DocumentImpl)28 Subject (org.exist.security.Subject)23 XPathException (org.exist.xquery.XPathException)22 LockedDocument (org.exist.dom.persistent.LockedDocument)21 TriggerException (org.exist.collections.triggers.TriggerException)20 Path (java.nio.file.Path)19 URISyntaxException (java.net.URISyntaxException)18 BrokerPool (org.exist.storage.BrokerPool)18 TransactionManager (org.exist.storage.txn.TransactionManager)18 InputSource (org.xml.sax.InputSource)18 Sequence (org.exist.xquery.value.Sequence)17