Search in sources :

Example 96 with OrientVertex

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();
    }
}
Also used : OrientElement(com.tinkerpop.blueprints.impls.orient.OrientElement) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) ORID(com.orientechnologies.orient.core.id.ORID) Edge(com.tinkerpop.blueprints.Edge) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) HashSet(java.util.HashSet)

Example 97 with OrientVertex

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();
        }
    });
}
Also used : OSQLFunctionRuntime(com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime) OCommandParameters(com.orientechnologies.orient.core.sql.OCommandParameters) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)

Example 98 with OrientVertex

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;
}
Also used : OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 99 with OrientVertex

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();
    }
}
Also used : OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph) ORecordId(com.orientechnologies.orient.core.id.ORecordId) Random(java.util.Random) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException)

Example 100 with OrientVertex

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;
        }
    });
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) Collection(java.util.Collection) OrientGraphNoTx(com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Aggregations

OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)155 Test (org.junit.Test)73 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)42 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)40 OrientBaseGraph (com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)37 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)35 OrientGraphNoTx (com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx)26 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)25 OrientEdge (com.tinkerpop.blueprints.impls.orient.OrientEdge)25 Vertex (com.tinkerpop.blueprints.Vertex)24 HashMap (java.util.HashMap)20 OBasicCommandContext (com.orientechnologies.orient.core.command.OBasicCommandContext)13 OrientGraphFactory (com.tinkerpop.blueprints.impls.orient.OrientGraphFactory)13 OrientVertexType (com.tinkerpop.blueprints.impls.orient.OrientVertexType)13 Edge (com.tinkerpop.blueprints.Edge)12 OConcurrentModificationException (com.orientechnologies.orient.core.exception.OConcurrentModificationException)10 GraphNoTxAbstractTest (com.orientechnologies.orient.graph.GraphNoTxAbstractTest)9 ORID (com.orientechnologies.orient.core.id.ORID)7 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)7 GraphTxAbstractTest (com.orientechnologies.orient.graph.GraphTxAbstractTest)7