Search in sources :

Example 26 with OrientBaseGraph

use of com.tinkerpop.blueprints.impls.orient.OrientBaseGraph in project orientdb by orientechnologies.

the class TestDistributedDatabaseRepair method testWinnerIsMajority.

private void testWinnerIsMajority(OrientGraphFactory localFactory0, OrientGraphFactory localFactory1, OrientGraphFactory localFactory2) throws Exception {
    OrientBaseGraph graph = localFactory0.getTx();
    OrientVertex product;
    try {
        product = graph.addVertex("class:ProductType");
        product.setProperty("status", "ok");
    } finally {
        graph.shutdown();
    }
    banner("CORRUPT ONLY 1 RECORD IN SERVER 0");
    graph = localFactory0.getNoTx();
    try {
        final OrientVertex product2 = graph.getVertex(product.getIdentity());
        product2.getRecord().field("status", "corrupted0");
        final ODistributedResponse result = updateRemoteRecord(0, product2.getRecord(), new String[] { serverInstance.get(0).getServerInstance().getDistributedManager().getLocalNodeName() });
        Assert.assertFalse(result.getPayload() instanceof Throwable);
    } finally {
        graph.shutdown();
    }
    serverInstance.get(0).getServerInstance().getDistributedManager().getMessageService().getDatabase(getDatabaseName()).getDatabaseRepairer().enqueueRepairRecord((ORecordId) product.getIdentity());
    Thread.sleep(3000);
    banner("EXPECTING AUTO RECOVER ON ALL NODES...");
    // TEST RECORD IS CHANGED
    graph = localFactory0.getNoTx();
    try {
        final OrientVertex product2 = graph.getVertex(product.getIdentity());
        Assert.assertEquals("ok", product2.getProperty("status"));
    } finally {
        graph.shutdown();
    }
    graph = localFactory1.getNoTx();
    try {
        final OrientVertex product2 = graph.getVertex(product.getIdentity());
        Assert.assertEquals("ok", product2.getProperty("status"));
    } finally {
        graph.shutdown();
    }
    graph = localFactory2.getNoTx();
    try {
        final OrientVertex product2 = graph.getVertex(product.getIdentity());
        Assert.assertEquals("ok", product2.getProperty("status"));
    } finally {
        graph.shutdown();
    }
}
Also used : OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)

Example 27 with OrientBaseGraph

use of com.tinkerpop.blueprints.impls.orient.OrientBaseGraph in project orientdb by orientechnologies.

the class OServerCommandGetGephi method execute.

@Override
public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
    String[] urlParts = checkSyntax(iRequest.url, 4, "Syntax error: gephi/<database>/<language>/<query-text>[/<limit>][/<fetchPlan>].<br>Limit is optional and is setted to 20 by default. Set expressely to 0 to have no limits.");
    final String language = urlParts[2];
    final String text = urlParts[3];
    final int limit = urlParts.length > 4 ? Integer.parseInt(urlParts[4]) : 20;
    final String fetchPlan = urlParts.length > 5 ? urlParts[5] : null;
    iRequest.data.commandInfo = "Gephi";
    iRequest.data.commandDetail = text;
    final ODatabaseDocument db = getProfiledDatabaseInstance(iRequest);
    final OModifiableBoolean shutdownFlag = new OModifiableBoolean();
    final OrientBaseGraph graph = OGraphCommandExecutorSQLFactory.getAnyGraph(shutdownFlag);
    try {
        final Iterable<OrientElement> vertices;
        if (language.equals("sql"))
            vertices = graph.command(new OSQLSynchQuery<OrientVertex>(text, limit).setFetchPlan(fetchPlan)).execute();
        else if (language.equals("gremlin")) {
            List<Object> result = new ArrayList<Object>();
            OGremlinHelper.execute(graph, text, null, null, result, null, null);
            vertices = new ArrayList<OrientElement>(result.size());
            for (Object o : result) {
                ((ArrayList<OrientElement>) vertices).add(graph.getVertex(o));
            }
        } else
            throw new IllegalArgumentException("Language '" + language + "' is not supported. Use 'sql' or 'gremlin'");
        sendRecordsContent(iRequest, iResponse, vertices, fetchPlan);
    } finally {
        if (graph != null && shutdownFlag.getValue())
            graph.shutdown(false, false);
        if (db != null)
            db.close();
    }
    return false;
}
Also used : ArrayList(java.util.ArrayList) OrientElement(com.tinkerpop.blueprints.impls.orient.OrientElement) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ArrayList(java.util.ArrayList) List(java.util.List) OModifiableBoolean(com.orientechnologies.common.types.OModifiableBoolean)

