Search in sources :

Example 41 with NodeCursor

use of org.neo4j.internal.kernel.api.NodeCursor in project neo4j by neo4j.

the class RelationshipTransactionStateTestBase method assertCount.

private void assertCount(int count, RelationshipDirection direction, Consumer<Degrees> asserter) throws Exception {
    long start;
    int type;
    try (KernelTransaction tx = beginTransaction()) {
        Write write = tx.dataWrite();
        start = write.nodeCreate();
        type = tx.tokenWrite().relationshipTypeGetOrCreateForName("R");
        for (int i = 0; i < count; i++) {
            createRelationship(direction, start, type, write);
        }
        tx.commit();
    }
    try (KernelTransaction tx = beginTransaction()) {
        Write write = tx.dataWrite();
        createRelationship(direction, start, type, write);
        try (NodeCursor node = tx.cursors().allocateNodeCursor(NULL)) {
            Read read = tx.dataRead();
            read.singleNode(start, node);
            assertTrue(node.next());
            Degrees degrees = node.degrees(ALL_RELATIONSHIPS);
            asserter.accept(degrees);
        }
    }
}
Also used : Write(org.neo4j.internal.kernel.api.Write) Read(org.neo4j.internal.kernel.api.Read) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Degrees(org.neo4j.storageengine.api.Degrees) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor)

Example 42 with NodeCursor

use of org.neo4j.internal.kernel.api.NodeCursor in project neo4j by neo4j.

the class RelationshipTransactionStateTestBase method shouldIncludeAddedRelationshipsByTypeAndDirection.

@Test
void shouldIncludeAddedRelationshipsByTypeAndDirection() throws Exception {
    int typeId1;
    int typeId2;
    long relationship1;
    long relationship2;
    long sourceNode;
    long targetNode;
    try (KernelTransaction tx = beginTransaction()) {
        Write write = tx.dataWrite();
        typeId1 = tx.tokenWrite().relationshipTypeGetOrCreateForName("R");
        typeId2 = tx.tokenWrite().relationshipTypeGetOrCreateForName("R2");
        sourceNode = write.nodeCreate();
        relationship1 = write.relationshipCreate(sourceNode, typeId1, write.nodeCreate());
        relationship2 = write.relationshipCreate(sourceNode, typeId2, write.nodeCreate());
        targetNode = write.nodeCreate();
        tx.commit();
    }
    SecurityContext loginContext = new SecurityContext(AuthSubject.AUTH_DISABLED, new TestAccessMode(true, false, true, false), EMBEDDED_CONNECTION, null);
    try (KernelTransaction tx = beginTransaction(loginContext);
        NodeCursor node = tx.cursors().allocateNodeCursor(NULL);
        RelationshipTraversalCursor traversal = tx.cursors().allocateRelationshipTraversalCursor(NULL)) {
        Write write = tx.dataWrite();
        // OUTGOING :R
        long r1 = write.relationshipCreate(sourceNode, typeId1, targetNode);
        // INCOMING :R
        long r2 = write.relationshipCreate(targetNode, typeId1, sourceNode);
        // LOOP :R
        long r3 = write.relationshipCreate(sourceNode, typeId1, sourceNode);
        // OUTGOING :R2
        long r4 = write.relationshipCreate(sourceNode, typeId2, targetNode);
        // INCOMING :R2
        long r5 = write.relationshipCreate(targetNode, typeId2, sourceNode);
        // LOOP :R2
        long r6 = write.relationshipCreate(sourceNode, typeId2, sourceNode);
        org.neo4j.internal.kernel.api.Read read = tx.dataRead();
        read.singleNode(sourceNode, node);
        assertTrue(node.next());
        assertRelationships(node, traversal, ALL_RELATIONSHIPS, relationship1, relationship2, r1, r2, r3, r4, r5, r6);
        assertRelationships(node, traversal, selection(OUTGOING), relationship1, relationship2, r1, r3, r4, r6);
        assertRelationships(node, traversal, selection(typeId1, BOTH), relationship1, r1, r2, r3);
        assertRelationships(node, traversal, selection(typeId1, OUTGOING), relationship1, r1, r3);
        assertRelationships(node, traversal, selection(typeId1, INCOMING), r2, r3);
        assertRelationships(node, traversal, selection(typeId2, BOTH), relationship2, r4, r5, r6);
        assertRelationships(node, traversal, selection(typeId2, OUTGOING), relationship2, r4, r6);
        assertRelationships(node, traversal, selection(typeId2, INCOMING), r5, r6);
        assertRelationships(node, traversal, selection(new int[] { typeId1, typeId2 }, BOTH), relationship1, relationship2, r1, r2, r3, r4, r5, r6);
        assertRelationships(node, traversal, selection(new int[] { typeId1, typeId2 }, OUTGOING), relationship1, relationship2, r1, r3, r4, r6);
        assertRelationships(node, traversal, selection(new int[] { typeId1, typeId2 }, INCOMING), r2, r3, r5, r6);
    }
}
Also used : Write(org.neo4j.internal.kernel.api.Write) RelationshipTraversalCursor(org.neo4j.internal.kernel.api.RelationshipTraversalCursor) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Read(org.neo4j.internal.kernel.api.Read) SecurityContext(org.neo4j.internal.kernel.api.security.SecurityContext) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) TestAccessMode(org.neo4j.internal.kernel.api.security.TestAccessMode) Test(org.junit.jupiter.api.Test)

