Search in sources :

Example 46 with NewIndexDescriptor

use of org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor in project neo4j by neo4j.

the class StateHandlingStatementOperations method indexGetForLabelAndPropertyKey.

private NewIndexDescriptor indexGetForLabelAndPropertyKey(KernelStatement state, LabelSchemaDescriptor descriptor) {
    NewIndexDescriptor indexDescriptor = storeLayer.indexGetForLabelAndPropertyKey(descriptor);
    Iterator<NewIndexDescriptor> rules = iterator(indexDescriptor);
    if (state.hasTxStateWithChanges()) {
        rules = filter(SchemaDescriptor.equalTo(descriptor), state.txState().indexDiffSetsByLabel(descriptor.getLabelId(), GENERAL).apply(rules));
    }
    return singleOrNull(rules);
}
Also used : NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)

Example 47 with NewIndexDescriptor

use of org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor in project neo4j by neo4j.

the class ConstraintIndexCreator method createUniquenessConstraintIndex.

/**
     * You MUST hold a schema write lock before you call this method.
     * However the schema write lock is temporarily released while populating the index backing the constraint.
     * It goes a little like this:
     * <ol>
     * <li>Prerequisite: Getting here means that there's an open schema transaction which has acquired the
     * SCHEMA WRITE lock.</li>
     * <li>Index schema rule which is backing the constraint is created in a nested mini-transaction
     * which doesn't acquire any locking, merely adds tx state and commits so that the index rule is applied
     * to the store, which triggers the index population</li>
     * <li>Release the SCHEMA WRITE lock</li>
     * <li>Await index population to complete</li>
     * <li>Acquire the SCHEMA WRITE lock (effectively blocking concurrent transactions changing
     * data related to this constraint, and it so happens, most other transactions as well) and verify
     * the uniqueness of the built index</li>
     * <li>Leave this method, knowing that the uniqueness constraint rule will be added to tx state
     * and this tx committed, which will create the uniqueness constraint</li>
     * </ol>
     */
public long createUniquenessConstraintIndex(KernelStatement state, SchemaReadOperations schema, LabelSchemaDescriptor descriptor) throws TransactionFailureException, CreateConstraintFailureException, DropIndexFailureException, UniquePropertyValueValidationException {
    UniquenessConstraintDescriptor constraint = ConstraintDescriptorFactory.uniqueForSchema(descriptor);
    NewIndexDescriptor index = createConstraintIndex(descriptor);
    boolean success = false;
    boolean reacquiredSchemaLock = false;
    Client locks = state.locks().pessimistic();
    try {
        long indexId = schema.indexGetCommittedId(state, index);
        // Release the SCHEMA WRITE lock during index population.
        // At this point the integrity of the constraint to be created was checked
        // while holding the lock and the index rule backing the soon-to-be-created constraint
        // has been created. Now it's just the population left, which can take a long time
        releaseSchemaLock(locks);
        awaitConstrainIndexPopulation(constraint, indexId);
        // Index population was successful, but at this point we don't know if the uniqueness constraint holds.
        // Acquire SCHEMA WRITE lock and verify the constraints here in this user transaction
        // and if everything checks out then it will be held until after the constraint has been
        // created and activated.
        acquireSchemaLock(state, locks);
        reacquiredSchemaLock = true;
        indexingService.getIndexProxy(indexId).verifyDeferredConstraints(propertyAccessor);
        success = true;
        return indexId;
    } catch (SchemaRuleNotFoundException | IndexNotFoundKernelException e) {
        throw new IllegalStateException(String.format("Index (%s) that we just created does not exist.", descriptor));
    } catch (IndexEntryConflictException e) {
        throw new UniquePropertyValueValidationException(constraint, VERIFICATION, e);
    } catch (InterruptedException | IOException e) {
        throw new CreateConstraintFailureException(constraint, e);
    } finally {
        if (!success) {
            if (!reacquiredSchemaLock) {
                acquireSchemaLock(state, locks);
            }
            dropUniquenessConstraintIndex(index);
        }
    }
}
Also used : UniquePropertyValueValidationException(org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException) SchemaRuleNotFoundException(org.neo4j.kernel.api.exceptions.schema.SchemaRuleNotFoundException) IndexNotFoundKernelException(org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException) IOException(java.io.IOException) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) UniquenessConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor) Client(org.neo4j.kernel.impl.locking.Locks.Client) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException) CreateConstraintFailureException(org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException)

