Search in sources :

Example 96 with InternalTransaction

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

the class DefaultValueMapperTest method shouldHandleSingleRelationshipPath.

@Test
void shouldHandleSingleRelationshipPath() {
    // 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();
    }
    // Then
    try (Transaction tx = db.beginTx()) {
        var mapper = new DefaultValueMapper((InternalTransaction) tx);
        Path mapped = mapper.mapPath(path(asNodeValues(start, end), asRelationshipsValues(relationship)));
        assertThat(mapped.length()).isEqualTo(1);
        assertThat(mapped.startNode()).isEqualTo(start);
        assertThat(mapped.endNode()).isEqualTo(end);
        assertThat(Iterables.asList(mapped.relationships())).isEqualTo(singletonList(relationship));
        assertThat(Iterables.asList(mapped.reverseRelationships())).isEqualTo(singletonList(relationship));
        assertThat(Iterables.asList(mapped.nodes())).isEqualTo(Arrays.asList(start, end));
        assertThat(Iterables.asList(mapped.reverseNodes())).isEqualTo(Arrays.asList(end, start));
        assertThat(mapped.lastRelationship()).isEqualTo(relationship);
        assertThat(Iterators.asList(mapped.iterator())).isEqualTo(Arrays.asList(start, relationship, end));
    }
}
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 97 with InternalTransaction

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

the class DenseNodeConcurrencyIT method shouldHandleDetachDeleteWithConcurrentAdds.

@Test
void shouldHandleDetachDeleteWithConcurrentAdds() {
    long initialNodes;
    long initialRelationships;
    try (Transaction tx = database.beginTx()) {
        initialNodes = Iterables.count(tx.getAllNodes());
        initialRelationships = Iterables.count(tx.getAllRelationships());
    }
    long denseNodeToDelete = createNode(new HashSet<>(), 100);
    AtomicInteger numNodes = new AtomicInteger(2);
    int numModifiers = Runtime.getRuntime().availableProcessors() - 1;
    int numCreators = Math.max(numModifiers * 2 / 3, 2);
    int numDeleters = Math.max(numModifiers / 3, 1);
    CountDownLatch latch = new CountDownLatch(numCreators * 2);
    AtomicBoolean done = new AtomicBoolean();
    Race race = new Race().withFailureAction(t -> done.set(true));
    race.addContestants(1, throwing(() -> {
        latch.await();
        while (true) {
            try (Transaction tx = database.beginTx()) {
                InternalTransaction internalTx = (InternalTransaction) tx;
                internalTx.kernelTransaction().dataWrite().nodeDetachDelete(denseNodeToDelete);
                tx.commit();
                break;
            } catch (DeadlockDetectedException ignore) {
            // ignore deadlock, try again
            }
        }
        numNodes.decrementAndGet();
        done.set(true);
    }));
    race.addContestants(numCreators, throwing(() -> {
        try {
            int creations = 0;
            while (!done.get() && creations < 20) {
                latch.countDown();
                try (Transaction tx = database.beginTx()) {
                    Node denseNode = tx.getNodeById(denseNodeToDelete);
                    var type = random.nextBoolean() ? TEST : TEST2;
                    switch(random.nextInt(3)) {
                        case 0:
                            denseNode.createRelationshipTo(tx.createNode(), type);
                            numNodes.incrementAndGet();
                            break;
                        case 1:
                            tx.createNode().createRelationshipTo(denseNode, type);
                            numNodes.incrementAndGet();
                        default:
                            denseNode.createRelationshipTo(denseNode, type);
                            break;
                    }
                    tx.commit();
                    creations++;
                    // Provoke race, since we do not have fair locking, this will give a small windows to grab exclusive
                    Thread.sleep(1);
                } catch (DeadlockDetectedException ignore) {
                // ignore deadlock, try again
                }
            }
        } catch (NotFoundException e) {
        // expected, exit!
        }
    }));
    race.addContestants(numDeleters, throwing(() -> {
        try {
            while (!done.get()) {
                try (Transaction tx = database.beginTx()) {
                    Node denseNode = tx.getNodeById(denseNodeToDelete);
                    Relationship[] relationships = Iterables.asArray(Relationship.class, denseNode.getRelationships());
                    if (relationships.length > 0) {
                        random.among(relationships).delete();
                    }
                    tx.commit();
                    // Provoke race, since we do not have fair locking, this will give a small windows to grab exclusive
                    Thread.sleep(1);
                } catch (DeadlockDetectedException ignore) {
                // deadlock, retry
                }
            }
        } catch (NotFoundException e) {
        // expected, exit!
        }
    }));
    race.goUnchecked();
    try (Transaction tx = database.beginTx()) {
        assertThat(Iterables.count(tx.getAllNodes())).isEqualTo(numNodes.get() + initialNodes);
        assertThat(Iterables.count(tx.getAllRelationships())).isEqualTo(initialRelationships);
        assertThatThrownBy(() -> tx.getNodeById(denseNodeToDelete)).isInstanceOf(NotFoundException.class);
    }
}
Also used : DeadlockDetectedException(org.neo4j.kernel.DeadlockDetectedException) CountDownLatch(java.util.concurrent.CountDownLatch) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Race(org.neo4j.test.Race) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 98 with InternalTransaction

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

