Search in sources :

Example 6 with IndexQuery

use of org.neo4j.kernel.api.schema_new.IndexQuery in project neo4j by neo4j.

the class StateHandlingStatementOperationsTest method shouldConsiderTransactionStateDuringIndexRangeSeekByPrefixWithIndexQuery.

@Test
public void shouldConsiderTransactionStateDuringIndexRangeSeekByPrefixWithIndexQuery() 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.indexUpdatesForRangeSeekByPrefix(index, "prefix")).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.StringPrefixPredicate indexQuery = IndexQuery.stringPrefix(index.schema().getPropertyId(), "prefix");
    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 7 with IndexQuery

use of org.neo4j.kernel.api.schema_new.IndexQuery in project neo4j by neo4j.

the class StateHandlingStatementOperationsTest method shouldConsiderTransactionStateDuringIndexBetweenRangeSeekByNumberWithIndexQuery.

@SuppressWarnings("unchecked")
@Test
public void shouldConsiderTransactionStateDuringIndexBetweenRangeSeekByNumberWithIndexQuery() throws Exception {
    // Given
    final int propertyKey = 2;
    final int inRange = 15;
    int lower = 10;
    int upper = 20;
    TransactionState txState = mock(TransactionState.class);
    KernelStatement statement = mock(KernelStatement.class);
    when(statement.hasTxStateWithChanges()).thenReturn(true);
    when(statement.txState()).thenReturn(txState);
    StorageStatement storageStatement = mock(StorageStatement.class);
    when(statement.getStoreStatement()).thenReturn(storageStatement);
    when(txState.indexUpdatesForRangeSeekByNumber(index, lower, true, upper, false)).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);
    when(txState.augmentSingleNodeCursor(any(Cursor.class), anyLong())).thenAnswer(invocationOnMock -> {
        long nodeId = (long) invocationOnMock.getArguments()[1];
        when(txState.augmentSinglePropertyCursor(any(Cursor.class), any(PropertyContainerState.class), eq(propertyKey))).thenReturn(asPropertyCursor(intProperty(propertyKey, inRange)));
        return asNodeCursor(nodeId, nodeId + 20000);
    });
    IndexReader indexReader = addMockedIndexReader(storageStatement);
    IndexQuery.NumberRangePredicate indexQuery = IndexQuery.range(index.schema().getPropertyId(), lower, true, upper, false);
    when(indexReader.query(indexQuery)).thenReturn(PrimitiveLongCollections.resourceIterator(PrimitiveLongCollections.iterator(43L, 44L, 46L), null));
    when(storageStatement.acquireSingleNodeCursor(anyLong())).thenAnswer(invocationOnMock -> {
        long nodeId = (long) invocationOnMock.getArguments()[0];
        when(storeReadLayer.nodeGetProperty(eq(storageStatement), any(NodeItem.class), eq(propertyKey))).thenReturn(asPropertyCursor(intProperty(propertyKey, inRange)));
        return asNodeCursor(nodeId, nodeId + 20000);
    });
    StateHandlingStatementOperations context = newTxStateOps(storeReadLayer);
    // When
    PrimitiveLongIterator results = context.indexQuery(statement, index, indexQuery);
    // Then
    assertEquals(asSet(42L, 43L), PrimitiveLongCollections.toSet(results));
}
Also used : TransactionState(org.neo4j.kernel.api.txstate.TransactionState) IndexQuery(org.neo4j.kernel.api.schema_new.IndexQuery) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement) StorageStatement(org.neo4j.storageengine.api.StorageStatement) StoreReadLayer(org.neo4j.storageengine.api.StoreReadLayer) Cursor(org.neo4j.cursor.Cursor) StubCursors.asNodeCursor(org.neo4j.kernel.impl.api.state.StubCursors.asNodeCursor) StubCursors.asPropertyCursor(org.neo4j.kernel.impl.api.state.StubCursors.asPropertyCursor) PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) NodeItem(org.neo4j.storageengine.api.NodeItem) PropertyContainerState(org.neo4j.storageengine.api.txstate.PropertyContainerState) IndexReader(org.neo4j.storageengine.api.schema.IndexReader) StateHandlingStatementOperations(org.neo4j.kernel.impl.api.StateHandlingStatementOperations) Test(org.junit.Test)

Example 8 with IndexQuery

use of org.neo4j.kernel.api.schema_new.IndexQuery in project neo4j by neo4j.

the class SimpleIndexReader method query.

@Override
public PrimitiveLongIterator query(IndexQuery... predicates) throws IndexNotApplicableKernelException {
    IndexQuery predicate = predicates[0];
    switch(predicate.type()) {
        case exact:
            Object[] values = new Object[predicates.length];
            for (int i = 0; i < predicates.length; i++) {
                assert predicates[i].type() == exact : "Exact followed by another query predicate type is not supported at this moment.";
                values[i] = ((IndexQuery.ExactPredicate) predicates[i]).value();
            }
            return seek(values);
        case exists:
            for (IndexQuery p : predicates) {
                if (p.type() != IndexQueryType.exists) {
                    throw new IndexNotApplicableKernelException("Exists followed by another query predicate type is not supported.");
                }
            }
            return scan();
        case rangeNumeric:
            assertNotComposite(predicates);
            IndexQuery.NumberRangePredicate np = (IndexQuery.NumberRangePredicate) predicate;
            return rangeSeekByNumberInclusive(np.from(), np.to());
        case rangeString:
            assertNotComposite(predicates);
            IndexQuery.StringRangePredicate sp = (IndexQuery.StringRangePredicate) predicate;
            return rangeSeekByString(sp.from(), sp.fromInclusive(), sp.to(), sp.toInclusive());
        case stringPrefix:
            assertNotComposite(predicates);
            IndexQuery.StringPrefixPredicate spp = (IndexQuery.StringPrefixPredicate) predicate;
            return rangeSeekByPrefix(spp.prefix());
        case stringContains:
            assertNotComposite(predicates);
            IndexQuery.StringContainsPredicate scp = (IndexQuery.StringContainsPredicate) predicate;
            return containsString(scp.contains());
        case stringSuffix:
            assertNotComposite(predicates);
            IndexQuery.StringSuffixPredicate ssp = (IndexQuery.StringSuffixPredicate) predicate;
            return endsWith(ssp.suffix());
        default:
            // todo figure out a more specific exception
            throw new RuntimeException("Index query not supported: " + Arrays.toString(predicates));
    }
}
Also used : IndexQuery(org.neo4j.kernel.api.schema_new.IndexQuery) IndexNotApplicableKernelException(org.neo4j.kernel.api.exceptions.index.IndexNotApplicableKernelException)

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