Search in sources :

Example 1 with ConstraintType

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

the class ConstraintsProcedureUtil method prettyPrint.

static String prettyPrint(ConstraintDescriptor constraintDescriptor, TokenNameLookup tokenNameLookup) {
    SchemaDescriptor schema = constraintDescriptor.schema();
    int[] entityTokenIds = schema.getEntityTokenIds();
    if (entityTokenIds.length != 1) {
        throw new IllegalArgumentException("Cannot pretty-print multi-token constraints: " + constraintDescriptor.userDescription(tokenNameLookup));
    }
    String entityTypeName = schema.entityType() == EntityType.NODE ? tokenNameLookup.labelGetName(entityTokenIds[0]) : tokenNameLookup.relationshipTypeGetName(entityTokenIds[0]);
    entityTypeName = escapeLabelOrRelTyp(entityTypeName);
    String entityName = entityTypeName.toLowerCase();
    String properties = formatProperties(schema.getPropertyIds(), tokenNameLookup, entityName);
    ConstraintType type = constraintDescriptor.type();
    switch(type) {
        case EXISTS:
            switch(schema.entityType()) {
                case NODE:
                    return "CONSTRAINT ON ( " + entityName + ":" + entityTypeName + " ) ASSERT " + properties + " IS NOT NULL";
                case RELATIONSHIP:
                    return "CONSTRAINT ON ()-[ " + entityName + ":" + entityTypeName + " ]-() ASSERT " + properties + " IS NOT NULL";
                default:
                    throw new IllegalStateException("Unknown schema entity type: " + schema.entityType() + ".");
            }
        case UNIQUE:
            return "CONSTRAINT ON ( " + entityName + ":" + entityTypeName + " ) ASSERT " + properties + " IS UNIQUE";
        case UNIQUE_EXISTS:
            return "CONSTRAINT ON ( " + entityName + ":" + entityTypeName + " ) ASSERT " + properties + " IS NODE KEY";
        default:
            throw new IllegalStateException("Unknown constraint type: " + type + ".");
    }
}
Also used : SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) ConstraintType(org.neo4j.internal.schema.ConstraintType)

Example 2 with ConstraintType

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

the class SchemaStore method schemaConstraintToMap.

private static void schemaConstraintToMap(ConstraintDescriptor rule, Map<String, Value> map) {
    // Rule
    putStringProperty(map, PROP_SCHEMA_RULE_TYPE, "CONSTRAINT");
    ConstraintType type = rule.type();
    switch(type) {
        case UNIQUE:
            putStringProperty(map, PROP_CONSTRAINT_RULE_TYPE, "UNIQUE");
            if (rule.asIndexBackedConstraint().hasOwnedIndexId()) {
                putLongProperty(map, PROP_OWNED_INDEX, rule.asIndexBackedConstraint().ownedIndexId());
            }
            break;
        case EXISTS:
            putStringProperty(map, PROP_CONSTRAINT_RULE_TYPE, "EXISTS");
            break;
        case UNIQUE_EXISTS:
            putStringProperty(map, PROP_CONSTRAINT_RULE_TYPE, "UNIQUE_EXISTS");
            if (rule.asIndexBackedConstraint().hasOwnedIndexId()) {
                putLongProperty(map, PROP_OWNED_INDEX, rule.asIndexBackedConstraint().ownedIndexId());
            }
            break;
        default:
            throw new UnsupportedOperationException("Unrecognized constraint type: " + type);
    }
}
Also used : ConstraintType(org.neo4j.internal.schema.ConstraintType)

Example 3 with ConstraintType

use of org.neo4j.internal.schema.ConstraintType 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)

Aggregations

ConstraintType (org.neo4j.internal.schema.ConstraintType)3 SchemaDescriptor (org.neo4j.internal.schema.SchemaDescriptor)2 String.format (java.lang.String.format)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Optional (java.util.Optional)1 ArrayUtils (org.apache.commons.lang3.ArrayUtils)1 LongSet (org.eclipse.collections.api.set.primitive.LongSet)1 NODE (org.neo4j.common.EntityType.NODE)1 RELATIONSHIP (org.neo4j.common.EntityType.RELATIONSHIP)1 Config (org.neo4j.configuration.Config)1 GraphDatabaseInternalSettings.additional_lock_verification (org.neo4j.configuration.GraphDatabaseInternalSettings.additional_lock_verification)1 GraphDatabaseSettings (org.neo4j.configuration.GraphDatabaseSettings)1 DbmsRuntimeRepository (org.neo4j.dbms.database.DbmsRuntimeRepository)1 KernelException (org.neo4j.exceptions.KernelException)1 UnspecifiedKernelException (org.neo4j.exceptions.UnspecifiedKernelException)1 ThrowingIntFunction (org.neo4j.function.ThrowingIntFunction)1 Iterators (org.neo4j.internal.helpers.collection.Iterators)1