Search in sources :

Example 1 with IndexQuery

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

the class BFile method getEntries.

public ArrayList<Value> getEntries() throws IOException, BTreeException, TerminatedException {
    final IndexQuery query = new IndexQuery(IndexQuery.ANY, "");
    final FindCallback cb = new FindCallback(FindCallback.BOTH);
    query(query, cb);
    return cb.getValues();
}
Also used : IndexQuery(org.exist.storage.btree.IndexQuery)

Example 2 with IndexQuery

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

the class NativeValueIndex method dropIndex.

@Override
public void dropIndex(final Collection collection) {
    try (final ManagedLock<ReentrantLock> bfileLock = lockManager.acquireBtreeWriteLock(dbValues.getLockName())) {
        flush();
        // remove generic index
        Value ref = new SimpleValue(collection.getId());
        dbValues.removeAll(null, new IndexQuery(IndexQuery.TRUNC_RIGHT, ref));
        // remove QName index
        ref = new QNameValue(collection.getId());
        dbValues.removeAll(null, new IndexQuery(IndexQuery.TRUNC_RIGHT, ref));
    } catch (final LockException e) {
        LOG.warn("Failed to acquire lock for '{}'", FileUtils.fileName(dbValues.getFile()), e);
    } catch (final BTreeException | IOException e) {
        LOG.error(e.getMessage(), e);
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) BTreeException(org.exist.storage.btree.BTreeException) IndexQuery(org.exist.storage.btree.IndexQuery) AtomicValue(org.exist.xquery.value.AtomicValue) StringValue(org.exist.xquery.value.StringValue) Value(org.exist.storage.btree.Value) IOException(java.io.IOException)

Example 3 with IndexQuery

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

the class NativeValueIndex method findAll.

/**
 * find.
 *
 * @param comparison The type of comparison the search is performing
 * @param docs       The documents to search for matches within
 * @param contextSet DOCUMENT ME!
 * @param axis       DOCUMENT ME!
 * @param qnames     DOCUMENT ME!
 * @param value      right hand comparison value
 * @param result     DOCUMENT ME!
 * @return DOCUMENT ME!
 * @throws TerminatedException DOCUMENT ME!
 */
private NodeSet findAll(final XQueryWatchDog watchDog, final Comparison comparison, final DocumentSet docs, final NodeSet contextSet, final int axis, final List<QName> qnames, final Indexable value, final NodeSet result) throws TerminatedException {
    final SearchCallback cb = new SearchCallback(docs, contextSet, result, axis == NodeSet.ANCESTOR);
    final int idxOp = toIndexQueryOp(comparison);
    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 = new SimpleValue(collectionId, value);
                final IndexQuery query = new IndexQuery(idxOp, searchKey);
                if (idxOp == IndexQuery.EQ) {
                    dbValues.query(query, cb);
                } else {
                    final Value prefixKey = new SimplePrefixValue(collectionId, value.getType());
                    dbValues.query(query, prefixKey, cb);
                }
            } catch (final EXistException | BTreeException | IOException 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())) {
                    // Compute a key for the value in the collection
                    final Value searchKey = new QNameValue(collectionId, qname, value, broker.getBrokerPool().getSymbols());
                    final IndexQuery query = new IndexQuery(idxOp, searchKey);
                    if (idxOp == IndexQuery.EQ) {
                        dbValues.query(query, cb);
                    } else {
                        final Value prefixKey = new QNamePrefixValue(collectionId, qname, value.getType(), broker.getBrokerPool().getSymbols());
                        dbValues.query(query, prefixKey, cb);
                    }
                } catch (final EXistException | BTreeException | IOException 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 : ReentrantLock(java.util.concurrent.locks.ReentrantLock) IndexQuery(org.exist.storage.btree.IndexQuery) QName(org.exist.dom.QName) EXistException(org.exist.EXistException) IOException(java.io.IOException) BTreeException(org.exist.storage.btree.BTreeException) AtomicValue(org.exist.xquery.value.AtomicValue) StringValue(org.exist.xquery.value.StringValue) Value(org.exist.storage.btree.Value) Collection(org.exist.collections.Collection)

Example 4 with IndexQuery

use of org.exist.storage.btree.IndexQuery 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 5 with IndexQuery

use of org.exist.storage.btree.IndexQuery 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)

Aggregations

IndexQuery (org.exist.storage.btree.IndexQuery)26 ReentrantLock (java.util.concurrent.locks.ReentrantLock)19 Value (org.exist.storage.btree.Value)19 IOException (java.io.IOException)12 BTreeException (org.exist.storage.btree.BTreeException)12 QName (org.exist.dom.QName)9 EXistException (org.exist.EXistException)7 PermissionDeniedException (org.exist.security.PermissionDeniedException)7 DatabaseConfigurationException (org.exist.util.DatabaseConfigurationException)7 LockException (org.exist.util.LockException)7 Collection (org.exist.collections.Collection)6 TerminatedException (org.exist.xquery.TerminatedException)6 AtomicValue (org.exist.xquery.value.AtomicValue)5 StringValue (org.exist.xquery.value.StringValue)5 NodeId (org.exist.numbering.NodeId)4 DOMFile (org.exist.storage.dom.DOMFile)4 DocumentImpl (org.exist.dom.persistent.DocumentImpl)3 NewArrayNodeSet (org.exist.dom.persistent.NewArrayNodeSet)3 NodeIdFactory (org.exist.numbering.NodeIdFactory)3 TransactionManager (org.exist.storage.txn.TransactionManager)3