在 OrientDb 中查找并删除重复边
Find and delete duplicate edges in OrientDb
假设我们有 Vertex User 和 Edge FriendsWith。
FriendsWith 可以在两个方向上进出(通常是在 2 个用户之间)。
重复是指从一个用户到另一个用户的出入次数超过一次(一起出入不被视为重复)
有没有办法找到重复的边并删除它们?
更新 添加了说明问题的图片
谢谢。
我创建了一个小型数据库来尝试您的案例。这是我的代码:
create class User extends V
create class follows extends E
create property User.id integer
create property User.name String
create vertex User set id=1, name="Paul"
create vertex User set id=2, name="John"
create vertex User set id=3, name="Mark"
create vertex User set id=4, name="Robert"
create edge follows from (select from User where id=1) to (select from User where id=2)
create edge follows from (select from User where id=2) to (select from User where id=1)
create edge follows from (select from User where id=1) to (select from User where id=3)
create edge follows from (select from User where id=2) to (select from User where id=3)
create edge follows from (select from User where id=3) to (select from User where id=2)
create edge follows from (select from User where id=3) to (select from User where id=4)
图表:
然后我创建了一个简单的 Javascript 函数,一旦找到重复的边,就会删除 in-direction 边。
输入:ridA(你开始@rid)
代码:
var g=orient.getGraph();
var outF=g.command('sql','select expand(out("follows")) from '+ridA);
var inF=g.command('sql','select expand(in("follows")) from '+ridA);
for(x=0;x<outF.length;x++){
var ridOut=outF[x].getId();
for(y=0;y<inF.length;y++){
var ridIn=inF[y].getId();
if(ridOut==ridIn){
g.command('sql','delete edge follows from '+ridIn+' to '+ridA);
}
}
}
编辑:
例如,如果您尝试删除顶点 #12:1 的重复边,则在启动该函数后,两条 in-direction 边 'follow' 将被删除。
你可以试试这个功能
var g=orient.getGraph();
var friends=g.command("sql","select from FriendsWith");
var paths=[];
for(i=0;i<friends.length;i++){
paths.push(friends[i]);
}
for(i=0;i<paths.length;i++){
var myEdge=paths[i];
var vIn=myEdge.getProperty("in").getId();
var vOut=myEdge.getProperty("out").getId();
for(j=0;j<paths.length;j++){
if(i<j){
var edge=paths[j];
var vInCopy=edge.getProperty("in").getId();
var vOutCopy=edge.getProperty("out").getId();
if((vIn==vInCopy && vOut==vOutCopy) || (vIn==vOutCopy && vOut==vInCopy)){
g.command("sql","delete edge FriendsWith where @rid="+edge.getId());
paths.splice(j, 1);
j--;
}
}
}
}
之前
后
这是我的 javascript 函数:
var g=orient.getGraph();
var C=g.command('sql','select from FriendsWith');
var arr = new Array(C.length);
var toRemove = new Array();
for(i=0;i<C.length;i++){
var found = false;
for (x = 0; x < i+1 && !found; x++) {
if (arr[x] === C[i].getProperty("out").getId()+" "+C[i].getProperty("in").getId()) {
found = true;
toRemove.push(C[i].getId());
}
}
arr[i] = C[i].getProperty("out").getId()+" "+C[i].getProperty("in").getId();
}
for(a=0;a<toRemove.length;a++){
var C=g.command('sql','delete edge '+toRemove[a]);
}
希望对您有所帮助。
再见
假设我们有 Vertex User 和 Edge FriendsWith。 FriendsWith 可以在两个方向上进出(通常是在 2 个用户之间)。
重复是指从一个用户到另一个用户的出入次数超过一次(一起出入不被视为重复)
有没有办法找到重复的边并删除它们?
更新 添加了说明问题的图片
谢谢。
我创建了一个小型数据库来尝试您的案例。这是我的代码:
create class User extends V
create class follows extends E
create property User.id integer
create property User.name String
create vertex User set id=1, name="Paul"
create vertex User set id=2, name="John"
create vertex User set id=3, name="Mark"
create vertex User set id=4, name="Robert"
create edge follows from (select from User where id=1) to (select from User where id=2)
create edge follows from (select from User where id=2) to (select from User where id=1)
create edge follows from (select from User where id=1) to (select from User where id=3)
create edge follows from (select from User where id=2) to (select from User where id=3)
create edge follows from (select from User where id=3) to (select from User where id=2)
create edge follows from (select from User where id=3) to (select from User where id=4)
图表:
然后我创建了一个简单的 Javascript 函数,一旦找到重复的边,就会删除 in-direction 边。
输入:ridA(你开始@rid)
代码:
var g=orient.getGraph();
var outF=g.command('sql','select expand(out("follows")) from '+ridA);
var inF=g.command('sql','select expand(in("follows")) from '+ridA);
for(x=0;x<outF.length;x++){
var ridOut=outF[x].getId();
for(y=0;y<inF.length;y++){
var ridIn=inF[y].getId();
if(ridOut==ridIn){
g.command('sql','delete edge follows from '+ridIn+' to '+ridA);
}
}
}
编辑:
例如,如果您尝试删除顶点 #12:1 的重复边,则在启动该函数后,两条 in-direction 边 'follow' 将被删除。
你可以试试这个功能
var g=orient.getGraph();
var friends=g.command("sql","select from FriendsWith");
var paths=[];
for(i=0;i<friends.length;i++){
paths.push(friends[i]);
}
for(i=0;i<paths.length;i++){
var myEdge=paths[i];
var vIn=myEdge.getProperty("in").getId();
var vOut=myEdge.getProperty("out").getId();
for(j=0;j<paths.length;j++){
if(i<j){
var edge=paths[j];
var vInCopy=edge.getProperty("in").getId();
var vOutCopy=edge.getProperty("out").getId();
if((vIn==vInCopy && vOut==vOutCopy) || (vIn==vOutCopy && vOut==vInCopy)){
g.command("sql","delete edge FriendsWith where @rid="+edge.getId());
paths.splice(j, 1);
j--;
}
}
}
}
之前
这是我的 javascript 函数:
var g=orient.getGraph();
var C=g.command('sql','select from FriendsWith');
var arr = new Array(C.length);
var toRemove = new Array();
for(i=0;i<C.length;i++){
var found = false;
for (x = 0; x < i+1 && !found; x++) {
if (arr[x] === C[i].getProperty("out").getId()+" "+C[i].getProperty("in").getId()) {
found = true;
toRemove.push(C[i].getId());
}
}
arr[i] = C[i].getProperty("out").getId()+" "+C[i].getProperty("in").getId();
}
for(a=0;a<toRemove.length;a++){
var C=g.command('sql','delete edge '+toRemove[a]);
}
希望对您有所帮助。 再见