From d708606d9977a11158375ac0edd24a022fd6c567 Mon Sep 17 00:00:00 2001 From: Taewoon Kim Date: Sat, 14 Nov 2026 15:50:51 +0900 Subject: [PATCH 1/4] Tests first: Vertex.new_edge is one call into the JVM, and the edge is the one the seven-call path writes (refs #388) The first test counts the calls on proxies of the source and target vertex, the database and GraphCalls: the parent makes seven calls on the Java objects and none on GraphCalls.newEdge. The others compare the edge with one written by the former code (reproduced in the test) for the three forms of target, edges with and without properties, a source read before other commits, and failures. Co-Authored-By: Claude Sonnet 5.5 --- tests/conftest.py | 1 + tests/test_vertex_new_edge_crossings.py | 152 ++++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 tests/test_vertex_new_edge_crossings.py diff --git a/tests/conftest.py b/tests/conftest.py index 22b929f..9c8a431 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -166,6 +166,7 @@ "test_read_crossings.py": "crossing counts of the JVM build's read path (JPype proxies of the Java objects and the bridge classes)", "test_reader_crossings.py": "crossing counts of the JVM build's record and row readers (JPype proxies of the Java objects and the bridge classes)", "test_reader_identity_crossings.py": "crossing counts of get_rid, get_vertex, get_edge and get_element on the JVM build (JPype proxies of the Java row and the bridge class)", + "test_vertex_new_edge_crossings.py": "crossing counts of Vertex.new_edge on the JVM build (JPype proxies of the Java vertex, database and bridge class)", "test_typed_batch_guard.py": "the object_hook and the side array of the JVM build's TypedRows batches (JPype arrays, the bridge class, a spy on json.loads)", "test_jvm_ingest_integrity_faults.py": "rows-dropped faults put into the JVM build's column conversion and bulk vertex path (core._column_to_java, GraphBatch._create_vertices_json_bulk)", } diff --git a/tests/test_vertex_new_edge_crossings.py b/tests/test_vertex_new_edge_crossings.py new file mode 100644 index 0000000..ccd5a0d --- /dev/null +++ b/tests/test_vertex_new_edge_crossings.py @@ -0,0 +1,152 @@ +"""Crossings of ``Vertex.new_edge`` on the JVM build. + +``new_edge`` resolved both ends by record id in Python: ``getIdentity``, ``isValid``, ``lookupByRID`` and ``asVertex`` for the source, +``getIdentity`` and ``isValid`` for the target, then ``newEdge``: seven calls into the JVM. ``GraphCalls.newEdge`` does all of it in one. The first +test pins the number of calls (counted on proxies of the source and target vertex, the database and the bridge class, the technique of +``test_reader_crossings.py``: a call on any of the Java objects would show up in the log). The rest pin that the edge is the one the seven-call +path writes, which ``_old_new_edge`` below reproduces from the former code, so a count cannot be bought with a change of behaviour. +""" + +import pytest + +from arcadedb import graph +from arcadedb.type_conversion import convert_python_to_java + + +class Counting: + """Counts the method calls made on a Java object or a Java class; a ``Counting`` argument is unwrapped, return values are the real objects.""" + + def __init__(self, target, log, label): + self._target = target + self._log = log + self._label = label + + def __getattr__(self, name): + attr = getattr(self._target, name) + if not callable(attr): + return attr + + def call(*args): + self._log.append(f"{self._label}.{name}") + args = [a._target if isinstance(a, Counting) else a for a in args] + return attr(*args) + + return call + + +def _old_new_edge(vertex, label, target, **kwargs): + """``Vertex.new_edge`` as it was before ``GraphCalls.newEdge``: seven calls into the JVM.""" + target_java = ( + target.get_java_document() if isinstance(target, graph.Vertex) else target + ) + source_java = vertex._java_document + source_rid = source_java.getIdentity() + if source_rid is not None and source_rid.isValid() and vertex._database is not None: + source_java = vertex._database._java_db.lookupByRID(source_rid, True).asVertex() + if hasattr(target_java, "getIdentity"): + target_rid = target_java.getIdentity() + if target_rid is not None and target_rid.isValid(): + target_java = target_rid + props = [] + for k, v in kwargs.items(): + props.append(k) + props.append(convert_python_to_java(v)) + return graph.Edge(source_java.newEdge(label, target_java, *props), vertex._database) + + +@pytest.fixture +def ends(temp_db): + temp_db.command("sql", "CREATE VERTEX TYPE V") + temp_db.command("sql", "CREATE EDGE TYPE E") + with temp_db.transaction(): + for name in "abcdef": + temp_db.command("sql", "CREATE VERTEX V SET name = ?", name) + rows = temp_db.query("sql", "SELECT @rid AS r FROM V ORDER BY name").to_list() + return temp_db, [temp_db.lookup_by_rid(str(row["r"])) for row in rows] + + +KW = {"s": "text", "i": 7, "big": 2**40, "f": 1.5, "flag": True, "none": None} + + +def test_new_edge_is_one_call_into_the_bridge(ends, monkeypatch): + db, (a, b, *_) = ends + calls = [] + real = graph._graph_calls() + assert real is not None, "the bridge jar must carry GraphCalls" + monkeypatch.setattr(graph, "_GRAPH_CALLS", Counting(real, calls, "GraphCalls")) + monkeypatch.setattr(db, "_java_db", Counting(db._java_db, calls, "db")) + monkeypatch.setattr(a, "_java_document", Counting(a._java_document, calls, "a")) + monkeypatch.setattr(b, "_java_document", Counting(b._java_document, calls, "b")) + with db.transaction(): + edge = a.new_edge("E", b, **KW) + assert calls == ["GraphCalls.newEdge"] + assert edge.get_rid().startswith("#") + + +def test_the_edge_is_the_one_the_seven_call_path_writes(ends): + db, (a, b, c, d, *_) = ends + with db.transaction(): + new = a.new_edge("E", b, **KW) + old = _old_new_edge(c, "E", d, **KW) + assert new.to_dict() == old.to_dict() + assert new.get("i") == 7 and new.get("s") == "text" + assert type(new) is type(old) + assert str(new._java_document.getClass().getName()) == str( + old._java_document.getClass().getName() + ) + with db.transaction(): + assert [e.get_rid() for e in a.get_out_edges("E")] == [new.get_rid()] + assert [e.get_rid() for e in b.get_in_edges("E")] == [new.get_rid()] + assert [e.get_rid() for e in c.get_out_edges("E")] == [old.get_rid()] + assert [e.get_rid() for e in d.get_in_edges("E")] == [old.get_rid()] + + +@pytest.mark.parametrize("form", ["vertex", "java vertex", "java rid"]) +def test_every_form_of_target_reaches_the_same_vertex(ends, form): + db, (a, b, *_) = ends + target = { + "vertex": b, + "java vertex": b.get_java_document(), + "java rid": b.get_identity(), + }[form] + with db.transaction(): + edge = a.new_edge("E", target, w=3) + assert edge.get_raw("w") == 3 + assert [e.get_rid() for e in b.get_in_edges("E")] == [edge.get_rid()] + + +def test_an_edge_without_properties_and_several_edges_of_one_vertex(ends): + db, (a, b, c, d, *_) = ends + with db.transaction(): + e1 = a.new_edge("E", b) + e2 = a.new_edge("E", c) + with db.transaction(): + e3 = a.new_edge("E", d, w=1) # a was read before the two commits above + assert e1.to_dict() == e2.to_dict() + assert e3.get("w") == 1 + assert sorted(e.get_rid() for e in a.get_out_edges("E")) == sorted( + e.get_rid() for e in (e1, e2, e3) + ) + + +def _outcome(db, action): + """None when ``action`` ran inside one transaction, else the class of what it raised.""" + try: + with db.transaction(): + action() + except Exception as exc: # the parity of the failure is what is compared + return type(exc) + return None + + +def test_a_failure_is_the_one_the_seven_call_path_raises(ends): + db, (a, b, c, d, *_) = ends + assert _outcome(db, lambda: a.new_edge("NoSuchEdgeType", b)) == _outcome( + db, lambda: _old_new_edge(c, "NoSuchEdgeType", d) + ) + assert _outcome(db, lambda: a.new_edge("E", b, w=object())) == _outcome( + db, lambda: _old_new_edge(c, "E", d, w=object()) + ) + unsaved_new = _outcome(db, lambda: db.new_vertex("V").new_edge("E", b)) + unsaved_old = _outcome(db, lambda: _old_new_edge(db.new_vertex("V"), "E", d)) + assert unsaved_new == unsaved_old From c828dc0f2cef80bf49895703e1c27579491ac4f1 Mon Sep 17 00:00:00 2001 From: Taewoon Kim Date: Sat, 14 Nov 2026 15:50:51 +0900 Subject: [PATCH 2/4] JVM: Vertex.new_edge resolves both ends and creates the edge in one crossing, through GraphCalls.newEdge (refs #388) The lookup of the source by record id inside the active transaction and the target's identity were seven JPype calls (getIdentity, isValid, lookupByRID, asVertex, getIdentity, isValid, newEdge). GraphCalls.newEdge does the same steps in Java, with the same rules: a source with a valid identity and a database is looked up again, the target goes in as its identity when that is valid. The wheel carries its own bridge jar, so the seven-call route is gone. Co-Authored-By: Claude Sonnet 5.5 --- src/arcadedb/graph.py | 35 ++++++-------------- src/java/com/arcadedb/python/GraphCalls.java | 23 +++++++++++++ 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/arcadedb/graph.py b/src/arcadedb/graph.py index 608790a..e125191 100644 --- a/src/arcadedb/graph.py +++ b/src/arcadedb/graph.py @@ -278,27 +278,6 @@ def new_edge(self, label: str, target: "Vertex", **kwargs) -> "Edge": target_java = ( target.get_java_document() if isinstance(target, Vertex) else target ) - # A vertex object is the state it had when it was read. The first edge - # of a vertex rewrites its record (the head chunk of its edge list), so - # an object read before that commit would make the engine's update fail - # with ConcurrentModificationException. Resolve both ends by record id - # inside the active transaction, as the native build does: the source - # is looked up (one lookup), the target goes in as its id and the - # engine loads it. - source_java = self._java_document - source_rid = source_java.getIdentity() - if ( - source_rid is not None - and source_rid.isValid() - and self._database is not None - ): - source_java = self._database._java_db.lookupByRID( - source_rid, True - ).asVertex() - if hasattr(target_java, "getIdentity"): - target_rid = target_java.getIdentity() - if target_rid is not None and target_rid.isValid(): - target_java = target_rid # Convert kwargs to flat list of [key, value, key, value...] props = [] @@ -306,9 +285,17 @@ def new_edge(self, label: str, target: "Vertex", **kwargs) -> "Edge": props.append(k) props.append(convert_python_to_java(v)) - # bidirectional is determined by the EdgeType schema - java_edge = source_java.newEdge(label, target_java, *props) - return Edge(java_edge, self._database) + # One crossing: GraphCalls.newEdge resolves both ends by record id inside the active + # transaction (see there), and the engine decides bidirectionality from the EdgeType. + database = self._database + java_edge = _graph_calls().newEdge( + database._java_db if database is not None else None, + self._java_document, + label, + target_java, + *props, + ) + return Edge(java_edge, database) def get_out_edges(self, *labels: str) -> List["Edge"]: """Get outgoing edges.""" diff --git a/src/java/com/arcadedb/python/GraphCalls.java b/src/java/com/arcadedb/python/GraphCalls.java index 6ff6838..ec9e25b 100644 --- a/src/java/com/arcadedb/python/GraphCalls.java +++ b/src/java/com/arcadedb/python/GraphCalls.java @@ -13,6 +13,7 @@ package com.arcadedb.python; import com.arcadedb.database.Database; +import com.arcadedb.database.Identifiable; import com.arcadedb.database.RID; import com.arcadedb.graph.Edge; import com.arcadedb.graph.Vertex; @@ -50,6 +51,28 @@ public static Edge[] bothEdges(final Vertex vertex, final String... labels) { return drain(vertex.getEdges(Vertex.DIRECTION.BOTH, labels)); } + /** + * {@code Vertex.new_edge()} in one crossing. Both ends are resolved by record id inside the active transaction: a vertex object is the + * state it had when it was read, and the first edge of a vertex rewrites its record (the head chunk of its edge list), so an object read + * before that commit would make the engine's update fail with ConcurrentModificationException. The source is looked up again by its + * identity (when it has a valid one and a database is given, else it is used as it is), the target goes in as its identity when that is + * valid and the engine loads it. Whether the edge is bidirectional is the EdgeType's. Python made seven calls for this + * (getIdentity, isValid, lookupByRID, asVertex, getIdentity, isValid, newEdge). + */ + public static Edge newEdge(final Database db, final Vertex source, final String label, final Identifiable target, final Object... props) { + Vertex from = source; + final RID sourceRid = source.getIdentity(); + if (db != null && sourceRid != null && sourceRid.isValid()) + from = db.lookupByRID(sourceRid, true).asVertex(); + Identifiable to = target; + if (target != null) { + final RID targetRid = target.getIdentity(); + if (targetRid != null && targetRid.isValid()) + to = targetRid; + } + return from.newEdge(label, to, props); + } + /** * A set of RIDs from their text, for the {@code allowed_rids} filter of a vector search: building it from Python took a RID * constructor call and an {@code add} call per RID (about 5 microseconds each), here the strings cross as one array. From 3673950f695d7b9211a3c6f364807032e9ddccb0 Mon Sep 17 00:00:00 2001 From: Taewoon Kim Date: Sat, 14 Nov 2026 20:47:09 +0900 Subject: [PATCH 3/4] Tests: the crossing test counts only the call (proxies off around begin and commit), and edge lists are read through a fresh vertex (refs #388) The tagged 26.11.1 JVM run failed three tests in every leg, all in tests/test_vertex_new_edge_crossings.py, and all three are the tests, not the new code: - test_new_edge_is_one_call_into_the_bridge put a counting proxy in place of db._java_db before opening the transaction, and the transaction's own begin goes through DbCalls.stackMark(db), which JPype refuses for a proxy ("No matching overloads found"). The proxies are now on only around the new_edge call. - test_the_edge_is_the_one_the_seven_call_path_writes and test_an_edge_without_properties_and_several_edges_of_one_vertex listed the edges of vertex objects read before the commit. A vertex object is the state it had when it was read: GraphEngine.getMostUpdatedVertex (26.11.1) hands it the newest record only inside the transaction that wrote it, so a stale object lists nothing (the same for the seven-call path, and the new_edge docstring says the object is not refreshed). The lists are read through a fresh lookup, and one assertion pins that the stale object is left as it was on both routes. The code is unchanged: the properties, the edge class and the failure parity assertions of the same file passed in all legs. Co-Authored-By: Claude Sonnet 5.5 --- tests/test_vertex_new_edge_crossings.py | 41 +++++++++++++++++-------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/tests/test_vertex_new_edge_crossings.py b/tests/test_vertex_new_edge_crossings.py index ccd5a0d..1ea21f7 100644 --- a/tests/test_vertex_new_edge_crossings.py +++ b/tests/test_vertex_new_edge_crossings.py @@ -65,6 +65,19 @@ def ends(temp_db): return temp_db, [temp_db.lookup_by_rid(str(row["r"])) for row in rows] +def _edges(db, vertex, direction): + """The RIDs of the "E" edges of ``vertex``, read through a vertex looked up now. + + A vertex object is the state it had when it was read: the engine (``GraphEngine.getMostUpdatedVertex``) hands it the + newest version of its record only inside the transaction that wrote it, so an object read before an edge was + committed lists none of it. ``new_edge`` does not refresh the object it is called on (its docstring says so), which + holds for the seven-call path as well, so the lists are read through a fresh lookup. + """ + fresh = db.lookup_by_rid(vertex.get_rid()) + listed = fresh.get_out_edges("E") if direction == "out" else fresh.get_in_edges("E") + return sorted(e.get_rid() for e in listed) + + KW = {"s": "text", "i": 7, "big": 2**40, "f": 1.5, "flag": True, "none": None} @@ -73,12 +86,15 @@ def test_new_edge_is_one_call_into_the_bridge(ends, monkeypatch): calls = [] real = graph._graph_calls() assert real is not None, "the bridge jar must carry GraphCalls" - monkeypatch.setattr(graph, "_GRAPH_CALLS", Counting(real, calls, "GraphCalls")) - monkeypatch.setattr(db, "_java_db", Counting(db._java_db, calls, "db")) - monkeypatch.setattr(a, "_java_document", Counting(a._java_document, calls, "a")) - monkeypatch.setattr(b, "_java_document", Counting(b._java_document, calls, "b")) with db.transaction(): - edge = a.new_edge("E", b, **KW) + # the proxies are on only for the call itself: the transaction's own begin and + # commit (``DbCalls.stackMark(db)`` among them) need the real database object + with monkeypatch.context() as patched: + patched.setattr(graph, "_GRAPH_CALLS", Counting(real, calls, "GraphCalls")) + patched.setattr(db, "_java_db", Counting(db._java_db, calls, "db")) + patched.setattr(a, "_java_document", Counting(a._java_document, calls, "a")) + patched.setattr(b, "_java_document", Counting(b._java_document, calls, "b")) + edge = a.new_edge("E", b, **KW) assert calls == ["GraphCalls.newEdge"] assert edge.get_rid().startswith("#") @@ -94,11 +110,12 @@ def test_the_edge_is_the_one_the_seven_call_path_writes(ends): assert str(new._java_document.getClass().getName()) == str( old._java_document.getClass().getName() ) - with db.transaction(): - assert [e.get_rid() for e in a.get_out_edges("E")] == [new.get_rid()] - assert [e.get_rid() for e in b.get_in_edges("E")] == [new.get_rid()] - assert [e.get_rid() for e in c.get_out_edges("E")] == [old.get_rid()] - assert [e.get_rid() for e in d.get_in_edges("E")] == [old.get_rid()] + assert _edges(db, a, "out") == [new.get_rid()] + assert _edges(db, b, "in") == [new.get_rid()] + assert _edges(db, c, "out") == [old.get_rid()] + assert _edges(db, d, "in") == [old.get_rid()] + # the object new_edge was called on is left as it was read, as on the seven-call path + assert len(a.get_out_edges("E")) == len(c.get_out_edges("E")) @pytest.mark.parametrize("form", ["vertex", "java vertex", "java rid"]) @@ -124,9 +141,7 @@ def test_an_edge_without_properties_and_several_edges_of_one_vertex(ends): e3 = a.new_edge("E", d, w=1) # a was read before the two commits above assert e1.to_dict() == e2.to_dict() assert e3.get("w") == 1 - assert sorted(e.get_rid() for e in a.get_out_edges("E")) == sorted( - e.get_rid() for e in (e1, e2, e3) - ) + assert _edges(db, a, "out") == sorted(e.get_rid() for e in (e1, e2, e3)) def _outcome(db, action): From 0c33e351ce0c87a824418aa6af010ae5fe3ce13a Mon Sep 17 00:00:00 2001 From: Taewoon Kim Date: Fri, 20 Nov 2026 13:31:38 +0900 Subject: [PATCH 4/4] JVM: Vertex.new_edge keeps the seven-call path when the bridge jar is missing (refs #388) _graph_calls() answers None when the bridge jar is missing, which sends every caller down the plain path (main fixed the same missing check for the reader calls in 84849b5). new_edge called GraphCalls.newEdge without that check and would raise an AttributeError. It now takes GraphCalls.newEdge when the bridge exists and the former seven calls otherwise. The test puts None under the bridge class and compares the edge with the one the bridge writes. Co-Authored-By: Claude Sonnet 5.5 --- src/arcadedb/graph.py | 38 ++++++++++++++++++------- tests/test_vertex_new_edge_crossings.py | 24 +++++++++++++++- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/src/arcadedb/graph.py b/src/arcadedb/graph.py index e125191..a987dec 100644 --- a/src/arcadedb/graph.py +++ b/src/arcadedb/graph.py @@ -285,17 +285,35 @@ def new_edge(self, label: str, target: "Vertex", **kwargs) -> "Edge": props.append(k) props.append(convert_python_to_java(v)) - # One crossing: GraphCalls.newEdge resolves both ends by record id inside the active - # transaction (see there), and the engine decides bidirectionality from the EdgeType. database = self._database - java_edge = _graph_calls().newEdge( - database._java_db if database is not None else None, - self._java_document, - label, - target_java, - *props, - ) - return Edge(java_edge, database) + calls = _graph_calls() + if calls is not None: + # One crossing: GraphCalls.newEdge resolves both ends by record id inside the active + # transaction (see there), and the engine decides bidirectionality from the EdgeType. + java_edge = calls.newEdge( + database._java_db if database is not None else None, + self._java_document, + label, + target_java, + *props, + ) + return Edge(java_edge, database) + # The bridge jar is missing (see _graph_calls): the plain path, seven calls. A vertex object + # is the state it had when it was read. The first edge of a vertex rewrites its record (the + # head chunk of its edge list), so an object read before that commit would make the engine's + # update fail with ConcurrentModificationException. Resolve both ends by record id inside + # the active transaction, as the native build does: the source is looked up (one lookup), + # the target goes in as its id and the engine loads it. + source_java = self._java_document + source_rid = source_java.getIdentity() + if source_rid is not None and source_rid.isValid() and database is not None: + source_java = database._java_db.lookupByRID(source_rid, True).asVertex() + if hasattr(target_java, "getIdentity"): + target_rid = target_java.getIdentity() + if target_rid is not None and target_rid.isValid(): + target_java = target_rid + # bidirectional is determined by the EdgeType schema + return Edge(source_java.newEdge(label, target_java, *props), database) def get_out_edges(self, *labels: str) -> List["Edge"]: """Get outgoing edges.""" diff --git a/tests/test_vertex_new_edge_crossings.py b/tests/test_vertex_new_edge_crossings.py index 1ea21f7..0a88cd7 100644 --- a/tests/test_vertex_new_edge_crossings.py +++ b/tests/test_vertex_new_edge_crossings.py @@ -9,7 +9,7 @@ import pytest -from arcadedb import graph +from arcadedb import graph, results from arcadedb.type_conversion import convert_python_to_java @@ -165,3 +165,25 @@ def test_a_failure_is_the_one_the_seven_call_path_raises(ends): unsaved_new = _outcome(db, lambda: db.new_vertex("V").new_edge("E", b)) unsaved_old = _outcome(db, lambda: _old_new_edge(db.new_vertex("V"), "E", d)) assert unsaved_new == unsaved_old + + +def test_without_the_bridge_class_new_edge_takes_the_plain_path_and_writes_the_same_edge( + ends, monkeypatch +): + """A jar without GraphCalls (``_bridge_class`` answers None) sends ``new_edge`` down the seven-call path, not into an AttributeError.""" + db, (a, b, c, d, *_) = ends + with db.transaction(): + bridged = a.new_edge("E", b, **KW) + with monkeypatch.context() as patched: + patched.setitem(results._BRIDGE_CLASSES, "GraphCalls", None) + patched.setattr(graph, "_GRAPH_CALLS", None) + assert graph._graph_calls() is None + with db.transaction(): + plain = c.new_edge("E", d, **KW) + assert plain.to_dict() == bridged.to_dict() + assert type(plain) is type(bridged) + assert str(plain._java_document.getClass().getName()) == str( + bridged._java_document.getClass().getName() + ) + assert _edges(db, c, "out") == [plain.get_rid()] + assert _edges(db, d, "in") == [plain.get_rid()]