use of com.tinkerpop.blueprints.impls.orient.OrientVertex in project orientdb by orientechnologies.
the class OServerCommandGetGephi method generateGraphDbOutput.
protected void generateGraphDbOutput(final Iterable<OrientElement> iVertices, final OJSONWriter json) throws IOException {
if (iVertices == null)
return;
// CREATE A SET TO SPEED UP SEARCHES ON VERTICES
final Set<OrientVertex> vertexes = new HashSet<OrientVertex>();
final Set<OrientEdge> edges = new HashSet<OrientEdge>();
for (OrientElement id : iVertices) if (id instanceof OrientVertex)
vertexes.add((OrientVertex) id);
else
edges.add((OrientEdge) id);
for (OrientVertex vertex : vertexes) {
json.resetAttributes();
json.beginObject(0, false, null);
json.beginObject(1, false, "an");
json.beginObject(2, false, vertex.getIdentity());
// ADD ALL THE EDGES
for (Edge e : vertex.getEdges(Direction.BOTH)) edges.add((OrientEdge) e);
// ADD ALL THE PROPERTIES
for (String field : vertex.getPropertyKeys()) {
final Object v = vertex.getProperty(field);
if (v != null)
json.writeAttribute(3, false, field, v);
}
json.endObject(2, false);
json.endObject(1, false);
json.endObject(0, false);
json.newline();
}
for (OrientEdge edge : edges) {
final ORID sourceVertex = (ORID) edge.getVertex(Direction.OUT).getId();
final ORID targetVertex = (ORID) edge.getVertex(Direction.IN).getId();
if (!vertexes.contains(sourceVertex) || !vertexes.contains(targetVertex))
continue;
json.resetAttributes();
json.beginObject();
json.beginObject(1, false, "ae");
json.beginObject(2, false, edge.getId());
json.writeAttribute(3, false, "directed", false);
json.writeAttribute(3, false, "source", sourceVertex);
json.writeAttribute(3, false, "target", targetVertex);
for (String field : edge.getPropertyKeys()) {
final Object v = edge.getProperty(field);
if (v != null)
json.writeAttribute(3, false, field, v);
}
json.endObject(2, false);
json.endObject(1, false);
json.endObject(0, false);
json.newline();
}
}
use of com.tinkerpop.blueprints.impls.orient.OrientVertex in project orientdb by orientechnologies.
the class OCommandExecutorSQLCreateVertex method execute.
/**
* Execute the command and return the ODocument object created.
*/
public Object execute(final Map<Object, Object> iArgs) {
if (clazz == null)
throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
// CREATE VERTEX DOES NOT HAVE TO BE IN TX
return OGraphCommandExecutorSQLFactory.runWithAnyGraph(new OGraphCommandExecutorSQLFactory.GraphCallBack<Object>() {
@Override
public Object call(final OrientBaseGraph graph) {
final OrientVertex vertex = graph.addTemporaryVertex(clazz.getName());
if (fields != null)
// EVALUATE FIELDS
for (final OPair<String, Object> f : fields) {
if (f.getValue() instanceof OSQLFunctionRuntime)
f.setValue(((OSQLFunctionRuntime) f.getValue()).getValue(vertex.getRecord(), null, context));
}
OSQLHelper.bindParameters(vertex.getRecord(), fields, new OCommandParameters(iArgs), context);
if (content != null)
vertex.getRecord().merge(content, true, false);
if (clusterName != null)
vertex.save(clusterName);
else
vertex.save();
return vertex.getRecord();
}
});
}
use of com.tinkerpop.blueprints.impls.orient.OrientVertex in project orientdb by orientechnologies.
the class OCommandExecutorSQLDeleteVertex method result.
/**
* Delete the current vertex.
*/
public boolean result(final Object iRecord) {
final OIdentifiable id = (OIdentifiable) iRecord;
if (id.getIdentity().isValid()) {
final ODocument record = id.getRecord();
final OrientBaseGraph g = currentGraph.get();
final OrientVertex v = g.getVertex(record);
if (v != null) {
v.remove();
if (!txAlreadyBegun && batch > 0 && removed % batch == 0) {
if (g instanceof OrientGraph) {
g.commit();
((OrientGraph) g).begin();
}
}
if (returning.equalsIgnoreCase("BEFORE"))
allDeletedRecords.add(record);
removed++;
}
}
return true;
}
use of com.tinkerpop.blueprints.impls.orient.OrientVertex in project orientdb by orientechnologies.
the class OGraphInsertWorkload method execute.
@Override
public void execute(final OStressTesterSettings settings, final ODatabaseIdentifier databaseIdentifier) {
connectionStrategy = settings.loadBalancing;
final List<OBaseWorkLoadContext> contexts = executeOperation(databaseIdentifier, resultVertices, settings, new OCallable<Void, OBaseWorkLoadContext>() {
@Override
public Void call(final OBaseWorkLoadContext context) {
final OWorkLoadContext graphContext = ((OWorkLoadContext) context);
final OrientBaseGraph graph = graphContext.graph;
final OrientVertex v = graph.addVertex("class:" + className, "_id", resultVertices.current.get(), "ts", System.currentTimeMillis());
if (graphContext.lastVertexToConnect != null) {
v.addEdge("E", graphContext.lastVertexToConnect);
resultEdges.current.incrementAndGet();
graphContext.lastVertexEdges++;
if (graphContext.lastVertexEdges > factor) {
graphContext.lastVertexEdges = 0;
if (strategy == STRATEGIES.LAST)
graphContext.lastVertexToConnect = v;
else if (strategy == STRATEGIES.RANDOM) {
do {
final int[] totalClusters = graph.getVertexBaseType().getClusterIds();
final int randomCluster = totalClusters[new Random().nextInt(totalClusters.length)];
long totClusterRecords = graph.getRawGraph().countClusterElements(randomCluster);
if (totClusterRecords > 0) {
final ORecordId randomRid = new ORecordId(randomCluster, new Random().nextInt((int) totClusterRecords));
graphContext.lastVertexToConnect = graph.getVertex(randomRid);
break;
}
} while (true);
} else if (strategy == STRATEGIES.SUPERNODE) {
final int[] totalClusters = graph.getVertexBaseType().getClusterIds();
final int firstCluster = totalClusters[0];
long totClusterRecords = graph.getRawGraph().countClusterElements(firstCluster);
if (totClusterRecords > 0) {
final ORecordId randomRid = new ORecordId(firstCluster, 0);
graphContext.lastVertexToConnect = graph.getVertex(randomRid);
}
}
}
} else
graphContext.lastVertexToConnect = v;
resultVertices.current.incrementAndGet();
return null;
}
});
final OrientBaseGraph graph = settings.operationsPerTransaction > 0 ? getGraph(databaseIdentifier) : getGraphNoTx(databaseIdentifier);
try {
// CONNECTED ALL THE SUB GRAPHS
OrientVertex lastVertex = null;
for (OBaseWorkLoadContext context : contexts) {
for (int retry = 0; retry < 100; ++retry) try {
if (lastVertex != null)
lastVertex.addEdge("E", ((OWorkLoadContext) context).lastVertexToConnect);
lastVertex = ((OWorkLoadContext) context).lastVertexToConnect;
} catch (ONeedRetryException e) {
if (lastVertex.getIdentity().isPersistent())
lastVertex.reload();
if (((OWorkLoadContext) context).lastVertexToConnect.getIdentity().isPersistent())
((OWorkLoadContext) context).lastVertexToConnect.reload();
}
}
} finally {
graph.shutdown();
}
}
use of com.tinkerpop.blueprints.impls.orient.OrientVertex in project orientdb by orientechnologies.
the class OGraphShortestPathWorkload method execute.
@Override
public void execute(final OStressTesterSettings settings, final ODatabaseIdentifier databaseIdentifier) {
connectionStrategy = settings.loadBalancing;
// RETRIEVE THE STARTING VERTICES
final OrientGraphNoTx g = getGraphNoTx(databaseIdentifier);
try {
for (OIdentifiable id : g.getRawGraph().browseClass("V")) {
startingVertices.add(id.getIdentity());
if (limit > -1 && startingVertices.size() >= limit)
break;
}
} finally {
g.shutdown();
}
result.total = startingVertices.size();
executeOperation(databaseIdentifier, result, settings, new OCallable<Void, OBaseWorkLoadContext>() {
@Override
public Void call(final OBaseWorkLoadContext context) {
final OWorkLoadContext graphContext = ((OWorkLoadContext) context);
final OrientBaseGraph graph = graphContext.graph;
for (int i = 0; i < startingVertices.size(); ++i) {
final Iterable<OrientVertex> commandResult = graph.command(new OCommandSQL("select shortestPath(?,?, 'both')")).execute(startingVertices.get(context.currentIdx), startingVertices.get(i));
for (OrientVertex v : commandResult) {
Collection depth = v.getRecord().field("shortestPath");
if (depth != null && !depth.isEmpty()) {
totalDepth.addAndGet(depth.size());
long max = maxDepth.get();
while (depth.size() > max) {
if (maxDepth.compareAndSet(max, depth.size()))
break;
max = maxDepth.get();
}
} else
notConnected.incrementAndGet();
}
}
result.current.incrementAndGet();
return null;
}
});
}
Aggregations