use of org.exist.storage.btree.Value 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);
}
}
use of org.exist.storage.btree.Value 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);
}
}
use of org.exist.storage.btree.Value 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;
}
use of org.exist.storage.btree.Value in project exist by eXist-db.
the class NGramIndexWorker method scanIndex.
@Override
public Occurrences[] scanIndex(final XQueryContext context, final DocumentSet docs, final NodeSet contextSet, final Map hints) {
List<QName> qnames = hints == null ? null : (List<QName>) hints.get(QNAMES_KEY);
// Expects a StringValue
final Object start = hints == null ? null : hints.get(START_VALUE);
// Expects a StringValue
final Object end = hints == null ? null : hints.get(END_VALUE);
if (qnames == null || qnames.isEmpty()) {
qnames = getDefinedIndexes(context.getBroker(), docs);
}
// TODO : use the IndexWorker.VALUE_COUNT hint, if present, to limit the number of returned entries
final IndexScanCallback cb = new IndexScanCallback(docs, contextSet);
for (final QName qname : qnames) {
for (final Iterator<Collection> i = docs.getCollectionIterator(); i.hasNext(); ) {
final int collectionId = i.next().getId();
final IndexQuery query;
if (start == null) {
final Value startRef = new NGramQNameKey(collectionId);
query = new IndexQuery(IndexQuery.TRUNC_RIGHT, startRef);
} else if (end == null) {
final Value startRef = new NGramQNameKey(collectionId, qname, index.getBrokerPool().getSymbols(), start.toString().toLowerCase());
query = new IndexQuery(IndexQuery.TRUNC_RIGHT, startRef);
} else {
final Value startRef = new NGramQNameKey(collectionId, qname, index.getBrokerPool().getSymbols(), start.toString().toLowerCase());
final Value endRef = new NGramQNameKey(collectionId, qname, index.getBrokerPool().getSymbols(), end.toString().toLowerCase());
query = new IndexQuery(IndexQuery.BW, startRef, endRef);
}
try (final ManagedLock<ReentrantLock> dbLock = lockManager.acquireBtreeReadLock(index.db.getLockName())) {
index.db.query(query, 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(e.getMessage(), e);
} catch (final TerminatedException e) {
LOG.warn(e.getMessage(), e);
}
}
}
return cb.map.values().toArray(new Occurrences[0]);
}
use of org.exist.storage.btree.Value in project exist by eXist-db.
the class NGramIndexWorker method removeCollection.
@Override
public void removeCollection(final Collection collection, final DBBroker broker, final boolean reindex) {
if (LOG.isDebugEnabled()) {
LOG.debug("Dropping NGram index for collection {}", collection.getURI());
}
try (final ManagedLock<ReentrantLock> dbLock = lockManager.acquireBtreeWriteLock(index.db.getLockName())) {
// remove generic index
final Value value = new NGramQNameKey(collection.getId());
index.db.removeAll(null, new IndexQuery(IndexQuery.TRUNC_RIGHT, value));
} catch (final LockException e) {
LOG.warn("Failed to acquire lock for '{}'", FileUtils.fileName(index.db.getFile()), e);
} catch (final BTreeException | IOException e) {
LOG.error(e.getMessage(), e);
}
}
Aggregations