use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.
the class CheckerTestBase method uniqueIndex.
long uniqueIndex(SchemaDescriptor descriptor) throws KernelException {
long indexId;
String constraintName = "me";
try (KernelTransaction tx = ktx()) {
tx.schemaWrite().uniquePropertyConstraintCreate(IndexPrototype.uniqueForSchema(descriptor).withName(constraintName));
tx.commit();
}
try (KernelTransaction tx = ktx()) {
ConstraintDescriptor constraint = tx.schemaRead().constraintGetForName(constraintName);
indexId = constraint.asUniquenessConstraint().ownedIndexId();
}
awaitIndexesOnline();
return indexId;
}
use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.
the class SchemaChecker method buildObligationsMap.
private void buildObligationsMap(long highId, RecordReader<SchemaRecord> reader, SchemaStorage schemaStorage, Map<Long, SchemaRecord> indexObligations, Map<Long, SchemaRecord> constraintObligations, Map<SchemaRuleKey, SchemaRecord> verifiedRulesWithRecords, CursorContext cursorContext) {
for (long id = schemaStore.getNumberOfReservedLowIds(); id < highId && !context.isCancelled(); id++) {
try {
SchemaRecord record = reader.read(id);
if (!record.inUse()) {
continue;
}
SchemaRule schemaRule = schemaStorage.loadSingleSchemaRule(id, cursorContext);
SchemaRecord previousContentRecord = verifiedRulesWithRecords.put(new SchemaRuleKey(schemaRule), record.copy());
if (previousContentRecord != null) {
reporter.forSchema(record).duplicateRuleContent(previousContentRecord);
}
if (schemaRule instanceof IndexDescriptor) {
IndexDescriptor rule = (IndexDescriptor) schemaRule;
if (rule.isUnique() && rule.getOwningConstraintId().isPresent()) {
SchemaRecord previousObligation = constraintObligations.put(rule.getOwningConstraintId().getAsLong(), record.copy());
if (previousObligation != null) {
reporter.forSchema(record).duplicateObligation(previousObligation);
}
}
} else if (schemaRule instanceof ConstraintDescriptor) {
ConstraintDescriptor rule = (ConstraintDescriptor) schemaRule;
if (rule.enforcesUniqueness()) {
SchemaRecord previousObligation = indexObligations.put(rule.asIndexBackedConstraint().ownedIndexId(), record.copy());
if (previousObligation != null) {
reporter.forSchema(record).duplicateObligation(previousObligation);
}
}
}
} catch (MalformedSchemaRuleException e) {
// This is OK, we'll report it below
}
}
}
use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.
the class Operations method assertSchemaRuleWithNameDoesNotExist.
private void assertSchemaRuleWithNameDoesNotExist(String name) throws IndexWithNameAlreadyExistsException, ConstraintWithNameAlreadyExistsException {
// Check constraints first because some of them will also be backed by indexes
final ConstraintDescriptor constraintWithSameName = allStoreHolder.constraintGetForName(name);
if (constraintWithSameName != null) {
throw new ConstraintWithNameAlreadyExistsException(name);
}
final IndexDescriptor indexWithSameName = allStoreHolder.indexGetForName(name);
if (indexWithSameName != IndexDescriptor.NO_INDEX) {
throw new IndexWithNameAlreadyExistsException(name);
}
if (name.equals(IndexDescriptor.NLI_GENERATED_NAME)) {
throw new IllegalArgumentException("The name '" + IndexDescriptor.NLI_GENERATED_NAME + "' is a reserved name and can't be used when creating indexes");
}
}
use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.
the class Operations method relationshipPropertyExistenceConstraintCreate.
@Override
public ConstraintDescriptor relationshipPropertyExistenceConstraintCreate(RelationTypeSchemaDescriptor schema, String name) throws KernelException {
ConstraintDescriptor constraint = lockAndValidatePropertyExistenceConstraint(schema, name);
// enforce constraints
enforceRelationshipPropertyExistenceConstraint(schema);
// Create
ktx.txState().constraintDoAdd(constraint);
return constraint;
}
use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.
the class Operations method assertNoBlockingSchemaRulesExists.
private void assertNoBlockingSchemaRulesExists(IndexPrototype prototype) throws EquivalentSchemaRuleAlreadyExistsException, IndexWithNameAlreadyExistsException, ConstraintWithNameAlreadyExistsException, AlreadyIndexedException, AlreadyConstrainedException {
Optional<String> prototypeName = prototype.getName();
if (prototypeName.isEmpty()) {
throw new IllegalStateException("Expected index to always have a name by this point");
}
String name = prototypeName.get();
// Equivalent index
IndexDescriptor indexWithSameSchema = IndexDescriptor.NO_INDEX;
Iterator<IndexDescriptor> indexesWithSameSchema = allStoreHolder.index(prototype.schema());
while (indexesWithSameSchema.hasNext()) {
indexWithSameSchema = indexesWithSameSchema.next();
if (indexWithSameSchema.getName().equals(name) && indexWithSameSchema.isUnique() == prototype.isUnique()) {
throw new EquivalentSchemaRuleAlreadyExistsException(indexWithSameSchema, INDEX_CREATION, token);
}
}
// Name conflict with other schema rule
assertSchemaRuleWithNameDoesNotExist(name);
// Already constrained
final Iterator<ConstraintDescriptor> constraintWithSameSchema = allStoreHolder.constraintsGetForSchema(prototype.schema());
while (constraintWithSameSchema.hasNext()) {
final ConstraintDescriptor constraint = constraintWithSameSchema.next();
if (constraint.type() != ConstraintType.EXISTS) {
throw new AlreadyConstrainedException(constraint, INDEX_CREATION, token);
}
}
// Already indexed
if (indexWithSameSchema != IndexDescriptor.NO_INDEX) {
throw new AlreadyIndexedException(prototype.schema(), INDEX_CREATION, token);
}
}
Aggregations