Example 48 with NewIndexDescriptor

use of org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor in project neo4j by neo4j.

the class IndexTxStateUpdater method onPropertyAdd.

// PROPERTY CHANGES
public void onPropertyAdd(KernelStatement state, NodeItem node, DefinedProperty after) throws EntityNotFoundException {
    Iterator<NewIndexDescriptor> indexes = getIndexesInvolvingProperty(state, after.propertyKeyId());
    nodeIndexMatcher.onMatchingSchema(state, indexes, node, after.propertyKeyId(), index -> {
        Validators.INDEX_VALUE_VALIDATOR.validate(after.value());
        OrderedPropertyValues values = getOrderedPropertyValues(state, node, after, index.schema().getPropertyIds());
        state.txState().indexDoUpdateEntry(index.schema(), node.id(), null, values);
    });
}
Also used : NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) OrderedPropertyValues(org.neo4j.kernel.api.schema_new.OrderedPropertyValues)

Example 49 with NewIndexDescriptor

use of org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor in project neo4j by neo4j.

the class IndexTxStateUpdater method onPropertyRemove.

public void onPropertyRemove(KernelStatement state, NodeItem node, DefinedProperty before) throws EntityNotFoundException {
    Iterator<NewIndexDescriptor> indexes = getIndexesInvolvingProperty(state, before.propertyKeyId());
    nodeIndexMatcher.onMatchingSchema(state, indexes, node, before.propertyKeyId(), index -> {
        OrderedPropertyValues values = getOrderedPropertyValues(state, node, before, index.schema().getPropertyIds());
        state.txState().indexDoUpdateEntry(index.schema(), node.id(), values, null);
    });
}
Also used : NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) OrderedPropertyValues(org.neo4j.kernel.api.schema_new.OrderedPropertyValues)

Example 50 with NewIndexDescriptor

use of org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor in project neo4j by neo4j.

the class IndexTxStateUpdater method onLabelChange.

public void onLabelChange(KernelStatement state, int labelId, NodeItem node, LabelChangeType changeType) throws EntityNotFoundException {
    PrimitiveIntSet nodePropertyIds = Primitive.intSet();
    nodePropertyIds.addAll(readOps.nodeGetPropertyKeys(state, node).iterator());
    Iterator<NewIndexDescriptor> indexes = Iterators.concat(schemaReadOps.indexesGetForLabel(state, labelId), schemaReadOps.uniqueIndexesGetForLabel(state, labelId));
    while (indexes.hasNext()) {
        NewIndexDescriptor index = indexes.next();
        int[] indexPropertyIds = index.schema().getPropertyIds();
        if (nodeHasIndexProperties(nodePropertyIds, indexPropertyIds)) {
            OrderedPropertyValues values = getOrderedPropertyValues(state, node, indexPropertyIds);
            if (changeType == LabelChangeType.ADDED_LABEL) {
                state.txState().indexDoUpdateEntry(index.schema(), node.id(), null, values);
            } else {
                state.txState().indexDoUpdateEntry(index.schema(), node.id(), values, null);
            }
        }
    }
}
Also used : NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) PrimitiveIntSet(org.neo4j.collection.primitive.PrimitiveIntSet) OrderedPropertyValues(org.neo4j.kernel.api.schema_new.OrderedPropertyValues)

Aggregations

NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)99 Test (org.junit.Test)55 Statement (org.neo4j.kernel.api.Statement)24 ReadOperations (org.neo4j.kernel.api.ReadOperations)17 IndexNotFoundKernelException (org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException)10 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)9 SchemaIndexProvider (org.neo4j.kernel.api.index.SchemaIndexProvider)9 InternalIndexState (org.neo4j.kernel.api.index.InternalIndexState)7 Transaction (org.neo4j.graphdb.Transaction)6 IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)5 IndexEntryConflictException (org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException)5 SchemaRuleNotFoundException (org.neo4j.kernel.api.exceptions.schema.SchemaRuleNotFoundException)5 LabelSchemaDescriptor (org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 PrimitiveLongSet (org.neo4j.collection.primitive.PrimitiveLongSet)4 Label (org.neo4j.graphdb.Label)4 NotFoundException (org.neo4j.graphdb.NotFoundException)4 KernelException (org.neo4j.kernel.api.exceptions.KernelException)4 NodePropertyDescriptor (org.neo4j.kernel.api.schema.NodePropertyDescriptor)4