Search in sources :

Example 1 with IndexQuery

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));
}
Also used : PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) TransactionState(org.neo4j.kernel.api.txstate.TransactionState) IndexQuery(org.neo4j.kernel.api.schema_new.IndexQuery) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement) StoreReadLayer(org.neo4j.storageengine.api.StoreReadLayer) IndexReader(org.neo4j.storageengine.api.schema.IndexReader) StateHandlingStatementOperations(org.neo4j.kernel.impl.api.StateHandlingStatementOperations) Test(org.junit.Test)

Example 2 with IndexQuery

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));
}
Also used : PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) TransactionState(org.neo4j.kernel.api.txstate.TransactionState) IndexQuery(org.neo4j.kernel.api.schema_new.IndexQuery) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement) StoreReadLayer(org.neo4j.storageengine.api.StoreReadLayer) IndexReader(org.neo4j.storageengine.api.schema.IndexReader) StateHandlingStatementOperations(org.neo4j.kernel.impl.api.StateHandlingStatementOperations) Test(org.junit.Test)

Example 3 with IndexQuery

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();
    }
}
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 4 with IndexQuery

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));
    }
}
Also used : PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) IndexQuery(org.neo4j.kernel.api.schema_new.IndexQuery) StorageStatement(org.neo4j.storageengine.api.StorageStatement) IndexReader(org.neo4j.storageengine.api.schema.IndexReader)

Example 5 with IndexQuery

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));
}
Also used : PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) TransactionState(org.neo4j.kernel.api.txstate.TransactionState) IndexQuery(org.neo4j.kernel.api.schema_new.IndexQuery) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement) StoreReadLayer(org.neo4j.storageengine.api.StoreReadLayer) IndexReader(org.neo4j.storageengine.api.schema.IndexReader) StateHandlingStatementOperations(org.neo4j.kernel.impl.api.StateHandlingStatementOperations) Test(org.junit.Test)

Aggregations

IndexQuery (org.neo4j.kernel.api.schema_new.IndexQuery)8 Test (org.junit.Test)6 PrimitiveLongIterator (org.neo4j.collection.primitive.PrimitiveLongIterator)6 IndexReader (org.neo4j.storageengine.api.schema.IndexReader)6 TransactionState (org.neo4j.kernel.api.txstate.TransactionState)5 KernelStatement (org.neo4j.kernel.impl.api.KernelStatement)5 StateHandlingStatementOperations (org.neo4j.kernel.impl.api.StateHandlingStatementOperations)5 StoreReadLayer (org.neo4j.storageengine.api.StoreReadLayer)5 StorageStatement (org.neo4j.storageengine.api.StorageStatement)2 Supplier (java.util.function.Supplier)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.fail (org.junit.Assert.fail)1 Rule (org.junit.Rule)1 Cursor (org.neo4j.cursor.Cursor)1 Label (org.neo4j.graphdb.Label)1 Label.label (org.neo4j.graphdb.Label.label)1 Transaction (org.neo4j.graphdb.Transaction)1 Iterators (org.neo4j.helpers.collection.Iterators)1 Statement (org.neo4j.kernel.api.Statement)1 IndexEntryConflictException (org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException)1