Search in sources :

Example 11 with BTreeException

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);
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) BTreeException(org.exist.storage.btree.BTreeException) Value(org.exist.storage.btree.Value) IOException(java.io.IOException) EXistException(org.exist.EXistException)

Example 12 with BTreeException

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;
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) BTreeException(org.exist.storage.btree.BTreeException) IndexQuery(org.exist.storage.btree.IndexQuery) Value(org.exist.storage.btree.Value) IOException(java.io.IOException) EXistException(org.exist.EXistException) TerminatedException(org.exist.xquery.TerminatedException)

Example 13 with BTreeException

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]);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) IndexQuery(org.exist.storage.btree.IndexQuery) QName(org.exist.dom.QName) IOException(java.io.IOException) BTreeException(org.exist.storage.btree.BTreeException) ElementValue(org.exist.storage.ElementValue) Value(org.exist.storage.btree.Value) Collection(org.exist.collections.Collection)

Example 14 with BTreeException

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);
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) BTreeException(org.exist.storage.btree.BTreeException) IndexQuery(org.exist.storage.btree.IndexQuery) ElementValue(org.exist.storage.ElementValue) Value(org.exist.storage.btree.Value) IOException(java.io.IOException)

Example 15 with BTreeException

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());
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) BTreeException(org.exist.storage.btree.BTreeException) LockException(org.exist.util.LockException) IOException(java.io.IOException) NodeProxy(org.exist.dom.persistent.NodeProxy)

Aggregations

BTreeException (org.exist.storage.btree.BTreeException)20 IOException (java.io.IOException)19 ReentrantLock (java.util.concurrent.locks.ReentrantLock)18 Value (org.exist.storage.btree.Value)15 IndexQuery (org.exist.storage.btree.IndexQuery)12 EXistException (org.exist.EXistException)11 TerminatedException (org.exist.xquery.TerminatedException)7 Collection (org.exist.collections.Collection)6 QName (org.exist.dom.QName)5 AtomicValue (org.exist.xquery.value.AtomicValue)5 StringValue (org.exist.xquery.value.StringValue)5 NodeProxy (org.exist.dom.persistent.NodeProxy)3 XMLStreamException (javax.xml.stream.XMLStreamException)2 ElementValue (org.exist.storage.ElementValue)2 LockException (org.exist.util.LockException)2 UnsynchronizedByteArrayOutputStream (org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)1 DocumentImpl (org.exist.dom.persistent.DocumentImpl)1 ExtArrayNodeSet (org.exist.dom.persistent.ExtArrayNodeSet)1 IStoredNode (org.exist.dom.persistent.IStoredNode)1 NodeSet (org.exist.dom.persistent.NodeSet)1