我正在构建一个基于d3力有向图的应用程序,后端有ArangoDB,我希望能够尽可能高效地从Arango加载节点和动态链接数据。
我不是d3方面的专家,但一般来说,力布局似乎希望它的数据成为一个节点数组和一个具有实际节点对象作为其源和目标的链接数组,如下所示:
var nodes = [
{id: 0, reflexive: false},
{id: 1, reflexive: true },
{id: 2, reflexive: false}
],
links = [
{source: nodes[0], target: nodes[1], left: false, right: true },
{source: nodes[1], target: nodes[2], left: false, right: true }
];目前,我正在使用下面的AQL查询来获取相邻节点,但这非常麻烦。困难之处在于,我希望包括节点的边缘信息,即使这些边缘没有被遍历(为了显示节点在从数据库加载这些链接之前的链接数量)。
LET docId = "ExampleDocClass/1234567"
// get data for all the edges
LET es = GRAPH_EDGES('EdgeClass',docId,{direction:'any',maxDepth:1,includeData:true})
// create an array of all the neighbor nodes
LET vArray = (
FOR v IN GRAPH_TRAVERSAL('EdgeClass',docId[0],'any',{ maxDepth:1})
FOR v1 IN v RETURN v1.vertex
)
// using node array, return inbound and outbound for each node
LET vs = (
FOR v IN vArray
// inbound and outbound are separate queries because I couldn't figure out
// how to get Arango to differentiate inbout and outbound in the query results
LET oe = (FOR oe1 IN GRAPH_EDGES('EdgeClass',v,{direction:'outbound',maxDepth:1,includeData:true}) RETURN oe1._to)
LET ie = (FOR ie1 IN GRAPH_EDGES('EdgeClass',v,{direction:'inbound',maxDepth:1,includeData:true}) RETURN ie1._from)
RETURN {'vertexData': v, 'outEdges': oe, 'inEdges': ie}
)
RETURN {'edges':es,'vertices':vs}最后的输出如下所示:http://pastebin.com/raw.php?i=B7uzaWxs ...which几乎可以直接读入d3 (我只需要稍微去重复一下)。
我的图形节点有大量的链接,因此性能非常重要(无论是服务器和客户端的负载,还是两者之间通信的文件大小)。除了简单地扩展相邻节点之外,我还计划创建各种命令来与图形交互。是否有办法更好地构造这个AQL查询(例如,避免四个单独的图形查询),或者使用arangojs函数或FOXX应用程序完全避免AQL,同时仍然以d3所需的格式构造响应(包括每个节点的链接数据)?
发布于 2015-12-04 07:43:14
我建议在数据库端做尽可能多的事情,因为通过网络复制和序列化/反序列化JSON通常很昂贵,所以传输尽可能少的数据应该是一个很好的目标。
首先,我使用了您的查询,并在我创建的示例数据集上执行了它(大约800个顶点,800个边在我的数据集中)作为基线,我使用了您的查询的执行时间,在我的例子中是~5.0s。
因此,我试图创建完全相同的结果,你只需要在AQL。我在您的查询中发现了一些改进: 1. GRAPH_NEIGHBORS比GRAPH_EDGES快一些。2.如果可能的话,如果您不需要数据,请避免使用{includeData: true},特别是如果您需要/来自vertices._id --只有GRAPH_NEIGHBORS和{includeData: false} --性能比GRAPH_EDGES高一个数量级。3. GRAPH_NEIGHBORS不重复,GRAPH_EDGES不重复。在你的情况下,这似乎是需要的。3.您可以在那里去掉几个子查询。
下面是我可以提出的纯AQL查询:
LET docId = "ExampleDocClass/1234567"
LET edges = GRAPH_EDGES('EdgeClass',docId,{direction:'any',maxDepth:1,includeData:true})
LET verticesTmp = (FOR v IN GRAPH_NEIGHBORS('EdgeClass', docId, {direction: 'any', maxDepth: 1, includeData: true})
RETURN {
vertexData: v,
outEdges: GRAPH_NEIGHBORS('EdgeClass', v, {direction: 'outbound', maxDepth: 1, includeData: false}),
inEdges: GRAPH_NEIGHBORS('EdgeClass', v, {direction: 'inbound', maxDepth: 1, includeData: false})
})
LET vertices = PUSH(verticesTmp, {
vertexData: DOCUMENT(docId),
outEdges: GRAPH_NEIGHBORS('EdgeClass', docId, {direction: 'outbound', maxDepth: 1, includeData: false}),
inEdges: GRAPH_NEIGHBORS('EdgeClass', docId, {direction: 'inbound', maxDepth: 1, includeData: false})
})
RETURN { edges, vertices }这将产生与查询相同的结果格式,并具有这样的优点:连接到docId的每个顶点都只在顶点中存储一次。而且,docId本身只在顶点中存储一次。客户端不需要重复。但是,在每个顶点的outEdges / inEdges中,所有连通顶点也都是一次,我不知道是否需要知道这个列表中的顶点之间是否存在多个边。
此查询在我的数据集中使用~0.06s。
但是,如果您对它做了更多的努力,您也可以考虑在Foxx应用程序中使用手工编写的遍历。这有点复杂,但在您的情况下可能会更快,因为您做的子查询更少。这方面的代码如下所示:
var traversal = require("org/arangodb/graph/traversal");
var result = {
edges: [],
vertices: {}
}
var myVisitor = function (config, result, vertex, path, connected) {
switch (path.edges.length) {
case 0:
if (! result.vertices.hasOwnProperty(vertex._id)) {
// If we visit a vertex, we store it's data and prepare out/in
result.vertices[vertex._id] = {
vertexData: vertex,
outEdges: [],
inEdges: []
};
}
// No further action
break;
case 1:
if (! result.vertices.hasOwnProperty(vertex._id)) {
// If we visit a vertex, we store it's data and prepare out/in
result.vertices[vertex._id] = {
vertexData: vertex,
outEdges: [],
inEdges: []
};
}
// First Depth, we need EdgeData
var e = path.edges[0];
result.edges.push(e);
// We fill from / to for both vertices
result.vertices[e._from].outEdges.push(e._to);
result.vertices[e._to].inEdges.push(e._from);
break;
case 2:
// Second Depth, we do not need EdgeData
var e = path.edges[1];
// We fill from / to for all vertices that exist
if (result.vertices.hasOwnProperty(e._from)) {
result.vertices[e._from].outEdges.push(e._to);
}
if (result.vertices.hasOwnProperty(e._to)) {
result.vertices[e._to].inEdges.push(e._from);
}
break;
}
};
var config = {
datasource: traversal.generalGraphDatasourceFactory("EdgeClass"),
strategy: "depthfirst",
order: "preorder",
visitor: myVisitor,
expander: traversal.anyExpander,
minDepth: 0,
maxDepth: 2
};
var traverser = new traversal.Traverser(config);
traverser.traverse(result, {_id: "ExampleDocClass/1234567"});
return {
edges: result.edges,
vertices: Object.keys(result.vertices).map(function (key) {
return result.vertices[key];
})
};这种遍历的思想是访问从起始顶点到两个边的所有顶点。所有0-1深度的顶点都将与数据一起添加到顶点对象中.来自起始顶点的所有边缘都将与数据一起添加到边缘列表中。深度2中的所有顶点只会在结果中设置outEdges / inEdges。
这样做的优点是,vertices是不重复的。如果在所有连通顶点之间存在多个边,则外边/内边多次包含它们。
这个遍历在~0.025s中在我的数据集上执行,因此它是AQL唯一解决方案的两倍。
希望这仍然有帮助;)
https://stackoverflow.com/questions/33855799
复制相似问题