Search in sources :

Example 1 with TransactionData

use of org.neo4j.graphdb.event.TransactionData in project neo4j by neo4j.

the class TestTransactionEvents method shouldBeAbleToTouchDataOutsideTxDataInAfterCommit.

@Test
public void shouldBeAbleToTouchDataOutsideTxDataInAfterCommit() throws Exception {
    // GIVEN
    final Node node = createNode("one", "Two", "three", "Four");
    dbRule.getGraphDatabaseAPI().registerTransactionEventHandler(new TransactionEventHandler.Adapter<Object>() {

        @Override
        public void afterCommit(TransactionData data, Object nothing) {
            try (Transaction tx = dbRule.beginTx()) {
                for (String key : node.getPropertyKeys()) {
                    // Just to see if one can reach them
                    node.getProperty(key);
                }
                tx.success();
            }
        }
    });
    try (Transaction tx = dbRule.beginTx()) {
        // WHEN/THEN
        dbRule.createNode();
        node.setProperty("five", "Six");
        tx.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) TransactionEventHandler(org.neo4j.graphdb.event.TransactionEventHandler) Node(org.neo4j.graphdb.Node) TransactionData(org.neo4j.graphdb.event.TransactionData) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 2 with TransactionData

use of org.neo4j.graphdb.event.TransactionData in project neo4j by neo4j.

the class TestTransactionEvents method modifiedPropertyCanByFurtherModifiedInBeforeCommit.

@Test
public void modifiedPropertyCanByFurtherModifiedInBeforeCommit() throws Exception {
    // Given
    // -- create node and set property on it in one transaction
    GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    final String key = "key";
    final Object value1 = "the old value";
    final Object value2 = "the new value";
    final Node node;
    try (Transaction tx = db.beginTx()) {
        node = db.createNode();
        node.setProperty(key, "initial value");
        tx.success();
    }
    // -- register a tx handler which will override a property
    TransactionEventHandler<Void> handler = new TransactionEventHandler.Adapter<Void>() {

        @Override
        public Void beforeCommit(TransactionData data) throws Exception {
            Node modifiedNode = data.assignedNodeProperties().iterator().next().entity();
            assertEquals(node, modifiedNode);
            modifiedNode.setProperty(key, value2);
            return null;
        }
    };
    db.registerTransactionEventHandler(handler);
    try (Transaction tx = db.beginTx()) {
        // When
        node.setProperty(key, value1);
        tx.success();
    }
    // Then
    assertThat(node, inTx(db, hasProperty(key).withValue(value2)));
    db.unregisterTransactionEventHandler(handler);
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TransactionData(org.neo4j.graphdb.event.TransactionData) Test(org.junit.Test)

Example 3 with TransactionData

use of org.neo4j.graphdb.event.TransactionData in project neo4j by neo4j.

the class TestTransactionEvents method shouldAllowToStringOnCreatedRelationshipInAfterCommit.

@Test
public void shouldAllowToStringOnCreatedRelationshipInAfterCommit() throws Exception {
    // GIVEN
    Relationship relationship;
    Node startNode, endNode;
    RelationshipType type = MyRelTypes.TEST;
    try (Transaction tx = dbRule.beginTx()) {
        startNode = dbRule.createNode();
        endNode = dbRule.createNode();
        relationship = startNode.createRelationshipTo(endNode, type);
        tx.success();
    }
    // WHEN
    AtomicReference<String> deletedToString = new AtomicReference<>();
    dbRule.registerTransactionEventHandler(new TransactionEventHandler.Adapter<Object>() {

        @Override
        public void afterCommit(TransactionData data, Object state) {
            for (Relationship relationship : data.deletedRelationships()) {
                deletedToString.set(relationship.toString());
            }
        }
    });
    try (Transaction tx = dbRule.beginTx()) {
        relationship.delete();
        tx.success();
    }
    // THEN
    assertNotNull(deletedToString.get());
    assertThat(deletedToString.get(), containsString(type.name()));
    assertThat(deletedToString.get(), containsString(format("(%d)", startNode.getId())));
    assertThat(deletedToString.get(), containsString(format("(%d)", endNode.getId())));
}
Also used : Transaction(org.neo4j.graphdb.Transaction) TransactionEventHandler(org.neo4j.graphdb.event.TransactionEventHandler) Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) RelationshipType(org.neo4j.graphdb.RelationshipType) AtomicReference(java.util.concurrent.atomic.AtomicReference) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TransactionData(org.neo4j.graphdb.event.TransactionData) Test(org.junit.Test)

Example 4 with TransactionData

use of org.neo4j.graphdb.event.TransactionData in project neo4j by neo4j.

the class TestTransactionEvents method shouldNotFireEventForNonDataTransactions.

@Test
public void shouldNotFireEventForNonDataTransactions() throws Exception {
    // GIVEN
    final AtomicInteger counter = new AtomicInteger();
    dbRule.getGraphDatabaseAPI().registerTransactionEventHandler(new TransactionEventHandler.Adapter<Void>() {

        @Override
        public Void beforeCommit(TransactionData data) throws Exception {
            assertTrue("Expected only transactions that had nodes or relationships created", data.createdNodes().iterator().hasNext() || data.createdRelationships().iterator().hasNext());
            counter.incrementAndGet();
            return null;
        }
    });
    Label label = label("Label");
    String key = "key";
    assertEquals(0, counter.get());
    // WHEN creating a label token
    try (Transaction tx = dbRule.beginTx()) {
        dbRule.createNode(label);
        tx.success();
    }
    assertEquals(1, counter.get());
    // ... a property key token
    try (Transaction tx = dbRule.beginTx()) {
        dbRule.createNode().setProperty(key, "value");
        tx.success();
    }
    assertEquals(2, counter.get());
    // ... and a relationship type
    try (Transaction tx = dbRule.beginTx()) {
        dbRule.createNode().createRelationshipTo(dbRule.createNode(), withName("A_TYPE"));
        tx.success();
    }
    assertEquals(3, counter.get());
    // ... also when creating an index
    try (Transaction tx = dbRule.beginTx()) {
        dbRule.schema().indexFor(label).on(key).create();
        tx.success();
    }
    // ... or a constraint
    try (Transaction tx = dbRule.beginTx()) {
        dbRule.schema().constraintFor(label).assertPropertyIsUnique("otherkey").create();
        tx.success();
    }
    // ... or even a legacy index
    try (Transaction tx = dbRule.beginTx()) {
        dbRule.index().forNodes("some index", stringMap(PROVIDER, IDENTIFIER));
        tx.success();
    }
    // THEN only three transaction events (all including graph data) should've been fired
    assertEquals(3, counter.get());
}
Also used : Transaction(org.neo4j.graphdb.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionEventHandler(org.neo4j.graphdb.event.TransactionEventHandler) Label(org.neo4j.graphdb.Label) TransactionData(org.neo4j.graphdb.event.TransactionData) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Test(org.junit.Test)

Example 5 with TransactionData

use of org.neo4j.graphdb.event.TransactionData in project neo4j by neo4j.

the class TestTransactionEventsWithIndexes method nodeCanBeLegacyIndexedInBeforeCommit.

@Test
public void nodeCanBeLegacyIndexedInBeforeCommit() throws Exception {
    // Given we have a legacy index...
    GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    final Index<Node> index;
    try (Transaction tx = db.beginTx()) {
        index = db.index().forNodes("index");
        tx.success();
    }
    // ... and a transaction event handler that likes to add nodes to that index
    db.registerTransactionEventHandler(new TransactionEventHandler<Object>() {

        @Override
        public Object beforeCommit(TransactionData data) throws Exception {
            Iterator<Node> nodes = data.createdNodes().iterator();
            if (nodes.hasNext()) {
                Node node = nodes.next();
                index.add(node, "key", "value");
            }
            return null;
        }

        @Override
        public void afterCommit(TransactionData data, Object state) {
        }

        @Override
        public void afterRollback(TransactionData data, Object state) {
        }
    });
    // When we create a node...
    try (Transaction tx = db.beginTx()) {
        db.schema().awaitIndexesOnline(10, TimeUnit.SECONDS);
        Node node = db.createNode();
        node.setProperty("random", 42);
        tx.success();
    }
    // Then we should be able to look it up through the index.
    try (Transaction ignore = db.beginTx()) {
        Node node = single(index.get("key", "value"));
        assertThat(node.getProperty("random"), is((Object) 42));
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Iterator(java.util.Iterator) TransactionData(org.neo4j.graphdb.event.TransactionData) Test(org.junit.Test)

Aggregations

TransactionData (org.neo4j.graphdb.event.TransactionData)12 Test (org.junit.Test)9 Transaction (org.neo4j.graphdb.Transaction)9 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)8 Node (org.neo4j.graphdb.Node)7 TransactionEventHandler (org.neo4j.graphdb.event.TransactionEventHandler)6 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)5 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)4 Iterator (java.util.Iterator)3 Label (org.neo4j.graphdb.Label)3 Relationship (org.neo4j.graphdb.Relationship)3 Vertex (com.tinkerpop.blueprints.Vertex)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 RelationshipType (org.neo4j.graphdb.RelationshipType)2 IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 NotInTransactionException (org.neo4j.graphdb.NotInTransactionException)1 PropertyEntry (org.neo4j.graphdb.event.PropertyEntry)1