Search in sources :

Example 11 with NewIndexDescriptor

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

the class ConstraintIndexCreator method createConstraintIndex.

public NewIndexDescriptor createConstraintIndex(final LabelSchemaDescriptor schema) {
    try (KernelTransaction transaction = kernelSupplier.get().newTransaction(KernelTransaction.Type.implicit, AUTH_DISABLED);
        Statement statement = transaction.acquireStatement()) {
        // NOTE: This creates the index (obviously) but it DOES NOT grab a schema
        // write lock. It is assumed that the transaction that invoked this "inner" transaction
        // holds a schema write lock, and that it will wait for this inner transaction to do its
        // work.
        // TODO (Ben+Jake): The Transactor is really part of the kernel internals, so it needs access to the
        // internal implementation of Statement. However it is currently used by the external
        // RemoveOrphanConstraintIndexesOnStartup job. This needs revisiting.
        NewIndexDescriptor index = NewIndexDescriptorFactory.uniqueForSchema(schema);
        ((KernelStatement) statement).txState().indexRuleDoAdd(index);
        transaction.success();
        return index;
    } catch (TransactionFailureException e) {
        throw new RuntimeException(e);
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) Statement(org.neo4j.kernel.api.Statement) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement)

Example 12 with NewIndexDescriptor

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

the class IndexingService method createIndexes.

/**
     * Creates one or more indexes. They will all be populated by one and the same store scan.
     *
     * This code is called from the transaction infrastructure during transaction commits, which means that
     * it is *vital* that it is stable, and handles errors very well. Failing here means that the entire db
     * will shut down.
     */
public void createIndexes(IndexRule... rules) throws IOException {
    IndexMap indexMap = indexMapRef.indexMapSnapshot();
    IndexPopulationJob populationJob = null;
    for (IndexRule rule : rules) {
        long ruleId = rule.getId();
        IndexProxy index = indexMap.getIndexProxy(ruleId);
        if (index != null && state == State.NOT_STARTED) {
            // During recovery we might run into this scenario:
            // - We're starting recovery on a database, where init() is called and all indexes that
            //   are found in the store, instantiated and put into the IndexMap. Among them is index X.
            // - While we recover the database we bump into a transaction creating index Y, with the
            //   same IndexDescriptor, i.e. same label/property, as X. This is possible since this took
            //   place before the creation of X.
            // - When Y is dropped in between this creation and the creation of X (it will have to be
            //   otherwise X wouldn't have had an opportunity to be created) the index is removed from
            //   the IndexMap, both by id AND descriptor.
            //
            // Because of the scenario above we need to put this created index into the IndexMap
            // again, otherwise it will disappear from the IndexMap (at least for lookup by descriptor)
            // and not be able to accept changes applied from recovery later on.
            indexMap.putIndexProxy(ruleId, index);
            indexMapRef.setIndexMap(indexMap);
            continue;
        }
        final NewIndexDescriptor descriptor = rule.getIndexDescriptor();
        SchemaIndexProvider.Descriptor providerDescriptor = rule.getProviderDescriptor();
        boolean flipToTentative = rule.canSupportUniqueConstraint();
        if (state == State.RUNNING) {
            populationJob = populationJob == null ? newIndexPopulationJob() : populationJob;
            index = indexProxyCreator.createPopulatingIndexProxy(ruleId, descriptor, providerDescriptor, flipToTentative, monitor, populationJob);
            index.start();
        } else {
            index = indexProxyCreator.createRecoveringIndexProxy(descriptor, providerDescriptor);
        }
        indexMap.putIndexProxy(rule.getId(), index);
    }
    if (populationJob != null) {
        startIndexPopulation(populationJob);
    }
    indexMapRef.setIndexMap(indexMap);
}
Also used : IndexRule(org.neo4j.kernel.impl.store.record.IndexRule) SchemaIndexProvider(org.neo4j.kernel.api.index.SchemaIndexProvider) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) Descriptor(org.neo4j.kernel.api.index.SchemaIndexProvider.Descriptor)

Example 13 with NewIndexDescriptor

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

the class LockingStatementOperationsTest method shouldAcquireSchemaWriteLockBeforeRemovingIndexRule.

@Test
public void shouldAcquireSchemaWriteLockBeforeRemovingIndexRule() throws Exception {
    // given
    NewIndexDescriptor index = NewIndexDescriptorFactory.forLabel(0, 0);
    // when
    lockingOps.indexDrop(state, index);
    // then
    order.verify(locks).acquireExclusive(LockTracer.NONE, ResourceTypes.SCHEMA, schemaResource());
    order.verify(schemaWriteOps).indexDrop(state, index);
}
Also used : NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) Test(org.junit.Test)

Example 14 with NewIndexDescriptor

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

the class OperationsFacadeTest method testThrowExceptionWhenDuplicateUniqueIndexFound.

@Test
public void testThrowExceptionWhenDuplicateUniqueIndexFound() throws SchemaRuleNotFoundException, DuplicateSchemaRuleException {
    SchemaReadOperations readOperations = setupSchemaReadOperations();
    NewIndexDescriptor index = NewIndexDescriptorFactory.forSchema(SchemaBoundary.map(descriptor));
    Mockito.when(readOperations.uniqueIndexesGetForLabel(Mockito.any(KernelStatement.class), Mockito.eq(descriptor.getLabelId()))).thenReturn(Iterators.iterator(index, index));
    TokenNameLookup tokenNameLookup = getDefaultTokenNameLookup();
    expectedException.expect(DuplicateSchemaRuleException.class);
    expectedException.expect(new KernelExceptionUserMessageMatcher<>(tokenNameLookup, "Multiple constraint indexs found for :Label1(Prop1)."));
    operationsFacade.uniqueIndexGetForLabelAndPropertyKey(descriptor);
}
Also used : TokenNameLookup(org.neo4j.kernel.api.TokenNameLookup) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) SchemaReadOperations(org.neo4j.kernel.impl.api.operations.SchemaReadOperations) Test(org.junit.Test)

Example 15 with NewIndexDescriptor

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

the class GraphDbStructureGuideTest method visitsIndexes.

@Test
public void visitsIndexes() throws Exception {
    DbStructureVisitor visitor = mock(DbStructureVisitor.class);
    int labelId = createLabel("Person");
    int pkId = createPropertyKey("name");
    commitAndReOpen();
    NewIndexDescriptor descriptor = createSchemaIndex(labelId, pkId);
    // WHEN
    accept(visitor);
    // THEN
    verify(visitor).visitIndex(descriptor, ":Person(name)", 1.0d, 0L);
}
Also used : NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) UniquenessConstraint(org.neo4j.kernel.api.constraints.UniquenessConstraint) Test(org.junit.Test)

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