Skip to content

Commit

Permalink
make previous tx id accessible to triggers
Browse files Browse the repository at this point in the history
One piece of information that could be useful for triggers to have access to is the previous transactions's id.
This allows for guaranteed in-order processing, even if TxData is received out of order.
  • Loading branch information
Thomas Matlak committed Feb 13, 2020
1 parent 7d91e9e commit 70be072
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/java/apoc/trigger/Trigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import org.neo4j.graphdb.event.TransactionData;
import org.neo4j.graphdb.event.TransactionEventHandler;
import org.neo4j.helpers.collection.Iterators;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.impl.core.GraphProperties;
import org.neo4j.kernel.impl.core.NodeManager;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
//import org.neo4j.procedure.Mode;
import org.neo4j.logging.Log;
import org.neo4j.procedure.*;

import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;
Expand Down Expand Up @@ -225,6 +227,19 @@ private void executeTriggers(TransactionData txData, String phase) {
if (phase.equals( "after" ))
{
params.putAll( txDataCollector( txData, phase, (Map<String,Object>) data.get( "config" ) ) );

try
{
Field f = txData.getClass().getDeclaredField( "transaction" );
f.setAccessible( true );
KernelTransaction kernelTransaction = (KernelTransaction) f.get( txData );
params.put( "lastTxId", kernelTransaction.lastTransactionIdWhenStarted() );
f.setAccessible( false );
}
catch ( NoSuchFieldException | IllegalAccessException e )
{
log.error( "Failed to get last transaction id: " + e.getMessage() );
}
}
if( ( (Map<String,Object>) data.get( "config" )).get( "params" ) != null)
{
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/apoc/trigger/TriggerDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ public void testTriggerData_TransactionId() throws Exception {
});
}

@Test
public void testTriggerData_lastTxId() {
db.execute("CALL apoc.trigger.add('test','WITH {createdNodes} AS createdNodes, {lastTxId} AS lastTxId UNWIND {createdNodes} AS n SET n.testProp = lastTxId',{phase: 'after'}, { uidKeys: ['uid'], params: {}})").close();
db.execute("CREATE (f:Foo {name:'Michael'})").close();
TestUtil.testCall(db, "MATCH (f:Foo) RETURN f", (row) -> {
assertEquals(true, ((Node)row.get("f")).hasProperty("testProp"));
assertNotEquals( "``:``:`0`", ((Node)row.get("f")).getProperty( "testProp") );
});
}

@Test
public void testTriggerData_CommitTime() throws Exception {
db.execute("CALL apoc.trigger.add('test','WITH {createdNodes} AS createdNodes, {txData} AS txData UNWIND {createdNodes} AS n SET n.testProp = txData." + COMMIT_TIME + "',{phase: 'after'}, { uidKeys: ['uid'], params: {}})").close();
Expand Down

0 comments on commit 70be072

Please sign in to comment.