use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.
the class FullCheckIntegrationTest method createNodeKeyConstraintRule.
private void createNodeKeyConstraintRule(final int labelId, final int... propertyKeyIds) throws KernelException {
SchemaStore schemaStore = fixture.directStoreAccess().nativeStores().getSchemaStore();
long ruleId1 = schemaStore.nextId(NULL);
long ruleId2 = schemaStore.nextId(NULL);
String name = "constraint_" + ruleId2;
IndexDescriptor indexRule = uniqueForSchema(forLabel(labelId, propertyKeyIds), DESCRIPTOR).withName(name).materialise(ruleId1).withOwningConstraintId(ruleId2);
ConstraintDescriptor nodeKeyRule = ConstraintDescriptorFactory.nodeKeyForLabel(labelId, propertyKeyIds).withId(ruleId2).withName(name).withOwnedIndexId(ruleId1);
writeToSchemaStore(schemaStore, indexRule);
writeToSchemaStore(schemaStore, nodeKeyRule);
}
use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.
the class FullCheckIntegrationTest method createNodePropertyExistenceConstraint.
private void createNodePropertyExistenceConstraint(int labelId, int propertyKeyId) throws KernelException {
SchemaStore schemaStore = fixture.directStoreAccess().nativeStores().getSchemaStore();
long ruleId = schemaStore.nextId(NULL);
ConstraintDescriptor rule = nodePropertyExistenceConstraintRule(ruleId, labelId, propertyKeyId).withName("constraint_" + ruleId);
writeToSchemaStore(schemaStore, rule);
}
use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.
the class BuiltInProceduresIT method listAllIndexesMustNotBlockOnConstraintCreatingTransaction.
@Test
@Timeout(value = 6, unit = MINUTES)
void listAllIndexesMustNotBlockOnConstraintCreatingTransaction() throws Throwable {
// Given
KernelTransaction transaction = newTransaction(AUTH_DISABLED);
int labelId1 = transaction.tokenWrite().labelGetOrCreateForName("Person");
int labelId2 = transaction.tokenWrite().labelGetOrCreateForName("Age");
int propertyKeyId1 = transaction.tokenWrite().propertyKeyGetOrCreateForName("foo");
int propertyKeyId2 = transaction.tokenWrite().propertyKeyGetOrCreateForName("bar");
int propertyKeyId3 = transaction.tokenWrite().propertyKeyGetOrCreateForName("baz");
LabelSchemaDescriptor personFooDescriptor = forLabel(labelId1, propertyKeyId1);
LabelSchemaDescriptor ageFooDescriptor = forLabel(labelId2, propertyKeyId1);
LabelSchemaDescriptor personFooBarDescriptor = forLabel(labelId1, propertyKeyId1, propertyKeyId2);
LabelSchemaDescriptor personBazDescriptor = forLabel(labelId1, propertyKeyId3);
transaction.schemaWrite().indexCreate(personFooDescriptor, "person foo index");
transaction.schemaWrite().uniquePropertyConstraintCreate(IndexPrototype.uniqueForSchema(ageFooDescriptor).withName("age foo constraint"));
transaction.schemaWrite().indexCreate(personFooBarDescriptor, "person foo bar index");
commit();
// let indexes come online
try (org.neo4j.graphdb.Transaction tx = db.beginTx()) {
tx.schema().awaitIndexesOnline(2, MINUTES);
tx.commit();
}
CountDownLatch constraintLatch = new CountDownLatch(1);
CountDownLatch commitLatch = new CountDownLatch(1);
AtomicLong personBazIndexId = new AtomicLong();
FutureTask<Void> createConstraintTask = new FutureTask<>(() -> {
SchemaWrite schemaWrite = schemaWriteInNewTransaction();
try (Resource ignore = captureTransaction()) {
ConstraintDescriptor personBazConstraint = schemaWrite.uniquePropertyConstraintCreate(IndexPrototype.uniqueForSchema(personBazDescriptor).withName("person baz constraint"));
personBazIndexId.set(personBazConstraint.asIndexBackedConstraint().ownedIndexId());
// We now hold a schema lock on the "MyLabel" label. Let the procedure calling transaction have a go.
constraintLatch.countDown();
commitLatch.await();
}
rollback();
return null;
});
Thread constraintCreator = new Thread(createConstraintTask);
constraintCreator.start();
// When
constraintLatch.await();
transaction = newTransaction();
final SchemaReadCore schemaRead = transaction.schemaRead().snapshot();
IndexDescriptor personFooIndex = single(schemaRead.index(personFooDescriptor));
IndexDescriptor ageFooIndex = single(schemaRead.index(ageFooDescriptor));
IndexDescriptor personFooBarIndex = single(schemaRead.index(personFooBarDescriptor));
IndexDescriptor personBazIndex = single(schemaRead.index(personBazDescriptor));
commit();
RawIterator<AnyValue[], ProcedureException> stream = procs().procedureCallRead(procs().procedureGet(procedureName("db", "indexes")).id(), new AnyValue[0], ProcedureCallContext.EMPTY);
Set<Object[]> result = new HashSet<>();
while (stream.hasNext()) {
result.add(stream.next());
}
// Then
try {
assertThat(result).contains(dbIndexesResult(ageFooIndex.getId(), ageFooIndex.getName(), "ONLINE", 100D, "UNIQUE", "BTREE", "NODE", singletonList("Age"), singletonList("foo"), ageFooIndex.getIndexProvider().name()), dbIndexesResult(personFooIndex.getId(), personFooIndex.getName(), "ONLINE", 100D, "NONUNIQUE", "BTREE", "NODE", singletonList("Person"), singletonList("foo"), personFooIndex.getIndexProvider().name()), dbIndexesResult(personFooBarIndex.getId(), personFooBarIndex.getName(), "ONLINE", 100D, "NONUNIQUE", "BTREE", "NODE", singletonList("Person"), Arrays.asList("foo", "bar"), personFooBarIndex.getIndexProvider().name()), dbIndexesResult(personBazIndex.getId(), personBazIndex.getName(), /*???*/
"POPULATING", 100D, "UNIQUE", "BTREE", "NODE", singletonList("Person"), singletonList("baz"), personBazIndex.getIndexProvider().name()));
commit();
} finally {
commitLatch.countDown();
}
createConstraintTask.get();
constraintCreator.join();
}
use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.
the class UniquenessConstraintValidationIT method addingUniqueNodeWithUnrelatedValueShouldNotAffectLookup.
@Test
void addingUniqueNodeWithUnrelatedValueShouldNotAffectLookup() throws Exception {
// given
ConstraintDescriptor constraint = createConstraint("Person", "id");
long ourNode;
{
KernelTransaction transaction = newTransaction(AnonymousContext.writeToken());
ourNode = createLabeledNode(transaction, "Person", "id", 1);
commit();
}
KernelTransaction transaction = newTransaction(AnonymousContext.writeToken());
TokenRead tokenRead = transaction.tokenRead();
int propId = tokenRead.propertyKey("id");
IndexDescriptor idx = transaction.schemaRead().indexGetForName(constraint.getName());
// when
createLabeledNode(transaction, "Person", "id", 2);
// then I should find the original node
try (NodeValueIndexCursor cursor = transaction.cursors().allocateNodeValueIndexCursor(transaction.cursorContext(), transaction.memoryTracker())) {
assertThat(transaction.dataRead().lockingNodeUniqueIndexSeek(idx, cursor, exact(propId, Values.of(1)))).isEqualTo(ourNode);
}
commit();
}
use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.
the class UniquenessConstraintValidationIT method unrelatedNodesWithSamePropertyShouldNotInterfereWithUniquenessCheck.
@Test
void unrelatedNodesWithSamePropertyShouldNotInterfereWithUniquenessCheck() throws Exception {
// given
ConstraintDescriptor constraint = createConstraint("Person", "id");
long ourNode;
{
KernelTransaction transaction = newTransaction(AnonymousContext.writeToken());
ourNode = createLabeledNode(transaction, "Person", "id", 1);
createLabeledNode(transaction, "Item", "id", 2);
commit();
}
KernelTransaction transaction = newTransaction(AnonymousContext.writeToken());
TokenRead tokenRead = transaction.tokenRead();
int propId = tokenRead.propertyKey("id");
IndexDescriptor idx = transaction.schemaRead().indexGetForName(constraint.getName());
// when
createLabeledNode(transaction, "Item", "id", 2);
// then I should find the original node
try (NodeValueIndexCursor cursor = transaction.cursors().allocateNodeValueIndexCursor(transaction.cursorContext(), transaction.memoryTracker())) {
assertThat(transaction.dataRead().lockingNodeUniqueIndexSeek(idx, cursor, exact(propId, Values.of(1)))).isEqualTo(ourNode);
}
commit();
}
Aggregations