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