Search in sources :

Example 41 with ConstraintDescriptor

use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.

the class SchemaRuleCommandTest method shouldSetLatestConstraintRule.

@Test
void shouldSetLatestConstraintRule() throws Exception {
    // Given
    SchemaRecord before = new SchemaRecord(id).initialize(true, 42);
    before.setCreated();
    SchemaRecord after = new SchemaRecord(id).initialize(true, 42);
    after.setConstraint(true);
    when(neoStores.getSchemaStore()).thenReturn(schemaStore);
    when(neoStores.getMetaDataStore()).thenReturn(metaDataStore);
    ConstraintDescriptor schemaRule = ConstraintDescriptorFactory.uniqueForLabel(labelId, propertyKey).withId(id).withOwnedIndexId(0);
    // WHEN
    visitSchemaRuleCommand(storeApplier, new SchemaRuleCommand(serialization, before, after, schemaRule));
    // THEN
    verify(schemaStore).updateRecord(eq(after), any(), any());
    verify(metaDataStore).setLatestConstraintIntroducingTx(txId, NULL);
}
Also used : SchemaRuleCommand(org.neo4j.internal.recordstorage.Command.SchemaRuleCommand) SchemaRecord(org.neo4j.kernel.impl.store.record.SchemaRecord) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 42 with ConstraintDescriptor

use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.

the class IndexIT method shouldDisallowDroppingConstraintByNameThatDoesNotExist.

@Test
void shouldDisallowDroppingConstraintByNameThatDoesNotExist() throws KernelException {
    // given
    String constraintName = "my constraint";
    ConstraintDescriptor constraint;
    {
        SchemaWrite statement = schemaWriteInNewTransaction();
        constraint = statement.uniquePropertyConstraintCreate(uniqueForSchema(schema).withName("constraint name"));
        commit();
    }
    {
        SchemaWrite statement = schemaWriteInNewTransaction();
        statement.constraintDrop(constraint);
        commit();
    }
    // when
    SchemaWrite statement = schemaWriteInNewTransaction();
    SchemaKernelException e = assertThrows(SchemaKernelException.class, () -> statement.constraintDrop(constraintName));
    assertEquals("Unable to drop constraint `my constraint`: No such constraint my constraint.", e.getMessage());
    rollback();
}
Also used : SchemaWrite(org.neo4j.internal.kernel.api.SchemaWrite) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) IndexBackedConstraintDescriptor(org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor) SchemaKernelException(org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException) Test(org.junit.jupiter.api.Test) KernelIntegrationTest(org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest)

Example 43 with ConstraintDescriptor

use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.

the class SchemaChecker method performSchemaCheck.

