Skip to content
Merged
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
16 changes: 11 additions & 5 deletions src/arcadedb/graph_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@ def new_edge(
f"Failed to buffer batch edge '{edge_type}': {e}"
) from e

@staticmethod
def _rid_text(value) -> str:
"""The RID text of a vertex or document (a record wrapper), or the text of a RID given as a string or a Java ``RID``."""
get_rid = getattr(value, "get_rid", None)
return get_rid() if get_rid is not None else str(value)

def new_edges(
self,
source_rids,
Expand All @@ -387,8 +393,8 @@ def new_edges(

The bulk counterpart of :meth:`new_edge` (which costs one boundary
crossing per edge — measured ~24x slower than Java-native for large
edge lists). RIDs may be strings ("#1:0") or objects with a string
representation.
edge lists). Each source and destination is a RID string ("#1:0"), a
Java ``RID``, or a vertex or document, as for :meth:`new_edge`.

Args:
source_rids: Sequence of source vertex RIDs.
Expand Down Expand Up @@ -453,8 +459,8 @@ def new_edges(
# copy; a Python list of N strings would be converted
# element-by-element by JPype and eat the batching win.
# Chunked so huge ingests never build one giant string.
sources = [str(r) for r in source_rids]
dests = [str(r) for r in destination_rids]
sources = [self._rid_text(r) for r in source_rids]
dests = [self._rid_text(r) for r in destination_rids]
for start in range(0, len(sources), self._BULK_CHUNK):
edge_batcher.newEdgesJoined(
self._java_graph_batch,
Expand All @@ -467,7 +473,7 @@ def new_edges(
rows = []
tagged = False # a value that needs the tagged text; False keeps the untagged code of before
for src, dst, p in zip(source_rids, destination_rids, properties):
row = {"_src": str(src), "_dst": str(dst)}
row = {"_src": self._rid_text(src), "_dst": self._rid_text(dst)}
for key, value in (p or {}).items():
ok = type(key) is str and key not in ("_src", "_dst")
if ok and not json_bulk_scalar_ok(value):
Expand Down
21 changes: 21 additions & 0 deletions tests/test_graph_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,27 @@ def test_graph_batch_edge_keeps_a_null_in_a_declared_property(temp_db_path, bulk
assert [(r.get("weight"), r.get("note")) for r in rows] == [(None, "hello")]


@pytest.mark.parametrize("with_properties", [False, True], ids=["plain", "properties"])
def test_new_edges_takes_vertex_objects_as_new_edge_does(temp_db_path, with_properties):
"""new_edge accepts a Vertex or its RID. new_edges stringified each item, so a Vertex became
"<Vertex rid=#1:0 type=P>" and the engine refused it as an invalid RID."""
with arcadedb.create_database(temp_db_path) as db:
_declared_edge_types(db)
with db.transaction():
a = db.new_vertex("P").set("id", 1).save()
b = db.new_vertex("P").set("id", 2).save()
c = db.new_vertex("P").set("id", 3).save()
properties = [{"note": "x"}] * 3 if with_properties else None
with db.graph_batch(parallel_flush=False) as batch:
batch.new_edges(
[a, b.get_rid(), c], "ViaBulk", [b, c, a.get_identity()], properties
)
rows = db.query(
"sql", "SELECT out.id AS src, in.id AS dst FROM ViaBulk ORDER BY src"
).to_list()
assert [(r.get("src"), r.get("dst")) for r in rows] == [(1, 2), (2, 3), (3, 1)]


def test_graph_batch_edge_refuses_an_out_of_range_short(temp_db_path):
"""ArcadeData/arcadedb#9019, fixed in 26.10.1 (upstream PR #9108): a GraphBatch edge
skipped the declared property's conversion, so 40000 in a SHORT was stored as -25536.
Expand Down
Loading