}
final OrientVertex product;
final OrientVertex fishing;
OrientBaseGraph graph = localFactory.getTx();
try {
product = graph.addVertex("class:Product");
fishing = graph.addVertex("class:Hobby");
fishing.setProperty("name", "Fishing");
} finally {
graph.shutdown();
}
vertices = new OrientVertex[serverInstance.size()];
for (int i = 0; i < vertices.length; ++i) {
final String nodeName = serverInstance.get(i).getServerInstance().getDistributedManager().getLocalNodeName();
OrientGraphFactory factory = new OrientGraphFactory("plocal:target/server" + i + "/databases/" + getDatabaseName());
graph = factory.getNoTx();
try {
vertices[i] = graph.addVertex("class:Client");
final int clId = vertices[i].getIdentity().getClusterId();
if (i == 0)
Assert.assertEquals("Error on assigning cluster client", clId, graph.getRawGraph().getClusterIdByName("client"));
else {
final int clusterId = graph.getRawGraph().getClusterIdByName("client_" + nodeName);
Assert.assertEquals("Error on assigning cluster client_" + nodeName, clId, clusterId);
}
vertices[i].setProperty("name", "shard_" + i);
vertices[i].setProperty("amount", i * 10000);
System.out.println("Create vertex, class: " + vertices[i].getLabel() + ", cluster: " + clId + " -> "
+ vertices[i].getRecord());
if (i > 1)
// CREATE A LIGHT-WEIGHT EDGE
vertices[i].addEdge("Knows", vertices[i - 1]);
// CREATE A REGULAR EDGE
final Edge edge = vertices[i].addEdge("Buy", product, new Object[] { "price", 1000 * i });
} finally {
graph.shutdown();
}
}
for (int i = 0; i < vertices.length; ++i)
System.out.println("Created vertex " + i + ": " + vertices[i].getRecord());
for (int i = 0; i < vertices.length; ++i) {
OrientGraphFactory factory = new OrientGraphFactory("plocal:target/server" + i + "/databases/" + getDatabaseName());
graph = factory.getNoTx();
try {
// CREATE A REGULAR EDGE
Iterable<OrientEdge> result = graph.command(
new OCommandSQL("create edge Loves from " + vertices[i].getIdentity() + " to " + fishing.getIdentity()
+ " set real = true")).execute();
Assert.assertTrue(result.iterator().hasNext());
OrientEdge e = result.iterator().next();
Assert.assertEquals(e.getProperty("real"), true);
final OrientVertex explain = graph.command(new OCommandSQL("explain select from " + e.getIdentity())).execute();
System.out
.println("explain select from " + e.getIdentity() + " -> " + ((ODocument) explain.getRecord()).field("servers"));
result = graph.command(new OCommandSQL("select from " + e.getIdentity())).execute();
Assert.assertTrue(result.iterator().hasNext());
OrientEdge e2 = result.iterator().next();
Assert.assertEquals(e2.getProperty("real"), true);
} finally {
graph.shutdown();
}
}
// FOR ALL THE DATABASES QUERY THE SINGLE CLUSTER TO TEST ROUTING
for (int server = 0; server < vertices.length; ++server) {
OrientGraphFactory f = new OrientGraphFactory("plocal:target/server" + server + "/databases/" + getDatabaseName());
OrientGraphNoTx g = f.getNoTx();
System.out.println("Query from server " + server + "...");
try {
for (int cluster = 0; cluster < vertices.length; ++cluster) {
final String query = "select from cluster:client_" + cluster;
final OrientVertex explain = g.command(new OCommandSQL("explain " + query)).execute();
System.out.println("explain " + query + " -> " + ((ODocument) explain.getRecord()).field("servers"));
Iterable<OrientVertex> result = g.command(new OCommandSQL(query)).execute();
Assert.assertTrue("Error on query against 'cluster_" + cluster + "' on server '" + server + "': " + query, result
.iterator().hasNext());
OrientVertex v = result.iterator().next();
Assert.assertEquals("Returned vertices name property is != shard_" + cluster + " on server " + server, "shard_"
+ cluster, v.getProperty("name"));
final Iterable<Vertex> knows = v.getVertices(Direction.OUT, "Knows");
final Iterable<Vertex> boughtV = v.getVertices(Direction.OUT, "Buy");
Assert.assertTrue(boughtV.iterator().hasNext());
Assert.assertEquals(boughtV.iterator().next(), product);
final Iterable<Edge> boughtE = v.getEdges(Direction.OUT, "Buy");
Assert.assertNotNull(boughtE.iterator().next().getProperty("price"));
}
} finally {
graph.shutdown();
}
}
// TEST DISTRIBUTED QUERY + AGGREGATION + SUB_QUERY AGAINST ALL 3 DATABASES TO TEST MAP/REDUCE
for (int server = 0; server < vertices.length; ++server) {