Search in sources :

Example 51 with InternalTransaction

use of org.neo4j.kernel.impl.coreapi.InternalTransaction in project neo4j by neo4j.

the class RelationshipCountsTest method countsForRelationship.

/**
 * @param start the label of the start node of relationships to get the number of, or {@code null} for "any".
 * @param type  the type of the relationships to get the number of, or {@code null} for "any".
 * @param end   the label of the end node of relationships to get the number of, or {@code null} for "any".
 */
private long countsForRelationship(Transaction tx, Label start, RelationshipType type, Label end) {
    KernelTransaction ktx = ((InternalTransaction) tx).kernelTransaction();
    TokenRead tokenRead = ktx.tokenRead();
    int startId;
    int typeId;
    int endId;
    // start
    if (start == null) {
        startId = TokenRead.ANY_LABEL;
    } else {
        if (TokenRead.NO_TOKEN == (startId = tokenRead.nodeLabel(start.name()))) {
            return 0;
        }
    }
    // type
    if (type == null) {
        typeId = TokenRead.ANY_RELATIONSHIP_TYPE;
    } else {
        if (TokenRead.NO_TOKEN == (typeId = tokenRead.relationshipType(type.name()))) {
            return 0;
        }
    }
    // end
    if (end == null) {
        endId = TokenRead.ANY_LABEL;
    } else {
        if (TokenRead.NO_TOKEN == (endId = tokenRead.nodeLabel(end.name()))) {
            return 0;
        }
    }
    return ktx.dataRead().countsForRelationship(startId, typeId, endId);
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) TokenRead(org.neo4j.internal.kernel.api.TokenRead)

Example 52 with InternalTransaction

use of org.neo4j.kernel.impl.coreapi.InternalTransaction in project neo4j by neo4j.

the class DefaultValueMapperTest method shouldMapDirectRelationship.

@Test
void shouldMapDirectRelationship() {
    // Given
    Node start, end;
    Relationship relationship;
    try (Transaction tx = db.beginTx()) {
        start = tx.createNode();
        end = tx.createNode();
        relationship = start.createRelationshipTo(end, RelationshipType.withName("R"));
        tx.commit();
    }
    RelationshipValue relationshipValue = VirtualValues.relationshipValue(relationship.getId(), nodeValue(start.getId(), Values.EMPTY_TEXT_ARRAY, EMPTY_MAP), nodeValue(start.getId(), Values.EMPTY_TEXT_ARRAY, EMPTY_MAP), stringValue("R"), EMPTY_MAP);
    // Then
    try (Transaction tx = db.beginTx()) {
        var mapper = new DefaultValueMapper((InternalTransaction) tx);
        Relationship coreAPIRelationship = mapper.mapRelationship(relationshipValue);
        assertThat(coreAPIRelationship.getId()).isEqualTo(relationship.getId());
        assertThat(coreAPIRelationship.getStartNode()).isEqualTo(start);
        assertThat(coreAPIRelationship.getEndNode()).isEqualTo(end);
    }
}
Also used : InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) RelationshipValue(org.neo4j.values.virtual.RelationshipValue) Test(org.junit.jupiter.api.Test)

Example 53 with InternalTransaction

use of org.neo4j.kernel.impl.coreapi.InternalTransaction in project neo4j by neo4j.

the class DefaultValueMapperTest method shouldHandleLongPath.