Example 43 with NodeCursor

use of org.neo4j.internal.kernel.api.NodeCursor in project neo4j by neo4j.

the class RelationshipTraversalCursorTestBase method traverse.

private void traverse(RelationshipTestSupport.StartNode start, boolean detached) throws KernelException {
    // given
    try (NodeCursor node = cursors.allocateNodeCursor(NULL);
        RelationshipTraversalCursor relationship = cursors.allocateRelationshipTraversalCursor(NULL)) {
        // when
        read.singleNode(start.id, node);
        assertTrue(node.next(), "access node");
        if (detached) {
            read.relationships(start.id, node.relationshipsReference(), ALL_RELATIONSHIPS, relationship);
        } else {
            node.relationships(relationship, ALL_RELATIONSHIPS);
        }
        Map<String, Integer> counts = count(tx, relationship);
        // then
        assertCounts(start.expectedCounts(), counts);
    }
}
Also used : RelationshipTraversalCursor(org.neo4j.internal.kernel.api.RelationshipTraversalCursor) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor)

Example 44 with NodeCursor

use of org.neo4j.internal.kernel.api.NodeCursor in project neo4j by neo4j.

the class RelationshipTraversalCursorTestBase method shouldHaveBeenAbleToCreateDenseAndSparseNodes.

@Test
void shouldHaveBeenAbleToCreateDenseAndSparseNodes() {
    // given
    try (NodeCursor node = cursors.allocateNodeCursor(NULL)) {
        read.singleNode(dense.id, node);
        assertTrue(node.next(), "access dense node");
        assertTrue(node.supportsFastDegreeLookup(), "dense node");
        read.singleNode(sparse.id, node);
        assertTrue(node.next(), "access sparse node");
        assertFalse(node.supportsFastDegreeLookup() && supportsSparseNodes(), "sparse node");
    }
}
Also used : NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) Test(org.junit.jupiter.api.Test)

Example 45 with NodeCursor

use of org.neo4j.internal.kernel.api.NodeCursor in project neo4j by neo4j.

the class RelationshipTraversalCursorTestBase method shouldTraverseRelationshipsOfGivenType.

@Test
void shouldTraverseRelationshipsOfGivenType() {
    // given
    try (NodeCursor node = cursors.allocateNodeCursor(NULL);
        RelationshipTraversalCursor relationship = cursors.allocateRelationshipTraversalCursor(NULL)) {
        int empty = 0;
        // when
        read.allNodesScan(node);
        while (node.next()) {
            // then
            Degrees degrees = node.degrees(ALL_RELATIONSHIPS);
            boolean none = true;
            for (int type : degrees.types()) {
                none = false;
                Sizes degree = new Sizes();
                node.relationships(relationship, selection(type, BOTH));
                while (relationship.next()) {
                    assertEquals(type, relationship.type(), "node #" + node.nodeReference() + " relationship has label not part of selection");
                    if (relationship.sourceNodeReference() == node.nodeReference()) {
                        degree.outgoing++;
                    }
                    if (relationship.targetNodeReference() == node.nodeReference()) {
                        degree.incoming++;
                    }
                    degree.total++;
                }
                assertNotEquals(0, degree.total, "all");
                assertEquals(degrees.outgoingDegree(type), degree.outgoing, "node #" + node.nodeReference() + " outgoing");
                assertEquals(degrees.incomingDegree(type), degree.incoming, "node #" + node.nodeReference() + " incoming");
                assertEquals(degrees.totalDegree(type), degree.total, "node #" + node.nodeReference() + " all = incoming + outgoing - loop");
            }
            if (none) {
                empty++;
            }
        }
        // then
        assertEquals(1, empty, "number of empty nodes");
    }
}
Also used : RelationshipTraversalCursor(org.neo4j.internal.kernel.api.RelationshipTraversalCursor) Degrees(org.neo4j.storageengine.api.Degrees) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) Test(org.junit.jupiter.api.Test)

Aggregations

NodeCursor (org.neo4j.internal.kernel.api.NodeCursor)113 Test (org.junit.jupiter.api.Test)85 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)62 PropertyCursor (org.neo4j.internal.kernel.api.PropertyCursor)30 RelationshipTraversalCursor (org.neo4j.internal.kernel.api.RelationshipTraversalCursor)26 Read (org.neo4j.internal.kernel.api.Read)20 Write (org.neo4j.internal.kernel.api.Write)17 Degrees (org.neo4j.storageengine.api.Degrees)11 CursorFactory (org.neo4j.internal.kernel.api.CursorFactory)10 ArrayList (java.util.ArrayList)8 ExecutorService (java.util.concurrent.ExecutorService)7 LongList (org.eclipse.collections.api.list.primitive.LongList)7 LongArrayList (org.eclipse.collections.impl.list.mutable.primitive.LongArrayList)7 CachingExpandInto (org.neo4j.internal.kernel.api.helpers.CachingExpandInto)6 TokenWrite (org.neo4j.internal.kernel.api.TokenWrite)5 RelationshipSelection (org.neo4j.storageengine.api.RelationshipSelection)5 MutableLongSet (org.eclipse.collections.api.set.primitive.MutableLongSet)4 Future (java.util.concurrent.Future)3 MutableLongList (org.eclipse.collections.api.list.primitive.MutableLongList)3 NotFoundException (org.neo4j.graphdb.NotFoundException)3