Search in sources :

Example 6 with NewIndexDescriptor

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

the class ConstraintIndexConcurrencyTest method shouldNotAllowConcurrentViolationOfConstraint.

@Test
public void shouldNotAllowConcurrentViolationOfConstraint() throws Exception {
    // Given
    GraphDatabaseAPI graphDb = db.getGraphDatabaseAPI();
    Supplier<Statement> statementSupplier = graphDb.getDependencyResolver().resolveDependency(ThreadToStatementContextBridge.class);
    Label label = label("Foo");
    String propertyKey = "bar";
    String conflictingValue = "baz";
    // a constraint
    try (Transaction tx = graphDb.beginTx()) {
        graphDb.schema().constraintFor(label).assertPropertyIsUnique(propertyKey).create();
        tx.success();
    }
    // When
    try (Transaction tx = graphDb.beginTx()) {
        // create a statement and perform a lookup
        Statement statement = statementSupplier.get();
        int labelId = statement.readOperations().labelGetForName(label.name());
        int propertyKeyId = statement.readOperations().propertyKeyGetForName(propertyKey);
        NewIndexDescriptor index = NewIndexDescriptorFactory.uniqueForLabel(labelId, propertyKeyId);
        statement.readOperations().indexQuery(index, IndexQuery.exact(index.schema().getPropertyId(), "The value is irrelevant, we just want to perform some sort of lookup against this index"));
        // then let another thread come in and create a node
        threads.execute(db -> {
            try (Transaction transaction = db.beginTx()) {
                db.createNode(label).setProperty(propertyKey, conflictingValue);
                transaction.success();
            }
            return null;
        }, graphDb).get();
        // before we create a node with the same property ourselves - using the same statement that we have
        // already used for lookup against that very same index
        long node = statement.dataWriteOperations().nodeCreate();
        statement.dataWriteOperations().nodeAddLabel(node, labelId);
        try {
            statement.dataWriteOperations().nodeSetProperty(node, property(propertyKeyId, conflictingValue));
            fail("exception expected");
        }// Then
         catch (UniquePropertyValueValidationException e) {
            assertEquals(ConstraintDescriptorFactory.uniqueForLabel(labelId, propertyKeyId), e.constraint());
            IndexEntryConflictException conflict = Iterators.single(e.conflicts().iterator());
            assertEquals(conflictingValue, conflict.getSinglePropertyValue());
        }
        tx.success();
    }
}
Also used : ImpermanentDatabaseRule(org.neo4j.test.rule.ImpermanentDatabaseRule) Label(org.neo4j.graphdb.Label) IndexQuery(org.neo4j.kernel.api.schema_new.IndexQuery) ThreadToStatementContextBridge(org.neo4j.kernel.impl.core.ThreadToStatementContextBridge) Iterators(org.neo4j.helpers.collection.Iterators) NewIndexDescriptorFactory(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptorFactory) Test(org.junit.Test) Label.label(org.neo4j.graphdb.Label.label) Statement(org.neo4j.kernel.api.Statement) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) Property.property(org.neo4j.kernel.api.properties.Property.property) Supplier(java.util.function.Supplier) DatabaseRule(org.neo4j.test.rule.DatabaseRule) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Rule(org.junit.Rule) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException) ConstraintDescriptorFactory(org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptorFactory) UniquePropertyValueValidationException(org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException) ThreadingRule(org.neo4j.test.rule.concurrent.ThreadingRule) Assert.fail(org.junit.Assert.fail) Transaction(org.neo4j.graphdb.Transaction) Assert.assertEquals(org.junit.Assert.assertEquals) UniquePropertyValueValidationException(org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Transaction(org.neo4j.graphdb.Transaction) Statement(org.neo4j.kernel.api.Statement) Label(org.neo4j.graphdb.Label) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException) Test(org.junit.Test)

Example 7 with NewIndexDescriptor

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

the class BuiltInProceduresTest method givenIndex.

private void givenIndex(String label, String propKey) {
    int labelId = token(label, labels);
    int propId = token(propKey, propKeys);
    NewIndexDescriptor index = NewIndexDescriptorFactory.forLabel(labelId, propId);
    indexes.add(index);
}
Also used : NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)

