Search in sources :

Example 6 with LockManager

use of org.exist.storage.lock.LockManager 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 7 with LockManager

use of org.exist.storage.lock.LockManager 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 8 with LockManager

use of org.exist.storage.lock.LockManager 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 9 with LockManager

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

the class Repair method repair.

public void repair(String id) {
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        BTree btree = null;
        if ("collections".equals(id)) {
            btree = ((NativeBroker) broker).getStorage(NativeBroker.COLLECTIONS_DBX_ID);
        } else if ("dom".equals(id)) {
            btree = ((NativeBroker) broker).getStorage(NativeBroker.DOM_DBX_ID);
        } else if ("range".equals(id)) {
            btree = ((NativeBroker) broker).getStorage(NativeBroker.VALUES_DBX_ID);
        } else if ("structure".equals(id)) {
            NativeStructuralIndexWorker index = (NativeStructuralIndexWorker) broker.getIndexController().getWorkerByIndexName(StructuralIndex.STRUCTURAL_INDEX_ID);
            btree = index.getStorage();
        } else {
            // use index id defined in conf.xml
            Index index = pool.getIndexManager().getIndexByName(id);
            if (index != null) {
                btree = index.getStorage();
            }
        }
        if (btree == null) {
            System.console().printf("Unkown index: %s\n", id);
            return;
        }
        final LockManager lockManager = broker.getBrokerPool().getLockManager();
        try (final ManagedLock<ReentrantLock> btreeLock = lockManager.acquireBtreeWriteLock(btree.getLockName())) {
            System.console().printf("Rebuilding %15s ...", FileUtils.fileName(btree.getFile()));
            btree.rebuild();
            System.out.println("Done");
        }
    } catch (Exception e) {
        System.console().printf("An exception occurred during repair: %s\n", e.getMessage());
        e.printStackTrace();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) LockManager(org.exist.storage.lock.LockManager) DBBroker(org.exist.storage.DBBroker) StructuralIndex(org.exist.indexing.StructuralIndex) Index(org.exist.indexing.Index) NativeBroker(org.exist.storage.NativeBroker) NativeStructuralIndexWorker(org.exist.storage.structural.NativeStructuralIndexWorker) StartException(org.exist.start.StartException) EXistException(org.exist.EXistException) DatabaseConfigurationException(org.exist.util.DatabaseConfigurationException)

Example 10 with LockManager

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

the class DefaultDocumentSet method lock.

@Override
public ManagedLocks<ManagedDocumentLock> lock(final DBBroker broker, final boolean exclusive) throws LockException {
    final LockManager lockManager = broker.getBrokerPool().getLockManager();
    final List<ManagedDocumentLock> managedDocumentLocks = new ArrayList<>();
    final Iterator<DocumentImpl> documentIterator = getDocumentIterator();
    try {
        while (documentIterator.hasNext()) {
            final DocumentImpl document = documentIterator.next();
            final ManagedDocumentLock managedDocumentLock;
            if (exclusive) {
                managedDocumentLock = lockManager.acquireDocumentWriteLock(document.getURI());
            } else {
                managedDocumentLock = lockManager.acquireDocumentReadLock(document.getURI());
            }
            managedDocumentLocks.add(managedDocumentLock);
        }
        return new ManagedLocks<>(managedDocumentLocks);
    } catch (final LockException e) {
        // unlock any previously locked documents
        if (!managedDocumentLocks.isEmpty()) {
            new ManagedLocks<>(managedDocumentLocks).close();
        }
        throw e;
    }
}
Also used : ManagedLocks(org.exist.collections.ManagedLocks) LockManager(org.exist.storage.lock.LockManager) ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) LockException(org.exist.util.LockException)

Aggregations

LockManager (org.exist.storage.lock.LockManager)14 ManagedDocumentLock (org.exist.storage.lock.ManagedDocumentLock)9 DBBroker (org.exist.storage.DBBroker)6 XmldbURI (org.exist.xmldb.XmldbURI)6 EXistException (org.exist.EXistException)4 Collection (org.exist.collections.Collection)4 Txn (org.exist.storage.txn.Txn)4 LockException (org.exist.util.LockException)4 IOException (java.io.IOException)3 ManagedLocks (org.exist.collections.ManagedLocks)3 URISyntaxException (java.net.URISyntaxException)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)2 HttpSession (javax.servlet.http.HttpSession)2 HttpSessionEvent (javax.servlet.http.HttpSessionEvent)2 LockedDocument (org.exist.dom.persistent.LockedDocument)2 TransactionException (org.exist.storage.txn.TransactionException)2 Test (org.junit.Test)2 AtomicFSM (com.evolvedbinary.j8fu.fsm.AtomicFSM)1 FSM (com.evolvedbinary.j8fu.fsm.FSM)1 TransitionTable.transitionTable (com.evolvedbinary.j8fu.fsm.TransitionTable.transitionTable)1