Search in sources :

Example 26 with LockException

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

the class ExtCollection method eval.

@Override
public Sequence eval(final Sequence contextSequence, final Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
        if (contextItem != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
        }
    }
    final List<String> args = getParameterValues(contextSequence, contextItem);
    final Sequence result;
    try {
        if (args.isEmpty()) {
            final Sequence docs = toSequence(context.getStaticallyKnownDocuments());
            final Sequence dynamicCollection = context.getDynamicallyAvailableCollection("");
            if (dynamicCollection != null) {
                result = new ValueSequence();
                result.addAll(docs);
                result.addAll(dynamicCollection);
            } else {
                result = docs;
            }
        } else {
            final Sequence dynamicCollection = context.getDynamicallyAvailableCollection(asUri(args.get(0)).toString());
            if (dynamicCollection != null) {
                result = dynamicCollection;
            } else {
                final MutableDocumentSet ndocs = new DefaultDocumentSet();
                for (final String next : args) {
                    final XmldbURI uri = new AnyURIValue(next).toXmldbURI();
                    try (final Collection coll = context.getBroker().openCollection(uri, Lock.LockMode.READ_LOCK)) {
                        if (coll == null) {
                            if (context.isRaiseErrorOnFailedRetrieval()) {
                                throw new XPathException(this, ErrorCodes.FODC0002, "Can not access collection '" + uri + "'");
                            }
                        } else {
                            if (context.inProtectedMode()) {
                                context.getProtectedDocs().getDocsByCollection(coll, ndocs);
                            } else {
                                coll.allDocs(context.getBroker(), ndocs, includeSubCollections, context.getProtectedDocs());
                            }
                        }
                    }
                }
                result = toSequence(ndocs);
            }
        }
    } catch (final XPathException e) {
        // From AnyURIValue constructor
        e.setLocation(line, column);
        Sequence flattenedArgs = Sequence.EMPTY_SEQUENCE;
        try {
            flattenedArgs = argsToSeq(contextSequence, contextItem);
        } catch (final XPathException xe) {
            LOG.warn(e.getMessage(), xe);
        }
        throw new XPathException(this, ErrorCodes.FODC0002, e.getMessage(), flattenedArgs, e);
    } catch (final PermissionDeniedException e) {
        Sequence flattenedArgs = Sequence.EMPTY_SEQUENCE;
        try {
            flattenedArgs = argsToSeq(contextSequence, contextItem);
        } catch (final XPathException xe) {
            LOG.warn(e.getMessage(), xe);
        }
        throw new XPathException(this, ErrorCodes.FODC0002, "Can not access collection '" + e.getMessage() + "'", flattenedArgs, e);
    } catch (final LockException e) {
        Sequence flattenedArgs = Sequence.EMPTY_SEQUENCE;
        try {
            flattenedArgs = argsToSeq(contextSequence, contextItem);
        } catch (final XPathException xe) {
            LOG.warn(e.getMessage(), xe);
        }
        throw new XPathException(this, ErrorCodes.FODC0002, e.getMessage(), flattenedArgs, e);
    }
    // iterate through all docs and create the node set
    registerUpdateListener();
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : LockException(org.exist.util.LockException) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) XmldbURI(org.exist.xmldb.XmldbURI)

Example 27 with LockException

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

the class FluentBrokerAPITest method collectionThenCollectionAndDoc.

