Search in sources :

Example 31 with TransactionState

use of org.neo4j.kernel.api.txstate.TransactionState in project neo4j by neo4j.

the class StateHandlingStatementOperationsTest method shouldConsiderTransactionStateDuringIndexSeekWithIndexQuery.

@Test
public void shouldConsiderTransactionStateDuringIndexSeekWithIndexQuery() 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.indexUpdatesForSeek(index, OrderedPropertyValues.ofUndefined("value"))).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.ExactPredicate query = IndexQuery.exact(index.schema().getPropertyId(), "value");
    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 32 with TransactionState

use of org.neo4j.kernel.api.txstate.TransactionState 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 33 with TransactionState

use of org.neo4j.kernel.api.txstate.TransactionState 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 34 with TransactionState

use of org.neo4j.kernel.api.txstate.TransactionState in project ongdb by graphfoundation.

the class DefaultRelationshipTraversalCursor method next.

@Override
public boolean next() {
    boolean hasChanges;
    TransactionState txs;
    if (filterState == FilterState.NOT_INITIALIZED) {
        // Initialize filtering:
        // - Read first record
        // - Remember type and direction
        // - Return first relationship if it's not deleted
        // Subsequent relationships need to have same type and direction
        // <- will setup filter state if needed
        hasChanges = hasChanges();
        txs = hasChanges ? read.txState() : null;
        if (filterState == FilterState.NOT_INITIALIZED && filterStore) {
            read.relationshipFull(this, next, pageCursor);
            setupFilterState();
        }
        if (filterState != FilterState.NOT_INITIALIZED && (!hasChanges || !txs.relationshipIsDeletedInThisTx(getId()))) {
            return true;
        }
    } else {
        hasChanges = hasChanges();
        txs = hasChanges ? read.txState() : null;
    }
    // tx-state relationships
    if (hasChanges && addedRelationships.hasNext()) {
        loadFromTxState(addedRelationships.next());
        return true;
    }
    if (hasBufferedData()) {
        // We have buffered data, iterate the chain of buffered records
        return nextBuffered(txs);
    }
    do {
        if (traversingDenseNode()) {
            traverseDenseNode();
        }
        if (next == NO_ID) {
            reset();
            return false;
        }
        read.relationshipFull(this, next, pageCursor);
        computeNext();
    } while ((filterStore && !correctTypeAndDirection()) || (hasChanges && txs.relationshipIsDeletedInThisTx(getId())));
    return true;
}
Also used : TransactionState(org.neo4j.kernel.api.txstate.TransactionState)

Example 35 with TransactionState

use of org.neo4j.kernel.api.txstate.TransactionState in project ongdb by graphfoundation.

the class ConstraintIndexCreatorTest method shouldCreateConstraintIndexForSpecifiedProvider.

@Test
public void shouldCreateConstraintIndexForSpecifiedProvider() throws Exception {
    // given
    IndexingService indexingService = mock(IndexingService.class);
    StubKernel kernel = new StubKernel();
    long constraintIndexId = 111;
    when(schemaRead.indexGetCommittedId(indexReference)).thenReturn(constraintIndexId);
    IndexProxy indexProxy = mock(IndexProxy.class);
    when(indexingService.getIndexProxy(constraintIndexId)).thenReturn(indexProxy);
    when(indexingService.getIndexProxy(descriptor)).thenReturn(indexProxy);
    PropertyAccessor propertyAccessor = mock(PropertyAccessor.class);
    ConstraintIndexCreator creator = new ConstraintIndexCreator(() -> kernel, indexingService, propertyAccessor, logProvider);
    IndexProvider.Descriptor providerDescriptor = new IndexProvider.Descriptor("Groovy", "1.2");
    // when
    KernelTransactionImplementation transaction = createTransaction();
    creator.createUniquenessConstraintIndex(transaction, descriptor, providerDescriptor);
    // then
    assertEquals(1, kernel.transactions.size());
    TransactionState transactionState = kernel.transactions.get(0).txState();
    verify(transactionState).indexRuleDoAdd(SchemaIndexDescriptorFactory.uniqueForSchema(descriptor), providerDescriptor);
    verify(schemaRead).index(LABEL_ID, PROPERTY_KEY_ID);
    verify(schemaRead).indexGetCommittedId(any(IndexReference.class));
    verifyNoMoreInteractions(schemaRead);
}
Also used : IndexProvider(org.neo4j.kernel.api.index.IndexProvider) TransactionState(org.neo4j.kernel.api.txstate.TransactionState) ConstraintIndexCreator(org.neo4j.kernel.impl.api.state.ConstraintIndexCreator) PropertyAccessor(org.neo4j.kernel.api.index.PropertyAccessor) IndexingService(org.neo4j.kernel.impl.api.index.IndexingService) KernelTransactionImplementation(org.neo4j.kernel.impl.api.KernelTransactionImplementation) LabelSchemaDescriptor(org.neo4j.internal.kernel.api.schema.LabelSchemaDescriptor) SchemaIndexDescriptor(org.neo4j.kernel.api.schema.index.SchemaIndexDescriptor) IndexProxy(org.neo4j.kernel.impl.api.index.IndexProxy) IndexReference(org.neo4j.internal.kernel.api.IndexReference) DefaultCapableIndexReference(org.neo4j.kernel.impl.api.store.DefaultCapableIndexReference) CapableIndexReference(org.neo4j.internal.kernel.api.CapableIndexReference) Test(org.junit.Test)

Aggregations

TransactionState (org.neo4j.kernel.api.txstate.TransactionState)55 Test (org.junit.Test)14 StateHandlingStatementOperations (org.neo4j.kernel.impl.api.StateHandlingStatementOperations)13 KernelStatement (org.neo4j.kernel.impl.api.KernelStatement)12 StoreReadLayer (org.neo4j.storageengine.api.StoreReadLayer)10 IndexReader (org.neo4j.storageengine.api.schema.IndexReader)9 PrimitiveLongIterator (org.neo4j.collection.primitive.PrimitiveLongIterator)8 IndexQuery (org.neo4j.kernel.api.schema_new.IndexQuery)8 KernelTransactionImplementation (org.neo4j.kernel.impl.api.KernelTransactionImplementation)7 AddedAndRemoved (org.neo4j.kernel.impl.newapi.TxStateIndexChanges.AddedAndRemoved)5 AddedWithValuesAndRemoved (org.neo4j.kernel.impl.newapi.TxStateIndexChanges.AddedWithValuesAndRemoved)5 PrimitiveLongReadableDiffSets (org.neo4j.storageengine.api.txstate.PrimitiveLongReadableDiffSets)5 CreateConstraintFailureException (org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException)4 UniquenessConstraintDescriptor (org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor)4 StorageEngine (org.neo4j.storageengine.api.StorageEngine)4 ConstraintValidationException (org.neo4j.internal.kernel.api.exceptions.schema.ConstraintValidationException)3 CursorContext (org.neo4j.io.pagecache.context.CursorContext)3 SchemaIndexDescriptor (org.neo4j.kernel.api.schema.index.SchemaIndexDescriptor)3 ConstraintDescriptor (org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor)3 StorageStatement (org.neo4j.storageengine.api.StorageStatement)3