Search in sources :

Example 31 with OrientBaseGraph

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

the class OSQLFunctionGremlin method execute.

public Object execute(Object iThis, final OIdentifiable iCurrentRecord, Object iCurrentResult, final Object[] iParams, final OCommandContext iContext) {
    final ODatabaseDocumentTx db = OGremlinHelper.getGraphDatabase(ODatabaseRecordThreadLocal.INSTANCE.get());
    result = new ArrayList<Object>();
    OGremlinHelper.execute(db, (String) iParams[0], null, (Map) iContext.getVariables(), result, new OGremlinHelper.OGremlinCallback() {

        @Override
        public boolean call(final ScriptEngine iEngine, final OrientBaseGraph iGraph) {
            if (iCurrentRecord == null)
                // IGNORE PRE-PROCESSING
                return true;
            final ODocument document = (ODocument) iCurrentRecord;
            OClass clazz = ODocumentInternal.getImmutableSchemaClass(document);
            if (clazz != null && clazz.isSubClassOf(OrientEdgeType.CLASS_NAME)) {
                // EDGE TYPE, CREATE THE BLUEPRINTS'S WRAPPER
                OrientEdge graphElement = (OrientEdge) new OrientElementIterable<OrientEdge>(iGraph, Arrays.asList(new ODocument[] { document })).iterator().next();
                iEngine.getBindings(ScriptContext.ENGINE_SCOPE).put("current", graphElement);
                // FRAMES LIKE SYNTAX
                iEngine.getBindings(ScriptContext.ENGINE_SCOPE).put("it", graphElement);
            } else {
                // VERTEX TYPE, CREATE THE BLUEPRINTS'S WRAPPER
                OrientVertex graphElement = (OrientVertex) new OrientElementIterable<OrientVertex>(iGraph, Arrays.asList(new ODocument[] { document })).iterator().next();
                iEngine.getBindings(ScriptContext.ENGINE_SCOPE).put("current", graphElement);
                // FRAMES LIKE SYNTAX
                iEngine.getBindings(ScriptContext.ENGINE_SCOPE).put("it", graphElement);
            }
            return true;
        }
    }, null);
    return result;
}
Also used : ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph) ScriptEngine(javax.script.ScriptEngine) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) OGremlinHelper(com.orientechnologies.orient.graph.gremlin.OGremlinHelper) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) OrientElementIterable(com.tinkerpop.blueprints.impls.orient.OrientElementIterable)

Example 32 with OrientBaseGraph

use of com.tinkerpop.blueprints.impls.orient.OrientBaseGraph 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();
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) Vertex(com.tinkerpop.blueprints.Vertex) OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) OrientGraphFactory(com.tinkerpop.blueprints.impls.orient.OrientGraphFactory) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) Test(org.junit.Test)

Example 33 with OrientBaseGraph

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

the class AbstractServerClusterTest method prepare.

/**
   * Create the database on first node only
   *
   * @throws IOException
   */
protected void prepare(final boolean iCopyDatabaseToNodes, final boolean iCreateDatabase, final OCallable<Object, OrientGraphFactory> iCfgCallback) throws IOException {
    // CREATE THE DATABASE
    final Iterator<ServerRun> it = serverInstance.iterator();
    final ServerRun master = it.next();
    if (iCreateDatabase) {
        final OrientBaseGraph graph = master.createDatabase(getDatabaseName(), iCfgCallback);
        try {
            onAfterDatabaseCreation(graph);
        } finally {
            graph.shutdown();
            Orient.instance().closeAllStorages();
        }
    }
    // COPY DATABASE TO OTHER SERVERS
    while (it.hasNext()) {
        final ServerRun replicaSrv = it.next();
        replicaSrv.deleteNode();
        if (iCopyDatabaseToNodes)
            master.copyDatabase(getDatabaseName(), replicaSrv.getDatabasePath(getDatabaseName()));
    }
}
Also used : OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)

Example 34 with OrientBaseGraph

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

the class ConcurrentDistributedUpdateTest method createWriter.

