Search in sources :

Example 11 with TerminatedException

use of org.exist.xquery.TerminatedException 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 12 with TerminatedException

use of org.exist.xquery.TerminatedException in project exist by eXist-db.

the class NativeBroker method removeCollectionsDocumentNodes.

private void removeCollectionsDocumentNodes(final Txn transaction, @EnsureLocked(mode = LockMode.WRITE_LOCK) final Collection collection) throws TriggerException, PermissionDeniedException, LockException, IOException {
    final DocumentTrigger docTrigger = new DocumentTriggers(this, transaction, collection);
    for (final Iterator<DocumentImpl> itDocument = collection.iteratorNoLock(this); itDocument.hasNext(); ) {
        // NOTE: we already have a WRITE_LOCK on the collection
        final DocumentImpl doc = itDocument.next();
        docTrigger.beforeDeleteDocument(this, transaction, doc);
        // Remove doc's metadata
        // WM: now removed in one step. see above.
        // removeResourceMetadata(transaction, doc);
        // Remove document nodes' index entries
        new DOMTransaction(this, domDb, () -> lockManager.acquireBtreeWriteLock(domDb.getLockName())) {

            @Override
            public Object start() {
                try {
                    final Value ref = new NodeRef(doc.getDocId());
                    final IndexQuery query = new IndexQuery(IndexQuery.TRUNC_RIGHT, ref);
                    domDb.remove(transaction, query, null);
                } catch (final BTreeException e) {
                    LOG.error("btree error while removing document", e);
                } catch (final IOException e) {
                    LOG.error("io error while removing document", e);
                } catch (final TerminatedException e) {
                    LOG.error("method terminated", e);
                }
                return null;
            }
        }.run();
        // Remove nodes themselves
        new DOMTransaction(this, domDb, () -> lockManager.acquireBtreeWriteLock(domDb.getLockName())) {

            @Override
            public Object start() {
                if (doc.getResourceType() == DocumentImpl.XML_FILE) {
                    final NodeHandle node = (NodeHandle) doc.getFirstChild();
                    domDb.removeAll(transaction, node.getInternalAddress());
                }
                return null;
            }
        }.run();
        // if it is a binary document remove the content from disk
        if (doc instanceof BinaryDocument) {
            removeCollectionBinary(transaction, (BinaryDocument) doc);
        }
        docTrigger.afterDeleteDocument(this, transaction, doc.getURI());
        // Make doc's id available again
        collectionsDb.freeResourceId(doc.getDocId());
    }
}
Also used : DOMTransaction(org.exist.storage.dom.DOMTransaction) PooledObject(org.apache.commons.pool2.PooledObject) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) TerminatedException(org.exist.xquery.TerminatedException)

Example 13 with TerminatedException

use of org.exist.xquery.TerminatedException in project exist by eXist-db.

the class NativeBroker method dropDomNodes.

private void dropDomNodes(final Txn transaction, final DocumentImpl document) {
    try {
        if (!document.isReferenced()) {
            new DOMTransaction(this, domDb, () -> lockManager.acquireBtreeWriteLock(domDb.getLockName())) {

                @Override
                public Object start() {
                    final NodeHandle node = (NodeHandle) document.getFirstChild();
                    domDb.removeAll(transaction, node.getInternalAddress());
                    return null;
                }
            }.run();
        }
    } catch (final NullPointerException npe0) {
        LOG.error("Caught NPE in DOMTransaction to actually be able to remove the document.");
    }
    final NodeRef ref = new NodeRef(document.getDocId());
    final IndexQuery idx = new IndexQuery(IndexQuery.TRUNC_RIGHT, ref);
    new DOMTransaction(this, domDb, () -> lockManager.acquireBtreeWriteLock(domDb.getLockName())) {

        @Override
        public Object start() {
            try {
                domDb.remove(transaction, idx, null);
            } catch (final BTreeException | IOException e) {
                LOG.error("start() - " + "error while removing doc", e);
            } catch (final TerminatedException e) {
                LOG.error("method terminated", e);
            }
            return null;
        }
    }.run();
}
Also used : PooledObject(org.apache.commons.pool2.PooledObject) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) DOMTransaction(org.exist.storage.dom.DOMTransaction) TerminatedException(org.exist.xquery.TerminatedException)

