Search in sources :

Example 21 with RelationshipTraversalCursor

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

the class RelationshipTransactionStateTestBase method shouldSeeNewRelationshipPropertyInTransaction.

@Test
void shouldSeeNewRelationshipPropertyInTransaction() throws Exception {
    try (KernelTransaction tx = beginTransaction()) {
        String propKey1 = "prop1";
        String propKey2 = "prop2";
        long n1 = tx.dataWrite().nodeCreate();
        long n2 = tx.dataWrite().nodeCreate();
        int label = tx.tokenWrite().relationshipTypeGetOrCreateForName("R");
        long r = tx.dataWrite().relationshipCreate(n1, label, n2);
        int prop1 = tx.token().propertyKeyGetOrCreateForName(propKey1);
        int prop2 = tx.token().propertyKeyGetOrCreateForName(propKey2);
        assertEquals(tx.dataWrite().relationshipSetProperty(r, prop1, stringValue("hello")), NO_VALUE);
        assertEquals(tx.dataWrite().relationshipSetProperty(r, prop2, stringValue("world")), NO_VALUE);
        try (NodeCursor node = tx.cursors().allocateNodeCursor(NULL);
            RelationshipTraversalCursor relationship = tx.cursors().allocateRelationshipTraversalCursor(NULL);
            PropertyCursor property = tx.cursors().allocatePropertyCursor(NULL, INSTANCE)) {
            tx.dataRead().singleNode(n1, node);
            assertTrue(node.next(), "should access node");
            node.relationships(relationship, ALL_RELATIONSHIPS);
            assertTrue(relationship.next(), "should access relationship");
            relationship.properties(property);
            while (property.next()) {
                if (property.propertyKey() == prop1) {
                    assertEquals(property.propertyValue(), stringValue("hello"));
                } else if (property.propertyKey() == prop2) {
                    assertEquals(property.propertyValue(), stringValue("world"));
                } else {
                    fail(property.propertyKey() + " was not the property key you were looking for");
                }
            }
            assertFalse(relationship.next(), "should only find one relationship");
        }
        tx.commit();
    }
}
Also used : RelationshipTraversalCursor(org.neo4j.internal.kernel.api.RelationshipTraversalCursor) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) PropertyCursor(org.neo4j.internal.kernel.api.PropertyCursor) Test(org.junit.jupiter.api.Test)

Example 22 with RelationshipTraversalCursor

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

the class RelationshipTransactionStateTestBase method shouldNotSeeRelationshipDeletedInTransaction.

@Test
void shouldNotSeeRelationshipDeletedInTransaction() throws Exception {
    long n1, n2, r;
    try (KernelTransaction tx = beginTransaction()) {
        n1 = tx.dataWrite().nodeCreate();
        n2 = tx.dataWrite().nodeCreate();
        int label = tx.tokenWrite().relationshipTypeGetOrCreateForName("R");
        r = tx.dataWrite().relationshipCreate(n1, label, n2);
        tx.commit();
    }
    try (KernelTransaction tx = beginTransaction()) {
        tx.dataWrite().relationshipDelete(r);
        try (NodeCursor node = tx.cursors().allocateNodeCursor(NULL);
            RelationshipTraversalCursor relationship = tx.cursors().allocateRelationshipTraversalCursor(NULL)) {
            tx.dataRead().singleNode(n1, node);
            assertTrue(node.next(), "should access node");
            node.relationships(relationship, ALL_RELATIONSHIPS);
            assertFalse(relationship.next(), "should not find relationship");
        }
        tx.commit();
    }
}
Also used : RelationshipTraversalCursor(org.neo4j.internal.kernel.api.RelationshipTraversalCursor) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) Test(org.junit.jupiter.api.Test)

Example 23 with RelationshipTraversalCursor

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

the class RelationshipTransactionStateTestBase method shouldNewRelationshipBetweenAlreadyConnectedDenseNodes.

