Search in sources :

Example 1 with Degrees

use of org.neo4j.storageengine.api.Degrees in project neo4j by neo4j.

the class CachingExpandIntoTest method shouldComputeDegreeWithType.

@Test
void shouldComputeDegreeWithType() throws Exception {
    // GIVEN
    long node;
    int in, out, loop;
    try (KernelTransaction tx = transaction()) {
        Write write = tx.dataWrite();
        node = denseNode(tx);
        TokenWrite tokenWrite = tx.tokenWrite();
        out = tokenWrite.relationshipTypeGetOrCreateForName("OUT");
        in = tokenWrite.relationshipTypeGetOrCreateForName("IN");
        loop = tokenWrite.relationshipTypeGetOrCreateForName("LOOP");
        write.relationshipCreate(node, out, write.nodeCreate());
        write.relationshipCreate(node, out, write.nodeCreate());
        write.relationshipCreate(write.nodeCreate(), in, node);
        write.relationshipCreate(node, loop, node);
        tx.commit();
    }
    try (KernelTransaction tx = transaction()) {
        Read read = tx.dataRead();
        CursorFactory cursors = tx.cursors();
        try (NodeCursor nodes = cursors.allocateNodeCursor(tx.cursorContext())) {
            CachingExpandInto expand = new CachingExpandInto(tx.dataRead(), OUTGOING, MEMORY_TRACKER);
            read.singleNode(node, nodes);
            assertThat(nodes.next()).isEqualTo(true);
            assertThat(nodes.supportsFastDegreeLookup()).isEqualTo(true);
            Degrees degrees = nodes.degrees(ALL_RELATIONSHIPS);
            assertThat(degrees.outgoingDegree(out)).isEqualTo(2);
            assertThat(degrees.outgoingDegree(in)).isEqualTo(0);
            assertThat(degrees.outgoingDegree(loop)).isEqualTo(1);
            assertThat(degrees.incomingDegree(out)).isEqualTo(0);
            assertThat(degrees.incomingDegree(in)).isEqualTo(1);
            assertThat(degrees.incomingDegree(loop)).isEqualTo(1);
            assertThat(degrees.totalDegree(out)).isEqualTo(2);
            assertThat(degrees.totalDegree(in)).isEqualTo(1);
            assertThat(degrees.totalDegree(loop)).isEqualTo(1);
        }
    }
}
Also used : TokenWrite(org.neo4j.internal.kernel.api.TokenWrite) Write(org.neo4j.internal.kernel.api.Write) Read(org.neo4j.internal.kernel.api.Read) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) CursorFactory(org.neo4j.internal.kernel.api.CursorFactory) Degrees(org.neo4j.storageengine.api.Degrees) TokenWrite(org.neo4j.internal.kernel.api.TokenWrite) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) Test(org.junit.jupiter.api.Test)

Example 2 with Degrees

use of org.neo4j.storageengine.api.Degrees in project neo4j by neo4j.

the class NodeEntity method getRelationshipTypes.

@Override
public Iterable<RelationshipType> getRelationshipTypes() {
    KernelTransaction transaction = internalTransaction.kernelTransaction();
    try {
        NodeCursor nodes = transaction.ambientNodeCursor();
        TokenRead tokenRead = transaction.tokenRead();
        singleNode(transaction, nodes);
        Degrees degrees = nodes.degrees(ALL_RELATIONSHIPS);
        List<RelationshipType> types = new ArrayList<>();
        for (int type : degrees.types()) {
            // only include this type if there are any relationships with this type
            if (degrees.totalDegree(type) > 0) {
                types.add(RelationshipType.withName(tokenRead.relationshipTypeName(type)));
            }
        }
        return types;
    } catch (KernelException e) {
        throw new NotFoundException("Relationship name not found.", e);
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Degrees(org.neo4j.storageengine.api.Degrees) RelationshipType(org.neo4j.graphdb.RelationshipType) ArrayList(java.util.ArrayList) NotFoundException(org.neo4j.graphdb.NotFoundException) EntityNotFoundException(org.neo4j.internal.kernel.api.exceptions.EntityNotFoundException) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) PropertyKeyIdNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException) InvalidTransactionTypeKernelException(org.neo4j.internal.kernel.api.exceptions.InvalidTransactionTypeKernelException) TokenCapacityExceededKernelException(org.neo4j.internal.kernel.api.exceptions.schema.TokenCapacityExceededKernelException) LabelNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.LabelNotFoundKernelException) KernelException(org.neo4j.exceptions.KernelException) TokenRead(org.neo4j.internal.kernel.api.TokenRead)

Example 3 with Degrees

use of org.neo4j.storageengine.api.Degrees in project neo4j by neo4j.

the class RelationshipTransactionStateTestBase method shouldNewRelationshipBetweenAlreadyConnectedSparseNodes.

@Test
void shouldNewRelationshipBetweenAlreadyConnectedSparseNodes() throws Exception {
    long start;
    long end;
    long existingRelationship;
    int type;
    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);
        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());
            assertFalse(node.supportsFastDegreeLookup());
            Degrees degrees = node.degrees(selection(type, BOTH));
            assertEquals(2, degrees.outgoingDegree());
            assertEquals(0, degrees.incomingDegree());
            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 4 with Degrees

use of org.neo4j.storageengine.api.Degrees 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 5 with Degrees

use of org.neo4j.storageengine.api.Degrees 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)11 Degrees (org.neo4j.storageengine.api.Degrees)11 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)10 Test (org.junit.jupiter.api.Test)9 Read (org.neo4j.internal.kernel.api.Read)9 Write (org.neo4j.internal.kernel.api.Write)9 RelationshipTraversalCursor (org.neo4j.internal.kernel.api.RelationshipTraversalCursor)7 CursorFactory (org.neo4j.internal.kernel.api.CursorFactory)2 TokenWrite (org.neo4j.internal.kernel.api.TokenWrite)2 ArrayList (java.util.ArrayList)1 KernelException (org.neo4j.exceptions.KernelException)1 NotFoundException (org.neo4j.graphdb.NotFoundException)1 RelationshipType (org.neo4j.graphdb.RelationshipType)1 TokenRead (org.neo4j.internal.kernel.api.TokenRead)1 EntityNotFoundException (org.neo4j.internal.kernel.api.exceptions.EntityNotFoundException)1 InvalidTransactionTypeKernelException (org.neo4j.internal.kernel.api.exceptions.InvalidTransactionTypeKernelException)1 LabelNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.LabelNotFoundKernelException)1 PropertyKeyIdNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException)1 TokenCapacityExceededKernelException (org.neo4j.internal.kernel.api.exceptions.schema.TokenCapacityExceededKernelException)1