Repository navigation
Expand file tree
/
Copy pathGraphCalls.java
More file actions
105 lines (94 loc) · 4.71 KB
/
Copy pathGraphCalls.java
File metadata and controls
105 lines (94 loc) · 4.71 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Python-bindings bridge: per-call glue for graph traversal and vector search results.
*
* Same idea as DbCalls: a JPype call costs a few microseconds of dispatch, so the paths that make one call per edge
* or per hit are dominated by the number of calls. Iterating the Iterable that Vertex.getEdges() returns took a
* hasNext() and a next() crossing per edge, and wrapping a vector search hit took getFirst(), getSecond(), and the
* record lookup. These return the same objects in one crossing, as arrays whose elements Python reads without method
* dispatch.
*
* Compiled into arcadedb-python-bridge.jar during the wheel build and consumed by Vertex.get_out_edges() and its
* siblings and by the vector index search methods in the Python bindings.
*/
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;
import com.arcadedb.utility.Pair;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public final class GraphCalls {
private GraphCalls() {
}
private static Edge[] drain(final Iterable<Edge> edges) {
final List<Edge> list = new ArrayList<>();
for (final Edge edge : edges)
list.add(edge);
return list.toArray(new Edge[0]);
}
/** The edges of {@code vertex.getEdges(Vertex.DIRECTION.OUT, labels)}, in iteration order. */
public static Edge[] outEdges(final Vertex vertex, final String... labels) {
return drain(vertex.getEdges(Vertex.DIRECTION.OUT, labels));
}
/** The edges of {@code vertex.getEdges(Vertex.DIRECTION.IN, labels)}, in iteration order. */
public static Edge[] inEdges(final Vertex vertex, final String... labels) {
return drain(vertex.getEdges(Vertex.DIRECTION.IN, labels));
}
/** The edges of {@code vertex.getEdges(Vertex.DIRECTION.BOTH, labels)}, in iteration order. */
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.
*/
public static Set<RID> ridSet(final String[] rids) {
final Set<RID> set = new HashSet<>(Math.max(16, (int) (rids.length / 0.75f) + 1));
for (final String rid : rids)
set.add(new RID(rid));
return set;
}
/**
* The records and scores of vector search hits: for each {@code Pair<RID, Float>}, {@code db.lookupByRID(rid, true)}
* and the score as a double. Returns {@code {Object[] records, double[] scores}}; a record is null where the engine
* returned null, so the caller can name that hit.
*/
public static Object[] hits(final Database db, final Iterable<?> pairs) {
final List<Object> records = new ArrayList<>();
final List<Double> scores = new ArrayList<>();
for (final Object item : pairs) {
final Pair<?, ?> pair = (Pair<?, ?>) item;
records.add(db.lookupByRID((RID) pair.getFirst(), true));
scores.add(((Number) pair.getSecond()).doubleValue());
}
final double[] s = new double[scores.size()];
for (int i = 0; i < s.length; i++)
s[i] = scores.get(i);
return new Object[] { records.toArray(), s };
}
}