@Test
void shouldNewRelationshipBetweenAlreadyConnectedDenseNodes() throws Exception {
    long start;
    long end;
    long existingRelationship;
    int type, bulk;
    try (KernelTransaction tx = beginTransaction()) {
        Write write = tx.dataWrite();
        start = write.nodeCreate();
        end = write.nodeCreate();
        type = tx.tokenWrite().relationshipTypeGetOrCreateForName("R");
        existingRelationship = write.relationshipCreate(start, type, end);
        bulk = tx.tokenWrite().relationshipTypeGetOrCreateForName("BULK");
        for (int i = 0; i < 100; i++) {
            write.relationshipCreate(start, bulk, 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());
            assertTrue(node.supportsFastDegreeLookup());
            Degrees degrees = node.degrees(ALL_RELATIONSHIPS);
            for (int t : degrees.types()) {
                if (t == type) {
                    assertEquals(2, degrees.outgoingDegree(t));
                    assertEquals(0, degrees.incomingDegree(t));
                    assertRelationships(OUTGOING, node, t, traversal, existingRelationship, newRelationship);
                } else if (t == bulk) {
                    assertEquals(bulk, t);
                    assertEquals(100, degrees.outgoingDegree(t));
                    assertEquals(0, degrees.incomingDegree(t));
                } 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 24 with RelationshipTraversalCursor

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

the class RandomRelationshipTraversalCursorTestBase method shouldManageRandomTraversals.

@Test
void shouldManageRandomTraversals() {
    // given
    try (NodeCursor node = cursors.allocateNodeCursor(NULL);
        RelationshipTraversalCursor relationship = cursors.allocateRelationshipTraversalCursor(NULL)) {
        for (int i = 0; i < N_TRAVERSALS; i++) {
            // when
            long nodeId = NODE_IDS.get(RANDOM.nextInt(N_NODES));
            read.singleNode(nodeId, node);
            assertTrue(node.next(), "access root node");
            int[] types = node.relationshipTypes();
            assertFalse(node.next(), "single root");
            // then
            for (int type : types) {
                node.relationships(relationship, selection(type, INCOMING));
                while (relationship.next()) {
                    assertEquals(nodeId, relationship.originNodeReference(), "incoming origin");
                    relationship.otherNode(node);
                }
                node.relationships(relationship, selection(type, OUTGOING));
                while (relationship.next()) {
                    assertEquals(nodeId, relationship.originNodeReference(), "outgoing origin");
                    relationship.otherNode(node);
                }
            }
        }
    } catch (Throwable t) {
        throw new RuntimeException("Failed with random seed " + SEED, t);
    }
}
Also used : RelationshipTraversalCursor(org.neo4j.internal.kernel.api.RelationshipTraversalCursor) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) Test(org.junit.jupiter.api.Test)

Example 25 with RelationshipTraversalCursor

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

the class DeepRelationshipTraversalCursorTestBase method shouldTraverseTreeOfDepthThree.

@Test
void shouldTraverseTreeOfDepthThree() {
    try (NodeCursor node = cursors.allocateNodeCursor(NULL);
        RelationshipTraversalCursor relationship1 = cursors.allocateRelationshipTraversalCursor(NULL);
        RelationshipTraversalCursor relationship2 = cursors.allocateRelationshipTraversalCursor(NULL)) {
        MutableLongSet leafs = new LongHashSet();
        long total = 0;
        // when
        read.singleNode(three_root, node);
        assertTrue(node.next(), "access root node");
        node.relationships(relationship1, selection(parentRelationshipTypeId, Direction.INCOMING));
        while (relationship1.next()) {
            relationship1.otherNode(node);
            assertTrue(node.next(), "child level 1");
            node.relationships(relationship2, selection(parentRelationshipTypeId, Direction.INCOMING));
            while (relationship2.next()) {
                leafs.add(relationship2.otherNodeReference());
                total++;
            }
        }
        // then
        assertEquals(expected_total, total, "total number of leaf nodes");
        assertEquals(expected_unique, leafs.size(), "number of distinct leaf nodes");
    }
}
Also used : RelationshipTraversalCursor(org.neo4j.internal.kernel.api.RelationshipTraversalCursor) LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) MutableLongSet(org.eclipse.collections.api.set.primitive.MutableLongSet) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) Test(org.junit.jupiter.api.Test)

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