private void performSchemaCheck(long highId, RecordReader<SchemaRecord> reader, Map<Long, SchemaRecord> indexObligations, Map<Long, SchemaRecord> constraintObligations, SchemaStorage schemaStorage, MutableIntObjectMap<MutableIntSet> mandatoryNodeProperties, MutableIntObjectMap<MutableIntSet> mandatoryRelationshipProperties, CursorContext cursorContext) {
    SchemaRecord record = reader.record();
    SchemaProcessor basicSchemaCheck = new BasicSchemaCheck(record, cursorContext);
    SchemaProcessor mandatoryPropertiesBuilder = new MandatoryPropertiesBuilder(mandatoryNodeProperties, mandatoryRelationshipProperties);
    for (long id = schemaStore.getNumberOfReservedLowIds(); id < highId && !context.isCancelled(); id++) {
        try {
            reader.read(id);
            if (record.inUse()) {
                SchemaRule schemaRule = schemaStorage.loadSingleSchemaRule(id, cursorContext);
                schemaRule.schema().processWith(basicSchemaCheck);
                if (schemaRule instanceof IndexDescriptor) {
                    IndexDescriptor rule = (IndexDescriptor) schemaRule;
                    if (rule.isUnique()) {
                        SchemaRecord obligation = indexObligations.get(rule.getId());
                        if (// no pointer to here
                        obligation == null) {
                            if (// we only expect a pointer if we have an owner
                            rule.getOwningConstraintId().isPresent()) {
                                reporter.forSchema(record).missingObligation(SchemaRuleKind.UNIQUENESS_CONSTRAINT.name());
                            }
                        } else {
                            // if someone points to here, it must be our owner
                            OptionalLong owningConstraintId = rule.getOwningConstraintId();
                            if (owningConstraintId.isEmpty() || obligation.getId() != owningConstraintId.getAsLong()) {
                                reporter.forSchema(record).constraintIndexRuleNotReferencingBack(obligation);
                            }
                        }
                    }
                    if (indexAccessors.notOnlineRules().contains(rule)) {
                        reporter.forSchema(record).schemaRuleNotOnline(rule);
                    }
                    if (indexAccessors.inconsistentRules().contains(rule)) {
                        reporter.forSchema(record).malformedSchemaRule();
                    }
                } else if (schemaRule instanceof ConstraintDescriptor) {
                    ConstraintDescriptor rule = (ConstraintDescriptor) schemaRule;
                    if (rule.enforcesUniqueness()) {
                        SchemaRecord obligation = constraintObligations.get(rule.getId());
                        if (obligation == null) {
                            reporter.forSchema(record).missingObligation(SchemaRuleKind.CONSTRAINT_INDEX_RULE.name());
                        } else {
                            if (obligation.getId() != rule.asIndexBackedConstraint().ownedIndexId()) {
                                reporter.forSchema(record).uniquenessConstraintNotReferencingBack(obligation);
                            }
                        }
                    }
                    if (rule.enforcesPropertyExistence()) {
                        rule.schema().processWith(mandatoryPropertiesBuilder);
                    }
                } else {
                    reporter.forSchema(record).unsupportedSchemaRuleType(null);
                }
            }
        } catch (MalformedSchemaRuleException e) {
            reporter.forSchema(record).malformedSchemaRule();
        }
    }
}
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) OptionalLong(java.util.OptionalLong) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) SchemaProcessor(org.neo4j.internal.schema.SchemaProcessor)

Example 44 with ConstraintDescriptor

use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.

the class Operations method constraintDrop.

