Search in sources :

Example 11 with ManagedDocumentLock

use of org.exist.storage.lock.ManagedDocumentLock 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();
    }
}
Also used : LockManager(org.exist.storage.lock.LockManager) DBBroker(org.exist.storage.DBBroker) ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) Txn(org.exist.storage.txn.Txn) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 12 with ManagedDocumentLock

use of org.exist.storage.lock.ManagedDocumentLock in project exist by eXist-db.

the class AbstractCompressFunction method compressCollection.

/**
 * Adds a Collection and its child collections and resources recursively to
 * a archive
 *
 * @param os
 *            The Output Stream to add the document to
 * @param col
 *            The Collection to add to the archive
 * @param useHierarchy
 *            Whether to use a folder hierarchy in the archive file that
 *            reflects the collection hierarchy
 */
private void compressCollection(OutputStream os, Collection col, boolean useHierarchy, String stripOffset) throws IOException, SAXException, LockException, PermissionDeniedException {
    // iterate over child documents
    final DBBroker broker = context.getBroker();
    final LockManager lockManager = broker.getBrokerPool().getLockManager();
    final MutableDocumentSet childDocs = new DefaultDocumentSet();
    col.getDocuments(broker, childDocs);
    for (final Iterator<DocumentImpl> itChildDocs = childDocs.getDocumentIterator(); itChildDocs.hasNext(); ) {
        DocumentImpl childDoc = itChildDocs.next();
        try (final ManagedDocumentLock updateLock = lockManager.acquireDocumentReadLock(childDoc.getURI())) {
            compressResource(os, childDoc, useHierarchy, stripOffset, "", null);
        }
    }
    // iterate over child collections
    for (final Iterator<XmldbURI> itChildCols = col.collectionIterator(broker); itChildCols.hasNext(); ) {
        // get the child collection
        XmldbURI childColURI = itChildCols.next();
        Collection childCol = broker.getCollection(col.getURI().append(childColURI));
        // recurse
        compressCollection(os, childCol, useHierarchy, stripOffset);
    }
}
Also used : LockManager(org.exist.storage.lock.LockManager) DBBroker(org.exist.storage.DBBroker) ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) Collection(org.exist.collections.Collection) XmldbURI(org.exist.xmldb.XmldbURI)

Example 13 with ManagedDocumentLock

use of org.exist.storage.lock.ManagedDocumentLock in project exist by eXist-db.

the class RpcConnection method getCollectionDesc.

private Map<String, Object> getCollectionDesc(final XmldbURI rootUri) throws EXistException, PermissionDeniedException {
    return this.<Map<String, Object>>readCollection(rootUri).apply((collection, broker, transaction) -> {
        final Map<String, Object> desc = new HashMap<>();
        final List<Map<String, Object>> docs = new ArrayList<>();
        final List<String> collections = new ArrayList<>();
        if (collection.getPermissionsNoLock().validate(user, Permission.READ)) {
            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 perms = doc.getPermissions();
                    final Map<String, Object> hash = new HashMap<>(5);
                    hash.put("name", doc.getFileURI().toString());
                    hash.put("owner", perms.getOwner().getName());
                    hash.put("group", perms.getGroup().getName());
                    hash.put("permissions", perms.getMode());
                    hash.put("type", doc.getResourceType() == DocumentImpl.BINARY_FILE ? "BinaryResource" : "XMLResource");
                    docs.add(hash);
                }
            }
            for (final Iterator<XmldbURI> i = collection.collectionIterator(broker); i.hasNext(); ) {
                collections.add(i.next().toString());
            }
        }
        final Permission perms = collection.getPermissionsNoLock();
        desc.put("collections", collections);
        desc.put("documents", docs);
        desc.put("name", collection.getURI().toString());
        desc.put("created", Long.toString(collection.getCreated()));
        desc.put("owner", perms.getOwner().getName());
        desc.put("group", perms.getGroup().getName());
        desc.put("permissions", perms.getMode());
        return desc;
    });
}
Also used : ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) ACLPermission(org.exist.security.ACLPermission) Permission(org.exist.security.Permission) LockedDocumentMap(org.exist.storage.lock.LockedDocumentMap) XmldbURI(org.exist.xmldb.XmldbURI)

Example 14 with ManagedDocumentLock

use of org.exist.storage.lock.ManagedDocumentLock in project exist by eXist-db.

the class RpcConnection method copyCollection.

