use of org.exist.storage.lock.Lock.LockMode in project exist by eXist-db.
the class ExistDocument method resourceCopyMove.
/**
* Copy document or collection in database.
*/
void resourceCopyMove(XmldbURI destCollectionUri, String newName, Mode mode) throws EXistException {
if (LOG.isDebugEnabled()) {
LOG.debug("{} {} to {} named {}", String.valueOf(mode), xmldbUri, destCollectionUri, newName);
}
XmldbURI newNameUri = null;
try {
newNameUri = XmldbURI.xmldbUriFor(newName);
} catch (URISyntaxException ex) {
LOG.error(ex);
throw new EXistException(ex.getMessage());
}
// use WRITE_LOCK if moving or if src and dest collection are the same
final LockMode srcCollectionLockMode = mode == Mode.MOVE || destCollectionUri.equals(xmldbUri.removeLastSegment()) ? LockMode.WRITE_LOCK : LockMode.READ_LOCK;
DocumentImpl srcDocument = null;
// Need to split path into collection and document name
final XmldbURI srcCollectionUri = xmldbUri.removeLastSegment();
final XmldbURI srdDocumentUri = xmldbUri.lastSegment();
final TransactionManager txnManager = brokerPool.getTransactionManager();
try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
final Txn txn = txnManager.beginTransaction();
final Collection srcCollection = broker.openCollection(srcCollectionUri, srcCollectionLockMode)) {
// Open collection if possible, else abort
if (srcCollection == null) {
txnManager.abort(txn);
// TODO throw
return;
}
// Open document if possible, else abort
srcDocument = srcCollection.getDocument(broker, srdDocumentUri);
if (srcDocument == null) {
LOG.debug("No resource found for path: {}", xmldbUri);
txnManager.abort(txn);
return;
}
// Open collection if possible, else abort
try (final Collection destCollection = broker.openCollection(destCollectionUri, LockMode.WRITE_LOCK)) {
if (destCollection == null) {
LOG.debug("Destination collection {} does not exist.", xmldbUri);
txnManager.abort(txn);
return;
}
// Perform actial move/copy
if (mode == Mode.COPY) {
broker.copyResource(txn, srcDocument, destCollection, newNameUri);
} else {
broker.moveResource(txn, srcDocument, destCollection, newNameUri);
}
// Commit change
txnManager.commit(txn);
if (LOG.isDebugEnabled()) {
LOG.debug("Document {}d successfully", mode);
}
}
} catch (LockException e) {
LOG.error("Resource is locked.", e);
throw new EXistException(e.getMessage());
} catch (EXistException e) {
LOG.error(e);
throw e;
} catch (IOException | PermissionDeniedException | TriggerException e) {
LOG.error(e);
throw new EXistException(e.getMessage());
} finally {
if (LOG.isDebugEnabled()) {
LOG.debug("Finished {}", mode);
}
}
}
use of org.exist.storage.lock.Lock.LockMode in project exist by eXist-db.
the class ExistCollection method resourceCopyMove.
void resourceCopyMove(XmldbURI destCollectionUri, String newName, Mode mode) throws EXistException {
if (LOG.isDebugEnabled()) {
LOG.debug("{} '{}' to '{}' named '{}'", String.valueOf(mode), xmldbUri, destCollectionUri, newName);
}
XmldbURI newNameUri = null;
try {
newNameUri = XmldbURI.xmldbUriFor(newName);
} catch (URISyntaxException ex) {
LOG.error(ex);
throw new EXistException(ex.getMessage());
}
// This class contains already the URI of the resource that shall be moved/copied
XmldbURI srcCollectionUri = xmldbUri;
// use WRITE_LOCK if moving or if src and dest collection are the same
final LockMode srcCollectionLockMode = mode == Mode.MOVE || destCollectionUri.equals(srcCollectionUri) ? LockMode.WRITE_LOCK : LockMode.READ_LOCK;
final TransactionManager txnManager = brokerPool.getTransactionManager();
try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
final Txn txn = txnManager.beginTransaction();
final Collection srcCollection = broker.openCollection(srcCollectionUri, srcCollectionLockMode)) {
// Open collection if possible, else abort
if (srcCollection == null) {
txnManager.abort(txn);
// TODO throw
return;
}
// Open collection if possible, else abort
try (final Collection destCollection = broker.openCollection(destCollectionUri, LockMode.WRITE_LOCK)) {
if (destCollection == null) {
LOG.debug("Destination collection {} does not exist.", xmldbUri);
txnManager.abort(txn);
// TODO throw?
return;
}
// Perform actial move/copy
if (mode == Mode.COPY) {
broker.copyCollection(txn, srcCollection, destCollection, newNameUri);
} else {
broker.moveCollection(txn, srcCollection, destCollection, newNameUri);
}
// Commit change
txnManager.commit(txn);
if (LOG.isDebugEnabled()) {
LOG.debug("Collection {}d successfully", mode);
}
}
} catch (LockException e) {
LOG.error("Resource is locked.", e);
throw new EXistException(e.getMessage());
} catch (EXistException e) {
LOG.error(e);
throw e;
} catch (IOException | PermissionDeniedException | TriggerException e) {
LOG.error(e);
throw new EXistException(e.getMessage());
} finally {
if (LOG.isDebugEnabled()) {
LOG.debug("Finished {}", mode);
}
}
}
Aggregations