use of com.tinkerpop.blueprints.Edge in project orientdb by orientechnologies.
the class OGraphSONUtility method edgeFromJson.
/**
* Creates an edge from GraphSON using settings supplied in the constructor.
*/
public Edge edgeFromJson(final JsonNode json, final Vertex out, final Vertex in) throws IOException {
final Map<String, Object> props = OGraphSONUtility.readProperties(json, true, this.hasEmbeddedTypes);
final Object edgeId = getTypedValueFromJsonNode(json.get(GraphSONTokens._ID));
final JsonNode nodeLabel = json.get(GraphSONTokens._LABEL);
// assigned an empty string edge label in cases where one does not exist. this gets around the requirement
// that blueprints graphs have a non-null label while ensuring that GraphSON can stay flexible in parsing
// partial bits from the JSON. Not sure if there is any gotchas developing out of this.
final String label = nodeLabel == null ? EMPTY_STRING : nodeLabel.textValue();
final Edge e = factory.createEdge(edgeId, out, in, label);
for (Map.Entry<String, Object> entry : props.entrySet()) {
// if (this.edgePropertyKeys == null || this.edgePropertyKeys.contains(entry.getKey())) {
if (includeKey(entry.getKey(), edgePropertyKeys, this.edgePropertiesRule)) {
e.setProperty(entry.getKey(), entry.getValue());
}
}
return e;
}
use of com.tinkerpop.blueprints.Edge in project orientdb by orientechnologies.
the class GraphDatabaseTest method sqlNestedQueries.
@Test
public void sqlNestedQueries() {
Vertex vertex1 = database.addVertex(null, "driver", "John");
Vertex vertex2 = database.addVertex(null, "car", "ford");
Vertex targetVertex = database.addVertex(null, "car", "audi");
Edge edge = database.addEdge(null, vertex1, vertex2, "E");
edge.setProperty("color", "red");
edge.setProperty("action", "owns");
edge = database.addEdge(null, vertex1, targetVertex, "E");
edge.setProperty("color", "red");
edge.setProperty("action", "wants");
database.commit();
String query1 = "select driver from V where out().car contains 'ford'";
List<ODocument> result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>(query1));
Assert.assertEquals(result.size(), 1);
String query2 = "select driver from V where outE()[color='red'].inV().car contains 'ford'";
result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>(query2));
Assert.assertEquals(result.size(), 1);
// TODO these tests are broken, they should test "contains" instead of "="
String query3 = "select driver from V where outE()[action='owns'].inV().car = 'ford'";
result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>(query3));
Assert.assertEquals(result.size(), 1);
String query4 = "select driver from V where outE()[color='red'][action='owns'].inV().car = 'ford'";
result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>(query4));
Assert.assertEquals(result.size(), 1);
}
use of com.tinkerpop.blueprints.Edge in project orientdb by orientechnologies.
the class GraphDatabaseTest method testDeleteOfVerticesAndEdgesWithDeleteCommandAndUnsafe.
public void testDeleteOfVerticesAndEdgesWithDeleteCommandAndUnsafe() {
Iterable<OIdentifiable> deletedVertices = database.command(new OCommandSQL("delete from GraphVehicle return before limit 1 unsafe")).execute();
Assert.assertTrue(deletedVertices.iterator().hasNext());
OrientVertex v = (OrientVertex) deletedVertices.iterator().next();
Integer confirmDeleted = database.command(new OCommandSQL("delete from " + v.getIdentity() + " unsafe")).execute();
Assert.assertFalse(deletedVertices.iterator().hasNext());
Assert.assertEquals(confirmDeleted.intValue(), 0);
Iterable<Edge> edges = v.getEdges(Direction.BOTH);
for (Edge e : edges) {
Integer deletedEdges = database.command(new OCommandSQL("delete from " + ((OrientEdge) e).getIdentity() + " unsafe")).execute();
Assert.assertEquals(deletedEdges.intValue(), 1);
}
}
use of com.tinkerpop.blueprints.Edge in project orientdb by orientechnologies.
the class GraphDatabaseTest method populate.
@Test
public void populate() {
OClass vehicleClass = database.createVertexType("GraphVehicle");
database.createVertexType("GraphCar", vehicleClass);
database.createVertexType("GraphMotocycle", "GraphVehicle");
ODocument carNode = database.addVertex("class:GraphCar", "brand", "Hyundai", "model", "Coupe", "year", 2003).getRecord();
ODocument motoNode = database.addVertex("class:GraphMotocycle", "brand", "Yamaha", "model", "X-City 250", "year", 2009).getRecord();
database.commit();
database.addEdge(null, database.getVertex(carNode), database.getVertex(motoNode), "E").save();
List<ODocument> result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>("select from GraphVehicle"));
Assert.assertEquals(result.size(), 2);
for (ODocument v : result) {
Assert.assertTrue(v.getSchemaClass().isSubClassOf(vehicleClass));
}
database.shutdown();
database = new OrientGraph(url);
database.setUseLightweightEdges(false);
database.getRawGraph().getMetadata().getSchema().reload();
result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>("select from GraphVehicle"));
Assert.assertEquals(result.size(), 2);
Edge edge1 = null;
Edge edge2 = null;
for (ODocument v : result) {
Assert.assertTrue(v.getSchemaClass().isSubClassOf("GraphVehicle"));
if (v.getClassName().equals("GraphCar")) {
Assert.assertEquals(database.getVertex(v).countEdges(Direction.OUT), 1);
edge1 = database.getVertex(v).getEdges(Direction.OUT).iterator().next();
} else {
Assert.assertEquals(database.getVertex(v).countEdges(Direction.IN), 1);
edge2 = database.getVertex(v).getEdges(Direction.IN).iterator().next();
}
}
Assert.assertEquals(edge1, edge2);
}
use of com.tinkerpop.blueprints.Edge in project orientdb by orientechnologies.
the class ServerClusterAsyncGraphTest method executeTest.
@Override
protected void executeTest() throws Exception {
{
OrientGraphFactory factory = new OrientGraphFactory("plocal:target/server0/databases/" + getDatabaseName());
OrientGraphNoTx g = factory.getNoTx();
try {
g.createVertexType("Post");
g.createVertexType("User");
g.createEdgeType("Own");
g.addVertex("class:User");
g.command(new OCommandSQL("insert into Post (content, timestamp) values('test', 1)")).execute();
} finally {
g.shutdown();
}
}
// CHECK VERTEX CREATION ON ALL THE SERVERS
for (int s = 0; s < SERVERS; ++s) {
OrientGraphFactory factory2 = new OrientGraphFactory("plocal:target/server" + s + "/databases/" + getDatabaseName());
OrientGraphNoTx g2 = factory2.getNoTx();
try {
Iterable<OrientVertex> result = g2.command(new OCommandSQL("select from Post")).execute();
Assert.assertTrue(result.iterator().hasNext());
Assert.assertNotNull(result.iterator().next());
} finally {
g2.shutdown();
}
}
{
OrientGraphFactory factory = new OrientGraphFactory("plocal:target/server0/databases/" + getDatabaseName());
OrientGraphNoTx g = factory.getNoTx();
try {
g.command(new OCommandSQL("create edge Own from (select from User) to (select from Post)").onAsyncReplicationError(new OAsyncReplicationError() {
@Override
public ACTION onAsyncReplicationError(Throwable iException, int iRetry) {
return iException instanceof ONeedRetryException && iRetry <= 3 ? ACTION.RETRY : ACTION.IGNORE;
}
})).execute();
} finally {
g.shutdown();
}
}
Thread.sleep(1000);
// CHECK VERTEX CREATION ON ALL THE SERVERS
for (int s = 0; s < SERVERS; ++s) {
OrientGraphFactory factory2 = new OrientGraphFactory("plocal:target/server" + s + "/databases/" + getDatabaseName());
OrientGraphNoTx g2 = factory2.getNoTx();
try {
Iterable<OrientVertex> result = g2.command(new OCommandSQL("select from Own")).execute();
Assert.assertTrue(result.iterator().hasNext());
Assert.assertNotNull(result.iterator().next());
result = g2.command(new OCommandSQL("select from Post")).execute();
Assert.assertTrue(result.iterator().hasNext());
final OrientVertex v = result.iterator().next();
Assert.assertNotNull(v);
final Iterable<Edge> inEdges = v.getEdges(Direction.IN);
Assert.assertTrue(inEdges.iterator().hasNext());
Assert.assertNotNull(inEdges.iterator().next());
result = g2.command(new OCommandSQL("select from User")).execute();
Assert.assertTrue(result.iterator().hasNext());
final OrientVertex v2 = result.iterator().next();
Assert.assertNotNull(v2);
final Iterable<Edge> outEdges = v2.getEdges(Direction.OUT);
Assert.assertTrue(outEdges.iterator().hasNext());
Assert.assertNotNull(outEdges.iterator().next());
} finally {
g2.shutdown();
}
}
}
Aggregations