Search in sources :

Example 46 with NodeCursor

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

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

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

Example 49 with NodeCursor

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

the class RelationshipTransactionStateTestBase method shouldSeeNewTypes.

@Test
void shouldSeeNewTypes() throws Exception {
    try (KernelTransaction tx = beginTransaction()) {
        Write write = tx.dataWrite();
        long start = write.nodeCreate();
        int outgoing = tx.tokenWrite().relationshipTypeGetOrCreateForName("OUT");
        int incoming = tx.tokenWrite().relationshipTypeGetOrCreateForName("IN");
        int looping = tx.tokenWrite().relationshipTypeGetOrCreateForName("LOOP");
        long out = write.relationshipCreate(start, outgoing, write.nodeCreate());
        long in1 = write.relationshipCreate(write.nodeCreate(), incoming, start);
        long in2 = write.relationshipCreate(write.nodeCreate(), incoming, start);
        long loop = write.relationshipCreate(start, looping, start);
        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(ALL_RELATIONSHIPS);
            for (int t : degrees.types()) {
                if (t == outgoing) {
                    assertEquals(1, degrees.outgoingDegree(t));
                    assertEquals(0, degrees.incomingDegree(t));
                    assertRelationships(OUTGOING, node, t, traversal, out);
                    assertNoRelationships(INCOMING, node, t, traversal);
                } else if (t == incoming) {
                    assertEquals(0, degrees.outgoingDegree(t));
                    assertEquals(2, degrees.incomingDegree(t));
                    assertRelationships(INCOMING, node, t, traversal, in1, in2);
                    assertNoRelationships(OUTGOING, node, t, traversal);
                } else if (t == looping) {
                    assertEquals(1, degrees.outgoingDegree(t));
                    assertEquals(1, degrees.incomingDegree(t));
                    assertRelationships(BOTH, node, t, traversal, loop);
                    assertRelationships(OUTGOING, node, t, traversal, loop);
                    assertRelationships(INCOMING, node, t, traversal, loop);
                } else {
                    fail(t + "  is not the type you're looking for ");
                }
            }
        }
    }
}
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 50 with NodeCursor

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

the class CachingExpandIntoTest method shouldComputeDegreeOfStartAndEndNode.

@Test
void shouldComputeDegreeOfStartAndEndNode() throws Exception {
    // Given
    CachingExpandInto expandInto = new CachingExpandInto(mock(Read.class), OUTGOING, memoryTracker);
    NodeCursor cursor = mockCursor();
    // Then
    assertEstimatesCorrectly(expandInto);
    // When
    findConnections(expandInto, cursor, 42, 43);
    // Then
    verify(cursor, times(2)).degree(any(RelationshipSelection.class));
    assertReleasesHeap(expandInto);
}
Also used : Read(org.neo4j.internal.kernel.api.Read) CachingExpandInto(org.neo4j.internal.kernel.api.helpers.CachingExpandInto) RelationshipSelection(org.neo4j.storageengine.api.RelationshipSelection) 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