Skip to content

Instantly share code, notes, and snippets.

@marioosh
Created July 11, 2013 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marioosh/5974959 to your computer and use it in GitHub Desktop.
Save marioosh/5974959 to your computer and use it in GitHub Desktop.
Test for delete verticies in orientDB 1.4.1
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Created with IntelliJ IDEA.
* User: marioosh
* Date: 11.07.2013
* Time: 13:31
*/
public class deleteVertexTest {
@Test
public void deleteVertexBluprints() {
OrientGraph db = new OrientGraph("memory:test");
db.createVertexType("Person");
//create 3 verticies
Vertex v1 = db.addVertex("class:Person");
Vertex v2 = db.addVertex("class:Person");
Vertex v3 = db.addVertex("class:Person");
db.commit();
//3 are saved
assert (3 == db.countVertices("Person"));
//create edge from v1 likes v2 and v3
v1.addEdge("LIKES", v2);
v1.addEdge("LIKES", v3);
db.commit();
List<Vertex> v1LikesList = copyIterator(v1.getVertices(Direction.OUT, "LIKES").iterator());
List<Vertex> v2LikedList = copyIterator(v2.getVertices(Direction.IN, "LIKES").iterator());
List<Vertex> v3LikedList = copyIterator(v3.getVertices(Direction.IN, "LIKES").iterator());
assert 2 == v1LikesList.size(); //v1 likes 2 persons
assert 1 == v2LikedList.size(); //v2 is liked by 1 person
assert 1 == v3LikedList.size(); //v3 is liked by 1 person
//OSQL commands
//find somebody that likes somebody
Iterable<Vertex> likesSomebodyList = db.command(new OCommandSQL("select from Person where out_LIKES is not null")).execute();
List<Vertex> likesSomebody = copyIterator(likesSomebodyList.iterator());
assert 1 == likesSomebody.size();
//find somebody that is liked by somebody
Iterable<Vertex> likedBySomebodyList = db.command(new OCommandSQL("select from Person where in_LIKES is not null")).execute();
List<Vertex> likedBySomebody = copyIterator(likedBySomebodyList.iterator());
assert 2 == likedBySomebody.size();
/**
*
* NOW TRY TO DELETE v1
*
*/
db.removeVertex(v1);
db.commit();
//2 persons in database
assert (2 == db.countVertices("Person"));
//for sure reload verticies from database
Vertex v2reloaded = db.getVertex(v2.getId());
Vertex v3reloaded = db.getVertex(v3.getId());
v2LikedList = copyIterator(v2reloaded.getVertices(Direction.IN, "LIKES").iterator());
v3LikedList = copyIterator(v3reloaded.getVertices(Direction.IN, "LIKES").iterator());
assert 1 == v2LikedList.size(); //v2 is still liked by 1 person but should be liked by nobody now
// assert 0 == v2LikedList.size(); //it should be true
assert 1 == v3LikedList.size(); //v3 is still liked by 1 person but should be liked by nobody now
// assert 0 == v3LikedList.size(); //it should be true
//
//
//
//
//OSQL commands
//
//
//
//find somebody that likes somebody
likesSomebodyList = db.command(new OCommandSQL("select from Person where out_LIKES is not null")).execute();
likesSomebody = copyIterator(likesSomebodyList.iterator());
assert 0 == likesSomebody.size();//GOOD!! nobody likes other persons
//find somebody that is liked by somebody
likedBySomebodyList = db.command(new OCommandSQL("select from Person where in_LIKES is not null")).execute();
likedBySomebody = copyIterator(likedBySomebodyList.iterator());
assert 2 == likedBySomebody.size(); //are stil liked by v1? SHOULD BE FALSE
//then rid of v1 should be on liked list
//check it out
assert !likedBySomebody.contains(v1);//fail, liked list doesn't containt v1 vertex
assert 0 == likedBySomebody.size();//should be TRUE!
}
private <T> List<T> copyIterator(Iterator<T> iter) {
List<T> copy = new ArrayList<T>();
while (iter.hasNext())
copy.add(iter.next());
return copy;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment