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);
}
}
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();
}
}
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;
}
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));
}
}
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();
}
Aggregations