Search in sources :

Example 6 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)

Example 7 with TransactionData

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();
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Vertex(com.tinkerpop.blueprints.Vertex) TransactionData(org.neo4j.graphdb.event.TransactionData) NotInTransactionException(org.neo4j.graphdb.NotInTransactionException)

Example 8 with TransactionData

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;
}
Also used : TxStateTransactionDataSnapshot(org.neo4j.kernel.impl.coreapi.TxStateTransactionDataSnapshot) TransactionData(org.neo4j.graphdb.event.TransactionData)

Example 9 with TransactionData

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));
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) Transaction(org.neo4j.graphdb.Transaction) TransactionEventHandler(org.neo4j.graphdb.event.TransactionEventHandler) Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) Iterator(java.util.Iterator) TransactionData(org.neo4j.graphdb.event.TransactionData) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Test(org.junit.Test)

Example 10 with TransactionData

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));
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) Transaction(org.neo4j.graphdb.Transaction) TransactionEventHandler(org.neo4j.graphdb.event.TransactionEventHandler) Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) Iterator(java.util.Iterator) TransactionData(org.neo4j.graphdb.event.TransactionData) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) 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