Search in sources :

Example 91 with ConstraintDescriptor

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;
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString)

Example 92 with ConstraintDescriptor

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
        }
    }
}
Also used : MalformedSchemaRuleException(org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException) SchemaRecord(org.neo4j.kernel.impl.store.record.SchemaRecord) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) SchemaRule(org.neo4j.internal.schema.SchemaRule) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) SchemaRuleKey(org.neo4j.consistency.checking.SchemaRuleKey)

Example 93 with ConstraintDescriptor

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");
    }
}
Also used : ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) IndexBackedConstraintDescriptor(org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor) IndexWithNameAlreadyExistsException(org.neo4j.kernel.api.exceptions.schema.IndexWithNameAlreadyExistsException) ConstraintWithNameAlreadyExistsException(org.neo4j.kernel.api.exceptions.schema.ConstraintWithNameAlreadyExistsException) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor)

Example 94 with ConstraintDescriptor

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;
}
Also used : ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) IndexBackedConstraintDescriptor(org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor)

Example 95 with ConstraintDescriptor

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);
    }
}
Also used : AlreadyIndexedException(org.neo4j.kernel.api.exceptions.schema.AlreadyIndexedException) EquivalentSchemaRuleAlreadyExistsException(org.neo4j.kernel.api.exceptions.schema.EquivalentSchemaRuleAlreadyExistsException) AlreadyConstrainedException(org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) IndexBackedConstraintDescriptor(org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor)

Aggregations

ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)107 Test (org.junit.jupiter.api.Test)62 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)34 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)32 UniquenessConstraintDescriptor (org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor)26 SchemaRead (org.neo4j.internal.kernel.api.SchemaRead)21 NodeKeyConstraintDescriptor (org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor)20 IndexBackedConstraintDescriptor (org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor)19 SchemaReadCore (org.neo4j.internal.kernel.api.SchemaReadCore)16 TokenRead (org.neo4j.internal.kernel.api.TokenRead)9 ArrayList (java.util.ArrayList)8 RepeatedTest (org.junit.jupiter.api.RepeatedTest)6 SchemaDescriptor (org.neo4j.internal.schema.SchemaDescriptor)6 SchemaStore (org.neo4j.kernel.impl.store.SchemaStore)6 InternalIndexState (org.neo4j.internal.kernel.api.InternalIndexState)5 SchemaWrite (org.neo4j.internal.kernel.api.SchemaWrite)5 LabelSchemaDescriptor (org.neo4j.internal.schema.LabelSchemaDescriptor)5 SchemaRule (org.neo4j.internal.schema.SchemaRule)5 SchemaRecord (org.neo4j.kernel.impl.store.record.SchemaRecord)5 OptionalLong (java.util.OptionalLong)4