How to mutate all elements in a Neo4j chart

Read the following code, which adds a property for each node on an arbitrarily large chart. An iterator is created inside a transaction. During iteration, the transaction it was created is completed and a new one is created (to limit the memory used for the writes made). From now on, we continue to remove nodes from this iterator, but from a different transaction. This is normal? Are these methods meant to work without a transaction after they are called?

The JavaDoc contains a cryptic warning: Please take care that the returned one is ResourceIterable

closed correctly and as soon as possible within your transaction to avoid potential blocking of write operations. Obviously it ResourceIterable

cannot be closed before we finish iterating, so what is this warning trying to convey?

Transaction tx = database.beginTx();
try {ResourceIterable<Node> nodes = GlobalGraphOperations.at(database).getAllNodesWithLabel(label);
    long i = 0L;
    for (Node node : nodes) {
        node.setProperty("property", "update");
        i++;
        if (i % commitInterval == 0) {
            tx.success();
            tx.close();
            tx = database.beginTx();
        }
    }
    tx.success();
}
finally {
    tx.close();
}

      

+3


source to share


2 answers


The warning message is trying to say that if you do a lock like this, other things in the database may not be able to modify the database at the same time. Since you are doing this globally, this can be problematic for you.

Fortunately, it looks like what you are trying to do could be accomplished much easier using one cypher command, then java and this method. The request will look like this:

 MATCH n SET n.property="update";

      



You can consult the docs on how to run this cypher request from java.

Also, I think it GlobalGraphOperations

might be outdated in general , so you might want to look into other ways to mutate the entire graph (like what I provide here) rather than using this java class.

+1


source


You shouldn't be pulling nodes from the same iterator instance, but you can keep fetching them from another, you will see some nodes with updates and some not depending on how far your update went.



Don't use an iterator from another tx somewhere else.

+1


source







All Articles