在OrientDB Javascript函数中访问连接的顶点 [英] Accessing Connected Vertices in OrientDB Javascript Functions

查看:232
本文介绍了在OrientDB Javascript函数中访问连接的顶点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm attempting to use the build in Functions in OrientDB studio to group Workstations that aren't in use by a Person. The query to get these vertices works fine but I'm trying to avoid Traverse as it is very slow - too slow to be used in production. Instead of iterating through each free station and grouping it together with all it's neighbours, keeping each grouped 'named' with the smallest @rid in the set.

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,以便可以将@rid分为几类.

My problem comes from accessing a vertex's neighbours. It correctly determines whether there are neighbours in the if statement return freeDesks[j].getRecord().field("out_NEIGHBOUR_OF") but I want to be able to get each neighbour's @rid so I can sort the @rids into groups.

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

freeDesks[j].getRecord().field("out_NEIGHBOUR_OF") returns the edge record but I dont seem to be able to get the "in" or "out" fields using the field() method (not found on this object) or accessing it as an array [].

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

您能帮忙获取邻居@rids的列表/数组,以便我用其余的代码遍历它们吗?

Can you help be get a list/array of the neighbour @rids so I can iterate over them with the rest of the code?

干杯!

推荐答案

一个简单的示例,以了解您可以做什么:

A simpler example to understand what you can do:

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函数:

Define this Javascript Function:

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")

这篇关于在OrientDB Javascript函数中访问连接的顶点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