Search in sources :

Example 16 with BTreeException

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);
    }
}
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 17 with BTreeException

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);
    }
}
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 18 with BTreeException

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

Example 19 with BTreeException

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

Example 20 with BTreeException

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

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