Search in sources :

Example 6 with LockException

use of org.exist.util.LockException in project exist by eXist-db.

the class LockFunction method eval.

/* (non-Javadoc)
     * @see org.exist.xquery.Function#eval(org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
     */
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    final Sequence docsArg = getArgument(0).eval(contextSequence, contextItem);
    final DocumentSet docs = docsArg.getDocumentSet();
    try (final ManagedLocks<ManagedDocumentLock> managedLocks = docs.lock(context.getBroker(), exclusive)) {
        return getArgument(1).eval(contextSequence, contextItem);
    } catch (final LockException e) {
        throw new XPathException(this, "Could not lock document set", e);
    }
}
Also used : ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) LockException(org.exist.util.LockException) Sequence(org.exist.xquery.value.Sequence) DocumentSet(org.exist.dom.persistent.DocumentSet)

Example 7 with LockException

use of org.exist.util.LockException in project exist by eXist-db.

the class XMLDBDefragment method eval.

/* (non-Javadoc)
     * @see org.exist.xquery.Expression#eval(org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
     */
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // Get nodes
    final NodeSet nodes = args[0].toNodeSet();
    final DocumentSet docs = nodes.getDocumentSet();
    try {
        if (args.length > 1) {
            // Use supplied parameter
            final int splitCount = ((IntegerValue) args[1].itemAt(0)).getInt();
            Modification.checkFragmentation(context, docs, splitCount);
        } else {
            // Use conf.xml configured value or -1 if not existent
            Modification.checkFragmentation(context, docs);
        }
    } catch (final LockException | EXistException e) {
        logger.error("An error occurred while defragmenting documents: {}", e.getMessage());
        throw new XPathException(this, "An error occurred while defragmenting documents: " + e.getMessage(), e);
    }
    return Sequence.EMPTY_SEQUENCE;
}
Also used : NodeSet(org.exist.dom.persistent.NodeSet) LockException(org.exist.util.LockException) XPathException(org.exist.xquery.XPathException) IntegerValue(org.exist.xquery.value.IntegerValue) DocumentSet(org.exist.dom.persistent.DocumentSet) EXistException(org.exist.EXistException)

Example 8 with LockException

use of org.exist.util.LockException in project exist by eXist-db.

the class LocalCollectionManagementService method createCollection.

@Override
public Collection createCollection(final XmldbURI name, final Date created) throws XMLDBException {
    final XmldbURI collName = resolve(name);
    withDb((broker, transaction) -> {
        try {
            final org.exist.collections.Collection coll = broker.getOrCreateCollection(transaction, collName, Optional.ofNullable(created).map(c -> new Tuple2<>(null, c.getTime())));
            try (final ManagedCollectionLock collectionLock = broker.getBrokerPool().getLockManager().acquireCollectionWriteLock(collName)) {
                broker.saveCollection(transaction, coll);
            }
            return null;
        } catch (final LockException | TriggerException e) {
            throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
        }
    });
    return new LocalCollection(user, brokerPool, collection, collName);
}
Also used : XMLDBException(org.xmldb.api.base.XMLDBException) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2) ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) LockedDocument(org.exist.dom.persistent.LockedDocument) BrokerPool(org.exist.storage.BrokerPool) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) ManagedCollectionLock(org.exist.storage.lock.ManagedCollectionLock) PreserveType(org.exist.storage.DBBroker.PreserveType) LockException(org.exist.util.LockException) Subject(org.exist.security.Subject) Optional(java.util.Optional) DocumentImpl(org.exist.dom.persistent.DocumentImpl) EXistException(org.exist.EXistException) TriggerException(org.exist.collections.triggers.TriggerException) ErrorCodes(org.xmldb.api.base.ErrorCodes) Lock(org.exist.storage.lock.Lock) Nullable(javax.annotation.Nullable) Collection(org.xmldb.api.base.Collection) LockException(org.exist.util.LockException) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2) XMLDBException(org.xmldb.api.base.XMLDBException) TriggerException(org.exist.collections.triggers.TriggerException) ManagedCollectionLock(org.exist.storage.lock.ManagedCollectionLock)

Example 9 with LockException

use of org.exist.util.LockException in project exist by eXist-db.

the class TestDataGenerator method generate.

