在我的数据库上,运行以下查询:
SELECT @rid AS module_rid, out('USES').out('BELONGS_TO').@rid AS project_rid FROM MODULES LIMIT 10
我收到了以下回复:
module_rid | project_rid
-----------|----------------
#12:0 | []
#12:1 | []
#12:2 | []
#12:3 |
#11:48677 | #11:48677 #11:48677 #11:48677 #11:48677 #11:48677 ..More(49)
#12:4 |
#11:48677 | #11:48677 #11:48677 #11:48677 #11:48677 #11:48677 ..More(49)
#12:5 |
#11:2526 | #11:2526 #11:2526 #11:47148 #11:47148 #11:25338 ..More(30)
#12:6 | []
如何在模块和它们所依赖的项目(它们至少使用项目的一个模块)之间创建边缘(例如RELIES_ON)?
这就是我最终要做的事情:
createEdges
使用三个参数定义函数:createEdges(from, to, type)
// Check whether "from" is invalid or empty
if (from instanceof java.util.Collection) {
if (from.isEmpty()) {
return [];
} else {
var it = from.iterator();
var obj = it.next();
if (!(obj instanceof com.orientechnologies.orient.core.id.ORecordId)) {
throw "Bad Input: createdEdges() only accepts ORecordIds or Collections of ORecordIds";
}
}
} else if (!(from instanceof com.orientechnologies.orient.core.id.ORecordId)) {
throw "Bad Input: createdEdges() only accepts ORecordIds or Collections of ORecordIds";
}
// Check whether "to" is invalid or empty
if (to instanceof java.util.Collection) {
if (to.isEmpty()) {
return [];
} else {
var it = to.iterator();
var obj = it.next();
if (!(obj instanceof com.orientechnologies.orient.core.id.ORecordId)) {
throw "Bad Input: createdEdges() only accepts ORecordIds or Collections of ORecordIds";
}
}
} else if (!(to instanceof com.orientechnologies.orient.core.id.ORecordId)) {
throw "Bad Input: createdEdges() only accepts ORecordIds or Collections of ORecordIds";
}
var g = orient.getGraph();
var cmd = "CREATE EDGE " + type + " FROM " + from + " TO " + to;
return g.command("sql", cmd);
uniq
使用一个参数定义函数:uniq(collection)
if (collection instanceof java.util.Collection) {
if (collection.isEmpty()) {
return collection
} else {
return new java.util.HashSet(collection)
}
} else {
throw "Bad Input: uniq() only accepts Java collections as input"
}
现在,我可以运行以下SQL命令:
SELECT createEdges(src, dst, 'RELIES_ON') FROM
(SELECT @rid AS src, uniq(out('USES').out('BELONGS_TO').@rid) AS dst FROM Modules)
这将在模块和它们依赖的项目之间创建独特的优势,而且速度非常快。
感谢@vitorenesduarte,他提供了最初的回应和主要灵感...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句