Example 28 with OrientBaseGraph

use of com.tinkerpop.blueprints.impls.orient.OrientBaseGraph in project orientdb by orientechnologies.

the class OCommandExecutorSQLCreateEdge 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");
    return OGraphCommandExecutorSQLFactory.runInConfiguredTxMode(new OGraphCommandExecutorSQLFactory.GraphCallBack<List<Object>>() {

        @Override
        public List<Object> call(OrientBaseGraph graph) {
            final Set<OIdentifiable> fromIds = OSQLEngine.getInstance().parseRIDTarget(graph.getRawGraph(), from, context, iArgs);
            final Set<OIdentifiable> toIds = OSQLEngine.getInstance().parseRIDTarget(graph.getRawGraph(), to, context, iArgs);
            // CREATE EDGES
            final List<Object> edges = new ArrayList<Object>();
            for (OIdentifiable from : fromIds) {
                final OrientVertex fromVertex = graph.getVertex(from);
                if (fromVertex == null)
                    throw new OCommandExecutionException("Source vertex '" + from + "' not exists");
                for (OIdentifiable to : toIds) {
                    final OrientVertex toVertex;
                    if (from.equals(to)) {
                        toVertex = fromVertex;
                    } else {
                        toVertex = graph.getVertex(to);
                    }
                    if (fields != null)
                        // EVALUATE FIELDS
                        for (final OPair<String, Object> f : fields) {
                            if (f.getValue() instanceof OSQLFunctionRuntime) {
                                f.setValue(((OSQLFunctionRuntime) f.getValue()).getValue(to, null, context));
                            } else if (f.getValue() instanceof OSQLFilterItem) {
                                f.setValue(((OSQLFilterItem) f.getValue()).getValue(to, null, context));
                            }
                        }
                    OrientEdge edge = null;
                    if (content != null) {
                        if (fields != null)
                            // MERGE CONTENT WITH FIELDS
                            fields.addAll(OPair.convertFromMap(content.toMap()));
                        else
                            fields = OPair.convertFromMap(content.toMap());
                    }
                    edge = fromVertex.addEdge(null, toVertex, edgeLabel, clusterName, fields);
                    if (fields != null && !fields.isEmpty()) {
                        if (edge.isLightweight())
                            edge.convertToDocument();
                        OSQLHelper.bindParameters(edge.getRecord(), fields, new OCommandParameters(iArgs), context);
                    }
                    edge.save(clusterName);
                    edges.add(edge);
                    if (batch > 0 && edges.size() % batch == 0) {
                        graph.commit();
                        graph.begin();
                    }
                }
            }
            if (edges.isEmpty()) {
                if (fromIds.isEmpty())
                    throw new OCommandExecutionException("No edge has been created because no source vertices");
                else if (toIds.isEmpty())
                    throw new OCommandExecutionException("No edge has been created because no target vertices");
                throw new OCommandExecutionException("No edge has been created between " + fromIds + " and " + toIds);
            }
            return edges;
        }
    });
}
Also used : OSQLFunctionRuntime(com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OSQLFilterItem(com.orientechnologies.orient.core.sql.filter.OSQLFilterItem) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException)

Example 29 with OrientBaseGraph

use of com.tinkerpop.blueprints.impls.orient.OrientBaseGraph in project orientdb by orientechnologies.

the class OCommandExecutorSQLDeleteVertex method execute.

/**
   * Execute the command and return the ODocument object created.
   */
