[英]Accessing Connected Vertices in OrientDB Javascript Functions
我正在嘗試使用OrientDB studio中的“函數”中的內置功能將未使用的Workstation分組。 獲取這些頂點的查詢工作正常,但是我試圖避免使用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,因此可以將@rid分為幾類。
freeDesks[j].getRecord().field("out_NEIGHBOUR_OF")
返回邊緣記錄,但是我似乎無法使用field()方法獲取“ in”或“ out”字段(在此對象上找不到)或將其作為數組[]訪問。
[
{
"@type": "d",
"@rid": "#34:18176",
"@version": 6,
"@class": "NEIGHBOUR_OF",
"out": "#16:13",
"in": "#16:1408",
"@fieldTypes": "out=x,in=x"
}
]
您能否幫助獲得鄰居@rids的列表/數組,以便我可以用其余代碼遍歷它們?
干杯!
一個簡單的示例,以了解您可以做什么:
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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.