use of org.exist.storage.btree.BTreeException in project exist by eXist-db.
the class SortIndexWorker method createIndex.
/**
* Create a new sort index identified by a name. The method iterates through all items in
* the items list and adds the nodes to the index. It assumes that the list is already ordered.
*
* @param name the name by which the index will be identified
* @param items ordered list of items to store
*
* @throws EXistException if an error occurs with the database
* @throws LockException if a locking error occurs
*/
public void createIndex(final String name, final List<SortItem> items) throws EXistException, LockException {
// get an id for the new index
final short id = getOrRegisterId(name);
try (final ManagedLock<ReentrantLock> btreeLock = lockManager.acquireBtreeWriteLock(index.btree.getLockName())) {
long idx = 0;
for (final SortItem item : items) {
final byte[] key = computeKey(id, item.getNode());
index.btree.addValue(new Value(key), idx++);
}
} catch (final LockException | IOException | BTreeException e) {
throw new EXistException("Exception caught while creating sort index: " + e.getMessage(), e);
}
}
use of org.exist.storage.btree.BTreeException in project exist by eXist-db.
the class SortIndexWorker method remove.
/**
* Completely remove the index identified by its name.
*
* @param name the name of the index
*
* @throws EXistException if an error occurs with the database
* @throws LockException if a locking error occurs
*/
public void remove(final String name) throws EXistException, LockException {
final short id = getId(name);
try (final ManagedLock<ReentrantLock> btreeLock = lockManager.acquireBtreeWriteLock(index.btree.getLockName())) {
final byte[] fromKey = computeKey(id);
final byte[] toKey = computeKey((short) (id + 1));
final IndexQuery query = new IndexQuery(IndexQuery.RANGE, new Value(fromKey), new Value(toKey));
index.btree.remove(query, null);
removeId(name);
} catch (final BTreeException | TerminatedException | IOException e) {
throw new EXistException("Exception caught while deleting sort index: " + e.getMessage(), e);
}
}
use of org.exist.storage.btree.BTreeException in project exist by eXist-db.
the class SortIndexWorker method remove.
public void remove(final DocumentImpl doc) {
if (index.btree == null)
return;
final byte[] fromKey = new byte[] { 1 };
final byte[] endKey = new byte[] { 2 };
try (final ManagedLock<ReentrantLock> btreeLock = lockManager.acquireBtreeWriteLock(index.btree.getLockName())) {
final IndexQuery query = new IndexQuery(IndexQuery.RANGE, new Value(fromKey), new Value(endKey));
final FindIdCallback callback = new FindIdCallback(true);
index.btree.query(query, callback);
for (final long id : callback.allIds) {
remove(doc, (short) id);
}
} catch (final BTreeException | EXistException | LockException | TerminatedException | IOException e) {
SortIndex.LOG.debug("Exception caught while reading sort index: {}", e.getMessage(), e);
}
}
use of org.exist.storage.btree.BTreeException in project exist by eXist-db.
the class NativeValueIndex method scanIndexKeys.
/**
* Scan all index keys indexed by the given QName. Return {@link org.exist.util.ValueOccurrences} for those index entries pointing to descendants
* of the specified context set. The first argument specifies the set of documents to include in the scan. Nodes which are not in this document
* set will be ignored.
*
* @param docs set of documents to scan
* @param contextSet if != null, return only index entries pointing to nodes which are descendants of nodes in the context set
* @param qnames an array of QNames: defines the index entries to be scanned.
* @param start an optional start value: only index keys starting with or being greater than this start value (depends on the type of the
* index key) will be scanned
* @return a list of ValueOccurrences
*/
public ValueOccurrences[] scanIndexKeys(final DocumentSet docs, final NodeSet contextSet, QName[] qnames, final Indexable start) {
if (qnames == null) {
final List<QName> qnlist = getDefinedIndexes(docs);
qnames = new QName[qnlist.size()];
qnames = qnlist.toArray(qnames);
}
final int type = start.getType();
final boolean stringType = Type.subTypeOf(type, Type.STRING);
final IndexScanCallback cb = new IndexScanCallback(docs, contextSet, type, true);
for (final QName qname : qnames) {
for (final Iterator<Collection> i = docs.getCollectionIterator(); i.hasNext(); ) {
try (final ManagedLock<ReentrantLock> bfileLock = lockManager.acquireBtreeReadLock(dbValues.getLockName())) {
final int collectionId = i.next().getId();
// Compute a key for the start value in the collection
final Value startKey = new QNameValue(collectionId, qname, start, broker.getBrokerPool().getSymbols());
if (stringType) {
final IndexQuery query = new IndexQuery(IndexQuery.TRUNC_RIGHT, startKey);
dbValues.query(query, cb);
} else {
final Value prefixKey = new QNamePrefixValue(collectionId, qname, start.getType(), broker.getBrokerPool().getSymbols());
final IndexQuery query = new IndexQuery(IndexQuery.GEQ, startKey);
dbValues.query(query, prefixKey, cb);
}
} catch (final EXistException | BTreeException | IOException | TerminatedException e) {
LOG.error(e.getMessage(), e);
} catch (final LockException e) {
LOG.warn("Failed to acquire lock for '{}'", FileUtils.fileName(dbValues.getFile()), e);
}
}
}
final Map<AtomicValue, ValueOccurrences> map = cb.map;
final ValueOccurrences[] result = new ValueOccurrences[map.size()];
return map.values().toArray(result);
}
use of org.exist.storage.btree.BTreeException in project exist by eXist-db.
the class NativeValueIndex method scanIndexKeys.
public ValueOccurrences[] scanIndexKeys(final DocumentSet docs, final NodeSet contextSet, final Indexable start) {
final int type = start.getType();
final boolean stringType = Type.subTypeOf(type, Type.STRING);
final IndexScanCallback cb = new IndexScanCallback(docs, contextSet, type, false);
for (final Iterator<Collection> i = docs.getCollectionIterator(); i.hasNext(); ) {
try (final ManagedLock<ReentrantLock> bfileLock = lockManager.acquireBtreeReadLock(dbValues.getLockName())) {
final Collection c = i.next();
final int collectionId = c.getId();
// Compute a key for the start value in the collection
final Value startKey = new SimpleValue(collectionId, start);
if (stringType) {
final IndexQuery query = new IndexQuery(IndexQuery.TRUNC_RIGHT, startKey);
dbValues.query(query, cb);
} else {
final Value prefixKey = new SimplePrefixValue(collectionId, start.getType());
final IndexQuery query = new IndexQuery(IndexQuery.GEQ, startKey);
dbValues.query(query, prefixKey, cb);
}
} catch (final EXistException | IOException | TerminatedException | BTreeException e) {
LOG.error(e.getMessage(), e);
} catch (final LockException e) {
LOG.warn("Failed to acquire lock for '{}'", FileUtils.fileName(dbValues.getFile()), e);
}
}
final Map<AtomicValue, ValueOccurrences> map = cb.map;
final ValueOccurrences[] result = new ValueOccurrences[map.size()];
return map.values().toArray(result);
}
Aggregations