Search in sources :

Example 11 with RelationshipTraversalCursor

use of org.neo4j.internal.kernel.api.RelationshipTraversalCursor 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 12 with RelationshipTraversalCursor

use of org.neo4j.internal.kernel.api.RelationshipTraversalCursor 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)

Example 13 with RelationshipTraversalCursor

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

the class RelationshipTraversalCursorTestBase method shouldFollowSpecificRelationship.

@Test
void shouldFollowSpecificRelationship() {
    // given
    try (NodeCursor node = cursors.allocateNodeCursor(NULL);
        RelationshipTraversalCursor relationship = cursors.allocateRelationshipTraversalCursor(NULL)) {
        // when - traversing from start to end
        read.singleNode(start, node);
        assertTrue(node.next(), "access start node");
        int[] types = node.relationshipTypes();
        assertTrue(types.length > 0);
        node.relationships(relationship, selection(types[0], OUTGOING));
        assertTrue(relationship.next(), "access outgoing relationships");
        // then
        assertEquals(start, relationship.sourceNodeReference(), "source node");
        assertEquals(end, relationship.targetNodeReference(), "target node");
        assertEquals(start, relationship.originNodeReference(), "node of origin");
        assertEquals(end, relationship.otherNodeReference(), "neighbouring node");
        assertEquals(types[0], relationship.type(), "relationship should have correct label");
        assertFalse(relationship.next(), "only a single relationship");
        node.relationships(relationship, selection(types[0], INCOMING));
        assertFalse(relationship.next(), "no incoming relationships");
        // when - traversing from end to start
        read.singleNode(end, node);
        assertTrue(node.next(), "access start node");
        types = node.relationshipTypes();
        assertTrue(types.length > 0);
        node.relationships(relationship, selection(types[0], INCOMING));
        assertTrue(relationship.next(), "access incoming relationships");
        // then
        assertEquals(start, relationship.sourceNodeReference(), "source node");
        assertEquals(end, relationship.targetNodeReference(), "target node");
        assertEquals(end, relationship.originNodeReference(), "node of origin");
        assertEquals(start, relationship.otherNodeReference(), "neighbouring node");
        assertEquals(types[0], relationship.type(), "relationship should have correct label");
        assertFalse(relationship.next(), "only a single relationship");
        node.relationships(relationship, selection(types[0], OUTGOING));
        assertFalse(relationship.next(), "no outgoing relationships");
    }
}
Also used : RelationshipTraversalCursor(org.neo4j.internal.kernel.api.RelationshipTraversalCursor) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) Test(org.junit.jupiter.api.Test)

Example 14 with RelationshipTraversalCursor

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

the class RelationshipTransactionStateTestBase method shouldAddToCountFromTxState.

@Test
void shouldAddToCountFromTxState() throws Exception {
    long start;
    long existingRelationship;
    int type;
    try (KernelTransaction tx = beginTransaction()) {
        Write write = tx.dataWrite();
        start = write.nodeCreate();
        type = tx.tokenWrite().relationshipTypeGetOrCreateForName("OUT");
        existingRelationship = write.relationshipCreate(start, type, write.nodeCreate());
        tx.commit();
    }
    try (KernelTransaction tx = beginTransaction()) {
        Write write = tx.dataWrite();
        long newRelationship = write.relationshipCreate(start, type, write.nodeCreate());
        try (NodeCursor node = tx.cursors().allocateNodeCursor(NULL);
            RelationshipTraversalCursor traversal = tx.cursors().allocateRelationshipTraversalCursor(NULL)) {
            org.neo4j.internal.kernel.api.Read read = tx.dataRead();
            read.singleNode(start, node);
            assertTrue(node.next());
            Degrees degrees = node.degrees(selection(type, BOTH));
            assertEquals(2, degrees.outgoingDegree(type));
            assertEquals(0, degrees.incomingDegree(type));
            assertRelationships(OUTGOING, node, type, traversal, newRelationship, existingRelationship);
        }
    }
}
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) Degrees(org.neo4j.storageengine.api.Degrees) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) Test(org.junit.jupiter.api.Test)

Example 15 with RelationshipTraversalCursor

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

the class RelationshipTransactionStateTestBase method traverse.

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

Aggregations

RelationshipTraversalCursor (org.neo4j.internal.kernel.api.RelationshipTraversalCursor)29 NodeCursor (org.neo4j.internal.kernel.api.NodeCursor)26 Test (org.junit.jupiter.api.Test)23 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)17 Write (org.neo4j.internal.kernel.api.Write)10 Read (org.neo4j.internal.kernel.api.Read)7 Degrees (org.neo4j.storageengine.api.Degrees)7 TokenWrite (org.neo4j.internal.kernel.api.TokenWrite)3 PropertyCursor (org.neo4j.internal.kernel.api.PropertyCursor)2 MutableLongSet (org.eclipse.collections.api.set.primitive.MutableLongSet)1 LongHashSet (org.eclipse.collections.impl.set.mutable.primitive.LongHashSet)1 SecurityContext (org.neo4j.internal.kernel.api.security.SecurityContext)1 TestAccessMode (org.neo4j.internal.kernel.api.security.TestAccessMode)1