public Path[] generate(final DBBroker broker, final Collection collection, final String xqueryContent) throws SAXException {
    try {
        final DocumentSet docs = collection.allDocs(broker, new DefaultDocumentSet(), true);
        final XQuery service = broker.getBrokerPool().getXQueryService();
        final XQueryContext context = new XQueryContext(broker.getBrokerPool());
        context.declareVariable("filename", "");
        context.declareVariable("count", "0");
        context.setStaticallyKnownDocuments(docs);
        final String query = IMPORT + xqueryContent;
        final CompiledXQuery compiled = service.compile(context, query);
        for (int i = 0; i < count; i++) {
            generatedFiles[i] = Files.createTempFile(prefix, ".xml");
            context.declareVariable("filename", generatedFiles[i].getFileName().toString());
            context.declareVariable("count", new Integer(i));
            final Sequence results = service.execute(broker, compiled, Sequence.EMPTY_SEQUENCE);
            final Serializer serializer = broker.borrowSerializer();
            try (final Writer out = Files.newBufferedWriter(generatedFiles[i], StandardCharsets.UTF_8)) {
                final SAXSerializer sax = new SAXSerializer(out, outputProps);
                serializer.setSAXHandlers(sax, sax);
                for (final SequenceIterator iter = results.iterate(); iter.hasNext(); ) {
                    final Item item = iter.nextItem();
                    if (!Type.subTypeOf(item.getType(), Type.NODE)) {
                        continue;
                    }
                    serializer.toSAX((NodeValue) item);
                }
            } finally {
                broker.returnSerializer(serializer);
            }
        }
    } catch (final XPathException | PermissionDeniedException | LockException | IOException e) {
        LOG.error(e.getMessage(), e);
        throw new SAXException(e.getMessage(), e);
    }
    return generatedFiles;
}
Also used : DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) XPathException(org.exist.xquery.XPathException) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) SAXException(org.xml.sax.SAXException) LockException(org.exist.util.LockException) PermissionDeniedException(org.exist.security.PermissionDeniedException) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) DocumentSet(org.exist.dom.persistent.DocumentSet) SAXSerializer(org.exist.util.serializer.SAXSerializer) SAXSerializer(org.exist.util.serializer.SAXSerializer) Serializer(org.exist.storage.serializers.Serializer)

Example 10 with LockException

use of org.exist.util.LockException in project exist by eXist-db.

the class ExistDocument method delete.

/**
 * Remove document from database.
 */
void delete() {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Deleting {}", xmldbUri);
    }
    // Need to split path into collection and document name
    final XmldbURI collName = xmldbUri.removeLastSegment();
    final XmldbURI docName = xmldbUri.lastSegment();
    final TransactionManager txnManager = brokerPool.getTransactionManager();
    try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
        final Txn txn = txnManager.beginTransaction();
        final Collection collection = broker.openCollection(collName, LockMode.WRITE_LOCK)) {
        // Open collection if possible, else abort
        if (collection == null) {
            LOG.debug("Collection does not exist");
            txnManager.abort(txn);
            return;
        }
        // Open document if possible, else abort
        try (final LockedDocument lockedResource = collection.getDocumentWithLock(broker, docName, LockMode.WRITE_LOCK)) {
            if (lockedResource == null) {
                LOG.debug("No resource found for path: {}", xmldbUri);
                txnManager.abort(txn);
                return;
            }
            final DocumentImpl resource = lockedResource.getDocument();
            if (resource.getResourceType() == DocumentImpl.BINARY_FILE) {
                collection.removeBinaryResource(txn, broker, resource.getFileURI());
            } else {
                collection.removeXMLResource(txn, broker, resource.getFileURI());
            }
            // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
            collection.close();
            // Commit change
            txnManager.commit(txn);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Document deleted sucessfully");
            }
        }
    } catch (final LockException e) {
        LOG.error("Resource is locked.", e);
    } catch (final EXistException | IOException | TriggerException | PermissionDeniedException e) {
        LOG.error(e);
    } finally {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finished delete");
        }
    }
}
Also used : Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) IOException(java.io.IOException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) DBBroker(org.exist.storage.DBBroker) LockException(org.exist.util.LockException) TransactionManager(org.exist.storage.txn.TransactionManager) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) TriggerException(org.exist.collections.triggers.TriggerException) XmldbURI(org.exist.xmldb.XmldbURI)

Aggregations

LockException (org.exist.util.LockException)72 PermissionDeniedException (org.exist.security.PermissionDeniedException)44 EXistException (org.exist.EXistException)29 DocumentImpl (org.exist.dom.persistent.DocumentImpl)26 XmldbURI (org.exist.xmldb.XmldbURI)26 ReentrantLock (java.util.concurrent.locks.ReentrantLock)19 Collection (org.exist.collections.Collection)16 IOException (java.io.IOException)12 Tuple3 (com.evolvedbinary.j8fu.tuple.Tuple3)11 LockedDocument (org.exist.dom.persistent.LockedDocument)11 Value (org.exist.storage.btree.Value)11 Txn (org.exist.storage.txn.Txn)11 TriggerException (org.exist.collections.triggers.TriggerException)10 NodeProxy (org.exist.dom.persistent.NodeProxy)10 DatabaseConfigurationException (org.exist.util.DatabaseConfigurationException)10 QName (org.exist.dom.QName)9 LockMode (org.exist.storage.lock.Lock.LockMode)9 XPathException (org.exist.xquery.XPathException)9 SAXException (org.xml.sax.SAXException)9 Tuple2 (com.evolvedbinary.j8fu.tuple.Tuple2)8