@Test
public void collectionThenCollectionAndDoc() throws PermissionDeniedException, EXistException, LockException {
    final XmldbURI docUri = uri("all-test.xml");
    final long collectionCreated = 1234;
    final IMocksControl ctrl = createStrictControl();
    ctrl.checkOrder(true);
    final BrokerPool mockBrokerPool = ctrl.createMock(BrokerPool.class);
    final DBBroker mockBroker = ctrl.createMock(DBBroker.class);
    final Collection mockCollection = ctrl.createMock(Collection.class);
    final LockedDocument mockLockedDocument = ctrl.createMock(LockedDocument.class);
    final DocumentImpl mockDocument = ctrl.createMock(DocumentImpl.class);
    expect(mockBrokerPool.getBroker()).andReturn(mockBroker);
    expect(mockBroker.openCollection(TEST_COLLECTION_URI, READ_LOCK)).andReturn(mockCollection);
    expect(mockCollection.getCreated()).andReturn(collectionCreated);
    expect(mockCollection.getDocumentWithLock(mockBroker, docUri, READ_LOCK)).andReturn(mockLockedDocument);
    expect(mockLockedDocument.getDocument()).andReturn(mockDocument);
    expect(mockCollection.getURI()).andReturn(TEST_COLLECTION_URI);
    expect(mockDocument.getFileURI()).andReturn(docUri);
    // NOTE: checks that Collection lock is release before Document lock
    mockCollection.close();
    mockLockedDocument.close();
    mockBroker.close();
    ctrl.replay();
    final Function<Collection, Long> collectionOp = collection -> collection.getCreated();
    final BiFunction<Collection, DocumentImpl, String> collectionDocOp = (collection, doc) -> collection.getURI().append(doc.getFileURI()).toString();
    final Tuple2<Long, String> result = FluentBrokerAPI.builder(mockBrokerPool).withCollection(TEST_COLLECTION_URI, READ_LOCK).execute(collectionOp).withDocument(collection -> new Tuple2<>(docUri, READ_LOCK)).execute(collectionDocOp).doAll();
    assertEquals(collectionCreated, result._1.longValue());
    assertEquals(TEST_COLLECTION_URI.append(docUri), result._2);
    ctrl.verify();
}
Also used : Tuple3(com.evolvedbinary.j8fu.tuple.Tuple3) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2) IMocksControl(org.easymock.IMocksControl) LockMode(org.exist.storage.lock.Lock.LockMode) LockedDocument(org.exist.dom.persistent.LockedDocument) EasyMock.createStrictControl(org.easymock.EasyMock.createStrictControl) BiFunction(java.util.function.BiFunction) Test(org.junit.Test) PermissionDeniedException(org.exist.security.PermissionDeniedException) EasyMock.expect(org.easymock.EasyMock.expect) Function(java.util.function.Function) FluentBrokerAPI.uri(org.exist.storage.FluentBrokerAPI.uri) LockException(org.exist.util.LockException) Collection(org.exist.collections.Collection) XmldbURI(org.exist.xmldb.XmldbURI) DocumentImpl(org.exist.dom.persistent.DocumentImpl) EXistException(org.exist.EXistException) Lock(org.exist.storage.lock.Lock) Assert.assertEquals(org.junit.Assert.assertEquals) DocumentImpl(org.exist.dom.persistent.DocumentImpl) IMocksControl(org.easymock.IMocksControl) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) XmldbURI(org.exist.xmldb.XmldbURI) Test(org.junit.Test)

Example 28 with LockException

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

the class FluentBrokerAPITest method collectionOnly.

