Is the insertion order guaranteed with this example code?

Taking the following code which is found at https://tinkerpop.apache.org/docs/current/reference/#gremlin-javascript-transactions, is the insertion order guaranteed for these two new vertices?
const g = traversal().withRemote(new DriverRemoteConnection('ws://localhost:8182/gremlin'));
const tx = g.tx(); // create a Transaction

// spawn a new GraphTraversalSource binding all traversals established from it to tx
const gtx = tx.begin();

// execute traversals using gtx occur within the scope of the transaction held by tx. the
// tx is closed after calls to commit or rollback and cannot be re-used. simply spawn a
// new Transaction from g.tx() to create a new one as needed. the g context remains
// accessible through all this as a sessionless connection.
Promise.all([
gtx.addV("person").property("name", "jorge").iterate(),
gtx.addV("person").property("name", "josh").iterate()
]).then(() => {
return tx.commit();
}).catch(() => {
return tx.rollback();
});
const g = traversal().withRemote(new DriverRemoteConnection('ws://localhost:8182/gremlin'));
const tx = g.tx(); // create a Transaction

// spawn a new GraphTraversalSource binding all traversals established from it to tx
const gtx = tx.begin();

// execute traversals using gtx occur within the scope of the transaction held by tx. the
// tx is closed after calls to commit or rollback and cannot be re-used. simply spawn a
// new Transaction from g.tx() to create a new one as needed. the g context remains
// accessible through all this as a sessionless connection.
Promise.all([
gtx.addV("person").property("name", "jorge").iterate(),
gtx.addV("person").property("name", "josh").iterate()
]).then(() => {
return tx.commit();
}).catch(() => {
return tx.rollback();
});
Solution:
right, there are no any guaranties with Promise.all if for some reason the insertion order is important, then you need to call sequentially gtx.addV("person").property("name", "jorge").iterate(); gtx.addV("person").property("name", "josh").iterate();...
Jump to solution
10 Replies
ManabuBeach
ManabuBeach6mo ago
Note you can do addV twice in the same traversal I think. If you do it’s atomic and do not need to wrap in TX
danielcraig23
danielcraig236mo ago
Yes that's certainly true, for reasons of breaking up a series of several long .mergeV() and .mergeE()'s I want to use a transaction so that there's space in between.
spmallette
spmallette6mo ago
i'm no javascript expert, but in that particular example, i dont think Promise.all(...) offers any guaranteed of execution order for each job given to it, so no, i dont think you can always expect "josh" to come after "jorge". maybe we should have a better example there cc/ @Valentyn Kahamlyk
Solution
Valentyn Kahamlyk
right, there are no any guaranties with Promise.all if for some reason the insertion order is important, then you need to call sequentially gtx.addV("person").property("name", "jorge").iterate(); gtx.addV("person").property("name", "josh").iterate(); this is critical for mergeE/mergeV to avoid concurrent mergeV to add/update Vertex and mergeE which will use this Vertex
danielcraig23
danielcraig236mo ago
Is this an acceptable way of making sequential writes? I am using async.retry to retry the promise returned by tx.commit(), similar to this example from Amazon: https://docs.aws.amazon.com/neptune/latest/userguide/lambda-functions-examples.html#lambda-functions-examples-javascript ``` const tx = g.tx(); try { // get the graph traversal source from the transaction const gtx = tx.begin();
gtx.addV("owner").property(id, "1111").iterate() gtx.addV("laptop").property(id, "macbookAir2024").property("yearReleased", 2024).iterate() gtx.V() .hasLabel('laptop') .has(t.id, textP.startingWith(partialMacbookId)) .order() .by('yearReleased', desc) .limit(1) .as("mostRecentMacbook") .mergeE( new Map([ [t.label, 'hasOwner'], [direction.out, merge.outV], [direction.in, merge.inV]] ) ) .option(merge.outV, .select('mostRecentMacbook')) .option(merge.inV, .V(ownerId)) .iterate(); return tx.commit(); } catch (error) { logger.error(error); // roll back the transaction return tx.rollback(); }
AWS Lambda function examples for Amazon Neptune - Amazon Neptune
The following example AWS Lambda functions, written in Java, JavaScript and Python, illustrate upserting a single vertex with a randomly generated ID using the fold().coalesce().unfold() idiom.
danielcraig23
danielcraig236mo ago
@Valentyn Kahamlyk does gtx....iterate() have a strategy for maintaining the order of these writes?
Valentyn Kahamlyk
for TinkerPop 3.7.x data is updated in the order that it is received, but strictly speaking this is not documented
danielcraig23
danielcraig236mo ago
for 3.6.2 is this also the case?
spmallette
spmallette6mo ago
when you call `iterate() that should be the trigger to make the request to the server within a session. the order in which those requests to the session are received by the server should be the order in which they are executed.
danielcraig23
danielcraig236mo ago
Ok thank you!!
Want results from more Discord servers?
Add your server