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);
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations