use of org.neo4j.kernel.api.schema_new.IndexQuery in project neo4j by neo4j.
the class StateHandlingStatementOperationsTest method shouldConsiderTransactionStateDuringIndexScanWithIndexQuery.
@Test
public void shouldConsiderTransactionStateDuringIndexScanWithIndexQuery() throws Exception {
// Given
TransactionState txState = mock(TransactionState.class);
KernelStatement statement = mock(KernelStatement.class);
when(statement.hasTxStateWithChanges()).thenReturn(true);
when(statement.txState()).thenReturn(txState);
when(txState.indexUpdatesForScan(index)).thenReturn(new DiffSets<>(Collections.singleton(42L), Collections.singleton(44L)));
when(txState.addedAndRemovedNodes()).thenReturn(new DiffSets<>(Collections.singleton(45L), Collections.singleton(46L)));
StoreReadLayer storeReadLayer = mock(StoreReadLayer.class);
IndexReader indexReader = addMockedIndexReader(statement);
IndexQuery query = IndexQuery.exists(index.schema().getPropertyId());
when(indexReader.query(query)).thenReturn(PrimitiveLongCollections.resourceIterator(PrimitiveLongCollections.iterator(43L, 44L, 46L), null));
StateHandlingStatementOperations context = newTxStateOps(storeReadLayer);
// When
PrimitiveLongIterator results = context.indexQuery(statement, index, query);
// Then
assertEquals(asSet(42L, 43L), PrimitiveLongCollections.toSet(results));
}
use of org.neo4j.kernel.api.schema_new.IndexQuery in project neo4j by neo4j.
the class StateHandlingStatementOperationsTest method shouldConsiderTransactionStateDuringIndexRangeSeekByContainsWithIndexQuery.
@Test
public void shouldConsiderTransactionStateDuringIndexRangeSeekByContainsWithIndexQuery() throws Exception {
// Given
TransactionState txState = mock(TransactionState.class);
KernelStatement statement = mock(KernelStatement.class);
when(statement.hasTxStateWithChanges()).thenReturn(true);
when(statement.txState()).thenReturn(txState);
when(txState.indexUpdatesForScan(index)).thenReturn(new DiffSets<>(Collections.singleton(42L), Collections.singleton(44L)));
when(txState.addedAndRemovedNodes()).thenReturn(new DiffSets<>(Collections.singleton(45L), Collections.singleton(46L)));
StoreReadLayer storeReadLayer = mock(StoreReadLayer.class);
IndexReader indexReader = addMockedIndexReader(statement);
IndexQuery.StringContainsPredicate indexQuery = IndexQuery.stringContains(index.schema().getPropertyId(), "contains");
when(indexReader.query(indexQuery)).thenReturn(PrimitiveLongCollections.resourceIterator(PrimitiveLongCollections.iterator(43L, 44L, 46L), null));
StateHandlingStatementOperations context = newTxStateOps(storeReadLayer);
// When
PrimitiveLongIterator results = context.indexQuery(statement, index, indexQuery);
// Then
assertEquals(asSet(42L, 43L), PrimitiveLongCollections.toSet(results));
}
use of org.neo4j.kernel.api.schema_new.IndexQuery 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.IndexQuery in project neo4j by neo4j.
the class StateHandlingStatementOperations method indexQuery.
@Override
public PrimitiveLongIterator indexQuery(KernelStatement state, NewIndexDescriptor index, IndexQuery... predicates) throws IndexNotFoundKernelException, IndexNotApplicableKernelException {
StorageStatement storeStatement = state.getStoreStatement();
IndexReader reader = storeStatement.getIndexReader(index);
PrimitiveLongIterator committed = reader.query(predicates);
PrimitiveLongIterator exactMatches = LookupFilter.exactIndexMatches(this, state, committed, predicates);
IndexQuery firstPredicate = predicates[0];
switch(firstPredicate.type()) {
case exact:
IndexQuery.ExactPredicate[] exactPreds = assertOnlyExactPredicates(predicates);
return filterIndexStateChangesForSeek(state, exactMatches, index, OrderedPropertyValues.of(exactPreds));
case stringSuffix:
case stringContains:
case exists:
return filterIndexStateChangesForScan(state, exactMatches, index);
case rangeNumeric:
{
assertSinglePredicate(predicates);
IndexQuery.NumberRangePredicate numPred = (IndexQuery.NumberRangePredicate) firstPredicate;
return filterIndexStateChangesForRangeSeekByNumber(state, index, numPred.from(), numPred.fromInclusive(), numPred.to(), numPred.toInclusive(), exactMatches);
}
case rangeString:
{
assertSinglePredicate(predicates);
IndexQuery.StringRangePredicate strPred = (IndexQuery.StringRangePredicate) firstPredicate;
return filterIndexStateChangesForRangeSeekByString(state, index, strPred.from(), strPred.fromInclusive(), strPred.to(), strPred.toInclusive(), committed);
}
case stringPrefix:
{
assertSinglePredicate(predicates);
IndexQuery.StringPrefixPredicate strPred = (IndexQuery.StringPrefixPredicate) firstPredicate;
return filterIndexStateChangesForRangeSeekByPrefix(state, index, strPred.prefix(), committed);
}
default:
throw new UnsupportedOperationException("Query not supported: " + Arrays.toString(predicates));
}
}
use of org.neo4j.kernel.api.schema_new.IndexQuery in project neo4j by neo4j.
the class StateHandlingStatementOperationsTest method shouldConsiderTransactionStateDuringIndexRangeSeekBySuffixWithIndexQuery.
@Test
public void shouldConsiderTransactionStateDuringIndexRangeSeekBySuffixWithIndexQuery() throws Exception {
// Given
TransactionState txState = mock(TransactionState.class);
KernelStatement statement = mock(KernelStatement.class);
when(statement.hasTxStateWithChanges()).thenReturn(true);
when(statement.txState()).thenReturn(txState);
when(txState.indexUpdatesForScan(index)).thenReturn(new DiffSets<>(Collections.singleton(42L), Collections.singleton(44L)));
when(txState.addedAndRemovedNodes()).thenReturn(new DiffSets<>(Collections.singleton(45L), Collections.singleton(46L)));
StoreReadLayer storeReadLayer = mock(StoreReadLayer.class);
IndexReader indexReader = addMockedIndexReader(statement);
IndexQuery.StringSuffixPredicate indexQuery = IndexQuery.stringSuffix(index.schema().getPropertyId(), "suffix");
when(indexReader.query(indexQuery)).thenReturn(PrimitiveLongCollections.resourceIterator(PrimitiveLongCollections.iterator(43L, 44L, 46L), null));
StateHandlingStatementOperations context = newTxStateOps(storeReadLayer);
// When
PrimitiveLongIterator results = context.indexQuery(statement, index, indexQuery);
// Then
assertEquals(asSet(42L, 43L), PrimitiveLongCollections.toSet(results));
}
Aggregations