Search in sources :

Example 91 with KernelTransaction

use of org.neo4j.kernel.api.KernelTransaction in project neo4j by neo4j.

the class RebuildCountsTest method shouldRebuildMissingCountsStoreAfterRecovery.

@Test
void shouldRebuildMissingCountsStoreAfterRecovery() throws IOException, TransactionFailureException {
    // given
    createAliensAndHumans();
    // when
    rotateLog();
    deleteHumans();
    FileSystemAbstraction fs = crash();
    deleteCounts(fs);
    restart(fs);
    // then
    Kernel kernel = ((GraphDatabaseAPI) db).getDependencyResolver().resolveDependency(Kernel.class);
    try (KernelTransaction tx = kernel.beginTransaction(EXPLICIT, AUTH_DISABLED)) {
        assertEquals(ALIENS, tx.dataRead().countsForNode(-1));
        assertEquals(ALIENS, tx.dataRead().countsForNode(labelId(ALIEN)));
        assertEquals(0, tx.dataRead().countsForNode(labelId(HUMAN)));
    }
    // and also
    assertRebuildLogged();
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) UncloseableDelegatingFileSystemAbstraction(org.neo4j.io.fs.UncloseableDelegatingFileSystemAbstraction) EphemeralFileSystemAbstraction(org.neo4j.io.fs.EphemeralFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) Kernel(org.neo4j.kernel.api.Kernel) Test(org.junit.jupiter.api.Test)

Example 92 with KernelTransaction

use of org.neo4j.kernel.api.KernelTransaction in project neo4j by neo4j.

the class NodeScanIT method trackPageCacheAccessOnNodeLabelScan.

