use of org.exist.util.LockException in project exist by eXist-db.
the class NativeStructuralIndexWorker method scanByType.
public NodeSet scanByType(byte type, int axis, NodeTest test, boolean useSelfAsContext, DocumentSet docs, NodeSet contextSet, int contextId) {
final NewArrayNodeSet result = new NewArrayNodeSet();
final FindDescendantsCallback callback = new FindDescendantsCallback(type, axis, null, contextId, useSelfAsContext, result, null);
for (final NodeProxy ancestor : contextSet) {
final DocumentImpl doc = ancestor.getOwnerDocument();
final NodeId ancestorId = ancestor.getNodeId();
final List<QName> qnames = getQNamesForDoc(doc);
try (final ManagedLock<ReentrantLock> btreeLock = index.lockManager.acquireBtreeReadLock(index.btree.getLockName())) {
for (final QName qname : qnames) {
if (test.getName() == null || test.matches(qname)) {
callback.setAncestor(doc, ancestor);
byte[] fromKey, toKey;
if (ancestorId == NodeId.DOCUMENT_NODE) {
fromKey = computeKey(type, qname, doc.getDocId());
toKey = computeKey(type, qname, doc.getDocId() + 1);
} else {
fromKey = computeKey(type, qname, doc.getDocId(), ancestorId);
toKey = computeKey(type, qname, doc.getDocId(), ancestorId.nextSibling());
}
final IndexQuery query = new IndexQuery(IndexQuery.RANGE, new Value(fromKey), new Value(toKey));
try {
index.btree.query(query, callback);
} catch (final Exception e) {
NativeStructuralIndex.LOG.error("Error while searching structural index: {}", e.getMessage(), e);
}
}
}
} catch (final LockException e) {
NativeStructuralIndex.LOG.warn("Lock problem while searching structural index: {}", e.getMessage(), e);
}
}
// result.updateNoSort();
return result;
}
use of org.exist.util.LockException in project exist by eXist-db.
the class NativeStructuralIndexWorker method getQNamesForDoc.
protected List<QName> getQNamesForDoc(DocumentImpl doc) {
final List<QName> qnames = new ArrayList<>();
if (index.btree == null) {
return qnames;
}
final byte[] fromKey = computeDocKey(doc.getDocId());
final byte[] toKey = computeDocKey(doc.getDocId() + 1);
final IndexQuery query = new IndexQuery(IndexQuery.RANGE, new Value(fromKey), new Value(toKey));
try (final ManagedLock<ReentrantLock> btreeLock = index.lockManager.acquireBtreeWriteLock(index.btree.getLockName())) {
index.btree.query(query, (value, pointer) -> {
final QName qname = readQName(value.getData());
qnames.add(qname);
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);
}
return qnames;
}
use of org.exist.util.LockException in project exist by eXist-db.
the class NativeStructuralIndexWorker method findAncestorsByTagName.
public NodeSet findAncestorsByTagName(byte type, QName qname, int axis, DocumentSet docs, NodeSet contextSet, int contextId) {
final NewArrayNodeSet result = new NewArrayNodeSet();
try (final ManagedLock<ReentrantLock> btreeLock = index.lockManager.acquireBtreeReadLock(index.btree.getLockName())) {
for (final NodeProxy descendant : contextSet) {
NodeId parentId;
if (axis == Constants.ANCESTOR_SELF_AXIS || axis == Constants.SELF_AXIS) {
parentId = descendant.getNodeId();
} else {
parentId = descendant.getNodeId().getParentId();
}
final DocumentImpl doc = descendant.getOwnerDocument();
while (parentId != NodeId.DOCUMENT_NODE) {
final byte[] key = computeKey(type, qname, doc.getDocId(), parentId);
final long address = index.btree.findValue(new Value(key));
if (address != -1) {
final NodeProxy storedNode = new NodeProxy(doc, parentId, type == ElementValue.ATTRIBUTE ? Node.ATTRIBUTE_NODE : Node.ELEMENT_NODE, address);
result.add(storedNode);
if (Expression.NO_CONTEXT_ID != contextId) {
storedNode.deepCopyContext(descendant, contextId);
} else {
storedNode.copyContext(descendant);
}
if (contextSet.getTrackMatches()) {
storedNode.addMatches(descendant);
}
}
// stop after first iteration if we are on the self axis
if (axis == Constants.SELF_AXIS || axis == Constants.PARENT_AXIS) {
break;
}
// continue with the parent of the parent
parentId = parentId.getParentId();
}
}
} catch (final LockException e) {
NativeStructuralIndex.LOG.warn("Lock problem while searching structural index: {}", e.getMessage(), e);
} catch (final Exception e) {
NativeStructuralIndex.LOG.error("Error while searching structural index: {}", e.getMessage(), e);
}
result.sort(true);
return result;
}
use of org.exist.util.LockException in project exist by eXist-db.
the class CreateOrderIndex method eval.
@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
if (args[1].isEmpty())
return Sequence.EMPTY_SEQUENCE;
final String id = args[0].getStringValue();
// check how the function was called and prepare callback
FunctionReference call = null;
if (isCalledAs("create-index-callback")) {
call = (FunctionReference) args[2].itemAt(0);
} else if (args[2].getItemCount() != args[1].getItemCount())
throw new XPathException(this, "$nodes and $values sequences need to have the same length.");
// options
if (args[3].getItemCount() > 0) {
final NodeValue optionValue = (NodeValue) args[3].itemAt(0);
final Element options = (Element) optionValue.getNode();
String option = options.getAttribute("order");
if (option != null) {
descending = option.equalsIgnoreCase("descending");
}
option = options.getAttribute("empty");
if (option != null) {
emptyLeast = option.equalsIgnoreCase("least");
}
}
// create the input list to be sorted below
final List<SortItem> items = new ArrayList<>(args[1].getItemCount());
final Sequence[] params = new Sequence[1];
SequenceIterator valuesIter = null;
if (call == null)
valuesIter = args[2].iterate();
int c = 0;
final int len = args[1].getItemCount();
final int logChunk = 1 + (len / 20);
for (final SequenceIterator nodesIter = args[1].iterate(); nodesIter.hasNext(); ) {
final NodeValue nv = (NodeValue) nodesIter.nextItem();
if (nv.getImplementationType() == NodeValue.IN_MEMORY_NODE)
throw new XPathException(this, "Cannot create order-index on an in-memory node");
final NodeProxy node = (NodeProxy) nv;
final SortItem si = new SortItemImpl(node);
if (LOG.isDebugEnabled() && ++c % logChunk == 0) {
LOG.debug("Storing item {} out of {} to sort index.", c, len);
}
if (call != null) {
// call the callback function to get value
params[0] = node;
final Sequence r = call.evalFunction(contextSequence, null, params);
if (!r.isEmpty()) {
AtomicValue v = r.itemAt(0).atomize();
if (v.getType() == Type.UNTYPED_ATOMIC)
v = v.convertTo(Type.STRING);
si.setValue(v);
}
} else {
// no callback, take value from second sequence
AtomicValue v = valuesIter.nextItem().atomize();
if (v.getType() == Type.UNTYPED_ATOMIC)
v = v.convertTo(Type.STRING);
si.setValue(v);
}
items.add(si);
}
// sort the set
FastQSort.sort(items, 0, items.size() - 1);
// create the index
final SortIndexWorker index = (SortIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(SortIndex.ID);
try {
index.createIndex(id, items);
} catch (final EXistException e) {
throw new XPathException(this, e.getMessage(), e);
} catch (final LockException e) {
throw new XPathException(this, "Caught lock error while creating index. Giving up.", e);
}
return Sequence.EMPTY_SEQUENCE;
}
use of org.exist.util.LockException in project exist by eXist-db.
the class AbstractCompressFunction method compressFromUri.
private void compressFromUri(OutputStream os, URI uri, boolean useHierarchy, String stripOffset, String method, String resourceName) throws XPathException {
try {
if ("file".equals(uri.getScheme())) {
if (!context.getSubject().hasDbaRole()) {
XPathException xPathException = new XPathException(this, "Permission denied, calling user '" + context.getSubject().getName() + "' must be a DBA to call this function.");
logger.error("Invalid user", xPathException);
throw xPathException;
}
// got a file
Path file = Paths.get(uri.getPath());
compressFile(os, file, useHierarchy, stripOffset, method, resourceName);
} else {
final XmldbURI xmldburi = XmldbURI.create(uri);
// try for a collection
try (final Collection collection = context.getBroker().openCollection(xmldburi, LockMode.READ_LOCK)) {
if (collection != null) {
compressCollection(os, collection, useHierarchy, stripOffset);
return;
}
} catch (final PermissionDeniedException | LockException | SAXException | IOException pde) {
throw new XPathException(this, pde.getMessage());
}
// otherwise, try for a doc
try (final Collection collection = context.getBroker().openCollection(xmldburi.removeLastSegment(), LockMode.READ_LOCK)) {
if (collection == null) {
throw new XPathException(this, "Invalid URI: " + uri.toString());
}
try (final LockedDocument doc = collection.getDocumentWithLock(context.getBroker(), xmldburi.lastSegment(), LockMode.READ_LOCK)) {
// NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
collection.close();
if (doc == null) {
throw new XPathException(this, "Invalid URI: " + uri.toString());
}
compressResource(os, doc.getDocument(), useHierarchy, stripOffset, method, resourceName);
return;
}
} catch (final PermissionDeniedException | LockException | SAXException | IOException pde) {
throw new XPathException(this, pde.getMessage());
}
}
} catch (final IOException e) {
throw new XPathException(this, e.getMessage());
}
}
Aggregations