use of org.exist.storage.btree.BTreeException 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.BTreeException 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.BTreeException 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.BTreeException 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);
}
}
use of org.exist.storage.btree.BTreeException in project exist by eXist-db.
the class RawNodeIterator method seek.
@Override
public final void seek(final NodeHandle node) throws IOException {
try (final ManagedLock<ReentrantLock> domFileLock = lockManager.acquireBtreeReadLock(db.getLockName())) {
RecordPos rec = null;
if (StorageAddress.hasAddress(node.getInternalAddress())) {
rec = db.findRecord(node.getInternalAddress());
}
if (rec == null) {
try {
final long address = db.findValue(broker, new NodeProxy(node));
if (address == BTree.KEY_NOT_FOUND) {
throw new IOException("Node not found.");
}
rec = db.findRecord(address);
} catch (final BTreeException e) {
throw new IOException("Node not found: " + e.getMessage());
}
}
pageNum = rec.getPage().getPageNum();
// Position the stream at the very beginning of the record
offset = rec.offset - DOMFile.LENGTH_TID;
page = rec.getPage();
} catch (final LockException e) {
throw new IOException("Exception while scanning document: " + e.getMessage());
}
}
Aggregations