Reputation: 497
Consider we have Vertex User and Edge FriendsWith. FriendsWith can be in both directions out and in (usually it is either out or in between 2 users).
Duplicates are when either out or in from one user to another is found more then one time (out and in together is not considered as duplicate)
Is there any way to find duplicate edges and delete them?
UPDATE Added picture illustrating problem
Thank you.
Upvotes: 2
Views: 1280
Reputation: 1982
here is my javascript function:
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]);
}
Hope it helps. Bye
Upvotes: 5
Reputation: 3570
You can try this function
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--;
}
}
}
}
Upvotes: 3
Reputation: 1418
I created a small DB to try your case. This is my code:
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)
Graph:
Then I created a simple Javascript function which deletes, once found a duplicated edge, the in-direction edge.
Input: ridA (yout starting @rid)
Code:
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);
}
}
}
Edit:
For example, if you try to delete the duplicated edges from the vertex #12:1, after having launched the function the two in-direction edges 'follow' will be deleted.
Upvotes: 2