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