Search in sources :

Example 6 with SchemaDescriptor

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

the class Operations method ensureIndexPrototypeHasName.

private IndexPrototype ensureIndexPrototypeHasName(IndexPrototype prototype) throws KernelException {
    if (prototype.getName().isEmpty()) {
        SchemaDescriptor schema = prototype.schema();
        int[] entityTokenIds = schema.getEntityTokenIds();
        String[] entityTokenNames;
        switch(schema.entityType()) {
            case NODE:
                entityTokenNames = resolveTokenNames(token::nodeLabelName, entityTokenIds);
                break;
            case RELATIONSHIP:
                entityTokenNames = resolveTokenNames(token::relationshipTypeName, entityTokenIds);
                break;
            default:
                throw new UnspecifiedKernelException(Status.General.UnknownError, "Cannot create index for entity type %s in the schema %s.", schema.entityType(), schema);
        }
        int[] propertyIds = schema.getPropertyIds();
        String[] propertyNames = resolveTokenNames(token::propertyKeyName, propertyIds);
        prototype = prototype.withName(SchemaRule.generateName(prototype, entityTokenNames, propertyNames));
    }
    return prototype;
}
Also used : RelationTypeSchemaDescriptor(org.neo4j.internal.schema.RelationTypeSchemaDescriptor) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) UnspecifiedKernelException(org.neo4j.exceptions.UnspecifiedKernelException)

Example 7 with SchemaDescriptor

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

the class Operations method ensureConstraintHasName.

@SuppressWarnings("unchecked")
private <T extends ConstraintDescriptor> T ensureConstraintHasName(T constraint) throws KernelException {
    if (constraint.getName() == null) {
        SchemaDescriptor schema = constraint.schema();
        int[] entityTokenIds = schema.getEntityTokenIds();
        String[] entityTokenNames;
        switch(schema.entityType()) {
            case NODE:
                entityTokenNames = resolveTokenNames(token::nodeLabelName, entityTokenIds);
                break;
            case RELATIONSHIP:
                entityTokenNames = resolveTokenNames(token::relationshipTypeName, entityTokenIds);
                break;
            default:
                throw new UnspecifiedKernelException(Status.General.UnknownError, "Cannot create constraint for entity type %s in the schema %s.", schema.entityType(), schema);
        }
        int[] propertyIds = schema.getPropertyIds();
        String[] propertyNames = resolveTokenNames(token::propertyKeyName, propertyIds);
        constraint = (T) constraint.withName(SchemaRule.generateName(constraint, entityTokenNames, propertyNames));
    }
    return constraint;
}
Also used : RelationTypeSchemaDescriptor(org.neo4j.internal.schema.RelationTypeSchemaDescriptor) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) UnspecifiedKernelException(org.neo4j.exceptions.UnspecifiedKernelException)

Example 8 with SchemaDescriptor

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

the class Operations method validateNoExistingNodeWithExactValues.

/**
 * Check so that there is not an existing node with the exact match of label and property
 */
private void validateNoExistingNodeWithExactValues(IndexBackedConstraintDescriptor constraint, PropertyIndexQuery.ExactPredicate[] propertyValues, long modifiedNode) throws UniquePropertyValueValidationException, UnableToValidateConstraintException {
    IndexDescriptor index = allStoreHolder.indexGetForName(constraint.getName());
    try (FullAccessNodeValueIndexCursor valueCursor = cursors.allocateFullAccessNodeValueIndexCursor(ktx.cursorContext(), memoryTracker);
        IndexReaders indexReaders = new IndexReaders(index, allStoreHolder)) {
        assertIndexOnline(index);
        SchemaDescriptor schema = index.schema();
        long[] labelIds = schema.lockingKeys();
        if (labelIds.length != 1) {
            throw new UnableToValidateConstraintException(constraint, new AssertionError(format("Constraint indexes are not expected to be multi-token indexes, " + "but the constraint %s was referencing an index with the following schema: %s.", constraint.userDescription(token), schema.userDescription(token))), token);
        }
        // Take a big fat lock, and check for existing node in index
        ktx.lockClient().acquireExclusive(ktx.lockTracer(), INDEX_ENTRY, indexEntryResourceId(labelIds[0], propertyValues));
        allStoreHolder.nodeIndexSeekWithFreshIndexReader(valueCursor, indexReaders.createReader(), propertyValues);
        if (valueCursor.next() && valueCursor.nodeReference() != modifiedNode) {
            throw new UniquePropertyValueValidationException(constraint, VALIDATION, new IndexEntryConflictException(valueCursor.nodeReference(), NO_SUCH_NODE, PropertyIndexQuery.asValueTuple(propertyValues)), token);
        }
    } catch (IndexNotFoundKernelException | IndexBrokenKernelException | IndexNotApplicableKernelException e) {
        throw new UnableToValidateConstraintException(constraint, e, token);
    }
}
Also used : RelationTypeSchemaDescriptor(org.neo4j.internal.schema.RelationTypeSchemaDescriptor) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) UniquePropertyValueValidationException(org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException) UnableToValidateConstraintException(org.neo4j.kernel.api.exceptions.schema.UnableToValidateConstraintException) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) IndexNotApplicableKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotApplicableKernelException) IndexBrokenKernelException(org.neo4j.kernel.api.exceptions.schema.IndexBrokenKernelException) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException)

