use of org.exist.storage.txn.Txn in project exist by eXist-db.
the class ExistCollection method createCollection.
public XmldbURI createCollection(String name) throws PermissionDeniedException, CollectionExistsException, EXistException {
if (LOG.isDebugEnabled()) {
LOG.debug("Create '{}' in '{}'", name, xmldbUri);
}
XmldbURI newCollection = xmldbUri.append(name);
final TransactionManager txnManager = brokerPool.getTransactionManager();
try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
final Txn txn = txnManager.beginTransaction();
final Collection collection = broker.openCollection(newCollection, LockMode.WRITE_LOCK)) {
if (collection != null) {
final String msg = "Collection already exists";
LOG.debug(msg);
// XXX: double "abort" is bad thing!!!
txnManager.abort(txn);
throw new CollectionExistsException(msg);
}
// Create collection
try (final Collection created = broker.getOrCreateCollection(txn, newCollection)) {
broker.saveCollection(txn, created);
broker.flush();
// Commit change
txnManager.commit(txn);
if (LOG.isDebugEnabled()) {
LOG.debug("Collection created sucessfully");
}
}
} catch (EXistException | PermissionDeniedException e) {
LOG.error(e);
throw e;
} catch (Throwable e) {
LOG.error(e);
throw new EXistException(e);
} finally {
if (LOG.isDebugEnabled()) {
LOG.debug("Finished creation");
}
}
return newCollection;
}
use of org.exist.storage.txn.Txn in project exist by eXist-db.
the class ExistCollection method delete.
/*
* Delete document or collection.
*/
void delete() {
if (LOG.isDebugEnabled()) {
LOG.debug("Deleting '{}'", xmldbUri);
}
final TransactionManager txnManager = brokerPool.getTransactionManager();
try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
final Txn txn = txnManager.beginTransaction();
final Collection collection = broker.openCollection(xmldbUri, LockMode.WRITE_LOCK)) {
// Open collection if possible, else abort
if (collection == null) {
txnManager.abort(txn);
return;
}
// Remove collection
broker.removeCollection(txn, collection);
// Commit change
txnManager.commit(txn);
if (LOG.isDebugEnabled()) {
LOG.debug("Document deleted sucessfully");
}
} catch (EXistException | IOException | PermissionDeniedException | TriggerException e) {
LOG.error(e);
} finally {
if (LOG.isDebugEnabled()) {
LOG.debug("Finished delete");
}
}
}
use of org.exist.storage.txn.Txn in project exist by eXist-db.
the class EXistServlet method doPatch.
protected void doPatch(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
// first, adjust the path
String path = adjustPath(request);
// second, perform descriptor actions
final Descriptor descriptor = Descriptor.getDescriptorSingleton();
if (descriptor != null) {
// TODO: figure out a way to log PATCH requests with
// HttpServletRequestWrapper and
// Descriptor.doLogRequestInReplayLog()
// map's the path if a mapping is specified in the descriptor
path = descriptor.mapPath(path);
}
// third, authenticate the user
final Subject user = authenticate(request, response);
if (user == null) {
// You now get a HTTP Authentication challenge if there is no user
return;
}
// fourth, process the request
try (final DBBroker broker = getPool().get(Optional.of(user));
final Txn transaction = getPool().getTransactionManager().beginTransaction()) {
final XmldbURI dbpath = XmldbURI.createInternal(path);
try (final Collection collection = broker.getCollection(dbpath)) {
if (collection != null) {
transaction.abort();
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "A PATCH request is not allowed against a plain collection path.");
return;
}
}
try {
srvREST.doPatch(broker, transaction, dbpath, request, response);
transaction.commit();
} catch (final Throwable t) {
transaction.abort();
throw t;
}
} catch (final MethodNotAllowedException e) {
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, e.getMessage());
} catch (final BadRequestException e) {
if (response.isCommitted()) {
throw new ServletException(e.getMessage(), e);
}
response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
} catch (final PermissionDeniedException e) {
// Else return a FORBIDDEN Error
if (user.equals(getDefaultUser())) {
getAuthenticator().sendChallenge(request, response);
} else {
response.sendError(HttpServletResponse.SC_FORBIDDEN, e.getMessage());
}
} catch (final EXistException e) {
if (response.isCommitted()) {
throw new ServletException(e.getMessage(), e);
}
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
} catch (final Throwable e) {
LOG.error(e);
throw new ServletException("An unknown error occurred: " + e.getMessage(), e);
}
}
use of org.exist.storage.txn.Txn in project exist by eXist-db.
the class RpcConnection method withDb.
/**
* Higher-order-function for performing an XMLDB operation on
* the database
*
* @param user The user to execute the operation as
* @param dbOperation The operation to perform on the database
* @param <R> The return type of the operation
* @throws EXistException if an internal error occurs
* @throws PermissionDeniedException If the current user is not allowed to perform this action
*/
private <R> R withDb(final Subject user, final XmlRpcFunction<R> dbOperation) throws EXistException, PermissionDeniedException {
try (final DBBroker broker = factory.getBrokerPool().get(Optional.of(user));
final Txn transaction = factory.getBrokerPool().getTransactionManager().beginTransaction()) {
final R result = dbOperation.apply(broker, transaction);
transaction.commit();
return result;
}
}
use of org.exist.storage.txn.Txn in project exist by eXist-db.
the class Modification method checkFragmentation.
/**
* Check if any of the modified documents needs defragmentation.
*
* Defragmentation will take place if the number of split pages in the
* document exceeds the limit defined in the configuration file.
*
* @param context current context
* @param docs document set to check
* @param splitCount number of page splits
* @throws EXistException on general errors during defrag
* @throws LockException in case locking failed
*/
public static void checkFragmentation(XQueryContext context, DocumentSet docs, int splitCount) throws EXistException, LockException {
final DBBroker broker = context.getBroker();
final LockManager lockManager = broker.getBrokerPool().getLockManager();
// if there is no batch update transaction, start a new individual transaction
try (final Txn transaction = broker.continueOrBeginTransaction()) {
for (final Iterator<DocumentImpl> i = docs.getDocumentIterator(); i.hasNext(); ) {
final DocumentImpl next = i.next();
if (next.getSplitCount() > splitCount) {
try (final ManagedDocumentLock nextLock = lockManager.acquireDocumentWriteLock(next.getURI())) {
broker.defragXMLResource(transaction, next);
}
}
broker.checkXMLResourceConsistency(next);
}
transaction.commit();
}
}
Aggregations