![](/img/trans.png)
[英]How to prevent creating duplicates of edges between the same vertices in OrientDB?
[英]OrientDB SQL: How to find vertices and create edges between them?
在我的数据库上,运行以下查询:
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)?
create class Module extends V
create class Project extends V
create class Uses extends E
create class ReliesOn extends E
create vertex Module set name = 'm1'
create vertex Module set name = 'm2'
create vertex Module set name = 'm3'
create vertex Project set name = 'p1'
create vertex Project set name = 'p2'
create vertex Project set name = 'p3'
create edge Uses from (select from Module where name = 'm2') to (select from Project where name = 'p1')
create edge Uses from (select from Module where name = 'm3') to (select from Project where name = 'p2')
create edge Uses from (select from Module where name = 'm3') to (select from Project where name = 'p3')
我了解上述情况与您所遇到的情况有些不同,但是我相信足以了解可能解决您的问题的方法。
您可以定义一个函数createEdges例如,如下所示:
var gdb = orient.getGraph();
if(to.size() != 0){
var command = "create edge ReliesOn from " + from + " to " + to;
gdb.command("sql", command);
}
return;
现在,以下查询将在创建边的同时找到顶点:
select from (
select @rid as module_rid, out('Uses').@rid as project_rid from Module
)
let $ce = createEdges(module_rid, project_rid)
更新:
如果要确保“收件人”不包含重复项,则可以:
select from (
select @rid as module_rid, $aux[0].set.@rid as project_rid from Module
let $aux = ( select set(out('Uses')) from $current )
)
let $ce = createEdges(module_rid, project_rid)
这就是我最终要做的事情:
使用三个参数定义函数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,他提供了最初的回应和主要灵感...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.