Embedded: Use cypher or api calls to update one to many relationship where many may be quite large

I am using embedded neo4j. I want to implement functionality to update a one to many relationship
(n:LN) -[:r]-> (m:LM). It may be that for a given node (n:LN) there is a huge number of nodes (m:LM) related by [:r].

For a given node (n:LN) i want to perform one of two update operations:

  1. Insert(Node n, Relationship r, Node m, boolean expectNotExists)
  2. Delete(Node n, Relationship r, Node m, boolean expectExists)

For the Insert operation if expectNotExists is true and the relationship already exists than an error should be generated. If expectNotExists is false then a merge operation is performed. Similarly for 2.

Let's say that for a particular transaction there can be many nodes n and many operations on each such node. Implementing the cypher is possible, but also complicated. However using the api, this would be fairly simple. It would be even simpler if the Node interface had a hasRelationship(RelationshipType rt, Node n) method. There is no such method, but it can be implemented using calls to the getRelationships( Direction direction, RelationshipType types ) method. However that requires iterating through the results to find if a relationship exists.

My questions is not so much about how to form the cypher(although that isnt easy and may require multiple ones) but rather:

  • If I do it programmatically will it essentially be as efficient as the cypher query? In other words will the cypher query ultimately have to iterate through the list of relationships to find if a particular one exists?