Search code examples
neo4jgraphaware

How to make node changes in a TransactionEventHandler that are returned within the same CREATE query


I am trying to implement a plugin for neo4j to add an autoincrement ID using GraphAware library. To this end, I've written the following classes:

public class ModuleBootstrapper implements RuntimeModuleBootstrapper
{
    public RuntimeModule bootstrapModule(String moduleId, Map<String, String> config, GraphDatabaseService database)
    {
        return new MyModule(moduleId, config, database);
    }
}

And:

public class MyModule extends BaseTxDrivenModule<Void>
{
    int counter = 0;
    public Void beforeCommit(ImprovedTransactionData transactionData)
        throws DeliberateTransactionRollbackException
    {
        if (transactionData.mutationsOccurred()) {
            for (Node newNode : transactionData.getAllCreatedNodes()) {
                newNode.setProperty("id", counter++);
            }
        }
    }
}

And for the testing I can execute:

CREATE (n);

And then:

MATCH (n) RETURN n;

And I can see the effect of my plugin as some id property added to the node. But when I run:

CREATE (n) RETURN n;

The returned node does not have the mentioned id property but again, when I match the node in a separate query, I see the things have worked out just fine. It's just that in the CREATE query, the returned node infos are the ones before my plugin modified them.

The questions are; why is that? Didn't I modify the nodes through my plugin within the transaction? Shouldn't the returned nodes be showing the modifications I've made on them? Is there any way I can make this happen?


Solution

  • While you're still within the transaction, the Cypher result is already computed and there is no clean way to add additional informations to it.

    I guess a feature request on the neo4j repository could be cool but in total honesty this would require a serious change into the neo4j core codebase.

    BTW, the incremental ID is already implemented in the graphaware-uuid plugin : https://github.com/graphaware/neo4j-uuid#specifying-the-generator-through-configuration