Hi Luca
Herewith the example
import java.util.List;
import com.orientechnologies.orient.core.db.graph.OGraphVertex;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.metadata.schema.OProperty.INDEX_TYPE;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.tinkerpop.blueprints.pgm.Vertex;
import com.tinkerpop.blueprints.pgm.TransactionalGraph.Conclusion;
import com.tinkerpop.blueprints.pgm.TransactionalGraph.Mode;
import com.tinkerpop.blueprints.pgm.impls.orientdb.OrientGraph;
public class OrientDBTests {
/**
* @param args
*/
public static void main(String[] args) {
OrientGraph graph = new OrientGraph("remote:localhost/scratchpad", "admin", "admin");
graph.setTransactionMode(Mode.MANUAL);
graph.getRawGraph().getMetadata().getSchema().getClass(OGraphVertex.class).createProperty("name", OType.STRING).createIndex(INDEX_TYPE.UNIQUE);
graph.getRawGraph().getMetadata().getSchema().save();
addVertices(graph);
addEdges(graph);
List<ODocument> results = graph.getRawGraph().query(new OSQLSynchQuery<Vertex>("select from V where out contains (label = 'knows')"));
for (ODocument document : results) {
String[] fieldNames = document.fieldNames();
for (int i = 0; i < fieldNames.length; i++) {
String fieldName = (String) fieldNames[i];
System.out.println(fieldName);
}
}
graph.shutdown();
}
private static void addVertices(OrientGraph graph) {
graph.startTransaction();
for (int i = 0; i < 1000; i++) {
Vertex luca = graph.addVertex(null);
luca.setProperty("name", "Name-" + i);
System.out.println("Added Vertex : " + luca.getProperty("name") + " Id : " + luca.getId());
}
graph.stopTransaction(Conclusion.SUCCESS);
System.out.println("Committing Vertices !!!");
}
private static void addEdges(OrientGraph graph) {
OSQLSynchQuery<ODocument> query = new OSQLSynchQuery<ODocument>("SELECT FROM OGraphVertex WHERE name = ? or name = ?");
for (int j = 1; j < 500; j++) {
long q1 = 0;
long q2 = 0;
long g1 = 0;
long g2 = 0;
long add = 0;
long start = System.currentTimeMillis();
List<ODocument> results = graph.getRawGraph().query(query, "Name-" + j, "Name-" + (j*2));
long end = System.currentTimeMillis();
q1 = (end - start);
if (results != null ){
graph.startTransaction();
Vertex v1 = graph.getVertex(results.get(0).getIdentity());
Vertex v2 = graph.getVertex(results.get(1).getIdentity());
graph.addEdge(null, v1, v2, "knows");
graph.stopTransaction(Conclusion.SUCCESS);
}
System.out.println("Added Edge Stats for Edge " + j + "->" + (j*2));
}
}
}
PS. Let me know if somethig is obviously wrong
Regards
Ivanhoe