use of org.exist.storage.lock.LockManager in project exist by eXist-db.
the class ExtCollection method toSequence.
private Sequence toSequence(final DocumentSet docs) throws XPathException {
final Sequence result = new ValueSequence();
final LockManager lockManager = context.getBroker().getBrokerPool().getLockManager();
for (final Iterator<DocumentImpl> i = docs.getDocumentIterator(); i.hasNext(); ) {
final DocumentImpl doc = i.next();
// filter out binary documents, fn:collection should only return XML documents
if (doc.getResourceType() == DocumentImpl.XML_FILE) {
ManagedDocumentLock dlock = null;
try {
if (!context.inProtectedMode()) {
dlock = lockManager.acquireDocumentReadLock(doc.getURI());
}
result.add(new NodeProxy(doc));
} catch (final LockException e) {
throw new XPathException(this, ErrorCodes.FODC0002, e);
} finally {
if (dlock != null) {
dlock.close();
}
}
}
}
return result;
}
use of org.exist.storage.lock.LockManager in project exist by eXist-db.
the class InMemoryOutputStream method stream.
public void stream(final XmldbURL xmldbURL, final InputStream is, @Deprecated final int length) throws IOException {
BrokerPool db;
try {
db = BrokerPool.getInstance();
} catch (EXistException e) {
throw new IOException(e);
}
try (final DBBroker broker = db.getBroker()) {
final XmldbURI collectionUri = XmldbURI.create(xmldbURL.getCollection());
final XmldbURI documentUri = XmldbURI.create(xmldbURL.getDocumentName());
final TransactionManager transact = db.getTransactionManager();
try (final Txn txn = transact.beginTransaction();
final Collection collection = broker.getOrCreateCollection(txn, collectionUri)) {
if (collection == null) {
throw new IOException("Resource " + collectionUri.toString() + " is not a collection.");
}
final LockManager lockManager = db.getLockManager();
txn.acquireCollectionLock(() -> lockManager.acquireCollectionWriteLock(collectionUri));
if (collection.hasChildCollection(broker, documentUri)) {
throw new IOException("Resource " + documentUri.toString() + " is a collection.");
}
try (final FilterInputStreamCache cache = FilterInputStreamCacheFactory.getCacheInstance(() -> (String) broker.getConfiguration().getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY), is);
final CachingFilterInputStream cfis = new CachingFilterInputStream(cache)) {
final MimeType mime = MimeTable.getInstance().getContentTypeFor(documentUri);
try (final ManagedDocumentLock lock = lockManager.acquireDocumentWriteLock(documentUri)) {
broker.storeDocument(txn, documentUri, new CachingFilterInputStreamInputSource(cfis), mime, collection);
}
}
txn.commit();
}
} catch (final IOException ex) {
LOG.debug(ex);
throw ex;
} catch (final Exception ex) {
LOG.debug(ex);
throw new IOException(ex.getMessage(), ex);
}
}
use of org.exist.storage.lock.LockManager in project exist by eXist-db.
the class NewArrayNodeSet method lock.
@Override
public ManagedLocks<ManagedDocumentLock> lock(final DBBroker broker, final boolean exclusive) throws LockException {
sort();
final LockManager lockManager = broker.getBrokerPool().getLockManager();
final ManagedDocumentLock[] managedDocumentLocks = new ManagedDocumentLock[documentCount];
try {
for (int idx = 0; idx < documentCount; idx++) {
final DocumentImpl doc = nodes[documentNodesOffset[idx]].getOwnerDocument();
final ManagedDocumentLock managedDocumentLock;
if (exclusive) {
managedDocumentLock = lockManager.acquireDocumentWriteLock(doc.getURI());
} else {
managedDocumentLock = lockManager.acquireDocumentReadLock(doc.getURI());
}
managedDocumentLocks[idx] = managedDocumentLock;
}
return new ManagedLocks<>(managedDocumentLocks);
} catch (final LockException e) {
// unlock any previously locked documents
new ManagedLocks<>(managedDocumentLocks).close();
throw e;
}
}
use of org.exist.storage.lock.LockManager in project exist by eXist-db.
the class ExtArrayNodeSet method lock.
@Override
public ManagedLocks<ManagedDocumentLock> lock(final DBBroker broker, final boolean exclusive) throws LockException {
final LockManager lockManager = broker.getBrokerPool().getLockManager();
final ManagedDocumentLock[] managedDocumentLocks = new ManagedDocumentLock[partCount];
try {
for (int i = 0; i < partCount; i++) {
final DocumentImpl doc = parts[i].getOwnerDocument();
final ManagedDocumentLock docLock;
if (exclusive) {
docLock = lockManager.acquireDocumentWriteLock(doc.getURI());
} else {
docLock = lockManager.acquireDocumentReadLock(doc.getURI());
}
managedDocumentLocks[i] = docLock;
}
return new ManagedLocks<>(managedDocumentLocks);
} catch (final LockException e) {
// unlock any previously locked documents
new ManagedLocks<>(managedDocumentLocks).close();
throw e;
}
}
Aggregations