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