@Override
public boolean copyCollection(final String name, final String namedest) throws PermissionDeniedException, EXistException {
    createCollection(namedest);
    final Map<String, Object> parameters = new HashMap<>();
    parameters.put(OutputKeys.INDENT, "no");
    parameters.put(EXistOutputKeys.EXPAND_XINCLUDES, "no");
    parameters.put(OutputKeys.ENCODING, DEFAULT_ENCODING);
    final Map<String, Object> desc = getCollectionDesc(name);
    final Object[] collections = (Object[]) desc.get("collections");
    final Object[] documents = (Object[]) desc.get("documents");
    // recurse the collection
    for (final Object collection : collections) {
        final String nome = collection.toString();
        createCollection(namedest + "/" + nome);
        copyCollection(name + "/" + nome, namedest + "/" + nome);
    }
    // Copy i file
    int p, dsize = documents.length;
    for (Object document : documents) {
        final Map<String, Object> hash = (Map<String, Object>) document;
        String docName = (String) hash.get("name");
        // TODO : use dedicated function in XmldbURI
        if ((p = docName.lastIndexOf('/')) != Constants.STRING_NOT_FOUND) {
            docName = docName.substring(p + 1);
        }
        final String srcDocUri = name + "/" + docName;
        final String destDocUri = namedest + "/" + docName;
        withDb((broker, transaction) -> {
            final LockManager lockManager = broker.getBrokerPool().getLockManager();
            try (final ManagedDocumentLock srcDocLock = lockManager.acquireDocumentReadLock(XmldbURI.create(srcDocUri));
                final ManagedDocumentLock destDocLock = lockManager.acquireDocumentWriteLock(XmldbURI.create(destDocUri))) {
                final byte[] xml = getDocument(srcDocUri, parameters);
                parse(xml, destDocUri);
                return null;
            } catch (final URISyntaxException e) {
                throw new EXistException(e);
            }
        });
    }
    return true;
}
Also used : LockManager(org.exist.storage.lock.LockManager) ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) URISyntaxException(java.net.URISyntaxException) EXistException(org.exist.EXistException) LockedDocumentMap(org.exist.storage.lock.LockedDocumentMap)

Example 15 with ManagedDocumentLock

use of org.exist.storage.lock.ManagedDocumentLock in project exist by eXist-db.

the class RpcConnection method parseLocal.

private boolean parseLocal(final String localFile, final XmldbURI docUri, final int overwrite, final String mimeType, final Boolean isXML, final Date created, 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("Old document exists and overwrite is not allowed");
                }
            }
            // get the source for parsing
            SupplierE<FileInputSource, IOException> sourceSupplier;
            try {
                final int handle = Integer.parseInt(localFile);
                final SerializedResult sr = factory.resultSets.getSerializedResult(handle);
                if (sr == null) {
                    // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
                    collection.close();
                    throw new EXistException("Invalid handle specified");
                }
                sourceSupplier = () -> {
                    final FileInputSource source = new FileInputSource(sr.result);
                    // de-reference the temp file in the SerializeResult, so it is not re-claimed before we need it
                    sr.result = null;
                    factory.resultSets.remove(handle);
                    return source;
                };
            } catch (final NumberFormatException nfe) {
                // As this file can be a non-temporal one, we should not
                // blindly erase it!
                final Path path = Paths.get(localFile);
                if (!Files.isReadable(path)) {
                    // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
                    collection.close();
                    throw new EXistException("unable to read file " + path.toAbsolutePath().toString());
                }
                sourceSupplier = () -> new FileInputSource(path);
            }
            // parse the source
            try (final FileInputSource source = sourceSupplier.get()) {
                final MimeType mime = Optional.ofNullable(MimeTable.getInstance().getContentType(mimeType)).orElse(MimeType.BINARY_TYPE);
                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();
                return true;
            }
        }
    });
}
Also used : ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) PermissionDeniedException(org.exist.security.PermissionDeniedException) EXistException(org.exist.EXistException)

Aggregations

ManagedDocumentLock (org.exist.storage.lock.ManagedDocumentLock)23 LockManager (org.exist.storage.lock.LockManager)10 LockException (org.exist.util.LockException)7 EXistException (org.exist.EXistException)6 URISyntaxException (java.net.URISyntaxException)5 DocumentImpl (org.exist.dom.persistent.DocumentImpl)5 XmldbURI (org.exist.xmldb.XmldbURI)5 ManagedLocks (org.exist.collections.ManagedLocks)4 PermissionDeniedException (org.exist.security.PermissionDeniedException)4 DBBroker (org.exist.storage.DBBroker)4 Tuple2 (com.evolvedbinary.j8fu.tuple.Tuple2)3 Nullable (javax.annotation.Nullable)3 Collection (org.exist.collections.Collection)3 TriggerException (org.exist.collections.triggers.TriggerException)3 LockedDocument (org.exist.dom.persistent.LockedDocument)3 ACLPermission (org.exist.security.ACLPermission)3 Permission (org.exist.security.Permission)3 BrokerPool (org.exist.storage.BrokerPool)3 Lock (org.exist.storage.lock.Lock)3 ManagedCollectionLock (org.exist.storage.lock.ManagedCollectionLock)3