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();
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations