Search in sources :

Example 6 with BTreeException

use of org.exist.storage.btree.BTreeException in project exist by eXist-db.

the class NativeValueIndex method matchAll.

/**
 * Regular expression search.
 *
 * @param docs               DOCUMENT ME!
 * @param contextSet         DOCUMENT ME!
 * @param axis               DOCUMENT ME!
 * @param expr               DOCUMENT ME!
 * @param qnames             DOCUMENT ME!
 * @param type               like type argument for {@link org.exist.storage.RegexMatcher} constructor
 * @param flags              like flags argument for {@link org.exist.storage.RegexMatcher} constructor
 * @param caseSensitiveQuery DOCUMENT ME!
 * @param result             DOCUMENT ME!
 * @param collator           DOCUMENT ME!
 * @param truncation         The type of string truncation to apply
 * @param watchDog  the watchdog
 * @return DOCUMENT ME!
 * @throws TerminatedException DOCUMENT ME!
 * @throws EXistException      DOCUMENT ME!
 */
public NodeSet matchAll(final XQueryWatchDog watchDog, final DocumentSet docs, final NodeSet contextSet, final int axis, final String expr, final List<QName> qnames, final int type, final int flags, final boolean caseSensitiveQuery, final NodeSet result, final Collator collator, final StringTruncationOperator truncation) throws TerminatedException, EXistException {
    // if the match expression starts with a char sequence, we restrict the index scan to entries starting with
    // the same sequence. Otherwise, we have to scan the whole index.
    final StringValue startTerm;
    if (type == DBBroker.MATCH_REGEXP && expr.startsWith("^") && caseSensitiveQuery == caseSensitive) {
        final StringBuilder term = new StringBuilder();
        for (int j = 1; j < expr.length(); j++) {
            if (Character.isLetterOrDigit(expr.charAt(j))) {
                term.append(expr.charAt(j));
            } else {
                break;
            }
        }
        if (term.length() > 0) {
            startTerm = new StringValue(term.toString());
            LOG.debug("Match will begin index scan at '{}'", startTerm);
        } else {
            startTerm = null;
        }
    } else if (collator == null && (type == DBBroker.MATCH_EXACT || type == DBBroker.MATCH_STARTSWITH)) {
        startTerm = new StringValue(expr);
        LOG.debug("Match will begin index scan at '{}'", startTerm);
    } else {
        startTerm = null;
    }
    // Select appropriate matcher/comparator
    final TermMatcher matcher;
    if (collator == null) {
        switch(type) {
            case DBBroker.MATCH_EXACT:
                matcher = new ExactMatcher(expr);
                break;
            case DBBroker.MATCH_CONTAINS:
                matcher = new ContainsMatcher(expr);
                break;
            case DBBroker.MATCH_STARTSWITH:
                matcher = new StartsWithMatcher(expr);
                break;
            case DBBroker.MATCH_ENDSWITH:
                matcher = new EndsWithMatcher(expr);
                break;
            default:
                matcher = new RegexMatcher(expr, flags);
        }
    } else {
        matcher = new CollatorMatcher(expr, truncation, collator);
    }
    final MatcherCallback cb = new MatcherCallback(docs, contextSet, result, matcher, axis == NodeSet.ANCESTOR);
    for (final Iterator<Collection> iter = docs.getCollectionIterator(); iter.hasNext(); ) {
        final int collectionId = iter.next().getId();
        watchDog.proceed(null);
        if (qnames == null) {
            try (final ManagedLock<ReentrantLock> bfileLock = lockManager.acquireBtreeReadLock(dbValues.getLockName())) {
                final Value searchKey;
                if (startTerm != null) {
                    // Compute a key for the start term in the collection
                    searchKey = new SimpleValue(collectionId, startTerm);
                } else {
                    // Compute a key for an arbitrary string in the collection
                    searchKey = new SimplePrefixValue(collectionId, Type.STRING);
                }
                final IndexQuery query = new IndexQuery(IndexQuery.TRUNC_RIGHT, searchKey);
                dbValues.query(query, cb);
            } catch (final IOException | BTreeException e) {
                LOG.error(e.getMessage(), e);
            } catch (final LockException e) {
                LOG.warn("Failed to acquire lock for '{}'", FileUtils.fileName(dbValues.getFile()), e);
            }
        } else {
            for (final QName qname : qnames) {
                try (final ManagedLock<ReentrantLock> bfileLock = lockManager.acquireBtreeReadLock(dbValues.getLockName())) {
                    final Value searchKey;
                    if (startTerm != null) {
                        searchKey = new QNameValue(collectionId, qname, startTerm, broker.getBrokerPool().getSymbols());
                    } else {
                        LOG.debug("Searching with QName prefix");
                        searchKey = new QNamePrefixValue(collectionId, qname, Type.STRING, broker.getBrokerPool().getSymbols());
                    }
                    final IndexQuery query = new IndexQuery(IndexQuery.TRUNC_RIGHT, searchKey);
                    dbValues.query(query, cb);
                } catch (final IOException | BTreeException e) {
                    LOG.error(e.getMessage(), e);
                } catch (final LockException e) {
                    LOG.warn("Failed to acquire lock for '{}'", FileUtils.fileName(dbValues.getFile()), e);
                }
            }
        }
    }
    return result;
}
Also used : BTreeException(org.exist.storage.btree.BTreeException) StringValue(org.exist.xquery.value.StringValue) ReentrantLock(java.util.concurrent.locks.ReentrantLock) IndexQuery(org.exist.storage.btree.IndexQuery) QName(org.exist.dom.QName) IOException(java.io.IOException) AtomicValue(org.exist.xquery.value.AtomicValue) StringValue(org.exist.xquery.value.StringValue) Value(org.exist.storage.btree.Value) Collection(org.exist.collections.Collection)

