use of com.tinkerpop.blueprints.Vertex in project orientdb by orientechnologies.
the class OrientVertexIterator method createGraphElement.
@Override
public Vertex createGraphElement(final Object iObject) {
if (iObject instanceof OrientVertex)
return (OrientVertex) iObject;
if (iObject == null) {
return null;
}
final ORecord rec = ((OIdentifiable) iObject).getRecord();
if (rec == null || !(rec instanceof ODocument))
return null;
final ODocument value = (ODocument) rec;
final OImmutableClass immutableClass = ODocumentInternal.getImmutableSchemaClass(value);
return OGraphCommandExecutorSQLFactory.runWithAnyGraph(new OGraphCommandExecutorSQLFactory.GraphCallBack<Vertex>() {
@Override
public Vertex call(OrientBaseGraph graph) {
final OrientVertex v;
if (immutableClass.isVertexType()) {
// DIRECT VERTEX
v = graph.getVertex(value);
} else if (immutableClass.isEdgeType()) {
// EDGE
if (vertex.settings.isUseVertexFieldsForEdgeLabels() || OrientEdge.isLabeled(OrientEdge.getRecordLabel(value), iLabels))
v = graph.getVertex(OrientEdge.getConnection(value, connection.getKey().opposite()));
else
v = null;
} else
throw new IllegalStateException("Invalid content found between connections: " + value);
return v;
}
});
}
use of com.tinkerpop.blueprints.Vertex in project orientdb by orientechnologies.
the class IndexTest method testPreservingIdentityInIndexTx.
public void testPreservingIdentityInIndexTx() {
OrientGraph graph = new OrientGraph((ODatabaseDocumentTx) database.getUnderlying(), true);
graph.setAutoScaleEdgeType(true);
OrientVertexType fieldClass = graph.getVertexType("PreservingIdentityInIndexTxChild");
if (fieldClass == null) {
fieldClass = graph.createVertexType("PreservingIdentityInIndexTxChild");
fieldClass.createProperty("name", OType.STRING);
fieldClass.createProperty("in_field", OType.LINK);
fieldClass.createIndex("nameParentIndex", OClass.INDEX_TYPE.NOTUNIQUE, "in_field", "name");
}
Vertex parent = graph.addVertex("class:PreservingIdentityInIndexTxParent");
Vertex child = graph.addVertex("class:PreservingIdentityInIndexTxChild");
parent.addEdge("preservingIdentityInIndexTxEdge", child);
child.setProperty("name", "pokus");
Vertex parent2 = graph.addVertex("class:PreservingIdentityInIndexTxParent");
Vertex child2 = graph.addVertex("class:PreservingIdentityInIndexTxChild");
parent2.addEdge("preservingIdentityInIndexTxEdge", child2);
child2.setProperty("name", "pokus2");
graph.commit();
{
fieldClass = graph.getVertexType("PreservingIdentityInIndexTxChild");
OIndex<?> index = fieldClass.getClassIndex("nameParentIndex");
OCompositeKey key = new OCompositeKey(parent.getId(), "pokus");
Set<ORecordId> h = (Set<ORecordId>) index.get(key);
for (ORecordId o : h) {
Assert.assertNotNull(graph.getVertex(o));
}
}
{
fieldClass = graph.getVertexType("PreservingIdentityInIndexTxChild");
OIndex<?> index = fieldClass.getClassIndex("nameParentIndex");
OCompositeKey key = new OCompositeKey(parent2.getId(), "pokus2");
Set<ORecordId> h = (Set<ORecordId>) index.get(key);
for (ORecordId o : h) {
Assert.assertNotNull(graph.getVertex(o));
}
}
parent.remove();
child.remove();
parent2.remove();
child2.remove();
graph.shutdown();
}
use of com.tinkerpop.blueprints.Vertex in project orientdb by orientechnologies.
the class OrientdbEdgeTest method testEdges.
@Test
public void testEdges() throws Exception {
OrientGraphFactory factory = getGraphFactory();
OrientBaseGraph g = factory.getNoTx();
try {
try {
g.createEdgeType("some-label");
} catch (OSchemaException ex) {
if (!ex.getMessage().contains("exists"))
throw (ex);
g.command(new OCommandSQL("delete edge some-label")).execute();
}
try {
g.createVertexType("some-v-label");
} catch (OSchemaException ex) {
if (!ex.getMessage().contains("exists"))
throw (ex);
g.command(new OCommandSQL("delete vertex some-v-label")).execute();
}
} finally {
g.shutdown();
}
OrientGraph t = factory.getTx();
try {
Vertex v1 = t.addVertex("class:some-v-label");
Vertex v2 = t.addVertex("class:some-v-label");
v1.setProperty("_id", "v1");
v2.setProperty("_id", "v2");
OrientEdge edge = t.addEdge(null, v1, v2, "some-label");
edge.setProperty("some", "thing");
t.commit();
t.shutdown();
t = factory.getTx();
assertEquals(2, t.countVertices("some-v-label"));
assertEquals(1, t.countEdges());
assertNotNull(t.getVertices("_id", "v1").iterator().next());
assertNotNull(t.getVertices("_id", "v2").iterator().next());
t.commit();
t.shutdown();
t = factory.getTx();
// works
assertEquals(1, t.getVertices("_id", "v1").iterator().next().query().labels("some-label").count());
// NoSuchElementException
assertNotNull(t.getVertices("_id", "v1").iterator().next().query().labels("some-label").edges().iterator().next());
t.commit();
} finally {
t.shutdown();
}
}
use of com.tinkerpop.blueprints.Vertex in project orientdb by orientechnologies.
the class DistributedConfigReloadTest method startCreateDeleteVertex.
private Runnable startCreateDeleteVertex(final int id, final OrientGraphFactory graphFactory, final String className) {
Runnable th = new Runnable() {
@Override
public void run() {
log("Starting runnable to create index " + className);
long st = System.currentTimeMillis();
OrientGraph graph = graphFactory.getTx();
boolean isSelectSuccessful = true;
try {
boolean isRunning = true;
for (int j = 0; j < 1000000; j++) {
isSelectSuccessful = true;
String sql = "SELECT FROM " + className;
int deleteErrorCounter = 0;
try {
graph.command(new OCommandSQL(sql)).execute();
Iterable<Vertex> vtxs = graph.command(new OCommandSQL(sql)).execute();
for (Vertex vtx : vtxs) {
boolean needRetry = true;
for (int i = 0; i < 10 && needRetry; i++) {
try {
vtx.remove();
graph.commit();
needRetry = false;
} catch (ONeedRetryException ex) {
try {
((OrientElement) vtx).reload();
} catch (ORecordNotFoundException e) {
// BY LUCA
log("[" + id + "] Caught [" + e + "] during reload because the record was already deleted, no errors just go ahead");
}
} catch (ORecordNotFoundException e) {
// BY LUCA
log("[" + id + "] Caught [" + e + "] because the record was already deleted, no errors just go ahead");
} catch (Exception ex) {
log("[" + j + "] Failed to delete vertex [" + className + "] Vertex: [" + vtx + "] Property 1: [" + vtx.getProperty("property1") + "] Error: [" + ex + "]");
deleteErrorCounter++;
needRetry = false;
}
}
}
log(" [" + id + "] Delete vertex : [" + j + "] [" + className + "]");
} catch (Exception ex) {
log("***************** [" + id + "] Failed to select vertex [" + className + "][" + ex + "]");
isSelectSuccessful = false;
}
if (isSelectSuccessful) {
graph.command(new OCommandSQL(sql)).execute();
Iterable<Vertex> vtxs = graph.command(new OCommandSQL(sql)).execute();
ArrayList<String> vtxList = new ArrayList();
for (Vertex vtx : vtxs) {
vtxList.add(vtx.toString());
}
if (vtxList.size() > 0) {
log("########## [" + id + "] Records present after delete vertex [" + className + "] Error on delete [" + deleteErrorCounter + "] List: " + vtxList + "");
} else {
log("########## [" + id + "] Records removed after delete vertex [" + className + "] Error on delete [" + deleteErrorCounter + "]");
}
boolean showException = true;
int counter = 0;
for (int i = 1; i < 2000 && isRunning; i++) {
if ((i % 2000) == 0) {
long et = System.currentTimeMillis();
log(" [" + id + "] Total Records Processed: [" + i + "] Time taken for [2000] records: [" + (et - st) / 2000 + "] seconds");
st = System.currentTimeMillis();
}
Vertex vertex = graph.addVertex("class:" + className);
try {
vertex.setProperty("property1", "value-" + id + "-" + i);
vertex.setProperty("property2", "value2-" + (System.currentTimeMillis() + "-" + i));
vertex.setProperty("property3", "value3-" + i);
vertex.setProperty("property4", "value4-1");
vertex.setProperty("prop-6", "value6-" + i);
vertex.setProperty("prop-7", "value7-" + i);
vertex.setProperty("prop-8", "value7-1");
vertex.setProperty("prop-9", "value7-1");
vertex.setProperty("prop-10", "value7-1");
vertex.setProperty("prop11", "value7-1");
vertex.setProperty("prop12", "value7-1");
vertex.setProperty("prop13", "value7-1");
vertex.setProperty("prop14", System.currentTimeMillis());
vertex.setProperty("prop15", System.currentTimeMillis());
graph.commit();
} catch (ONeedRetryException ex) {
if (ex instanceof ONeedRetryException || ex.getCause() instanceof ONeedRetryException) {
log("[" + id + "] OrientDB Retry Exception [" + ex + "]");
} else {
log("[" + id + "] OrientDB Exception [" + ex + "]");
}
if (!(ex instanceof ODistributedConfigurationChangedException || ex.getCause() instanceof ODistributedConfigurationChangedException)) {
//reloadVertex(vertex, ex);
} else {
log("[" + id + "] ODistributedConfigurationChangedException {} while updating vertex " + vertex);
}
} catch (ORecordDuplicatedException ex) {
// BY LUCA
log("[" + id + "] Caught [" + ex + "], no errors just go ahead");
} catch (ODistributedException ex) {
if (ex.getCause() instanceof ONeedRetryException) {
log("[" + id + "] OrientDB Retry Exception [" + ex + "]");
} else {
log("[" + id + "] OrientDB Exception [" + ex + "]");
}
if (!(ex.getCause() instanceof ODistributedConfigurationChangedException)) {
//reloadVertex(vertex, ex);
} else {
log("[" + id + "] ODistributedConfigurationChangedException {} while updating vertex " + vertex);
}
} catch (Exception ex) {
if (showException) {
log("[" + id + "] Failed to create record Exception [" + ex + "]");
showException = false;
counter++;
}
}
}
log("************ [" + id + "] Total number of errors: [" + counter + "] Delete error counter:[" + deleteErrorCounter + "]");
} else {
log("##################### [" + id + "] Select failed. Skipping create..");
}
}
} finally {
graph.shutdown();
}
}
};
return th;
}
use of com.tinkerpop.blueprints.Vertex 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"));
}
}
Aggregations