[英]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.