use of org.exist.storage.lock.ManagedDocumentLock in project exist by eXist-db.
the class Txn method acquireDocumentLock.
public void acquireDocumentLock(final SupplierE<ManagedDocumentLock, LockException> fnLockAcquire) throws LockException {
final ManagedDocumentLock lock = fnLockAcquire.get();
locksHeld.add(new LockInfo(lock, lock::close));
}
use of org.exist.storage.lock.ManagedDocumentLock in project exist by eXist-db.
the class RpcConnection method listDocumentPermissions.
private Map<String, List> listDocumentPermissions(final XmldbURI collUri) throws EXistException, PermissionDeniedException {
return this.<Map<String, List>>readCollection(collUri).apply((collection, broker, transaction) -> {
final Map<String, List> result = new HashMap<>(collection.getDocumentCount(broker));
for (final Iterator<DocumentImpl> i = collection.iterator(broker); i.hasNext(); ) {
final DocumentImpl doc = i.next();
try (final ManagedDocumentLock documentLock = broker.getBrokerPool().getLockManager().acquireDocumentReadLock(doc.getURI())) {
final Permission perm = doc.getPermissions();
result.put(doc.getFileURI().toString(), toList(perm));
}
}
return result;
});
}
use of org.exist.storage.lock.ManagedDocumentLock in project exist by eXist-db.
the class RpcConnection method storeBinary.
private boolean storeBinary(final byte[] data, final XmldbURI docUri, final String mimeType, final int overwrite, final Date created, final Date modified) throws EXistException, PermissionDeniedException {
return this.<Boolean>writeCollection(docUri.removeLastSegment()).apply((collection, broker, transaction) -> {
// keep a write lock in the transaction
transaction.acquireCollectionLock(() -> broker.getBrokerPool().getLockManager().acquireCollectionWriteLock(collection.getURI()));
try (final ManagedDocumentLock lockedDocument = broker.getBrokerPool().getLockManager().acquireDocumentWriteLock(docUri)) {
if (overwrite == 0) {
// NOTE: we have the document write lock above
final DocumentImpl old = collection.getDocument(broker, docUri.lastSegment());
if (old != null) {
// NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
collection.close();
throw new PermissionDeniedException("Old document exists and overwrite is not allowed");
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("Storing binary resource to collection {}", collection.getURI());
}
broker.storeDocument(transaction, docUri.lastSegment(), new StringInputSource(data), MimeTable.getInstance().getContentType(mimeType), created, modified, null, null, null, collection);
// NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
collection.close();
return true;
}
});
}
use of org.exist.storage.lock.ManagedDocumentLock in project exist by eXist-db.
the class RpcConnection method parse.
private boolean parse(final byte[] xml, final XmldbURI docUri, final int overwrite, @Nullable final Date created, @Nullable final Date modified) throws EXistException, PermissionDeniedException {
return this.<Boolean>writeCollection(docUri.removeLastSegment()).apply((collection, broker, transaction) -> {
try (final ManagedDocumentLock lockedDocument = broker.getBrokerPool().getLockManager().acquireDocumentWriteLock(docUri)) {
if (overwrite == 0) {
// NOTE: we have the document write lock above
final DocumentImpl old = collection.getDocument(broker, docUri.lastSegment());
if (old != null) {
// NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
collection.close();
throw new PermissionDeniedException("Document exists and overwrite is not allowed");
}
}
final InputSource source = new StringInputSource(xml);
final long startTime = System.currentTimeMillis();
final MimeType mime = MimeTable.getInstance().getContentTypeFor(docUri.lastSegment());
broker.storeDocument(transaction, docUri.lastSegment(), source, mime, created, modified, null, null, null, collection);
// NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
collection.close();
if (LOG.isDebugEnabled()) {
LOG.debug("parsing {} took {}ms.", docUri, System.currentTimeMillis() - startTime);
}
return true;
}
});
}
use of org.exist.storage.lock.ManagedDocumentLock in project exist by eXist-db.
the class DocumentNameOrId method getResourceByAbsoluteId.
private Sequence getResourceByAbsoluteId(final IntegerValue ivAbsoluteId) throws XPathException, PermissionDeniedException, LockException, IOException {
final BigInteger absoluteId = ivAbsoluteId.toJavaObject(BigInteger.class);
final Tuple3<Integer, Integer, Byte> decoded = decodeAbsoluteResourceId(absoluteId);
final DocumentImpl doc = context.getBroker().getResourceById(decoded._1, decoded._3, decoded._2);
if (doc != null) {
try (final ManagedDocumentLock docLock = context.getBroker().getBrokerPool().getLockManager().acquireDocumentReadLock(doc.getURI())) {
if (doc instanceof BinaryDocument) {
final BinaryDocument bin = (BinaryDocument) doc;
final InputStream is = context.getBroker().getBinaryResource(bin);
return Base64BinaryDocument.getInstance(context, is);
} else {
return new NodeProxy(doc);
}
}
}
return Sequence.EMPTY_SEQUENCE;
}
Aggregations