Search in sources :

Example 11 with DataWriteOperations

use of org.neo4j.kernel.api.DataWriteOperations in project neo4j by neo4j.

the class TransactionHookIT method shouldRollbackOnFailureInBeforeCommit.

@Test
public void shouldRollbackOnFailureInBeforeCommit() throws Exception {
    // Given
    TransactionHook hook = mock(TransactionHook.class);
    final String message = "Original";
    when(hook.beforeCommit(any(ReadableTransactionState.class), any(KernelTransaction.class), any(StoreReadLayer.class), any(StorageStatement.class))).thenReturn(new TransactionHook.Outcome() {

        @Override
        public boolean isSuccessful() {
            return false;
        }

        @Override
        public Throwable failure() {
            return new Throwable(message);
        }
    });
    kernel.registerTransactionHook(hook);
    // When
    DataWriteOperations ops = dataWriteOperationsInNewTransaction();
    ops.nodeCreate();
    try {
        commit();
        fail("Expected this to fail.");
    } catch (org.neo4j.kernel.api.exceptions.TransactionFailureException e) {
        assertThat(e.getCause().getMessage(), equalTo("Transaction handler failed."));
        assertThat(e.getCause().getCause().getMessage(), equalTo(message));
    }
    // Then
    verify(hook).beforeCommit(any(ReadableTransactionState.class), any(KernelTransaction.class), any(StoreReadLayer.class), any(StorageStatement.class));
    verify(hook).afterRollback(any(ReadableTransactionState.class), any(KernelTransaction.class), any(TransactionHook.Outcome.class));
    verifyNoMoreInteractions(hook);
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) StorageStatement(org.neo4j.storageengine.api.StorageStatement) StoreReadLayer(org.neo4j.storageengine.api.StoreReadLayer) DataWriteOperations(org.neo4j.kernel.api.DataWriteOperations) ReadableTransactionState(org.neo4j.storageengine.api.txstate.ReadableTransactionState) TransactionHook(org.neo4j.kernel.api.TransactionHook) Test(org.junit.Test)

Example 12 with DataWriteOperations

use of org.neo4j.kernel.api.DataWriteOperations in project neo4j by neo4j.

the class LabelIT method addingAndRemovingLabelInSameTxShouldHaveNoEffect.

@Test
public void addingAndRemovingLabelInSameTxShouldHaveNoEffect() throws Exception {
    // Given a node with a label
    Statement statement = statementInNewTransaction(AnonymousContext.writeToken());
    int label = statement.tokenWriteOperations().labelGetOrCreateForName("Label 1");
    long node = statement.dataWriteOperations().nodeCreate();
    statement.dataWriteOperations().nodeAddLabel(node, label);
    commit();
    // When I add and remove that label in the same tx
    DataWriteOperations dataWriteOperations = dataWriteOperationsInNewTransaction();
    dataWriteOperations.nodeRemoveLabel(node, label);
    dataWriteOperations.nodeAddLabel(node, label);
    // Then commit should not throw exceptions
    commit();
    // And then the node should have the label
    assertTrue(readOperationsInNewTransaction().nodeHasLabel(node, label));
}
Also used : DataWriteOperations(org.neo4j.kernel.api.DataWriteOperations) Statement(org.neo4j.kernel.api.Statement) Test(org.junit.Test)

Example 13 with DataWriteOperations

use of org.neo4j.kernel.api.DataWriteOperations in project neo4j by neo4j.

the class NodeGetUniqueFromIndexSeekIT method shouldBlockUniqueIndexSeekFromCompetingTransaction.

@Test(timeout = 1000)
public void shouldBlockUniqueIndexSeekFromCompetingTransaction() throws Exception {
    // This is the interleaving that we are trying to verify works correctly:
    // ----------------------------------------------------------------------
    // Thread1 (main)        : Thread2
    // create unique node    :
    // lookup(node)          :
    // open start latch ----->
    //    |                  : lookup(node)
    // wait for T2 to block  :      |
    //                       :    *block*
    // commit --------------->   *unblock*
    // wait for T2 end latch :      |
    //                       : finish transaction
    //                       : open end latch
    // *unblock* <-------------‘
    // assert that we complete before timeout
    final DoubleLatch latch = new DoubleLatch();
    final NewIndexDescriptor index = createUniquenessConstraint(labelId, propertyId1);
    final String value = "value";
    DataWriteOperations dataStatement = dataWriteOperationsInNewTransaction();
    long nodeId = dataStatement.nodeCreate();
    dataStatement.nodeAddLabel(nodeId, labelId);
    // This adds the node to the unique index and should take an index write lock
    dataStatement.nodeSetProperty(nodeId, Property.stringProperty(propertyId1, value));
    Runnable runnableForThread2 = () -> {
        latch.waitForAllToStart();
        try (Transaction tx = db.beginTx()) {
            try (Statement statement1 = statementContextSupplier.get()) {
                statement1.readOperations().nodeGetFromUniqueIndexSeek(index, exact(propertyId1, value));
            }
            tx.success();
        } catch (IndexNotFoundKernelException | IndexNotApplicableKernelException | IndexBrokenKernelException e) {
            throw new RuntimeException(e);
        } finally {
            latch.finish();
        }
    };
    Thread thread2 = new Thread(runnableForThread2, "Transaction Thread 2");
    thread2.start();
    latch.startAndWaitForAllToStart();
    //noinspection UnusedLabel
    spinUntilBlocking: for (; ; ) {
        if (thread2.getState() == Thread.State.TIMED_WAITING || thread2.getState() == Thread.State.WAITING) {
            break;
        }
        Thread.yield();
    }
    commit();
    latch.waitForAllToFinish();
}
Also used : NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) Transaction(org.neo4j.graphdb.Transaction) DataWriteOperations(org.neo4j.kernel.api.DataWriteOperations) Statement(org.neo4j.kernel.api.Statement) DoubleLatch(org.neo4j.test.DoubleLatch) Test(org.junit.Test)

