Arangodb custom filter/visitor for my tree graph

浪子不回头ぞ 提交于 2019-12-06 02:57:00
mchacki

This was indeed a challenging question, thank you very much ;)

You can solve this by adding user-defined functions to AQL and use them in the TRAVERSER.

First of all I have registered two AQL visitor functions through arangosh:

var aqlfunctions = require("org/arangodb/aql/functions");
aqlfunctions.register("myvisitor::indirectAccess", "function (config, result, vertex) { if(result.length === 0) {result.push({});} result[0][vertex._key] = {hasAccess: true};}")
aqlfunctions.register("myvisitor::noAccess", "function (config, result, vertex) { if (result.length === 0) {result.push({});} result[0][vertex._key] = {hasAccess: false};}")

These functions simply do the following:

  • myvisitor::indirectAccess will be used to traverse down the tree. As in AQL the result is always an Array, we simply at a first document to it (if necessary) to store all the data. Then we assign to the vertexes _key property the value {hasAccess: true}.
  • myvisitor::noAccess will be used to traverse up the tree und will store '{hasAccess: false}` in the same way.

Now we can execute the following query which makes use of these visitors:

FOR x IN GRAPH_NEIGHBORS(@graph, @userId, {direction: 'outbound'})
LET upwards = TRAVERSAL(organisation, isDepartment, x, 'inbound', {visitor: 'myvisitor::noAccess'})[0]
LET downwards = TRAVERSAL(organisation, isDepartment, x, 'outbound', {visitor: 'myvisitor::indirectAccess'})[0]
RETURN MERGE(upwards, downwards)

Short explaination:

  1. Find the organizations this user has direct access to.
  2. Go up the tree upwards and mark everything as "noAccess".
  3. Go down the tree downwards and mark everything as "access".
  4. Merge upwards and downwards.

If you would like to modify the result format you have to change the registered visitor functions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!