Search in sources :

Example 11 with TransactionData

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

the class TestTransactionEvents method shouldProvideTheCorrectRelationshipData.

@Test
public void shouldProvideTheCorrectRelationshipData() {
    GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    // create a rel type so the next type id is non zero
    try (Transaction tx = db.beginTx()) {
        db.createNode().createRelationshipTo(db.createNode(), withName("TYPE"));
    }
    RelationshipType livesIn = withName("LIVES_IN");
    long relId;
    try (Transaction tx = db.beginTx()) {
        Node person = db.createNode(label("Person"));
        Node city = db.createNode(label("City"));
        Relationship rel = person.createRelationshipTo(city, livesIn);
        rel.setProperty("since", 2009);
        relId = rel.getId();
        tx.success();
    }
    final Set<String> changedRelationships = new HashSet<>();
    db.registerTransactionEventHandler(new TransactionEventHandler.Adapter<Void>() {

        @Override
        public Void beforeCommit(TransactionData data) throws Exception {
            for (PropertyEntry<Relationship> entry : data.assignedRelationshipProperties()) {
                changedRelationships.add(entry.entity().getType().name());
            }
            return null;
        }
    });
    try (Transaction tx = db.beginTx()) {
        Relationship rel = db.getRelationshipById(relId);
        rel.setProperty("since", 2010);
        tx.success();
    }
    assertEquals(1, changedRelationships.size());
    assertTrue(livesIn + " not in " + changedRelationships.toString(), changedRelationships.contains(livesIn.name()));
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Node(org.neo4j.graphdb.Node) RelationshipType(org.neo4j.graphdb.RelationshipType) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(org.neo4j.graphdb.Transaction) TransactionEventHandler(org.neo4j.graphdb.event.TransactionEventHandler) PropertyEntry(org.neo4j.graphdb.event.PropertyEntry) Relationship(org.neo4j.graphdb.Relationship) TransactionData(org.neo4j.graphdb.event.TransactionData) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 12 with TransactionData

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

the class TestTransactionEvents method shouldAccessRelationshipDataInAfterCommit.

@Test
public void shouldAccessRelationshipDataInAfterCommit() throws Exception {
    // GIVEN
    final GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    final AtomicInteger accessCount = new AtomicInteger();
    final Map<Long, RelationshipData> expectedRelationshipData = new HashMap<>();
    TransactionEventHandler<Void> handler = new TransactionEventHandler.Adapter<Void>() {

        @Override
        public void afterCommit(TransactionData data, Void state) {
            accessCount.set(0);
            try (Transaction tx = db.beginTx()) {
                for (Relationship relationship : data.createdRelationships()) {
                    accessData(relationship);
                }
                for (PropertyEntry<Relationship> change : data.assignedRelationshipProperties()) {
                    accessData(change.entity());
                }
                for (PropertyEntry<Relationship> change : data.removedRelationshipProperties()) {
                    accessData(change.entity());
                }
                tx.success();
            }
        }

        private void accessData(Relationship relationship) {
            accessCount.incrementAndGet();
            RelationshipData expectancy = expectedRelationshipData.get(relationship.getId());
            assertNotNull(expectancy);
            assertEquals(expectancy.startNode, relationship.getStartNode());
            assertEquals(expectancy.type, relationship.getType().name());
            assertEquals(expectancy.endNode, relationship.getEndNode());
        }
    };
    db.registerTransactionEventHandler(handler);
    // WHEN
    try {
        Relationship relationship;
        try (Transaction tx = db.beginTx()) {
            relationship = db.createNode().createRelationshipTo(db.createNode(), MyRelTypes.TEST);
            expectedRelationshipData.put(relationship.getId(), new RelationshipData(relationship));
            tx.success();
        }
        // THEN
        assertEquals(1, accessCount.get());
        // and WHEN
        try (Transaction tx = db.beginTx()) {
            relationship.setProperty("name", "Smith");
            Relationship otherRelationship = db.createNode().createRelationshipTo(db.createNode(), MyRelTypes.TEST2);
            expectedRelationshipData.put(otherRelationship.getId(), new RelationshipData(otherRelationship));
            tx.success();
        }
        // THEN
        assertEquals(2, accessCount.get());
        // and WHEN
        try (Transaction tx = db.beginTx()) {
            relationship.delete();
            tx.success();
        }
        // THEN
        assertEquals(1, accessCount.get());
    } finally {
        db.unregisterTransactionEventHandler(handler);
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) HashMap(java.util.HashMap) Transaction(org.neo4j.graphdb.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Relationship(org.neo4j.graphdb.Relationship) 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