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));
}
}
use of org.neo4j.graphdb.event.TransactionData in project blueprints by tinkerpop.
the class Neo4j2GraphSpecificTestSuite method testRollbackExceptionOnBeforeTxCommit.
public void testRollbackExceptionOnBeforeTxCommit() throws Exception {
Neo4j2Graph graph = (Neo4j2Graph) graphTest.generateGraph();
GraphDatabaseService rawGraph = graph.getRawGraph();
rawGraph.registerTransactionEventHandler(new TransactionEventHandler<Object>() {
@Override
public Object beforeCommit(TransactionData data) throws Exception {
if (true) {
throw new RuntimeException("jippo validation exception");
}
//To change body of implemented methods use File | Settings | File Templates.
return null;
}
@Override
public void afterCommit(TransactionData data, Object state) {
//To change body of implemented methods use File | Settings | File Templates.
}
@Override
public void afterRollback(TransactionData data, Object state) {
//To change body of implemented methods use File | Settings | File Templates.
}
});
try {
Vertex vertex = graph.addVertex(null);
graph.commit();
} catch (Exception e) {
graph.rollback();
}
assertTrue(!graph.getVertices().iterator().hasNext());
graph.shutdown();
}
use of org.neo4j.graphdb.event.TransactionData in project neo4j by neo4j.
the class TransactionEventHandlers method beforeCommit.
@Override
public TransactionHandlerState beforeCommit(ReadableTransactionState state, KernelTransaction transaction, StoreReadLayer storeReadLayer, StorageStatement statement) {
if (transactionEventHandlers.isEmpty()) {
return null;
}
TransactionData txData = state == null ? EMPTY_DATA : new TxStateTransactionDataSnapshot(state, nodeActions, relationshipActions, storeReadLayer, statement, transaction);
TransactionHandlerState handlerStates = new TransactionHandlerState(txData);
for (TransactionEventHandler<?> handler : this.transactionEventHandlers) {
try {
handlerStates.add(handler, handler.beforeCommit(txData));
} catch (Throwable t) {
handlerStates.failed(t);
}
}
return handlerStates;
}
use of org.neo4j.graphdb.event.TransactionData in project neo4j by neo4j.
the class TestTransactionEvents method nodeCanBecomeSchemaIndexableInBeforeCommitByAddingProperty.
@Test
public void nodeCanBecomeSchemaIndexableInBeforeCommitByAddingProperty() throws Exception {
// Given we have a schema index...
GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
Label label = label("Label");
IndexDefinition index;
try (Transaction tx = db.beginTx()) {
index = db.schema().indexFor(label).on("indexed").create();
tx.success();
}
// ... and a transaction event handler that likes to add the indexed property on nodes
db.registerTransactionEventHandler(new TransactionEventHandler.Adapter<Object>() {
@Override
public Object beforeCommit(TransactionData data) throws Exception {
Iterator<Node> nodes = data.createdNodes().iterator();
if (nodes.hasNext()) {
Node node = nodes.next();
node.setProperty("indexed", "value");
}
return null;
}
});
// When we create a node with the right label, but not the right property...
try (Transaction tx = db.beginTx()) {
db.schema().awaitIndexesOnline(10, TimeUnit.SECONDS);
Node node = db.createNode(label);
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 = db.findNode(label, "indexed", "value");
assertThat(node.getProperty("random"), is((Object) 42));
}
}
use of org.neo4j.graphdb.event.TransactionData in project neo4j by neo4j.
the class TestTransactionEvents method nodeCanBecomeSchemaIndexableInBeforeCommitByAddingLabel.
@Test
public void nodeCanBecomeSchemaIndexableInBeforeCommitByAddingLabel() throws Exception {
// Given we have a schema index...
GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
final Label label = label("Label");
IndexDefinition index;
try (Transaction tx = db.beginTx()) {
index = db.schema().indexFor(label).on("indexed").create();
tx.success();
}
// ... and a transaction event handler that likes to add the indexed property on nodes
db.registerTransactionEventHandler(new TransactionEventHandler.Adapter<Object>() {
@Override
public Object beforeCommit(TransactionData data) throws Exception {
Iterator<Node> nodes = data.createdNodes().iterator();
if (nodes.hasNext()) {
Node node = nodes.next();
node.addLabel(label);
}
return null;
}
});
// When we create a node with the right property, but not the right label...
try (Transaction tx = db.beginTx()) {
db.schema().awaitIndexesOnline(10, TimeUnit.SECONDS);
Node node = db.createNode();
node.setProperty("indexed", "value");
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 = db.findNode(label, "indexed", "value");
assertThat(node.getProperty("random"), is((Object) 42));
}
}
Aggregations