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);
}
}
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;
}
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);
}
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;
}
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");
}
}
}
Aggregations