Repository navigation
Expand file tree
/
Copy pathVertexBatcher.java
More file actions
65 lines (60 loc) · 2.62 KB
/
Copy pathVertexBatcher.java
File metadata and controls
65 lines (60 loc) · 2.62 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
/*
* Python-bindings bridge: bulk vertex creation for GraphBatch.
*
* GraphBatch.create_vertices() from Python marshals an Object[][] property
* matrix element-by-element (~19us/vertex measured vs 6.2us Java-native).
* This helper accepts the rows as ONE JSON string (json.dumps costs
* ~0.2us/vertex Python-side) and returns the created RIDs as ONE
* semicolon-joined string, so the whole batch costs two bulk string copies.
*
* Property values are str/int/float/bool/null, nested lists and maps thereof,
* and the tagged values DocumentBatcher.untag decodes (date-time, date,
* BigDecimal, byte[]) sitting directly in a row; the Python side falls back to
* the matrix path for anything else.
*/
package com.arcadedb.python;
import com.arcadedb.database.RID;
import com.arcadedb.graph.GraphBatch;
import com.arcadedb.serializer.json.JSONArray;
import com.arcadedb.serializer.json.JSONObject;
public final class VertexBatcher {
private VertexBatcher() {
}
public static String createVerticesJson(final GraphBatch batch, final String typeName, final String jsonRows) {
return createVerticesJson(batch, typeName, jsonRows, null, null);
}
/**
* As {@link #createVerticesJson(GraphBatch, String, String)}, with float vectors that did not go through the JSON text:
* {@code vectorNames[c]} is set on vertex i to row i of {@code vectors[c]}, a {@link FloatVectors.Matrix} with one row per
* JSON row. Both are null when there are none.
*/
public static String createVerticesJson(final GraphBatch batch, final String typeName, final String jsonRows,
final String[] vectorNames, final Object[] vectors) {
final JSONArray rows = new JSONArray(jsonRows);
FloatVectors.check(vectorNames, vectors, rows.length());
final int extra = vectorNames == null ? 0 : vectorNames.length;
final Object[][] matrix = new Object[rows.length()][];
for (int i = 0; i < rows.length(); i++) {
final JSONObject row = rows.getJSONObject(i);
final Object[] flat = new Object[(row.length() + extra) * 2];
int j = 0;
for (final String key : row.keySet()) {
flat[j++] = key;
flat[j++] = DocumentBatcher.value(row.isNull(key) ? null : row.get(key));
}
for (int c = 0; c < extra; c++) {
flat[j++] = vectorNames[c];
flat[j++] = ((FloatVectors.Matrix) vectors[c]).row(i);
}
matrix[i] = flat;
}
final RID[] rids = batch.createVertices(typeName, matrix);
final StringBuilder sb = new StringBuilder(rids.length * 8);
for (int i = 0; i < rids.length; i++) {
if (i > 0)
sb.append(';');
sb.append(rids[i]);
}
return sb.toString();
}
}