Search in sources :

Example 21 with IndexDescriptor

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

the class Operations method enforceNodeKeyConstraint.

private void enforceNodeKeyConstraint(SchemaDescriptor schema) throws KernelException {
    IndexDescriptor index = allStoreHolder.findUsableTokenIndex(NODE);
    if (index != IndexDescriptor.NO_INDEX) {
        try (var cursor = cursors.allocateFullAccessNodeLabelIndexCursor(ktx.cursorContext())) {
            var session = allStoreHolder.tokenReadSession(index);
            allStoreHolder.nodeLabelScan(session, cursor, unconstrained(), new TokenPredicate(schema.getLabelId()));
            constraintSemantics.validateNodeKeyConstraint(cursor, nodeCursor, propertyCursor, schema.asLabelSchemaDescriptor(), token);
        }
    } else {
        try (var cursor = cursors.allocateFullAccessNodeCursor(ktx.cursorContext())) {
            allStoreHolder.allNodesScan(cursor);
            constraintSemantics.validateNodeKeyConstraint(new FilteringNodeCursorWrapper(cursor, CursorPredicates.hasLabel(schema.getLabelId())), propertyCursor, schema.asLabelSchemaDescriptor(), token);
        }
    }
}
Also used : TokenPredicate(org.neo4j.internal.kernel.api.TokenPredicate) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor)

Example 22 with IndexDescriptor

use of org.neo4j.internal.schema.IndexDescriptor 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 23 with IndexDescriptor

use of org.neo4j.internal.schema.IndexDescriptor 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 24 with IndexDescriptor

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

the class Read method nodeLabelScan.

@Override
public final Scan<NodeLabelIndexCursor> nodeLabelScan(int label) {
    ktx.assertOpen();
    CursorContext cursorContext = ktx.cursorContext();
    TokenScan tokenScan;
    try {
        Iterator<IndexDescriptor> index = index(SchemaDescriptor.forAnyEntityTokens(EntityType.NODE));
        if (!index.hasNext()) {
            throw new IndexNotFoundKernelException("There is no index that can back a node label scan.");
        }
        IndexDescriptor nliDescriptor = index.next();
        DefaultTokenReadSession session = (DefaultTokenReadSession) tokenReadSession(nliDescriptor);
        tokenScan = session.reader.entityTokenScan(label, cursorContext);
    } catch (IndexNotFoundKernelException e) {
        throw new RuntimeException(e);
    }
    return new NodeLabelIndexCursorScan(this, label, tokenScan, cursorContext);
}
Also used : TokenScan(org.neo4j.kernel.impl.index.schema.TokenScan) CursorContext(org.neo4j.io.pagecache.context.CursorContext) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor)

Example 25 with IndexDescriptor

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

the class OnlineIndexUpdates method eagerlyGatherTokenIndexUpdates.

private void eagerlyGatherTokenIndexUpdates(EntityUpdates entityUpdates, EntityType entityType, long txId) {
    IndexDescriptor relatedToken = schemaCache.getTokenIndex(entityType);
    entityUpdates.tokenUpdateForIndexKey(relatedToken, txId).ifPresent(updates::add);
}
Also used : IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor)

Aggregations

IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)404 Test (org.junit.jupiter.api.Test)231 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)81 Value (org.neo4j.values.storable.Value)43 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)33 ArrayList (java.util.ArrayList)31 Transaction (org.neo4j.graphdb.Transaction)31 SchemaDescriptor (org.neo4j.internal.schema.SchemaDescriptor)31 IndexPrototype (org.neo4j.internal.schema.IndexPrototype)30 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)30 TokenRead (org.neo4j.internal.kernel.api.TokenRead)29 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)26 SchemaRead (org.neo4j.internal.kernel.api.SchemaRead)26 IndexUpdater (org.neo4j.kernel.api.index.IndexUpdater)25 IndexProxy (org.neo4j.kernel.impl.api.index.IndexProxy)25 IndexReadSession (org.neo4j.internal.kernel.api.IndexReadSession)23 IndexNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException)23 LabelSchemaDescriptor (org.neo4j.internal.schema.LabelSchemaDescriptor)23 IndexProviderDescriptor (org.neo4j.internal.schema.IndexProviderDescriptor)22 KernelException (org.neo4j.exceptions.KernelException)20