Example 7 with BTreeException

use of org.exist.storage.btree.BTreeException in project exist by eXist-db.

the class NGramIndexWorker method search.

public NodeSet search(final int contextId, final DocumentSet docs, final List<QName> qnames, final String query, final String ngram, final XQueryContext context, final NodeSet contextSet, final int axis) throws XPathException {
    final List<QName> searchQnames;
    if (qnames == null || qnames.isEmpty()) {
        searchQnames = getDefinedIndexes(context.getBroker(), docs);
    } else {
        searchQnames = qnames;
    }
    final NodeSet result = new ExtArrayNodeSet(docs.getDocumentCount(), 250);
    for (final Iterator<Collection> iter = docs.getCollectionIterator(); iter.hasNext(); ) {
        final int collectionId = iter.next().getId();
        for (final QName qname : searchQnames) {
            final NGramQNameKey key = new NGramQNameKey(collectionId, qname, index.getBrokerPool().getSymbols(), query);
            try (final ManagedLock<ReentrantLock> dbLock = lockManager.acquireBtreeReadLock(index.db.getLockName())) {
                final SearchCallback cb = new SearchCallback(contextId, query, ngram, docs, contextSet, context, result, axis == NodeSet.ANCESTOR);
                final int op = query.codePointCount(0, query.length()) < getN() ? IndexQuery.TRUNC_RIGHT : IndexQuery.EQ;
                index.db.query(new IndexQuery(op, key), cb);
            } catch (final LockException e) {
                LOG.warn("Failed to acquire lock for '{}'", FileUtils.fileName(index.db.getFile()), e);
            } catch (final IOException | BTreeException e) {
                LOG.error("{} in '{}'", e.getMessage(), FileUtils.fileName(index.db.getFile()), e);
            }
        }
    }
    // ensure result is ready to use
    result.iterate();
    return result;
}
Also used : ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) NodeSet(org.exist.dom.persistent.NodeSet) ReentrantLock(java.util.concurrent.locks.ReentrantLock) IndexQuery(org.exist.storage.btree.IndexQuery) QName(org.exist.dom.QName) IOException(java.io.IOException) BTreeException(org.exist.storage.btree.BTreeException) ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) Collection(org.exist.collections.Collection)

Example 8 with BTreeException

use of org.exist.storage.btree.BTreeException in project exist by eXist-db.

the class SortIndexWorker method registerId.

private void registerId(final short id, final String name) throws EXistException {
    final byte[] key = new byte[1 + UTF8.encoded(name)];
    key[0] = 1;
    UTF8.encode(name, key, 1);
    try (final ManagedLock<ReentrantLock> btreeLock = lockManager.acquireBtreeWriteLock(index.btree.getLockName())) {
        index.btree.addValue(new Value(key), id);
    } catch (final LockException | IOException | BTreeException 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 9 with BTreeException

use of org.exist.storage.btree.BTreeException in project exist by eXist-db.

the class SortIndexWorker method removeId.

private void removeId(final String name) throws EXistException {
    final byte[] key = new byte[1 + UTF8.encoded(name)];
    key[0] = 1;
    UTF8.encode(name, key, 1);
    try (final ManagedLock<ReentrantLock> btreeLock = lockManager.acquireBtreeWriteLock(index.btree.getLockName())) {
        index.btree.removeValue(new Value(key));
    } catch (final LockException | IOException | BTreeException 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 10 with BTreeException

use of org.exist.storage.btree.BTreeException in project exist by eXist-db.

the class SortIndexWorker method remove.

private void remove(final DocumentImpl doc, final short id) throws LockException, EXistException {
    try (final ManagedLock<ReentrantLock> btreeLock = lockManager.acquireBtreeWriteLock(index.btree.getLockName())) {
        final byte[] fromKey = computeKey(id, doc.getDocId());
        final byte[] toKey = computeKey(id, doc.getDocId() + 1);
        final IndexQuery query = new IndexQuery(IndexQuery.RANGE, new Value(fromKey), new Value(toKey));
        index.btree.remove(query, null);
    } catch (final BTreeException | TerminatedException | IOException e) {
        throw new EXistException("Exception caught while deleting sort index: " + e.getMessage(), e);
    }
}
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)

Aggregations

BTreeException (org.exist.storage.btree.BTreeException)20 IOException (java.io.IOException)19 ReentrantLock (java.util.concurrent.locks.ReentrantLock)18 Value (org.exist.storage.btree.Value)15 IndexQuery (org.exist.storage.btree.IndexQuery)12 EXistException (org.exist.EXistException)11 TerminatedException (org.exist.xquery.TerminatedException)7 Collection (org.exist.collections.Collection)6 QName (org.exist.dom.QName)5 AtomicValue (org.exist.xquery.value.AtomicValue)5 StringValue (org.exist.xquery.value.StringValue)5 NodeProxy (org.exist.dom.persistent.NodeProxy)3 XMLStreamException (javax.xml.stream.XMLStreamException)2 ElementValue (org.exist.storage.ElementValue)2 LockException (org.exist.util.LockException)2 UnsynchronizedByteArrayOutputStream (org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)1 DocumentImpl (org.exist.dom.persistent.DocumentImpl)1 ExtArrayNodeSet (org.exist.dom.persistent.ExtArrayNodeSet)1 IStoredNode (org.exist.dom.persistent.IStoredNode)1 NodeSet (org.exist.dom.persistent.NodeSet)1