@Override
public void constraintDrop(SchemaDescriptor schema, ConstraintType type) throws SchemaKernelException {
    ktx.assertOpen();
    Iterator<ConstraintDescriptor> constraints = ktx.schemaRead().constraintsGetForSchema(schema);
    constraints = Iterators.filter(constraint -> constraint.type() == type, constraints);
    if (constraints.hasNext()) {
        ConstraintDescriptor constraint = constraints.next();
        if (!constraints.hasNext()) {
            constraintDrop(constraint);
        } else {
            String schemaDescription = schema.userDescription(token);
            String constraintDescription = constraints.next().userDescription(token);
            throw new DropConstraintFailureException(constraint, new IllegalArgumentException("More than one " + type + " constraint was found with the '" + schemaDescription + "' schema: " + constraintDescription + ", please drop constraint by name instead."));
        }
    } else {
        throw new DropConstraintFailureException(schema, new NoSuchConstraintException(schema, token));
    }
}
Also used : StatementConstants(org.neo4j.kernel.api.StatementConstants) Arrays(java.util.Arrays) CreateConstraintFailureException(org.neo4j.internal.kernel.api.exceptions.schema.CreateConstraintFailureException) ThrowingIntFunction(org.neo4j.function.ThrowingIntFunction) CursorContext(org.neo4j.io.pagecache.context.CursorContext) Status(org.neo4j.kernel.api.exceptions.Status) IndexType(org.neo4j.internal.schema.IndexType) Config(org.neo4j.configuration.Config) Value(org.neo4j.values.storable.Value) IndexingProvidersService(org.neo4j.kernel.impl.api.index.IndexingProvidersService) NO_SUCH_LABEL(org.neo4j.kernel.api.StatementConstants.NO_SUCH_LABEL) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) DEGREES(org.neo4j.lock.ResourceTypes.DEGREES) GraphDatabaseInternalSettings.additional_lock_verification(org.neo4j.configuration.GraphDatabaseInternalSettings.additional_lock_verification) IndexWithNameAlreadyExistsException(org.neo4j.kernel.api.exceptions.schema.IndexWithNameAlreadyExistsException) ConstraintType(org.neo4j.internal.schema.ConstraintType) ConstraintWithNameAlreadyExistsException(org.neo4j.kernel.api.exceptions.schema.ConstraintWithNameAlreadyExistsException) NoSuchConstraintException(org.neo4j.kernel.api.exceptions.schema.NoSuchConstraintException) EquivalentSchemaRuleAlreadyExistsException(org.neo4j.kernel.api.exceptions.schema.EquivalentSchemaRuleAlreadyExistsException) AlreadyIndexedException(org.neo4j.kernel.api.exceptions.schema.AlreadyIndexedException) IndexQueryConstraints.unconstrained(org.neo4j.internal.kernel.api.IndexQueryConstraints.unconstrained) IndexPrototype(org.neo4j.internal.schema.IndexPrototype) Locks(org.neo4j.internal.kernel.api.Locks) PropertyIndexQuery(org.neo4j.internal.kernel.api.PropertyIndexQuery) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) RepeatedSchemaComponentException(org.neo4j.kernel.api.exceptions.schema.RepeatedSchemaComponentException) ConstraintSemantics(org.neo4j.kernel.impl.constraints.ConstraintSemantics) KernelVersion(org.neo4j.kernel.KernelVersion) ResourceType(org.neo4j.lock.ResourceType) DropConstraintFailureException(org.neo4j.kernel.api.exceptions.schema.DropConstraintFailureException) KernelVersionRepository(org.neo4j.storageengine.api.KernelVersionRepository) TokenPredicate(org.neo4j.internal.kernel.api.TokenPredicate) StorageReader(org.neo4j.storageengine.api.StorageReader) MemoryTracker(org.neo4j.memory.MemoryTracker) Read(org.neo4j.internal.kernel.api.Read) Iterators(org.neo4j.internal.helpers.collection.Iterators) NO_SUCH_PROPERTY_KEY(org.neo4j.kernel.api.StatementConstants.NO_SUCH_PROPERTY_KEY) VALIDATION(org.neo4j.internal.kernel.api.exceptions.schema.ConstraintValidationException.Phase.VALIDATION) RepeatedRelationshipTypeInSchemaException(org.neo4j.kernel.api.exceptions.schema.RepeatedRelationshipTypeInSchemaException) INDEX_CREATION(org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException.OperationContext.INDEX_CREATION) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) RelationTypeSchemaDescriptor(org.neo4j.internal.schema.RelationTypeSchemaDescriptor) KernelException(org.neo4j.exceptions.KernelException) EntityNotFoundException(org.neo4j.internal.kernel.api.exceptions.EntityNotFoundException) AlreadyConstrainedException(org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException) LongSet(org.eclipse.collections.api.set.primitive.LongSet) UnspecifiedKernelException(org.neo4j.exceptions.UnspecifiedKernelException) TransactionFailureException(org.neo4j.internal.kernel.api.exceptions.TransactionFailureException) DbmsRuntimeRepository(org.neo4j.dbms.database.DbmsRuntimeRepository) SchemaRule(org.neo4j.internal.schema.SchemaRule) UnableToValidateConstraintException(org.neo4j.kernel.api.exceptions.schema.UnableToValidateConstraintException) ResourceTypes(org.neo4j.lock.ResourceTypes) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) ADDED_LABEL(org.neo4j.kernel.impl.newapi.IndexTxStateUpdater.LabelChangeType.ADDED_LABEL) ConstraintValidationException(org.neo4j.internal.kernel.api.exceptions.schema.ConstraintValidationException) SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) RelationshipSelections(org.neo4j.internal.kernel.api.helpers.RelationshipSelections) IndexProviderDescriptor(org.neo4j.internal.schema.IndexProviderDescriptor) IndexBrokenKernelException(org.neo4j.kernel.api.exceptions.schema.IndexBrokenKernelException) NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) NO_VALUE(org.neo4j.values.storable.Values.NO_VALUE) Procedures(org.neo4j.internal.kernel.api.Procedures) RepeatedPropertyInSchemaException(org.neo4j.kernel.api.exceptions.schema.RepeatedPropertyInSchemaException) Collection(java.util.Collection) CursorFactory(org.neo4j.internal.kernel.api.CursorFactory) String.format(java.lang.String.format) NO_SUCH_NODE(org.neo4j.kernel.api.StatementConstants.NO_SUCH_NODE) List(java.util.List) CommandCreationContext(org.neo4j.storageengine.api.CommandCreationContext) RepeatedLabelInSchemaException(org.neo4j.kernel.api.exceptions.schema.RepeatedLabelInSchemaException) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) IndexNotApplicableKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotApplicableKernelException) Optional(java.util.Optional) NODE(org.neo4j.common.EntityType.NODE) GraphDatabaseSettings(org.neo4j.configuration.GraphDatabaseSettings) InternalIndexState(org.neo4j.internal.kernel.api.InternalIndexState) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) ArrayUtils(org.apache.commons.lang3.ArrayUtils) DropIndexFailureException(org.neo4j.kernel.api.exceptions.schema.DropIndexFailureException) ConstraintDescriptorFactory(org.neo4j.internal.schema.constraints.ConstraintDescriptorFactory) SchemaKernelException(org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException) Values(org.neo4j.values.storable.Values) ConstraintIndexCreator(org.neo4j.kernel.impl.api.state.ConstraintIndexCreator) Write(org.neo4j.internal.kernel.api.Write) SchemaDescriptorImplementation(org.neo4j.internal.schema.SchemaDescriptorImplementation) PropertyKeyIdNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException) CONSTRAINT_CREATION(org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException.OperationContext.CONSTRAINT_CREATION) REMOVED_LABEL(org.neo4j.kernel.impl.newapi.IndexTxStateUpdater.LabelChangeType.REMOVED_LABEL) IndexConfig(org.neo4j.internal.schema.IndexConfig) KernelTransactionImplementation(org.neo4j.kernel.impl.api.KernelTransactionImplementation) Iterator(java.util.Iterator) TransactionState(org.neo4j.kernel.api.txstate.TransactionState) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) ResourceIds.indexEntryResourceId(org.neo4j.kernel.impl.locking.ResourceIds.indexEntryResourceId) ResourceIds(org.neo4j.kernel.impl.locking.ResourceIds) RELATIONSHIP(org.neo4j.common.EntityType.RELATIONSHIP) INDEX_ENTRY(org.neo4j.lock.ResourceTypes.INDEX_ENTRY) IndexBackedConstraintDescriptor(org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor) IndexBelongsToConstraintException(org.neo4j.kernel.api.exceptions.schema.IndexBelongsToConstraintException) SCHEMA_NAME(org.neo4j.lock.ResourceTypes.SCHEMA_NAME) SchemaWrite(org.neo4j.internal.kernel.api.SchemaWrite) UniquePropertyValueValidationException(org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException) Token(org.neo4j.internal.kernel.api.Token) NoSuchConstraintException(org.neo4j.kernel.api.exceptions.schema.NoSuchConstraintException) 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) DropConstraintFailureException(org.neo4j.kernel.api.exceptions.schema.DropConstraintFailureException)

Example 45 with ConstraintDescriptor

use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.

the class TransactionToRecordStateVisitor method visitAddedUniquenessConstraint.

private void visitAddedUniquenessConstraint(UniquenessConstraintDescriptor uniqueConstraint, long constraintId) throws KernelException {
    IndexDescriptor indexRule = (IndexDescriptor) schemaStorage.loadSingleSchemaRule(uniqueConstraint.ownedIndexId(), cursorContext);
    ConstraintDescriptor constraint = constraintSemantics.createUniquenessConstraintRule(constraintId, uniqueConstraint, indexRule.getId());
    schemaStateChanger.createSchemaRule(recordState, constraint);
    schemaStateChanger.setConstraintIndexOwner(recordState, indexRule, constraintId);
}
Also used : UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) 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