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