use of org.exist.dom.persistent.DocumentImpl 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.dom.persistent.DocumentImpl 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.dom.persistent.DocumentImpl in project exist by eXist-db.
the class PermissionsFunction method getPermissions.
private Permission getPermissions(final XmldbURI pathUri) throws XPathException, PermissionDeniedException {
final Permission permissions;
final Collection col = context.getBroker().getCollection(pathUri);
if (col != null) {
permissions = col.getPermissionsNoLock();
} else {
final DocumentImpl doc = context.getBroker().getResource(pathUri, Permission.READ);
if (doc != null) {
permissions = doc.getPermissions();
} else {
throw new XPathException("Resource or collection '" + pathUri.toString() + "' does not exist.");
}
}
return permissions;
}
use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.
the class ConstructedNodesRecoveryTest method testDocumentIsValid.
private void testDocumentIsValid(final DBBroker broker, final TransactionManager transact, final String documentName) throws PermissionDeniedException, IOException, SAXException, LockException, TransactionException {
// create a transaction
try (final Txn transaction = transact.beginTransaction()) {
// get the test collection
final Collection root = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
assertNotNull(root);
broker.saveCollection(transaction, root);
// get the test document
try (final LockedDocument lockedDoc = root.getDocumentWithLock(broker, XmldbURI.create(documentName), LockMode.READ_LOCK)) {
final DocumentImpl doc = lockedDoc.getDocument();
assertNotNull(doc);
assertEquals(testDocument, serialize(broker, doc));
}
transact.commit(transaction);
}
}
use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.
the class ExceptTest method memtree_except_persistent.
/**
* Tests the XQuery `except` operator against an
* in-memory node on the left and a persistent node on the right
*/
@Test
public void memtree_except_persistent() throws XPathException, NoSuchMethodException {
final XQueryContext mockContext = createMock(XQueryContext.class);
final PathExpr mockLeft = createMock(PathExpr.class);
final PathExpr mockRight = createMock(PathExpr.class);
final Sequence mockContextSequence = createMock(Sequence.class);
final Item mockContextItem = createMock(Item.class);
final Profiler mockProfiler = createMock(Profiler.class);
final DocumentImpl mockPersistentDoc = createMock(DocumentImpl.class);
final NodeProxy mockPersistentNode = createMockBuilder(NodeProxy.class).withConstructor(DocumentImpl.class, NodeId.class).withArgs(mockPersistentDoc, new DLN(1)).addMockedMethods(NodeProxy.class.getMethod("isEmpty", new Class[] {}), NodeProxy.class.getMethod("getItemType", new Class[] {}), NodeProxy.class.getMethod("equals", new Class[] { Object.class })).createMock();
expect(mockContext.nextExpressionId()).andReturn(1);
expect(mockContext.getProfiler()).andReturn(mockProfiler);
// memtree node
expect(mockLeft.eval(mockContextSequence, mockContextItem)).andReturn((org.exist.dom.memtree.ElementImpl) createInMemoryDocument().getDocumentElement());
// persistent node
expect(mockRight.eval(mockContextSequence, mockContextItem)).andReturn(mockPersistentNode);
expect(mockPersistentNode.isEmpty()).andReturn(false);
expect(mockPersistentNode.getItemType()).andReturn(Type.NODE);
expect(mockPersistentDoc.getDocId()).andReturn(1).times(2);
expect(mockContext.getProfiler()).andReturn(mockProfiler);
replay(mockPersistentDoc, mockPersistentNode, mockRight, mockLeft, mockContext);
// test
final Except except = new Except(mockContext, mockLeft, mockRight);
final Sequence result = except.eval(mockContextSequence, mockContextItem);
assertEquals(1, ((ValueSequence) result).size());
verify(mockPersistentDoc, mockPersistentNode, mockRight, mockLeft, mockContext);
}
Aggregations