|
| 1 | +// This script can create a binary tree in ArangoDB with relatively large |
| 2 | +// vertex documents (approx. 1 MB each). You can give the depth and you can |
| 3 | +// choose what type of graph to create. |
| 4 | +// |
| 5 | +// Usage: |
| 6 | +// |
| 7 | +// makeGraph("G", "V", "E") - creates a general graph with name G, vertex |
| 8 | +// collection V and edge collection E |
| 9 | +// makeTree(6, "V", "E") - creates the actual tree after the graph was created |
| 10 | +// 6 is the depth and one has to name the vertex and |
| 11 | +// edge collections |
| 12 | +// |
| 13 | +// The following AQL query and its performance could be of interest: |
| 14 | +// |
| 15 | +// FOR v IN 0..6 OUTBOUND "V/S1:K1" GRAPH "G" |
| 16 | +// RETURN v.data |
| 17 | +// |
| 18 | +// This traverses the whole graph starting from the root but retrieves only |
| 19 | +// a tiny part of the vertex data. This tests the 3.10 feature of |
| 20 | +// traversal projections. You can see that it does this from this explain |
| 21 | +// output for the above query: |
| 22 | +// |
| 23 | +// Query String (58 chars, cacheable: true): |
| 24 | +// FOR v IN 0..6 OUTBOUND "V/S1:K1" GRAPH "G" |
| 25 | +// RETURN v.data |
| 26 | +// |
| 27 | +// Execution plan: |
| 28 | +// Id NodeType Site Est. Comment |
| 29 | +// 1 SingletonNode COOR 1 * ROOT |
| 30 | +// 2 TraversalNode COOR 64 - FOR v /* vertex (projections: `data`) */ IN 0..6 /* min..maxPathDepth */ OUTBOUND 'V/S1:K1' /* startnode */ GRAPH 'G' |
| 31 | +// 3 CalculationNode COOR 64 - LET #3 = v.`data` /* attribute expression */ |
| 32 | +// 4 ReturnNode COOR 64 - RETURN #3 |
| 33 | +// |
| 34 | +// In the line with Id 2 you can see that the TraversalNode uses a projection to the field `data`. |
| 35 | +// |
| 36 | + |
| 37 | +rand = require("internal").rand; |
| 38 | +time = require("internal").time; |
| 39 | + |
| 40 | +function makeRandomString(l) { |
| 41 | + var r = rand(); |
| 42 | + var d = rand(); |
| 43 | + var s = "x"; |
| 44 | + for (var i = 0; i < l; ++i) { |
| 45 | + s += r; |
| 46 | + r += d; |
| 47 | + } |
| 48 | + return s; |
| 49 | +} |
| 50 | + |
| 51 | +function makeGraph(graphName, vertexCollName, edgeCollName) { |
| 52 | + let graph = require("@arangodb/general-graph"); |
| 53 | + try { |
| 54 | + graph._drop(graphName, true); |
| 55 | + } |
| 56 | + catch { |
| 57 | + } |
| 58 | + graph._create(graphName, [graph._relation(edgeCollName, [vertexCollName], [vertexCollName])]); |
| 59 | +} |
| 60 | + |
| 61 | +function makeKey(i) { |
| 62 | + return "S" + (i % 3) + ":K" + i; |
| 63 | +} |
| 64 | + |
| 65 | +function makeTree(depth, vertexCollName, edgeCollName) { |
| 66 | + let V = db._collection(vertexCollName); |
| 67 | + let E = db._collection(edgeCollName); |
| 68 | + let klumpen = {}; |
| 69 | + for (let i = 0; i < 1000; ++i) { |
| 70 | + klumpen["K"+i] = makeRandomString(1024); |
| 71 | + } |
| 72 | + for (let i = 1; i <= 2 ** depth - 1; ++i) { |
| 73 | + let v = klumpen; |
| 74 | + v.data = "D"+i; |
| 75 | + v.smart = "S"+(i % 3); |
| 76 | + v._key = makeKey(i); |
| 77 | + V.insert(v); |
| 78 | + print("Have created", i, "vertices out of", 2 ** depth - 1); |
| 79 | + } |
| 80 | + |
| 81 | + // This is now a gigabyte of data, one megabyte per vertex. |
| 82 | + |
| 83 | + // Make a binary tree: |
| 84 | + for (let i = 1; i <= 2 ** (depth - 1) - 1; ++i) { |
| 85 | + let e = { _from: vertexCollName + "/" + makeKey(i), |
| 86 | + _to: vertexCollName + "/" + makeKey(2 * i)}; |
| 87 | + E.insert(e); |
| 88 | + e = { _from: vertexCollName + "/" + makeKey(i), |
| 89 | + _to: vertexCollName + "/" + makeKey(2 * i + 1)}; |
| 90 | + E.insert(e); |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments