use of org.exist.util.LockException in project exist by eXist-db.
the class NativeStructuralIndexWorker method removeSome.
protected void removeSome() {
if (pending.size() == 0) {
return;
}
try {
for (final Map.Entry<QName, List<NodeProxy>> entry : pending.entrySet()) {
final QName qname = entry.getKey();
try (final ManagedLock<ReentrantLock> btreeLock = index.lockManager.acquireBtreeWriteLock(index.btree.getLockName())) {
final List<NodeProxy> nodes = entry.getValue();
for (final NodeProxy proxy : nodes) {
final NodeId nodeId = proxy.getNodeId();
final byte[] key = computeKey(qname.getNameType(), qname, document.getDocId(), nodeId);
index.btree.removeValue(new Value(key));
}
} catch (final LockException e) {
NativeStructuralIndex.LOG.warn("Failed to lock structural index: {}", e.getMessage(), e);
} catch (final Exception e) {
NativeStructuralIndex.LOG.warn("Exception caught while writing to structural index: {}", e.getMessage(), e);
}
}
} finally {
pending.clear();
}
}
use of org.exist.util.LockException in project exist by eXist-db.
the class NativeStructuralIndexWorker method scanIndex.
/**
* Collect index statistics. Used by functions like util:index-keys.
*
* @param context the xquery context
* @param docs The documents to which the index entries belong
* @param contextSet ignored by this index
* @param hints Some "hints" for retrieving the index entries. See such hints in
* {@link org.exist.indexing.OrderedValuesIndex} and {@link org.exist.indexing.QNamedKeysIndex}.
* @return the matching occurrences
*/
public Occurrences[] scanIndex(XQueryContext context, DocumentSet docs, NodeSet contextSet, Map hints) {
final Map<String, Occurrences> occurrences = new TreeMap<>();
for (final Iterator<DocumentImpl> i = docs.getDocumentIterator(); i.hasNext(); ) {
final DocumentImpl doc = i.next();
final List<QName> qnames = getQNamesForDoc(doc);
for (final QName qname : qnames) {
final String name;
if (qname.getNameType() == ElementValue.ATTRIBUTE) {
name = "@" + qname.getLocalPart();
} else {
name = qname.getLocalPart();
}
final byte[] fromKey = computeKey(qname.getNameType(), qname, doc.getDocId());
final byte[] toKey = computeKey(qname.getNameType(), qname, doc.getDocId() + 1);
final IndexQuery query = new IndexQuery(IndexQuery.RANGE, new Value(fromKey), new Value(toKey));
try (final ManagedLock<ReentrantLock> btreeLock = index.lockManager.acquireBtreeReadLock(index.btree.getLockName())) {
index.btree.query(query, (value, pointer) -> {
Occurrences oc = occurrences.get(name);
if (oc == null) {
oc = new Occurrences(name);
occurrences.put(name, oc);
oc.addDocument(doc);
oc.addOccurrences(1);
} else {
oc.addOccurrences(1);
oc.addDocument(doc);
}
return true;
});
} catch (final LockException e) {
NativeStructuralIndex.LOG.warn("Failed to lock structural index: {}", e.getMessage(), e);
} catch (final Exception e) {
NativeStructuralIndex.LOG.warn("Exception caught while reading structural index for document {}: {}", doc.getURI(), e.getMessage(), e);
}
}
}
final Occurrences[] result = new Occurrences[occurrences.size()];
int i = 0;
for (Occurrences occ : occurrences.values()) {
result[i++] = occ;
}
return result;
}
use of org.exist.util.LockException in project exist by eXist-db.
the class NativeStructuralIndexWorker method processPending.
/**
* Process the map of pending entries and store them into the btree.
*/
private void processPending() {
if (pending.size() == 0 || index.btree == null) {
return;
}
try {
for (final Map.Entry<QName, List<NodeProxy>> entry : pending.entrySet()) {
final QName qname = entry.getKey();
try (final ManagedLock<ReentrantLock> btreeLock = index.lockManager.acquireBtreeWriteLock(index.btree.getLockName())) {
final List<NodeProxy> nodes = entry.getValue();
for (final NodeProxy proxy : nodes) {
final NodeId nodeId = proxy.getNodeId();
final byte[] key = computeKey(qname.getNameType(), qname, document.getDocId(), nodeId);
index.btree.addValue(new Value(key), computeValue(proxy));
}
final Value docKey = new Value(computeDocKey(qname.getNameType(), document.getDocId(), qname));
if (index.btree.findValue(docKey) == -1) {
index.btree.addValue(docKey, 0);
}
} catch (final LockException e) {
NativeStructuralIndex.LOG.warn("Failed to lock structural index: {}", e.getMessage(), e);
// } catch (ReadOnlyException e) {
// NativeStructuralIndex.LOG.warn("Read-only error: " + e.getMessage(), e);
} catch (final Exception e) {
NativeStructuralIndex.LOG.warn("Exception caught while writing to structural index: {}", e.getMessage(), e);
}
}
} finally {
pending.clear();
}
}
use of org.exist.util.LockException in project exist by eXist-db.
the class LockManager method acquireDocumentWriteLock.
/**
* Acquire a WRITE_LOCK on a Document
*
* @param documentPath The URI of the Document within the database
*
* @return the lock for the Document
*
* @throws LockException if the lock could not be acquired
*/
public ManagedDocumentLock acquireDocumentWriteLock(final XmldbURI documentPath) throws LockException {
if (usePathLocksForDocuments) {
final LockGroup lockGroup = acquirePathWriteLock(LockType.DOCUMENT, documentPath, false);
return new ManagedDocumentLock(documentPath, Arrays.stream(lockGroup.locks).map(Tuple3::get_1).toArray(MultiLock[]::new), () -> unlockAll(lockGroup.locks, l -> lockTable.released(lockGroup.groupId, l._3, LockType.DOCUMENT, l._2)));
} else {
final long groupId = System.nanoTime();
final String path = documentPath.toString();
final MultiLock lock = getDocumentLock(path);
lockTable.attempt(groupId, path, LockType.DOCUMENT, Lock.LockMode.WRITE_LOCK);
if (lock(lock, Lock.LockMode.WRITE_LOCK)) {
lockTable.acquired(groupId, path, LockType.DOCUMENT, Lock.LockMode.WRITE_LOCK);
} else {
lockTable.attemptFailed(groupId, path, LockType.DOCUMENT, Lock.LockMode.WRITE_LOCK);
throw new LockException("Unable to acquire WRITE_LOCK for: " + path);
}
return new ManagedDocumentLock(documentPath, lock, () -> {
lock.asWriteLock().unlock();
lockTable.released(groupId, path, LockType.DOCUMENT, Lock.LockMode.WRITE_LOCK);
});
}
}
use of org.exist.util.LockException in project exist by eXist-db.
the class LockManager method acquireDocumentReadLock.
/**
* Acquire a READ_LOCK on a Document
*
* @param documentPath The URI of the Document within the database
*
* @return the lock for the Document
*
* @throws LockException if the lock could not be acquired
*/
public ManagedDocumentLock acquireDocumentReadLock(final XmldbURI documentPath) throws LockException {
if (usePathLocksForDocuments) {
final LockGroup lockGroup = acquirePathReadLock(LockType.DOCUMENT, documentPath);
return new ManagedDocumentLock(documentPath, Arrays.stream(lockGroup.locks).map(Tuple3::get_1).toArray(MultiLock[]::new), () -> unlockAll(lockGroup.locks, l -> lockTable.released(lockGroup.groupId, l._3, LockType.DOCUMENT, l._2)));
} else {
final long groupId = System.nanoTime();
final String path = documentPath.toString();
final MultiLock lock = getDocumentLock(path);
lockTable.attempt(groupId, path, LockType.DOCUMENT, Lock.LockMode.READ_LOCK);
if (lock(lock, Lock.LockMode.READ_LOCK)) {
lockTable.acquired(groupId, path, LockType.DOCUMENT, Lock.LockMode.READ_LOCK);
} else {
lockTable.attemptFailed(groupId, path, LockType.DOCUMENT, Lock.LockMode.READ_LOCK);
throw new LockException("Unable to acquire READ_LOCK for: " + path);
}
return new ManagedDocumentLock(documentPath, lock, () -> {
lock.asReadLock().unlock();
lockTable.released(groupId, path, LockType.DOCUMENT, Lock.LockMode.READ_LOCK);
});
}
}
Aggregations