public Object execute(final Map<Object, Object> iArgs) {
    if (rid == null && query == null)
        throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
    if (!returning.equalsIgnoreCase("COUNT"))
        allDeletedRecords = new ArrayList<ORecord>();
    txAlreadyBegun = getDatabase().getTransaction().isActive();
    if (rid != null) {
        // REMOVE PUNCTUAL RID
        OGraphCommandExecutorSQLFactory.runInConfiguredTxMode(new OGraphCommandExecutorSQLFactory.GraphCallBack<Object>() {

            @Override
            public Object call(OrientBaseGraph graph) {
                final OrientVertex v = graph.getVertex(rid);
                if (v != null) {
                    v.remove();
                    removed = 1;
                }
                return null;
            }
        });
        // CLOSE PENDING TX
        end();
    } else if (query != null) {
        // TARGET IS A CLASS + OPTIONAL CONDITION
        OGraphCommandExecutorSQLFactory.runInConfiguredTxMode(new OGraphCommandExecutorSQLFactory.GraphCallBack<OrientGraph>() {

            @Override
            public OrientGraph call(final OrientBaseGraph iGraph) {
                // TARGET IS A CLASS + OPTIONAL CONDITION
                currentGraph.set(iGraph);
                query.setContext(getContext());
                query.execute(iArgs);
                return null;
            }
        });
    } else
        throw new OCommandExecutionException("Invalid target");
    if (returning.equalsIgnoreCase("COUNT"))
        // RETURNS ONLY THE COUNT
        return removed;
    else
        // RETURNS ALL THE DELETED RECORDS
        return allDeletedRecords;
}
Also used : ArrayList(java.util.ArrayList) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)

Example 30 with OrientBaseGraph

use of com.tinkerpop.blueprints.impls.orient.OrientBaseGraph in project orientdb by orientechnologies.

the class OGraphCommandExecutorSQLFactory method getGraphNoTx.

/**
   * @return a Non Transactional OrientGraph implementation from the current database in thread local.
   */
public static OrientGraphNoTx getGraphNoTx(final OModifiableBoolean shouldBeShutDown) {
    final ODatabaseDocument database = ODatabaseRecordThreadLocal.INSTANCE.get();
    final OrientBaseGraph result = OrientBaseGraph.getActiveGraph();
    if (result != null && (result instanceof OrientGraphNoTx)) {
        final ODatabaseDocumentTx graphDb = result.getRawGraph();
        // CHECK IF THE DATABASE + USER IN TL IS THE SAME IN ORDER TO USE IT
        if (canReuseActiveGraph(graphDb, database)) {
            if (!graphDb.isClosed()) {
                ODatabaseRecordThreadLocal.INSTANCE.set(graphDb);
                shouldBeShutDown.setValue(false);
                return (OrientGraphNoTx) result;
            }
        }
    }
    // Set it again on ThreadLocal because the getRawGraph() may have set a closed db in the thread-local
    shouldBeShutDown.setValue(true);
    ODatabaseRecordThreadLocal.INSTANCE.set((ODatabaseDocumentInternal) database);
    return (OrientGraphNoTx) OrientGraphFactory.getNoTxGraphImplFactory().getGraph((ODatabaseDocumentTx) database);
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OrientGraphNoTx(com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)

Aggregations

OrientBaseGraph (com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)70 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)40 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)19 Vertex (com.tinkerpop.blueprints.Vertex)13 OrientGraphNoTx (com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx)13 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)11 OrientGraphFactory (com.tinkerpop.blueprints.impls.orient.OrientGraphFactory)8 Test (org.junit.Test)8 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)7 Edge (com.tinkerpop.blueprints.Edge)7 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)6 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)5 OGraphRepair (com.tinkerpop.blueprints.impls.orient.OGraphRepair)5 ArrayList (java.util.ArrayList)5 ONeedRetryException (com.orientechnologies.common.concur.ONeedRetryException)4 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)4 OConcurrentModificationException (com.orientechnologies.orient.core.exception.OConcurrentModificationException)4 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)4 ORecordDuplicatedException (com.orientechnologies.orient.core.storage.ORecordDuplicatedException)4 OrientEdge (com.tinkerpop.blueprints.impls.orient.OrientEdge)4