the class LabelsAcceptanceTest method shouldAllowManyLabelsAndPropertyCursor.

@Test
void shouldAllowManyLabelsAndPropertyCursor() {
    int propertyCount = 10;
    int labelCount = 15;
    Node node;
    try (Transaction tx = db.beginTx()) {
        node = tx.createNode();
        for (int i = 0; i < propertyCount; i++) {
            node.setProperty("foo" + i, "bar");
        }
        for (int i = 0; i < labelCount; i++) {
            node.addLabel(label("label" + i));
        }
        tx.commit();
    }
    Set<Integer> seenProperties = new HashSet<>();
    Set<Integer> seenLabels = new HashSet<>();
    try (Transaction tx = db.beginTx()) {
        KernelTransaction ktx = ((InternalTransaction) tx).kernelTransaction();
        try (NodeCursor nodes = ktx.cursors().allocateNodeCursor(CursorContext.NULL);
            PropertyCursor propertyCursor = ktx.cursors().allocatePropertyCursor(CursorContext.NULL, INSTANCE)) {
            ktx.dataRead().singleNode(node.getId(), nodes);
            while (nodes.next()) {
                nodes.properties(propertyCursor);
                while (propertyCursor.next()) {
                    seenProperties.add(propertyCursor.propertyKey());
                }
                TokenSet labels = nodes.labels();
                for (int i = 0; i < labels.numberOfTokens(); i++) {
                    seenLabels.add(labels.token(i));
                }
            }
        }
        tx.commit();
    }
    assertEquals(propertyCount, seenProperties.size());
    assertEquals(labelCount, seenLabels.size());
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TokenSet(org.neo4j.internal.kernel.api.TokenSet) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) PropertyCursor(org.neo4j.internal.kernel.api.PropertyCursor) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 99 with InternalTransaction

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

the class NodeEntityTest method traceNodePageCacheAccessOnRelationshipsAccess.

@Test
void traceNodePageCacheAccessOnRelationshipsAccess() {
    long targetId;
    var relationshipType = RelationshipType.withName("connection");
    try (Transaction tx = db.beginTx()) {
        var target = tx.createNode();
        for (int i = 0; i < 100; i++) {
            tx.createNode().createRelationshipTo(target, relationshipType);
        }
        targetId = target.getId();
        tx.commit();
    }
    try (Transaction tx = db.beginTx()) {
        var cursorContext = ((InternalTransaction) tx).kernelTransaction().cursorContext();
        PageCursorTracer cursorTracer = cursorContext.getCursorTracer();
        var source = tx.getNodeById(targetId);
        cursorTracer.reportEvents();
        assertZeroTracer(cursorContext);
        assertThat(count(source.getRelationships(Direction.INCOMING, relationshipType))).isGreaterThan(0);
        assertThat(cursorTracer.hits()).isEqualTo(3);
        assertThat(cursorTracer.unpins()).isEqualTo(2);
        assertThat(cursorTracer.pins()).isEqualTo(3);
    }
}
Also used : PageCursorTracer(org.neo4j.io.pagecache.tracing.cursor.PageCursorTracer) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) Test(org.junit.jupiter.api.Test)

Example 100 with InternalTransaction

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

the class NodeEntityTest method traceNodePageCacheAccessOnDegreeCount.

@Test
void traceNodePageCacheAccessOnDegreeCount() {
    long sourceId;
    try (Transaction tx = db.beginTx()) {
        var source = tx.createNode();
        var relationshipType = RelationshipType.withName("connection");
        createDenseNodeWithShortIncomingChain(tx, source, relationshipType);
        sourceId = source.getId();
        tx.commit();
    }
    try (Transaction tx = db.beginTx()) {
        var cursorContext = ((InternalTransaction) tx).kernelTransaction().cursorContext();
        PageCursorTracer cursorTracer = cursorContext.getCursorTracer();
        var source = tx.getNodeById(sourceId);
        cursorTracer.reportEvents();
        assertZeroTracer(cursorContext);
        source.getDegree(Direction.INCOMING);
        assertThat(cursorTracer.hits()).isEqualTo(3);
        assertThat(cursorTracer.unpins()).isEqualTo(0);
        assertThat(cursorTracer.pins()).isEqualTo(3);
    }
}
Also used : PageCursorTracer(org.neo4j.io.pagecache.tracing.cursor.PageCursorTracer) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) Test(org.junit.jupiter.api.Test)

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