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