Example 14 with TerminatedException

use of org.exist.xquery.TerminatedException in project exist by eXist-db.

the class NativeBroker method defragXMLResource.

@Override
public void defragXMLResource(final Txn transaction, final DocumentImpl doc) {
    // TODO : use dedicated function in XmldbURI
    if (LOG.isDebugEnabled())
        LOG.debug("============> Defragmenting document {}", doc.getURI());
    final long start = System.currentTimeMillis();
    try {
        final long firstChild = doc.getFirstChildAddress();
        // dropping old structure index
        dropIndex(transaction, doc);
        // dropping dom index
        final NodeRef ref = new NodeRef(doc.getDocId());
        final IndexQuery idx = new IndexQuery(IndexQuery.TRUNC_RIGHT, ref);
        new DOMTransaction(this, domDb, () -> lockManager.acquireBtreeWriteLock(domDb.getLockName())) {

            @Override
            public Object start() {
                try {
                    domDb.remove(transaction, idx, null);
                    domDb.flush();
                } catch (final IOException | DBException e) {
                    LOG.error("start() - " + "error while removing doc", e);
                } catch (final TerminatedException e) {
                    LOG.error("method terminated", e);
                }
                return null;
            }
        }.run();
        // create a copy of the old doc to copy the nodes into it
        final DocumentImpl tempDoc = new DocumentImpl(pool, doc.getCollection(), doc.getDocId(), doc.getFileURI());
        tempDoc.copyOf(this, doc, doc);
        final StreamListener listener = getIndexController().getStreamListener(doc, ReindexMode.STORE);
        // copy the nodes
        final NodeList nodes = doc.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            final IStoredNode<?> node = (IStoredNode<?>) nodes.item(i);
            try (final INodeIterator iterator = getNodeIterator(node)) {
                iterator.next();
                copyNodes(transaction, iterator, node, new NodePath2(), tempDoc, true, listener);
            }
        }
        flush();
        // remove the old nodes
        new DOMTransaction(this, domDb, () -> lockManager.acquireBtreeWriteLock(domDb.getLockName())) {

            @Override
            public Object start() {
                domDb.removeAll(transaction, firstChild);
                try {
                    domDb.flush();
                } catch (final DBException e) {
                    LOG.error("start() - error while removing doc", e);
                }
                return null;
            }
        }.run();
        doc.copyChildren(tempDoc);
        doc.setSplitCount(0);
        doc.setPageCount(tempDoc.getPageCount());
        storeXMLResource(transaction, doc);
        closeDocument();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Defragmentation took {} ms.", (System.currentTimeMillis() - start));
        }
    } catch (final PermissionDeniedException | IOException e) {
        LOG.error(e);
    }
}
Also used : NodeList(org.w3c.dom.NodeList) DOMTransaction(org.exist.storage.dom.DOMTransaction) INodeIterator(org.exist.storage.dom.INodeIterator) PooledObject(org.apache.commons.pool2.PooledObject) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) TerminatedException(org.exist.xquery.TerminatedException) StreamListener(org.exist.indexing.StreamListener)

Example 15 with TerminatedException

use of org.exist.xquery.TerminatedException 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)

Aggregations

TerminatedException (org.exist.xquery.TerminatedException)19 EXistException (org.exist.EXistException)9 IOException (java.io.IOException)8 ReentrantLock (java.util.concurrent.locks.ReentrantLock)8 PermissionDeniedException (org.exist.security.PermissionDeniedException)6 BTreeException (org.exist.storage.btree.BTreeException)6 IndexQuery (org.exist.storage.btree.IndexQuery)6 Value (org.exist.storage.btree.Value)6 XMLStreamException (javax.xml.stream.XMLStreamException)3 PooledObject (org.apache.commons.pool2.PooledObject)3 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)3 Collection (org.exist.collections.Collection)3 DOMTransaction (org.exist.storage.dom.DOMTransaction)3 XmldbURI (org.exist.xmldb.XmldbURI)3 XPathException (org.exist.xquery.XPathException)3 DateTimeValue (org.exist.xquery.value.DateTimeValue)3 Path (java.nio.file.Path)2 ACLPermission (org.exist.security.ACLPermission)2 Permission (org.exist.security.Permission)2 DBBroker (org.exist.storage.DBBroker)2