@Test
public void collectionOnly() throws PermissionDeniedException, EXistException, LockException {
    final long collectionCreated = 1234;
    final IMocksControl ctrl = createStrictControl();
    ctrl.checkOrder(true);
    final BrokerPool mockBrokerPool = ctrl.createMock(BrokerPool.class);
    final DBBroker mockBroker = ctrl.createMock(DBBroker.class);
    final Collection mockCollection = ctrl.createMock(Collection.class);
    expect(mockBrokerPool.getBroker()).andReturn(mockBroker);
    expect(mockBroker.openCollection(TEST_COLLECTION_URI, READ_LOCK)).andReturn(mockCollection);
    expect(mockCollection.getCreated()).andReturn(collectionCreated);
    mockCollection.close();
    mockBroker.close();
    ctrl.replay();
    final Function<Collection, Long> collectionOp = collection -> collection.getCreated();
    final long result = FluentBrokerAPI.builder(mockBrokerPool).withCollection(TEST_COLLECTION_URI, READ_LOCK).execute(collectionOp).doAll();
    assertEquals(collectionCreated, result);
    ctrl.verify();
}
Also used : IMocksControl(org.easymock.IMocksControl) Tuple3(com.evolvedbinary.j8fu.tuple.Tuple3) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2) IMocksControl(org.easymock.IMocksControl) LockMode(org.exist.storage.lock.Lock.LockMode) LockedDocument(org.exist.dom.persistent.LockedDocument) EasyMock.createStrictControl(org.easymock.EasyMock.createStrictControl) BiFunction(java.util.function.BiFunction) Test(org.junit.Test) PermissionDeniedException(org.exist.security.PermissionDeniedException) EasyMock.expect(org.easymock.EasyMock.expect) Function(java.util.function.Function) FluentBrokerAPI.uri(org.exist.storage.FluentBrokerAPI.uri) LockException(org.exist.util.LockException) Collection(org.exist.collections.Collection) XmldbURI(org.exist.xmldb.XmldbURI) DocumentImpl(org.exist.dom.persistent.DocumentImpl) EXistException(org.exist.EXistException) Lock(org.exist.storage.lock.Lock) Assert.assertEquals(org.junit.Assert.assertEquals) Collection(org.exist.collections.Collection) Test(org.junit.Test)

Example 29 with LockException

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

the class RemoveIndex method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    final SortIndexWorker index = (SortIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(SortIndex.ID);
    final String id = args[0].getStringValue();
    try {
        if (getArgumentCount() == 2) {
            final NodeValue nv = (NodeValue) args[1].itemAt(0);
            if (nv.getImplementationType() == NodeValue.IN_MEMORY_NODE)
                throw new XPathException(this, "Second argument to remove should be a persistent node, not " + "an in-memory node.");
            final NodeProxy proxy = (NodeProxy) nv;
            index.remove(id, proxy.getOwnerDocument());
        } else {
            index.remove(id);
        }
    } catch (final EXistException e) {
        throw new XPathException(this, e.getMessage(), e);
    } catch (final LockException e) {
        throw new XPathException(this, "Caught lock error while removing index. Giving up.", e);
    }
    return Sequence.EMPTY_SEQUENCE;
}
Also used : SortIndexWorker(org.exist.indexing.sort.SortIndexWorker) LockException(org.exist.util.LockException) EXistException(org.exist.EXistException) NodeProxy(org.exist.dom.persistent.NodeProxy)

Example 30 with LockException

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

the class RangeIndexWorker method removeCollection.

@Override
public void removeCollection(Collection collection, DBBroker broker, boolean reindex) throws PermissionDeniedException {
    if (LOG.isDebugEnabled())
        LOG.debug("Removing collection {}", collection.getURI());
    IndexWriter writer = null;
    try {
        writer = index.getWriter();
        for (Iterator<DocumentImpl> i = collection.iterator(broker); i.hasNext(); ) {
            DocumentImpl doc = i.next();
            final BytesRefBuilder bytes = new BytesRefBuilder();
            NumericUtils.intToPrefixCoded(doc.getDocId(), 0, bytes);
            Term dt = new Term(FIELD_DOC_ID, bytes.toBytesRef());
            writer.deleteDocuments(dt);
        }
    } catch (IOException | PermissionDeniedException | LockException e) {
        LOG.error("Error while removing lucene index: {}", e.getMessage(), e);
    } finally {
        index.releaseWriter(writer);
        if (reindex) {
            try {
                index.sync();
            } catch (DBException e) {
                LOG.warn("Exception during reindex: {}", e.getMessage(), e);
            }
        }
        mode = ReindexMode.STORE;
    }
    if (LOG.isDebugEnabled())
        LOG.debug("Collection removed.");
}
Also used : DBException(org.exist.storage.btree.DBException) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) LockException(org.exist.util.LockException) PermissionDeniedException(org.exist.security.PermissionDeniedException) IOException(java.io.IOException) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

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