Example 8 with NewIndexDescriptor

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

the class BuiltInProceduresTest method givenUniqueConstraint.

private void givenUniqueConstraint(String label, String propKey) {
    int labelId = token(label, labels);
    int propId = token(propKey, propKeys);
    NewIndexDescriptor index = NewIndexDescriptorFactory.uniqueForLabel(labelId, propId);
    uniqueIndexes.add(index);
    constraints.add(ConstraintDescriptorFactory.uniqueForLabel(labelId, propId));
}
Also used : NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)

Example 9 with NewIndexDescriptor

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

the class OperationsFacade method uniqueIndexGetForLabelAndPropertyKey.

@Override
public NewIndexDescriptor uniqueIndexGetForLabelAndPropertyKey(NodePropertyDescriptor descriptor) throws SchemaRuleNotFoundException, DuplicateSchemaRuleException {
    NewIndexDescriptor result = null;
    Iterator<NewIndexDescriptor> indexes = uniqueIndexesGetForLabel(descriptor.getLabelId());
    while (indexes.hasNext()) {
        NewIndexDescriptor index = indexes.next();
        if (index.schema().equals(SchemaBoundary.map(descriptor))) {
            if (null == result) {
                result = index;
            } else {
                throw new DuplicateSchemaRuleException(SchemaRule.Kind.CONSTRAINT_INDEX_RULE, index.schema());
            }
        }
    }
    if (null == result) {
        throw new SchemaRuleNotFoundException(SchemaRule.Kind.CONSTRAINT_INDEX_RULE, SchemaBoundary.map(descriptor));
    }
    return result;
}
Also used : NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) SchemaRuleNotFoundException(org.neo4j.kernel.api.exceptions.schema.SchemaRuleNotFoundException) DuplicateSchemaRuleException(org.neo4j.kernel.api.exceptions.schema.DuplicateSchemaRuleException)

Example 10 with NewIndexDescriptor

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

the class IndexingService method init.

/**
     * Called while the database starts up, before recovery.
     */
@Override
public void init() {
    IndexMap indexMap = indexMapRef.indexMapSnapshot();
    for (IndexRule indexRule : indexRules) {
        IndexProxy indexProxy;
        long indexId = indexRule.getId();
        NewIndexDescriptor descriptor = indexRule.getIndexDescriptor();
        SchemaIndexProvider.Descriptor providerDescriptor = indexRule.getProviderDescriptor();
        SchemaIndexProvider provider = providerMap.apply(providerDescriptor);
        InternalIndexState initialState = provider.getInitialState(indexId, descriptor);
        log.info(indexStateInfo("init", indexId, initialState, descriptor));
        boolean constraintIndex = indexRule.canSupportUniqueConstraint();
        switch(initialState) {
            case ONLINE:
                indexProxy = indexProxyCreator.createOnlineIndexProxy(indexId, descriptor, providerDescriptor);
                break;
            case POPULATING:
                // The database was shut down during population, or a crash has occurred, or some other sad thing.
                if (constraintIndex && indexRule.getOwningConstraint() == null) {
                    // don't bother rebuilding if we are going to throw the index away anyhow
                    indexProxy = indexProxyCreator.createFailedIndexProxy(indexId, descriptor, providerDescriptor, failure("Constraint for index was not committed."));
                } else {
                    indexProxy = indexProxyCreator.createRecoveringIndexProxy(descriptor, providerDescriptor);
                }
                break;
            case FAILED:
                IndexPopulationFailure failure = failure(provider.getPopulationFailure(indexId));
                indexProxy = indexProxyCreator.createFailedIndexProxy(indexId, descriptor, providerDescriptor, failure);
                break;
            default:
                throw new IllegalArgumentException("" + initialState);
        }
        indexMap.putIndexProxy(indexId, indexProxy);
    }
    indexMapRef.setIndexMap(indexMap);
}
Also used : IndexRule(org.neo4j.kernel.impl.store.record.IndexRule) InternalIndexState(org.neo4j.kernel.api.index.InternalIndexState) SchemaIndexProvider(org.neo4j.kernel.api.index.SchemaIndexProvider) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) Descriptor(org.neo4j.kernel.api.index.SchemaIndexProvider.Descriptor)

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