Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 28 additions & 23 deletions src/arcadedb/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,37 +278,42 @@ 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 = []
for k, v in kwargs.items():
props.append(k)
props.append(convert_python_to_java(v))

database = self._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
java_edge = source_java.newEdge(label, target_java, *props)
return Edge(java_edge, self._database)
return Edge(source_java.newEdge(label, target_java, *props), database)

def get_out_edges(self, *labels: str) -> List["Edge"]:
"""Get outgoing edges."""
Expand Down
23 changes: 23 additions & 0 deletions src/java/com/arcadedb/python/GraphCalls.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
}
Expand Down
189 changes: 189 additions & 0 deletions tests/test_vertex_new_edge_crossings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
"""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, results
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]


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}


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"
with db.transaction():
# 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("#")


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()
)
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"])
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 _edges(db, a, "out") == 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


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()]
Loading