use of com.orientechnologies.orient.core.sql.query.OSQLSynchQuery in project orientdb by orientechnologies.
the class LuceneTransactionQueryTest method txRemoveTest.
@Test
public void txRemoveTest() {
db.begin();
ODocument doc = new ODocument("c1");
doc.field("p1", "abc");
OIndex<?> index = db.getMetadata().getIndexManager().getIndex("C1.p1");
db.save(doc);
String query = "select from C1 where p1 lucene \"abc\" ";
List<ODocument> vertices = ODatabaseRecordThreadLocal.INSTANCE.get().command(new OSQLSynchQuery<ODocument>(query)).execute();
Assert.assertEquals(vertices.size(), 1);
Assert.assertEquals(index.getSize(), 1);
db.commit();
query = "select from C1 where p1 lucene \"abc\" ";
vertices = db.command(new OSQLSynchQuery<ODocument>(query)).execute();
Assert.assertEquals(vertices.size(), 1);
Assert.assertEquals(index.getSize(), 1);
db.begin();
doc = new ODocument("c1");
doc.field("p1", "abc");
db.delete(vertices.get(0));
query = "select from C1 where p1 lucene \"abc\" ";
vertices = db.command(new OSQLSynchQuery<ODocument>(query)).execute();
Collection coll = (Collection) index.get("abc");
Assert.assertEquals(vertices.size(), 0);
Assert.assertEquals(coll.size(), 0);
Iterator iterator = coll.iterator();
int i = 0;
while (iterator.hasNext()) {
iterator.next();
i++;
}
Assert.assertEquals(i, 0);
Assert.assertEquals(index.getSize(), 0);
db.rollback();
query = "select from C1 where p1 lucene \"abc\" ";
vertices = db.command(new OSQLSynchQuery<ODocument>(query)).execute();
Assert.assertEquals(vertices.size(), 1);
Assert.assertEquals(index.getSize(), 1);
}
use of com.orientechnologies.orient.core.sql.query.OSQLSynchQuery in project orientdb by orientechnologies.
the class GraphDatabaseTest method nestedQuery.
@SuppressWarnings("unchecked")
public void nestedQuery() {
Vertex countryVertex1 = database.addVertex(null, "name", "UK", "area", "Europe", "code", "2");
Vertex cityVertex1 = database.addVertex(null, "name", "leicester", "lat", "52.64640", "long", "-1.13159");
Vertex cityVertex2 = database.addVertex(null, "name", "manchester", "lat", "53.47497", "long", "-2.25769");
database.addEdge(null, countryVertex1, cityVertex1, "owns");
database.addEdge(null, countryVertex1, cityVertex2, "owns");
database.commit();
String subquery = "select out('owns') from V where name = 'UK'";
List<OIdentifiable> result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>(subquery));
Assert.assertEquals(result.size(), 1);
Assert.assertEquals(((Collection) ((ODocument) result.get(0)).field("out")).size(), 2);
subquery = "select expand(out('owns')) from V where name = 'UK'";
result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>(subquery));
Assert.assertEquals(result.size(), 2);
for (int i = 0; i < result.size(); i++) {
// System.out.println("uno: " + result.get(i));
Assert.assertTrue(((ODocument) result.get(i).getRecord()).containsField("lat"));
}
String query = "select name, lat, long, distance(lat,long,51.5,0.08) as distance from (select expand(out('owns')) from V where name = 'UK') order by distance";
result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>(query));
Assert.assertEquals(result.size(), 2);
for (int i = 0; i < result.size(); i++) {
// System.out.println("dos: " + result.get(i));
Assert.assertTrue(((ODocument) result.get(i).getRecord()).containsField("lat"));
Assert.assertTrue(((ODocument) result.get(i).getRecord()).containsField("distance"));
}
}
use of com.orientechnologies.orient.core.sql.query.OSQLSynchQuery 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.orientechnologies.orient.core.sql.query.OSQLSynchQuery 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.orientechnologies.orient.core.sql.query.OSQLSynchQuery in project orientdb by orientechnologies.
the class JSONTest method testInnerDocCreation.
public void testInnerDocCreation() {
ODocument adamDoc = new ODocument("InnerDocCreation");
adamDoc.fromJSON("{\"name\":\"adam\"}");
adamDoc.save();
ODocument eveDoc = new ODocument("InnerDocCreation");
eveDoc.fromJSON("{\"@type\":\"d\",\"name\":\"eve\",\"friends\":[" + adamDoc.toJSON() + "]}");
eveDoc.save();
Map<ORID, ODocument> contentMap = new HashMap<ORID, ODocument>();
ODocument adam = new ODocument("InnerDocCreation");
adam.field("name", "adam");
contentMap.put(adamDoc.getIdentity(), adam);
ODocument eve = new ODocument("InnerDocCreation");
eve.field("name", "eve");
List<ORID> friends = new ArrayList<ORID>();
friends.add(adamDoc.getIdentity());
eve.field("friends", friends);
contentMap.put(eveDoc.getIdentity(), eve);
Map<ORID, List<ORID>> traverseMap = new HashMap<ORID, List<ORID>>();
List<ORID> adamTraverse = new ArrayList<ORID>();
adamTraverse.add(adamDoc.getIdentity());
traverseMap.put(adamDoc.getIdentity(), adamTraverse);
List<ORID> eveTraverse = new ArrayList<ORID>();
eveTraverse.add(eveDoc.getIdentity());
eveTraverse.add(adamDoc.getIdentity());
traverseMap.put(eveDoc.getIdentity(), eveTraverse);
for (ODocument o : database.browseClass("InnerDocCreation")) {
ODocument content = contentMap.get(o.getIdentity());
Assert.assertTrue(content.hasSameContentOf(o));
}
for (ODocument o : database.browseClass("InnerDocCreation")) {
List<ORID> traverse = traverseMap.remove(o.getIdentity());
for (OIdentifiable id : new OSQLSynchQuery<ODocument>("traverse * from " + o.getIdentity().toString())) {
Assert.assertTrue(traverse.remove(id.getIdentity()));
}
Assert.assertTrue(traverse.isEmpty());
}
Assert.assertTrue(traverseMap.isEmpty());
}
Aggregations