Search in sources :

Example 86 with KernelTransaction

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

the class MyCoreAPI method makeNode.

public long makeNode(Transaction tx, String label) throws ProcedureException {
    try {
        KernelTransaction ktx = ((InternalTransaction) tx).kernelTransaction();
        long nodeId = ktx.dataWrite().nodeCreate();
        int labelId = ktx.tokenWrite().labelGetOrCreateForName(label);
        ktx.dataWrite().nodeAddLabel(nodeId, labelId);
        return nodeId;
    } catch (Exception e) {
        log.error("Failed to create node: " + e.getMessage());
        throw new ProcedureException(Status.Procedure.ProcedureCallFailed, "Failed to create node: " + e.getMessage(), e);
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException)

Example 87 with KernelTransaction

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

the class MultiIndexPopulationConcurrentUpdatesIT method launchCustomIndexPopulation.

private void launchCustomIndexPopulation(GraphDatabaseSettings.SchemaIndex schemaIndex, Map<String, Integer> labelNameIdMap, int propertyId, Runnable customAction) throws Throwable {
    RecordStorageEngine storageEngine = getStorageEngine();
    try (Transaction transaction = db.beginTx()) {
        Config config = Config.defaults();
        KernelTransaction ktx = ((InternalTransaction) transaction).kernelTransaction();
        JobScheduler scheduler = getJobScheduler();
        NullLogProvider nullLogProvider = NullLogProvider.getInstance();
        IndexStoreViewFactory indexStoreViewFactory = mock(IndexStoreViewFactory.class);
        when(indexStoreViewFactory.createTokenIndexStoreView(any())).thenAnswer(invocation -> dynamicIndexStoreViewWrapper(customAction, storageEngine::newReader, invocation.getArgument(0), config, scheduler));
        IndexProviderMap providerMap = getIndexProviderMap();
        indexService = IndexingServiceFactory.createIndexingService(config, scheduler, providerMap, indexStoreViewFactory, ktx.tokenRead(), initialSchemaRulesLoader(storageEngine), nullLogProvider, nullLogProvider, IndexingService.NO_MONITOR, getSchemaState(), mock(IndexStatisticsStore.class), PageCacheTracer.NULL, INSTANCE, "", writable());
        indexService.start();
        rules = createIndexRules(schemaIndex, labelNameIdMap, propertyId);
        schemaCache = new SchemaCache(new StandardConstraintSemantics(), providerMap);
        schemaCache.load(iterable(rules));
        indexService.createIndexes(AUTH_DISABLED, rules);
        transaction.commit();
    }
}
Also used : JobScheduler(org.neo4j.scheduler.JobScheduler) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) RecordStorageEngine(org.neo4j.internal.recordstorage.RecordStorageEngine) Config(org.neo4j.configuration.Config) NullLogProvider(org.neo4j.logging.NullLogProvider) SchemaCache(org.neo4j.internal.recordstorage.SchemaCache) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) IndexProviderMap(org.neo4j.kernel.impl.api.index.IndexProviderMap) StandardConstraintSemantics(org.neo4j.kernel.impl.constraints.StandardConstraintSemantics) IndexStoreViewFactory(org.neo4j.kernel.impl.transaction.state.storeview.IndexStoreViewFactory)

Example 88 with KernelTransaction

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

the class MultiIndexPopulationConcurrentUpdatesIT method getLabelIdsByName.

private Map<String, Integer> getLabelIdsByName(Transaction tx, String... names) {
    Map<String, Integer> labelNameIdMap = new HashMap<>();
    KernelTransaction ktx = ((InternalTransaction) tx).kernelTransaction();
    TokenRead tokenRead = ktx.tokenRead();
    for (String name : names) {
        labelNameIdMap.put(name, tokenRead.nodeLabel(name));
    }
    return labelNameIdMap;
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) HashMap(java.util.HashMap) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) TokenRead(org.neo4j.internal.kernel.api.TokenRead)

Example 89 with KernelTransaction

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

the class ConstraintTestBase method shouldCheckUniquenessWhenAddingLabel.

@Test
void shouldCheckUniquenessWhenAddingLabel() throws Exception {
    // GIVEN
    long nodeConflicting, nodeNotConflicting;
    addConstraints("FOO", "prop");
    try (org.neo4j.graphdb.Transaction tx = graphDb.beginTx()) {
        Node conflict = tx.createNode();
        conflict.setProperty("prop", 1337);
        nodeConflicting = conflict.getId();
        Node ok = tx.createNode();
        ok.setProperty("prop", 42);
        nodeNotConflicting = ok.getId();
        // Existing node
        Node existing = tx.createNode();
        existing.addLabel(Label.label("FOO"));
        existing.setProperty("prop", 1337);
        tx.commit();
    }
    int label;
    try (KernelTransaction tx = beginTransaction()) {
        label = tx.tokenWrite().labelGetOrCreateForName("FOO");
        // This is ok, since it will satisfy constraint
        assertTrue(tx.dataWrite().nodeAddLabel(nodeNotConflicting, label));
        try {
            tx.dataWrite().nodeAddLabel(nodeConflicting, label);
            fail();
        } catch (ConstraintValidationException e) {
        // ignore
        }
        tx.commit();
    }
    // Verify
    try (KernelTransaction tx = beginTransaction();
        NodeCursor nodeCursor = tx.cursors().allocateNodeCursor(tx.cursorContext())) {
        // Node without conflict
        tx.dataRead().singleNode(nodeNotConflicting, nodeCursor);
        assertTrue(nodeCursor.next());
        assertTrue(nodeCursor.labels().contains(label));
        // Node with conflict
        tx.dataRead().singleNode(nodeConflicting, nodeCursor);
        assertTrue(nodeCursor.next());
        assertFalse(nodeCursor.labels().contains(label));
    }
}
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) Test(org.junit.jupiter.api.Test)

Example 90 with KernelTransaction

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

the class RebuildCountsTest method shouldRebuildMissingCountsStoreOnStart.

@Test
void shouldRebuildMissingCountsStoreOnStart() throws IOException, TransactionFailureException {
    // given
    createAliensAndHumans();
    // when
    FileSystemAbstraction fs = shutdown();
    deleteCounts(fs);
    restart(fs);
    // then
    Kernel kernel = ((GraphDatabaseAPI) db).getDependencyResolver().resolveDependency(Kernel.class);
    try (KernelTransaction tx = kernel.beginTransaction(EXPLICIT, AUTH_DISABLED)) {
        assertEquals(ALIENS + HUMANS, tx.dataRead().countsForNode(-1));
        assertEquals(ALIENS, tx.dataRead().countsForNode(labelId(ALIEN)));
        assertEquals(HUMANS, 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)

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