@Test
void shouldHandleLongPath() {
    // Given
    Node a, b, c, d, e;
    Relationship r1, r2, r3, r4;
    try (Transaction tx = db.beginTx()) {
        a = tx.createNode();
        b = tx.createNode();
        c = tx.createNode();
        d = tx.createNode();
        e = tx.createNode();
        r1 = a.createRelationshipTo(b, RelationshipType.withName("R"));
        r2 = b.createRelationshipTo(c, RelationshipType.withName("R"));
        r3 = c.createRelationshipTo(d, RelationshipType.withName("R"));
        r4 = d.createRelationshipTo(e, RelationshipType.withName("R"));
        tx.commit();
    }
    // Then
    try (Transaction tx = db.beginTx()) {
        var mapper = new DefaultValueMapper((InternalTransaction) tx);
        Path mapped = mapper.mapPath(path(asNodeValues(a, b, c, d, e), asRelationshipsValues(r1, r2, r3, r4)));
        assertThat(mapped.length()).isEqualTo(4);
        assertThat(mapped.startNode()).isEqualTo(a);
        assertThat(mapped.endNode()).isEqualTo(e);
        assertThat(Iterables.asList(mapped.relationships())).isEqualTo(Arrays.asList(r1, r2, r3, r4));
        assertThat(Iterables.asList(mapped.reverseRelationships())).isEqualTo(Arrays.asList(r4, r3, r2, r1));
        assertThat(Iterables.asList(mapped.nodes())).isEqualTo(Arrays.asList(a, b, c, d, e));
        assertThat(Iterables.asList(mapped.reverseNodes())).isEqualTo(Arrays.asList(e, d, c, b, a));
        assertThat(mapped.lastRelationship()).isEqualTo(r4);
        assertThat(Iterators.asList(mapped.iterator())).isEqualTo(Arrays.asList(a, r1, b, r2, c, r3, d, r4, e));
    }
}
Also used : Path(org.neo4j.graphdb.Path) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.jupiter.api.Test)

Example 54 with InternalTransaction

use of org.neo4j.kernel.impl.coreapi.InternalTransaction in project neo4j by neo4j.

the class GraphDbStructureGuide method accept.

@Override
public void accept(DbStructureVisitor visitor) {
    try (Transaction tx = db.beginTx()) {
        showStructure((InternalTransaction) tx, visitor);
        tx.commit();
    }
}
Also used : InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Transaction(org.neo4j.graphdb.Transaction)

Example 55 with InternalTransaction

use of org.neo4j.kernel.impl.coreapi.InternalTransaction in project neo4j by neo4j.

the class IndexRecoveryIT method createSomeBananas.

private Set<IndexEntryUpdate<?>> createSomeBananas(Label label) {
    Set<IndexEntryUpdate<?>> updates = new HashSet<>();
    try (Transaction tx = db.beginTx()) {
        KernelTransaction ktx = ((InternalTransaction) tx).kernelTransaction();
        int labelId = ktx.tokenRead().nodeLabel(label.name());
        int propertyKeyId = ktx.tokenRead().propertyKey(key);
        LabelSchemaDescriptor schemaDescriptor = SchemaDescriptor.forLabel(labelId, propertyKeyId);
        for (int number : new int[] { 4, 10 }) {
            Node node = tx.createNode(label);
            node.setProperty(key, number);
            updates.add(IndexEntryUpdate.add(node.getId(), schemaDescriptor, Values.of(number)));
        }
        tx.commit();
        return updates;
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) IndexEntryUpdate(org.neo4j.storageengine.api.IndexEntryUpdate) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Node(org.neo4j.graphdb.Node) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) HashSet(java.util.HashSet)

Aggregations

InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)126 Transaction (org.neo4j.graphdb.Transaction)58 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)53 Test (org.junit.jupiter.api.Test)46 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)20 MethodSource (org.junit.jupiter.params.provider.MethodSource)18 Node (org.neo4j.graphdb.Node)16 NodeValueIndexCursor (org.neo4j.internal.kernel.api.NodeValueIndexCursor)15 ArrayList (java.util.ArrayList)13 Test (org.junit.Test)12 Result (org.neo4j.graphdb.Result)12 TokenRead (org.neo4j.internal.kernel.api.TokenRead)11 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)10 IndexReadSession (org.neo4j.internal.kernel.api.IndexReadSession)8 ExecutingQuery (org.neo4j.kernel.api.query.ExecutingQuery)7 Relationship (org.neo4j.graphdb.Relationship)6 GraphDatabaseQueryService (org.neo4j.kernel.GraphDatabaseQueryService)6 GraphDatabaseFacade (org.neo4j.kernel.impl.factory.GraphDatabaseFacade)6 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)6 ReturnsDeepStubs (org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs)5