protected Callable<Void> createWriter(final int serverId, final int threadId, final String databaseURL) {
    return new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            final String id = serverId + "." + threadId;
            boolean isRunning = true;
            final OrientBaseGraph graph = new OrientGraph(databaseURL);
            try {
                String query = "select from Test where prop2='v2-1'";
                for (int i = 0; i < 100 && isRunning; i++) {
                    if ((i % 25) == 0) {
                        log("[" + id + "] Records Processed: [" + i + "]");
                    }
                    Iterable<Vertex> vtxs = graph.command(new OCommandSQL(query)).execute();
                    boolean update = true;
                    for (Vertex vtx : vtxs) {
                        if (update) {
                            update = true;
                            for (int k = 0; k < 10 && update; k++) {
                                OrientVertex vtx1 = (OrientVertex) vtx;
                                try {
                                    vtx1.setProperty("prop5", "prop55");
                                    graph.commit();
                                    // log("[" + id + "/" + i + "/" + k + "] OK!\n");
                                    break;
                                } catch (OConcurrentModificationException ex) {
                                    vtx1.reload();
                                } catch (ODistributedRecordLockedException ex) {
                                    log("[" + id + "/" + i + "/" + k + "] Distributed lock Exception " + ex + " for vertex " + vtx1 + " \n");
                                    //                    ex.printStackTrace();
                                    update = false;
                                    //                    isRunning = false;
                                    break;
                                } catch (Exception ex) {
                                    log("[" + id + "/" + i + "/" + k + "] Exception " + ex + " for vertex " + vtx1 + "\n\n");
                                    ex.printStackTrace();
                                    isRunning = false;
                                    break;
                                }
                            }
                            if (!isRunning)
                                break;
                        }
                    }
                }
            } catch (Exception ex) {
                System.out.println("ID: [" + id + "]********** Exception " + ex + " \n\n");
                ex.printStackTrace();
            } finally {
                log("[" + id + "] Done................>>>>>>>>>>>>>>>>>>");
                graph.shutdown();
                runningWriters.countDown();
            }
            Assert.assertTrue(isRunning);
            return null;
        }
    };
}
Also used : OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) Vertex(com.tinkerpop.blueprints.Vertex) ODistributedRecordLockedException(com.orientechnologies.orient.server.distributed.task.ODistributedRecordLockedException) OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph) OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) Callable(java.util.concurrent.Callable) ODistributedRecordLockedException(com.orientechnologies.orient.server.distributed.task.ODistributedRecordLockedException) OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL)

Example 35 with OrientBaseGraph

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

the class MultipleDBAlignmentOnNodesJoining method prepare.

/**
   * Creates the databases as follows:
   * - server1: db A, db B
   * - server2: db B, db C
   *
   * @throws IOException
   */
@Override
protected void prepare(final boolean iCopyDatabaseToNodes, final boolean iCreateDatabase, final OCallable<Object, OrientGraphFactory> iCfgCallback) throws IOException {
    serverInstance.remove(2);
    // creating databases on server1
    ServerRun master = serverInstance.get(0);
    if (iCreateDatabase) {
        final OrientBaseGraph graph1 = master.createDatabase(dbA, iCfgCallback);
        final OrientBaseGraph graph2 = master.createDatabase(dbB, iCfgCallback);
        try {
            onAfterDatabaseCreation(graph1, "plocal:" + serverInstance.get(0).getDatabasePath(dbA));
            onAfterDatabaseCreation(graph2, "plocal:" + serverInstance.get(0).getDatabasePath(dbB));
        } finally {
            if (!graph1.isClosed()) {
                graph1.shutdown();
            }
            if (!graph1.isClosed()) {
                graph2.shutdown();
            }
        }
    }
    // copying db-B on server2
    if (iCopyDatabaseToNodes)
        master.copyDatabase(dbB, serverInstance.get(1).getDatabasePath(dbB));
    // creating db-C on server2
    master = serverInstance.get(1);
    if (iCreateDatabase) {
        final OrientBaseGraph graph1 = master.createDatabase(dbC, iCfgCallback);
        try {
            onAfterDatabaseCreation(graph1, "plocal:" + serverInstance.get(1).getDatabasePath(dbC));
        } finally {
            if (!graph1.isClosed()) {
                graph1.shutdown();
            }
        }
    }
}
Also used : ServerRun(com.orientechnologies.orient.server.distributed.ServerRun) 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