Example 14 with DataWriteOperations

use of org.neo4j.kernel.api.DataWriteOperations in project neo4j by neo4j.

the class NodeGetUniqueFromIndexSeekIT method createNodeWithValue.

private long createNodeWithValue(String value) throws KernelException {
    DataWriteOperations dataStatement = dataWriteOperationsInNewTransaction();
    long nodeId = dataStatement.nodeCreate();
    dataStatement.nodeAddLabel(nodeId, labelId);
    dataStatement.nodeSetProperty(nodeId, Property.stringProperty(propertyId1, value));
    commit();
    return nodeId;
}
Also used : DataWriteOperations(org.neo4j.kernel.api.DataWriteOperations)

Example 15 with DataWriteOperations

use of org.neo4j.kernel.api.DataWriteOperations in project neo4j by neo4j.

the class PropertyIT method shouldBeAbleToRemoveResetAndTwiceRemovePropertyOnRelationship.

@Test
public void shouldBeAbleToRemoveResetAndTwiceRemovePropertyOnRelationship() throws Exception {
    // given
    Statement statement = statementInNewTransaction(AnonymousContext.writeToken());
    int prop = statement.tokenWriteOperations().propertyKeyGetOrCreateForName("foo");
    int type = statement.tokenWriteOperations().relationshipTypeGetOrCreateForName("RELATED");
    long startNodeId = statement.dataWriteOperations().nodeCreate();
    long endNodeId = statement.dataWriteOperations().nodeCreate();
    long rel = statement.dataWriteOperations().relationshipCreate(type, startNodeId, endNodeId);
    statement.dataWriteOperations().relationshipSetProperty(rel, property(prop, "bar"));
    commit();
    // when
    DataWriteOperations dataWriteOperations = dataWriteOperationsInNewTransaction();
    dataWriteOperations.relationshipRemoveProperty(rel, prop);
    dataWriteOperations.relationshipSetProperty(rel, property(prop, "bar"));
    dataWriteOperations.relationshipRemoveProperty(rel, prop);
    dataWriteOperations.relationshipRemoveProperty(rel, prop);
    commit();
    // then
    ReadOperations readOperations = readOperationsInNewTransaction();
    assertThat(readOperations.relationshipGetProperty(rel, prop), nullValue());
}
Also used : ReadOperations(org.neo4j.kernel.api.ReadOperations) DataWriteOperations(org.neo4j.kernel.api.DataWriteOperations) Statement(org.neo4j.kernel.api.Statement) Test(org.junit.Test)

Aggregations

DataWriteOperations (org.neo4j.kernel.api.DataWriteOperations)17 Test (org.junit.Test)11 Statement (org.neo4j.kernel.api.Statement)6 ReadOperations (org.neo4j.kernel.api.ReadOperations)4 DefinedProperty (org.neo4j.kernel.api.properties.DefinedProperty)4 PropertyItem (org.neo4j.storageengine.api.PropertyItem)4 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)2 TransactionHook (org.neo4j.kernel.api.TransactionHook)2 Property (org.neo4j.kernel.api.properties.Property)2 NodeItem (org.neo4j.storageengine.api.NodeItem)2 RelationshipItem (org.neo4j.storageengine.api.RelationshipItem)2 StorageProperty (org.neo4j.storageengine.api.StorageProperty)2 StorageStatement (org.neo4j.storageengine.api.StorageStatement)2 StoreReadLayer (org.neo4j.storageengine.api.StoreReadLayer)2 ReadableTransactionState (org.neo4j.storageengine.api.txstate.ReadableTransactionState)2 Transaction (org.neo4j.graphdb.Transaction)1 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)1 DoubleLatch (org.neo4j.test.DoubleLatch)1