Example 9 with SchemaDescriptor

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

the class Operations method constraintDrop.

@Override
public void constraintDrop(ConstraintDescriptor constraint) throws SchemaKernelException {
    // Lock
    SchemaDescriptor schema = constraint.schema();
    exclusiveLock(schema.keyType(), schema.lockingKeys());
    exclusiveSchemaNameLock(constraint.getName());
    ktx.assertOpen();
    // verify data integrity
    try {
        assertConstraintExists(constraint);
    } catch (NoSuchConstraintException e) {
        throw new DropConstraintFailureException(constraint, e);
    }
    // Drop it like it's hot
    TransactionState txState = ktx.txState();
    txState.constraintDoDrop(constraint);
    if (constraint.enforcesUniqueness()) {
        IndexDescriptor index = allStoreHolder.indexGetForName(constraint.getName());
        if (index != IndexDescriptor.NO_INDEX) {
            txState.indexDoDrop(index);
        }
    }
}
Also used : NoSuchConstraintException(org.neo4j.kernel.api.exceptions.schema.NoSuchConstraintException) TransactionState(org.neo4j.kernel.api.txstate.TransactionState) RelationTypeSchemaDescriptor(org.neo4j.internal.schema.RelationTypeSchemaDescriptor) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) DropConstraintFailureException(org.neo4j.kernel.api.exceptions.schema.DropConstraintFailureException) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor)

Example 10 with SchemaDescriptor

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

the class Read method acquireSharedSchemaLock.

<T extends SchemaDescriptorSupplier> T acquireSharedSchemaLock(T schemaLike) {
    SchemaDescriptor schema = schemaLike.schema();
    long[] lockingKeys = schema.lockingKeys();
    ktx.lockClient().acquireShared(ktx.lockTracer(), schema.keyType(), lockingKeys);
    return schemaLike;
}
Also used : SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor)

Aggregations

SchemaDescriptor (org.neo4j.internal.schema.SchemaDescriptor)58 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)27 LabelSchemaDescriptor (org.neo4j.internal.schema.LabelSchemaDescriptor)23 Test (org.junit.jupiter.api.Test)18 IndexPrototype (org.neo4j.internal.schema.IndexPrototype)18 RelationTypeSchemaDescriptor (org.neo4j.internal.schema.RelationTypeSchemaDescriptor)18 Value (org.neo4j.values.storable.Value)11 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)10 ArrayList (java.util.ArrayList)6 KernelTransactionImplementation (org.neo4j.kernel.impl.api.KernelTransactionImplementation)6 IndexNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException)5 IndexConfig (org.neo4j.internal.schema.IndexConfig)5 MemoryTracker (org.neo4j.memory.MemoryTracker)5 KernelException (org.neo4j.exceptions.KernelException)4 UnspecifiedKernelException (org.neo4j.exceptions.UnspecifiedKernelException)4 IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)4 IndexNotApplicableKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotApplicableKernelException)4 SchemaKernelException (org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException)4 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)4 IndexProviderDescriptor (org.neo4j.internal.schema.IndexProviderDescriptor)4