@Test
void trackPageCacheAccessOnNodeLabelScan() throws KernelException {
    var testLabel = Label.label("testLabel");
    try (KernelTransaction tx = kernel.beginTransaction(IMPLICIT, read())) {
        var cursorContext = tx.cursorContext();
        assertThat(cursorContext.getCursorTracer().pins()).isZero();
        var label = tx.tokenRead().nodeLabel(testLabel.name());
        IndexDescriptor index = tx.schemaRead().index(SchemaDescriptor.forAnyEntityTokens(EntityType.NODE)).next();
        TokenReadSession tokenReadSession = tx.dataRead().tokenReadSession(index);
        try (NodeLabelIndexCursor cursor = tx.cursors().allocateNodeLabelIndexCursor(cursorContext)) {
            tx.dataRead().nodeLabelScan(tokenReadSession, cursor, IndexQueryConstraints.unconstrained(), new TokenPredicate(label));
            assertThat(cursorContext.getCursorTracer().pins()).isNotZero();
        }
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TokenReadSession(org.neo4j.internal.kernel.api.TokenReadSession) TokenPredicate(org.neo4j.internal.kernel.api.TokenPredicate) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) NodeLabelIndexCursor(org.neo4j.internal.kernel.api.NodeLabelIndexCursor) Test(org.junit.jupiter.api.Test)

Example 93 with KernelTransaction

use of org.neo4j.kernel.api.KernelTransaction in project neo4j by neo4j.

the class ConstraintTestBase method shouldCheckUniquenessWhenAddingProperties.

@Test
void shouldCheckUniquenessWhenAddingProperties() throws Exception {
    // GIVEN
    long nodeConflicting, nodeNotConflicting;
    addConstraints("FOO", "prop");
    try (org.neo4j.graphdb.Transaction tx = graphDb.beginTx()) {
        Node conflict = tx.createNode();
        conflict.addLabel(Label.label("FOO"));
        nodeConflicting = conflict.getId();
        Node ok = tx.createNode();
        ok.addLabel(Label.label("BAR"));
        nodeNotConflicting = ok.getId();
        // Existing node
        Node existing = tx.createNode();
        existing.addLabel(Label.label("FOO"));
        existing.setProperty("prop", 1337);
        tx.commit();
    }
    int property;
    try (KernelTransaction tx = beginTransaction()) {
        property = tx.tokenWrite().propertyKeyGetOrCreateForName("prop");
        // This is ok, since it will satisfy constraint
        tx.dataWrite().nodeSetProperty(nodeNotConflicting, property, intValue(1337));
        try {
            tx.dataWrite().nodeSetProperty(nodeConflicting, property, intValue(1337));
            fail();
        } catch (ConstraintValidationException e) {
        // ignore
        }
        tx.commit();
    }
    // Verify
    try (KernelTransaction tx = beginTransaction();
        NodeCursor nodeCursor = tx.cursors().allocateNodeCursor(tx.cursorContext());
        PropertyCursor propertyCursor = tx.cursors().allocatePropertyCursor(tx.cursorContext(), tx.memoryTracker())) {
        // Node without conflict
        tx.dataRead().singleNode(nodeNotConflicting, nodeCursor);
        assertTrue(nodeCursor.next());
        nodeCursor.properties(propertyCursor);
        assertTrue(hasKey(propertyCursor, property));
        // Node with conflict
        tx.dataRead().singleNode(nodeConflicting, nodeCursor);
        assertTrue(nodeCursor.next());
        nodeCursor.properties(propertyCursor);
        assertFalse(hasKey(propertyCursor, property));
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Node(org.neo4j.graphdb.Node) ConstraintValidationException(org.neo4j.internal.kernel.api.exceptions.schema.ConstraintValidationException) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) PropertyCursor(org.neo4j.internal.kernel.api.PropertyCursor) Test(org.junit.jupiter.api.Test)

Example 94 with KernelTransaction

use of org.neo4j.kernel.api.KernelTransaction in project neo4j by neo4j.

the class ConstraintTestBase method shouldFindConstraintsByLabel.

@Test
void shouldFindConstraintsByLabel() throws Exception {
    // GIVEN
    addConstraints("FOO", "prop1", "FOO", "prop2");
    try (KernelTransaction tx = beginTransaction()) {
        int label = tx.tokenWrite().labelGetOrCreateForName("FOO");
        // WHEN
        List<ConstraintDescriptor> constraints = asList(tx.schemaRead().constraintsGetForLabel(label));
        // THEN
        assertThat(constraints).hasSize(2);
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) Test(org.junit.jupiter.api.Test)

Example 95 with KernelTransaction

use of org.neo4j.kernel.api.KernelTransaction in project neo4j by neo4j.

the class ConstraintTestBase method shouldBeAbleCheckExistenceOfConstraints.

@Test
void shouldBeAbleCheckExistenceOfConstraints() throws Exception {
    // GIVEN
    try (org.neo4j.graphdb.Transaction tx = graphDb.beginTx()) {
        tx.schema().constraintFor(label("FOO")).assertPropertyIsUnique("prop1").create();
        ConstraintDefinition dropped = tx.schema().constraintFor(label("FOO")).assertPropertyIsUnique("prop2").create();
        dropped.drop();
        tx.commit();
    }
    try (KernelTransaction tx = beginTransaction()) {
        int label = tx.tokenWrite().labelGetOrCreateForName("FOO");
        int prop1 = tx.tokenWrite().propertyKeyGetOrCreateForName("prop1");
        int prop2 = tx.tokenWrite().propertyKeyGetOrCreateForName("prop2");
        // THEN
        assertTrue(tx.schemaRead().constraintExists(uniqueConstraintDescriptor(label, prop1)));
        assertFalse(tx.schemaRead().constraintExists(uniqueConstraintDescriptor(label, prop2)));
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) Test(org.junit.jupiter.api.Test)

Aggregations

KernelTransaction (org.neo4j.kernel.api.KernelTransaction)581 Test (org.junit.jupiter.api.Test)349 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)74 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)66 Transaction (org.neo4j.graphdb.Transaction)64 NodeCursor (org.neo4j.internal.kernel.api.NodeCursor)62 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)57 Write (org.neo4j.internal.kernel.api.Write)51 IndexReadSession (org.neo4j.internal.kernel.api.IndexReadSession)47 TokenRead (org.neo4j.internal.kernel.api.TokenRead)42 ArrayList (java.util.ArrayList)37 SchemaRead (org.neo4j.internal.kernel.api.SchemaRead)35 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)34 PropertyCursor (org.neo4j.internal.kernel.api.PropertyCursor)33 Node (org.neo4j.graphdb.Node)31 LabelSchemaDescriptor (org.neo4j.internal.schema.LabelSchemaDescriptor)30 MethodSource (org.junit.jupiter.params.provider.MethodSource)28 Read (org.neo4j.internal.kernel.api.Read)28 RelationshipScanCursor (org.neo4j.internal.kernel.api.RelationshipScanCursor)28 NodeValueIndexCursor (org.neo4j.internal.kernel.api.NodeValueIndexCursor)25