Search in sources :

Example 16 with Transaction

use of org.neo4j.graphdb.Transaction in project neo4j by neo4j.

the class CompositeIndexingIT method shouldSeeNodeAddedToByLabelIndexInTransaction.

@Test
public void shouldSeeNodeAddedToByLabelIndexInTransaction() throws Exception {
    try (Transaction ignore = graphDatabaseAPI.beginTx()) {
        DataWriteOperations writeOperations = statement().dataWriteOperations();
        long nodeID = writeOperations.nodeCreate();
        for (int propID : index.schema().getPropertyIds()) {
            writeOperations.nodeSetProperty(nodeID, DefinedProperty.intProperty(propID, propID));
        }
        writeOperations.nodeAddLabel(nodeID, LABEL_ID);
        PrimitiveLongIterator resultIterator = seek();
        assertThat(resultIterator.next(), equalTo(nodeID));
        assertFalse(resultIterator.hasNext());
    }
}
Also used : PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) Transaction(org.neo4j.graphdb.Transaction) Test(org.junit.Test)

Example 17 with Transaction

use of org.neo4j.graphdb.Transaction in project neo4j by neo4j.

the class CompositeIndexingIT method shouldNotSeeNodeThatHasAPropertyRemovedInTransaction.

@Test
public void shouldNotSeeNodeThatHasAPropertyRemovedInTransaction() throws Exception {
    long nodeID = createNode();
    try (Transaction ignore = graphDatabaseAPI.beginTx()) {
        statement().dataWriteOperations().nodeRemoveProperty(nodeID, index.schema().getPropertyIds()[0]);
        assertFalse(seek().hasNext());
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Test(org.junit.Test)

Example 18 with Transaction

use of org.neo4j.graphdb.Transaction in project neo4j by neo4j.

the class UniqueFactoryTest method shouldCreateNodeAndIndexItIfMissing.

@Test
public void shouldCreateNodeAndIndexItIfMissing() {
    // given
    GraphDatabaseService graphdb = mock(GraphDatabaseService.class);
    @SuppressWarnings("unchecked") Index<Node> index = mock(Index.class);
    Transaction tx = mock(Transaction.class);
    when(graphdb.beginTx()).thenReturn(tx);
    when(index.getGraphDatabase()).thenReturn(graphdb);
    @SuppressWarnings("unchecked") IndexHits<Node> indexHits = mock(IndexHits.class);
    when(index.get("key1", "value1")).thenReturn(indexHits);
    Node indexedNode = mock(Node.class);
    when(graphdb.createNode()).thenReturn(indexedNode);
    final AtomicBoolean initializeCalled = new AtomicBoolean(false);
    UniqueFactory.UniqueNodeFactory unique = new UniqueFactory.UniqueNodeFactory(index) {

        @Override
        protected void initialize(Node created, Map<String, Object> properties) {
            initializeCalled.set(true);
            assertEquals(Collections.singletonMap("key1", "value1"), properties);
        }
    };
    // when
    Node node = unique.getOrCreate("key1", "value1");
    // then
    assertSame(node, indexedNode);
    verify(index).get("key1", "value1");
    verify(index).putIfAbsent(indexedNode, "key1", "value1");
    verify(graphdb, times(1)).createNode();
    verify(tx).success();
    assertTrue("Node not initialized", initializeCalled.get());
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Map(java.util.Map) Test(org.junit.Test)

Example 19 with Transaction

use of org.neo4j.graphdb.Transaction in project neo4j by neo4j.

the class UniqueFactoryTest method shouldCreateNodeWithOutcomeAndIndexItIfMissing.

@Test
public void shouldCreateNodeWithOutcomeAndIndexItIfMissing() {
    // given
    GraphDatabaseService graphdb = mock(GraphDatabaseService.class);
    @SuppressWarnings("unchecked") Index<Node> index = mock(Index.class);
    Transaction tx = mock(Transaction.class);
    when(graphdb.beginTx()).thenReturn(tx);
    when(index.getGraphDatabase()).thenReturn(graphdb);
    @SuppressWarnings("unchecked") IndexHits<Node> indexHits = mock(IndexHits.class);
    when(index.get("key1", "value1")).thenReturn(indexHits);
    Node indexedNode = mock(Node.class);
    when(graphdb.createNode()).thenReturn(indexedNode);
    final AtomicBoolean initializeCalled = new AtomicBoolean(false);
    UniqueFactory.UniqueNodeFactory unique = new UniqueFactory.UniqueNodeFactory(index) {

        @Override
        protected void initialize(Node created, Map<String, Object> properties) {
            initializeCalled.set(true);
            assertEquals(Collections.singletonMap("key1", "value1"), properties);
        }
    };
    // when
    UniqueEntity<Node> node = unique.getOrCreateWithOutcome("key1", "value1");
    // then
    assertSame(node.entity(), indexedNode);
    assertTrue(node.wasCreated());
    verify(index).get("key1", "value1");
    verify(index).putIfAbsent(indexedNode, "key1", "value1");
    verify(graphdb, times(1)).createNode();
    verify(tx).success();
    assertTrue("Node not initialized", initializeCalled.get());
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Map(java.util.Map) Test(org.junit.Test)

Example 20 with Transaction

use of org.neo4j.graphdb.Transaction in project neo4j by neo4j.

the class TestTransactionEvents method makeSureHandlerIsntCalledWhenTxRolledBack.

@Test
public void makeSureHandlerIsntCalledWhenTxRolledBack() {
    DummyTransactionEventHandler<Integer> handler = new DummyTransactionEventHandler<>(10);
    GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    db.registerTransactionEventHandler(handler);
    try {
        try (Transaction ignore = db.beginTx()) {
            db.createNode().delete();
        }
        assertNull(handler.beforeCommit);
        assertNull(handler.afterCommit);
        assertNull(handler.afterRollback);
    } finally {
        db.unregisterTransactionEventHandler(handler);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Test(org.junit.Test)

Aggregations

Transaction (org.neo4j.graphdb.Transaction)2409 Node (org.neo4j.graphdb.Node)1086 Test (org.junit.jupiter.api.Test)751 Test (org.junit.Test)607 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)352 Relationship (org.neo4j.graphdb.Relationship)307 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)302 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)241 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)177 Label (org.neo4j.graphdb.Label)154 Result (org.neo4j.graphdb.Result)142 HashMap (java.util.HashMap)105 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)104 MethodSource (org.junit.jupiter.params.provider.MethodSource)103 IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)86 DatabaseManagementService (org.neo4j.dbms.api.DatabaseManagementService)77 File (java.io.File)74 ArrayList (java.util.ArrayList)73 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)67 Path (java.nio.file.Path)64