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()));
}
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);
}
}
Aggregations