Repository navigation
Expand file tree
/
Copy pathEdgeBatcher.java
More file actions
70 lines (64 loc) · 2.94 KB
/
Copy pathEdgeBatcher.java
File metadata and controls
70 lines (64 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* Python-bindings bridge: bulk edge buffering for GraphBatch.
*
* GraphBatch.newEdge() from Python costs one JPype crossing per edge
* (measured 4.9us/edge vs 0.2us Java-native — 24x). This helper accepts the
* whole edge list as String[] RIDs (marshaled in one bulk array copy) and
* loops Java-side, so Python pays one crossing per batch.
*
* newEdgesJson takes edges with properties too, in the value shapes of
* VertexBatcher.createVerticesJson (JSON values and tagged date-times).
*/
package com.arcadedb.python;
import com.arcadedb.database.RID;
import com.arcadedb.graph.GraphBatch;
public final class EdgeBatcher {
private EdgeBatcher() {
}
public static void newEdges(final GraphBatch batch, final String[] sourceRids, final String edgeType,
final String[] destinationRids) {
if (sourceRids.length != destinationRids.length)
throw new IllegalArgumentException(
"sourceRids and destinationRids must have the same length (" + sourceRids.length + " vs "
+ destinationRids.length + ")");
for (int i = 0; i < sourceRids.length; i++)
batch.newEdge(new RID(sourceRids[i]), edgeType, new RID(destinationRids[i]));
}
/**
* Same as {@link #newEdges} but takes semicolon-joined RID lists. JPype
* converts a Python list of N strings to String[] element-by-element
* (measured ~4us/edge — as slow as the per-edge path); a single joined
* string crosses in one bulk copy and is split here.
*/
public static void newEdgesJoined(final GraphBatch batch, final String joinedSourceRids, final String edgeType,
final String joinedDestinationRids) {
final String[] sources = joinedSourceRids.split(";");
final String[] destinations = joinedDestinationRids.split(";");
newEdges(batch, sources, edgeType, destinations);
}
/**
* Bulk edges WITH properties: one JSON array of rows, each
* {"_src": "#1:0", "_dst": "#1:1", ...properties}. Property values are JSON values and the tagged values
* DocumentBatcher.untag decodes; the Python side falls back per-edge otherwise.
*/
public static void newEdgesJson(final GraphBatch batch, final String edgeType, final String jsonRows) {
final com.arcadedb.serializer.json.JSONArray rows = new com.arcadedb.serializer.json.JSONArray(jsonRows);
for (int i = 0; i < rows.length(); i++) {
final com.arcadedb.serializer.json.JSONObject row = rows.getJSONObject(i);
final RID src = new RID(row.getString("_src"));
final RID dst = new RID(row.getString("_dst"));
final Object[] props = new Object[(row.length() - 2) * 2];
int j = 0;
for (final String key : row.keySet()) {
if (key.equals("_src") || key.equals("_dst"))
continue;
props[j++] = key;
props[j++] = DocumentBatcher.value(row.isNull(key) ? null : row.get(key));
}
if (props.length == 0)
batch.newEdge(src, edgeType, dst);
else
batch.newEdge(src, edgeType, dst, props);
}
}
}