【问题标题】:Accessing Connected Vertices in OrientDB Javascript Functions在 OrientDB Javascript 函数中访问连接的顶点
【发布时间】:2015-05-27 08:19:22
【问题描述】:

我正在尝试使用 OrientDB 工作室中的内置功能对人员未使用的工作站进行分组。获取这些顶点的查询工作正常,但我试图避免 Traverse,因为它非常慢 - 太慢而无法在生产中使用。而不是遍历每个空闲站点并将其与所有邻居分组在一起,而是将每个分组的“命名”与集合中最小的@rid 保持在一起。

var groups = {};            //The list of groups of workpoints. The key is the lowest RID in the group
var mappedDesks = {};       //Every desk's RID is in this object with it's matching value being the group name they're in    

//Get all Workpoints that don't have a Locale CURRENTLY_LOCATED_ON them
var freeDesks = db.query("SELECT FROM Workpoint WHERE @rid NOT IN (SELECT @rid FROM (SELECT EXPAND(OUT('CURRENTLY_LOCATED_ON').OUT('LOCATED_ON')) FROM Person) WHERE @class = 'Workpoint')");    

//Iterate through all vacant Workpoints
for (var j=0; j < freeDesks.length; j++){
    var baseNodeRid = freeDesks[j].getRecord().getIdentity().toString();                    // The RID of the Workpoint
    var baseNodeNumber = parseFloat(baseNodeRid.replace("#", "").replace(":","."));         // The RID converted to a number for comparisons. The lower RID takes precedence
    var baseSanitized = baseNodeRid.replace(":","-")                                        // Keys cannot contain colon so they are replaced with a dash    

    if (freeDesks[j].getRecord().field("out_NEIGHBOUR_OF") == null ) {
        // Desks without neighbours can be put in a group on their own
        groups[baseSanitized] = new Array();
        groups[baseSanitized].push(baseNodeRid);
        mappedDesks[baseSanitized] = baseSanitized;
    } else {
        //Iterate over all the desk's neighbours
        for (var n = 0; n < freeDesks[j].getRecord().field("out_NEIGHBOUR_OF").length; n++){          

            //Convert the neighbour's RID to a number too
            var nSanitized = n.replace(":","-");

            if (parseFloat(n.replace("#", "").replace(":",".")) > baseNodeNumber ){
                //The neighbour's node group is larger than the current one. This needs to be merged into the group with the smaller rid    

                //Move the desks from the neighbour's group into the base's group. If it has none then do nothing
                var nGroup = groups[mappedDesks[nSanitized]]
                if ( nGroup != null) {
                    groups[baseSanitized] = groups[baseSanitized].concat(nGroup);

                    //Change the mapping of each moved desk to the base's
                    for (var g = 0; g < nGroup.length; g++){
                        mappedDesks[nGroup[g]] = baseSanitized;          
                    }
                }    



                //Delete the reference to the old group
                delete groups[mappedDesks[nSanitized]];    

                //Update the mappings for the desks dealt with
                mappedDesks[nSanitized] = baseSanitized;
                mappedDesks[baseSanitized] = baseSanitized;    

            } else {
                // The neighbour is lower than the current desk so the desk should be merged into the neighbour's group
                mappedDesks[baseSanitized] = nSanitized;
                groups[nSanitized].push(baseNodeRid);
            }
        }
    }    
}    

return groups;

我的问题来自访问顶点的邻居。它正确地确定 if 语句 return freeDesks[j].getRecord().field("out_NEIGHBOUR_OF") 中是否有邻居,但我希望能够获取每个邻居的 @rid,以便我可以将 @rids 分组。

freeDesks[j].getRecord().field("out_NEIGHBOUR_OF") 返回边缘记录,但我似乎无法使用 field() 方法获取“输入”或“输出”字段(在此对象上找不到)或将其作为数组 [] 访问。

[
    {
        "@type": "d",
        "@rid": "#34:18176",
        "@version": 6,
        "@class": "NEIGHBOUR_OF",
        "out": "#16:13",
        "in": "#16:1408",
        "@fieldTypes": "out=x,in=x"
    }
]

您能否帮助获取邻居@rids 的列表/数组,以便我可以使用其余代码对其进行迭代?

干杯!

【问题讨论】:

    标签: javascript graph-databases orientdb


    【解决方案1】:

    一个更简单的例子来理解你可以做什么:

    create class Person extends V    
    create class IsNeighbour extends E
    
    create vertex Person set name = 'P1'              // 12:0
    create vertex Person set name = 'P2'              // 12:1
    create vertex Person set name = 'P3'              // 12:2
    
    create edge IsNeighbour from #12:0 to #12:1
    create edge IsNeighbour from #12:0 to #12:2
    

    定义这个 Javascript 函数:

    var gdb = orient.getGraphNoTx();
    var v = gdb.command("sql", "select from " + personRID);
    var neighbours = v[0].getRecord().field("out_IsNeighbour").iterator();
    var result = [];
    
    while(neighbours.hasNext()){
      var neighbour = neighbours.next();
      result.push(neighbour.field("in"));
    }
    
    return result;
    

    像这样:

    然后你可以:

    select getNeighbours("#12:0")
    

    【讨论】:

    • 太棒了!感谢.iterator() 的提示!它返回每个邻居的完整记录,但我可以使用.getRecord().getIdentity().toString() 来获取每个邻居的@rid。很难从 Java 文档中说出我被允许在他们的 JS 函数中使用的内容,但我感谢你指出这一点
    • 很高兴它工作。我认为方法与 Java 相同。
    • @vitorenesduarte 你能告诉我你从哪里得到 getRecord() 和 iterator() 方法。我可以找到任何相关的文件。
    • @ArhamAliQureshi 老实说,我不记得了。但我认为它遵循 Java API。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多