Search in sources :

Example 81 with DocumentImpl

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;
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) NewArrayNodeSet(org.exist.dom.persistent.NewArrayNodeSet) IndexQuery(org.exist.storage.btree.IndexQuery) QName(org.exist.dom.QName) NodeProxy(org.exist.dom.persistent.NodeProxy) DocumentImpl(org.exist.dom.persistent.DocumentImpl) PermissionDeniedException(org.exist.security.PermissionDeniedException) LockException(org.exist.util.LockException) DatabaseConfigurationException(org.exist.util.DatabaseConfigurationException) LockException(org.exist.util.LockException) NodeId(org.exist.numbering.NodeId) Value(org.exist.storage.btree.Value)

Example 82 with DocumentImpl

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;
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) NewArrayNodeSet(org.exist.dom.persistent.NewArrayNodeSet) LockException(org.exist.util.LockException) NodeId(org.exist.numbering.NodeId) Value(org.exist.storage.btree.Value) NodeProxy(org.exist.dom.persistent.NodeProxy) DocumentImpl(org.exist.dom.persistent.DocumentImpl) PermissionDeniedException(org.exist.security.PermissionDeniedException) LockException(org.exist.util.LockException) DatabaseConfigurationException(org.exist.util.DatabaseConfigurationException)

Example 83 with DocumentImpl

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;
}
Also used : XPathException(org.exist.xquery.XPathException) Collection(org.exist.collections.Collection) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 84 with DocumentImpl

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);
    }
}
Also used : LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 85 with DocumentImpl

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);
}
Also used : DLN(org.exist.numbering.DLN) NodeId(org.exist.numbering.NodeId) DocumentImpl(org.exist.dom.persistent.DocumentImpl) NodeProxy(org.exist.dom.persistent.NodeProxy) Test(org.junit.Test)

Aggregations

DocumentImpl (org.exist.dom.persistent.DocumentImpl)128 PermissionDeniedException (org.exist.security.PermissionDeniedException)51 Collection (org.exist.collections.Collection)40 LockedDocument (org.exist.dom.persistent.LockedDocument)39 Txn (org.exist.storage.txn.Txn)35 XmldbURI (org.exist.xmldb.XmldbURI)34 EXistException (org.exist.EXistException)27 LockException (org.exist.util.LockException)26 DBBroker (org.exist.storage.DBBroker)25 IOException (java.io.IOException)19 NodeProxy (org.exist.dom.persistent.NodeProxy)18 URISyntaxException (java.net.URISyntaxException)17 BrokerPool (org.exist.storage.BrokerPool)17 TransactionManager (org.exist.storage.txn.TransactionManager)17 Test (org.junit.Test)17 XPathException (org.exist.xquery.XPathException)16 NodeId (org.exist.numbering.NodeId)14 SAXException (org.xml.sax.SAXException)13 StoredNode (org.exist.dom.persistent.StoredNode)12 BinaryDocument (org.exist.dom.persistent.BinaryDocument)11