Search in sources :

Example 31 with OrientGraph

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

the class OCommandExecutorSQLMoveVertex method execute.

/**
   * Executes the command and return the ODocument object created.
   */
public Object execute(final Map<Object, Object> iArgs) {
    if (className == null && clusterName == null)
        throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
    OModifiableBoolean shutdownGraph = new OModifiableBoolean();
    final boolean txAlreadyBegun = getDatabase().getTransaction().isActive();
    final OrientGraph graph = OGraphCommandExecutorSQLFactory.getGraph(true, shutdownGraph);
    try {
        final Set<OIdentifiable> sourceRIDs = OSQLEngine.getInstance().parseRIDTarget(graph.getRawGraph(), source, context, iArgs);
        // CREATE EDGES
        final List<ODocument> result = new ArrayList<ODocument>(sourceRIDs.size());
        for (OIdentifiable from : sourceRIDs) {
            final OrientVertex fromVertex = graph.getVertex(from);
            if (fromVertex == null)
                continue;
            final ORID oldVertex = fromVertex.getIdentity().copy();
            final ORID newVertex = fromVertex.moveTo(className, clusterName);
            final ODocument newVertexDoc = newVertex.getRecord();
            if (fields != null) {
                // EVALUATE FIELDS
                for (final OPair<String, Object> f : fields) {
                    if (f.getValue() instanceof OSQLFunctionRuntime)
                        f.setValue(((OSQLFunctionRuntime) f.getValue()).getValue(newVertex.getRecord(), null, context));
                }
                OSQLHelper.bindParameters(newVertexDoc, fields, new OCommandParameters(iArgs), context);
            }
            if (merge != null)
                newVertexDoc.merge(merge, true, false);
            // SAVE CHANGES
            newVertexDoc.save();
            // PUT THE MOVE INTO THE RESULT
            result.add(new ODocument().setTrackingChanges(false).field("old", oldVertex, OType.LINK).field("new", newVertex, OType.LINK));
            if (batch > 0 && result.size() % batch == 0) {
                graph.commit();
                if (!graph.isAutoStartTx())
                    graph.begin();
            }
        }
        graph.commit();
        return result;
    } finally {
        if (!txAlreadyBegun)
            graph.commit();
        if (shutdownGraph.getValue())
            graph.shutdown(false);
    }
}
Also used : OSQLFunctionRuntime(com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime) OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) ArrayList(java.util.ArrayList) OCommandParameters(com.orientechnologies.orient.core.sql.OCommandParameters) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) ORID(com.orientechnologies.orient.core.id.ORID) OModifiableBoolean(com.orientechnologies.common.types.OModifiableBoolean) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 32 with OrientGraph

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

the class GraphShutdownTest method graphCommitAfterShutdown.

@Test(expected = ODatabaseException.class)
public void graphCommitAfterShutdown() {
    OrientGraphFactory factory = new OrientGraphFactory("memory:graphCommitAfterShutdown");
    OrientGraph graph1 = factory.getTx();
    OrientGraph graph2 = factory.getTx();
    // in 2.2 this will not close the database because graph1 is still active in the pool
    graph2.shutdown(true);
    // this should fail
    graph2.commit();
    factory.drop();
}
Also used : OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) OrientGraphFactory(com.tinkerpop.blueprints.impls.orient.OrientGraphFactory) Test(org.junit.Test)

Example 33 with OrientGraph

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

the class ODatabaseFailDueCloseTest method createGraph.

private static void createGraph() {
    OrientGraph g = new OrientGraph("memory:temp", "admin", "admin");
    g.shutdown();
}
Also used : OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph)

Example 34 with OrientGraph

use of com.tinkerpop.blueprints.impls.orient.OrientGraph 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();
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) OrientVertexType(com.tinkerpop.blueprints.impls.orient.OrientVertexType) ORecordId(com.orientechnologies.orient.core.id.ORecordId)

Example 35 with OrientGraph

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

the class HAClusterStrategyTest method executeTest.

@Override
public void executeTest() throws Exception {
    final OrientGraphFactory factory = new OrientGraphFactory(getDatabaseURL(serverInstance.get(0)));
    final OrientGraphNoTx g = factory.getNoTx();
    g.createVertexType("Test");
    g.shutdown();
    for (int i = 0; i < 10; ++i) {
        // pressing 'return' 2 to 10 times should trigger the described behavior
        Thread.sleep(100);
        final OrientGraph graph = factory.getTx();
        // should always be 'local', but eventually changes to 'round-robin'
        System.out.println("StrategyClassName: " + graph.getVertexType("Test").getClusterSelection().getClass().getName());
        System.out.println("ClusterSelectionStrategy for " + graph.getRawGraph().getURL() + ": " + graph.getVertexType("Test").getClusterSelection().getName());
        Assert.assertEquals(graph.getVertexType("Test").getClusterSelection().getClass().getName(), OLocalClusterWrapperStrategy.class.getName());
        Assert.assertEquals(graph.getVertexType("Test").getClusterSelection().getName(), "round-robin");
        graph.addVertex("class:Test", "firstName", "Roger", "lastName", "Smith");
        graph.getRawGraph().commit();
        graph.shutdown();
    }
    factory.close();
    factory.drop();
}
Also used : OLocalClusterWrapperStrategy(com.orientechnologies.orient.server.distributed.impl.OLocalClusterWrapperStrategy) OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) OrientGraphFactory(com.tinkerpop.blueprints.impls.orient.OrientGraphFactory) OrientGraphNoTx(com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx)

Aggregations

OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)94 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)43 Test (org.junit.Test)33 Vertex (com.tinkerpop.blueprints.Vertex)22 OrientBaseGraph (com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)19 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)18 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)13 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)13 OrientVertexType (com.tinkerpop.blueprints.impls.orient.OrientVertexType)12 Edge (com.tinkerpop.blueprints.Edge)8 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)7 OrientEdge (com.tinkerpop.blueprints.impls.orient.OrientEdge)7 OrientGraphFactory (com.tinkerpop.blueprints.impls.orient.OrientGraphFactory)7 OConcurrentModificationException (com.orientechnologies.orient.core.exception.OConcurrentModificationException)6 Test (org.testng.annotations.Test)6 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)5 OrientGraphNoTx (com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx)5 Before (org.junit.Before)5 BeforeClass (org.junit.BeforeClass)5 ORecordDuplicatedException (com.orientechnologies.orient.core.storage.ORecordDuplicatedException)4