Search in sources :

Example 11 with OrientEdge

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

the class SQLUpdateEdgeTest method testUpdateEdgeOfTypeE.

@Test
public void testUpdateEdgeOfTypeE() {
    //issue #6378
    ODocument v1 = database.command(new OCommandSQL("create vertex")).execute();
    ODocument v2 = database.command(new OCommandSQL("create vertex")).execute();
    ODocument v3 = database.command(new OCommandSQL("create vertex")).execute();
    Iterable<OrientEdge> edges = database.command(new OCommandSQL("create edge E from " + v1.getIdentity() + " to " + v2.getIdentity())).execute();
    OrientEdge edge = edges.iterator().next();
    database.command(new OCommandSQL("UPDATE EDGE " + edge.getIdentity() + " SET in = " + v3.getIdentity())).execute();
    Iterable<ODocument> result = database.command(new OSQLSynchQuery<ODocument>("select expand(out()) from " + v1.getIdentity())).execute();
    Assert.assertEquals(result.iterator().next().getIdentity(), v3.getIdentity());
    result = database.command(new OSQLSynchQuery<ODocument>("select expand(in()) from " + v3.getIdentity())).execute();
    Assert.assertEquals(result.iterator().next().getIdentity(), v1.getIdentity());
    result = database.command(new OSQLSynchQuery<ODocument>("select expand(in()) from " + v2.getIdentity())).execute();
    Assert.assertFalse(result.iterator().hasNext());
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Example 12 with OrientEdge

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

the class SQLUpdateEdgeTest method testUpdateEdge.

@Test
public void testUpdateEdge() {
    database.command(new OCommandSQL("create class V1 extends V")).execute();
    database.command(new OCommandSQL("create class E1 extends E")).execute();
    database.getMetadata().getSchema().reload();
    // VERTEXES
    ODocument v1 = database.command(new OCommandSQL("create vertex")).execute();
    Assert.assertEquals(v1.getClassName(), OrientVertexType.CLASS_NAME);
    ODocument v2 = database.command(new OCommandSQL("create vertex V1")).execute();
    Assert.assertEquals(v2.getClassName(), "V1");
    ODocument v3 = database.command(new OCommandSQL("create vertex set vid = 'v3', brand = 'fiat'")).execute();
    Assert.assertEquals(v3.getClassName(), OrientVertexType.CLASS_NAME);
    Assert.assertEquals(v3.field("brand"), "fiat");
    ODocument v4 = database.command(new OCommandSQL("create vertex V1 set vid = 'v4',  brand = 'fiat',name = 'wow'")).execute();
    Assert.assertEquals(v4.getClassName(), "V1");
    Assert.assertEquals(v4.field("brand"), "fiat");
    Assert.assertEquals(v4.field("name"), "wow");
    List<OrientEdge> edges = database.command(new OCommandSQL("create edge E1 from " + v1.getIdentity() + " to " + v2.getIdentity())).execute();
    Assert.assertEquals(edges.size(), 1);
    OrientEdge edge = edges.get(0);
    Assert.assertEquals(edge.getLabel(), "E1");
    database.command(new OCommandSQL("update edge E1 set out = " + v3.getIdentity() + ", in = " + v4.getIdentity() + " where @rid = " + edge.getIdentity())).execute();
    List<ODocument> result = database.query(new OSQLSynchQuery("select expand(out('E1')) from " + v3.getIdentity()));
    Assert.assertEquals(edges.size(), 1);
    ODocument vertex4 = result.get(0);
    Assert.assertEquals(vertex4.field("vid"), "v4");
    result = database.query(new OSQLSynchQuery("select expand(in('E1')) from " + v4.getIdentity()));
    Assert.assertEquals(result.size(), 1);
    ODocument vertex3 = result.get(0);
    Assert.assertEquals(vertex3.field("vid"), "v3");
    result = database.query(new OSQLSynchQuery("select expand(out('E1')) from " + v1.getIdentity()));
    Assert.assertEquals(result.size(), 0);
    result = database.query(new OSQLSynchQuery("select expand(in('E1')) from " + v2.getIdentity()));
    Assert.assertEquals(result.size(), 0);
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Example 13 with OrientEdge

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

the class TestDeleteEdge method testDeleteEdgeValidation.

@Test
public void testDeleteEdgeValidation() {
    OrientVertex v1 = graph.addVertex("class:TestVertex");
    OrientVertex v2 = graph.addVertex("class:TestVertex");
    Map<String, Object> p1 = new HashMap<String, Object>();
    p1.put("based_on", "0001");
    OrientEdge e1 = v1.addEdge(null, v2, "TestEdge", null, p1);
    e1.save();
    graph.commit();
    graph.getRawGraph().commit();
    edgeType.createProperty("mand", OType.STRING).setMandatory(true);
    graph.getRawGraph().begin();
    graph.command(new OCommandSQL("delete edge TestEdge where based_on = '0001'")).execute();
    Iterable<OrientVertex> edges = graph.command(new OCommandSQL("select count(*) from TestEdge where based_on = '0001'")).execute();
    assertTrue(edges.iterator().hasNext());
    assertEquals(edges.iterator().next().getProperty("count"), 0l);
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) HashMap(java.util.HashMap) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) Test(org.junit.Test)

Example 14 with OrientEdge

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

the class SQLMoveVertexCommandTest method testMoveSingleRecordToAnotherCluster.

@Test
public void testMoveSingleRecordToAnotherCluster() {
    OrientVertex v1 = graph.addVertex("class:Customer").setProperties("name", "Jay1", "test", "testMoveSingleRecordToAnotherCluster");
    OrientVertex v2 = graph.addVertex("class:Customer").setProperties("name", "Jay2", "test", "testMoveSingleRecordToAnotherCluster");
    OrientVertex v3 = graph.addVertex("class:Customer").setProperties("name", "Jay3", "test", "testMoveSingleRecordToAnotherCluster");
    // SELF
    v1.addEdge("knows", v1);
    v1.addEdge("knows", v2);
    v1.addEdge("knows", v3);
    v2.addEdge("knows", v1);
    Assert.assertEquals(v1.getIdentity().getClusterId(), customer.getDefaultClusterId());
    Iterable<OrientVertex> result = graph.command(new OCommandSQL("MOVE VERTEX " + v1.getIdentity() + " TO CLUSTER:Customer_genius")).execute();
    // CHECK RESULT
    final ArrayList<OIdentifiable> newRids = new ArrayList<OIdentifiable>();
    int tot = 0;
    for (OrientVertex v : result) {
        tot++;
        ODocument fromTo = v.getRecord();
        OIdentifiable from = fromTo.field("old");
        OIdentifiable to = fromTo.field("new");
        newRids.add(to);
        // CHECK FROM
        Assert.assertEquals(from, v1.getIdentity());
        // CHECK DESTINATION
        Assert.assertEquals(to.getIdentity().getClusterId(), customerGeniusCluster);
        ODocument newDocument = to.getRecord();
        Assert.assertEquals(newDocument.field("name"), "Jay1");
        Assert.assertEquals(newDocument.field("test"), "testMoveSingleRecordToAnotherCluster");
    }
    Assert.assertEquals(tot, 1);
    Iterable<OrientEdge> result2 = graph.command(new OCommandSQL("SELECT FROM knows where out = " + v1.getIdentity() + " or in = " + v1.getIdentity())).execute();
    Assert.assertFalse(result2.iterator().hasNext());
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) ArrayList(java.util.ArrayList) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) GraphNoTxAbstractTest(com.orientechnologies.orient.graph.GraphNoTxAbstractTest) Test(org.junit.Test)

Example 15 with OrientEdge

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

the class OGraphImporterSTAPITest method main.

public static void main(String[] args) throws IOException, InterruptedException {
    // String dbUrl = "memory:amazonReviews";
    String dbUrl = "plocal:/temp/databases/amazonReviews";
    final File f = new File("/temp/databases/amazonReviews");
    if (f.exists())
        OFileUtils.deleteRecursively(f);
    final OrientGraph roGraph = new OrientGraph(dbUrl, "admin", "admin");
    final OrientGraph graph = new OrientGraph(dbUrl, "admin", "admin");
    OrientVertexType user = graph.createVertexType("User");
    user.createProperty("uid", OType.STRING);
    final OIndex<?> userIndex = user.createIndex("User.uid", OClass.INDEX_TYPE.UNIQUE.toString(), (OProgressListener) null, (ODocument) null, "AUTOSHARDING", new String[] { "uid" });
    OrientVertexType product = graph.createVertexType("Product");
    product.createProperty("uid", OType.STRING);
    final OIndex<?> productIndex = product.createIndex("Product.uid", OClass.INDEX_TYPE.UNIQUE.toString(), (OProgressListener) null, (ODocument) null, "AUTOSHARDING", new String[] { "uid" });
    graph.createEdgeType("Reviewed");
    final File file = new File("/Users/luca/Downloads/ratings_Books.csv");
    final BufferedReader br = new BufferedReader(new FileReader(file));
    Orient.instance().scheduleTask(new TimerTask() {

        @Override
        public void run() {
            roGraph.makeActive();
            final long vertexCount = roGraph.countVertices();
            final long edgeCount = roGraph.countEdges();
            System.out.println(String.format("%d vertices=%d %d/sec edges=%d %d/sec", row, vertexCount, ((vertexCount - lastVertexCount) * 1000 / 2000), edgeCount, ((edgeCount - lastEdgeCount) * 1000 / 2000)));
            lastVertexCount = vertexCount;
            lastEdgeCount = edgeCount;
        }
    }, 2000, 2000);
    try {
        for (String line; (line = br.readLine()) != null; ) {
            row++;
            final String[] parts = line.split(",");
            if (parts.length != 4) {
                // SKIP IT
                System.out.print("Skipped invalid line " + row + ": " + line);
                continue;
            }
            Map<String, Object> properties = new HashMap<String, Object>();
            properties.put("score", new Float(parts[2]).intValue());
            properties.put("date", Long.parseLong(parts[3]));
            final Object k1 = userIndex.get(parts[0]);
            OrientVertex v1;
            if (k1 == null) {
                v1 = graph.addVertex("class:User", "uid", parts[0]);
            } else
                v1 = graph.getVertex(k1);
            final Object k2 = productIndex.get(parts[1]);
            OrientVertex v2;
            if (k2 == null) {
                v2 = graph.addVertex("class:Product", "uid", parts[1]);
            } else
                v2 = graph.getVertex(k2);
            final OrientEdge edge = graph.addEdge(null, v1, v2, "Reviewed");
            edge.setProperties(properties);
            if (row % 2 == 0) {
                graph.commit();
            }
        }
    } finally {
        br.close();
    }
    graph.shutdown();
    roGraph.shutdown();
}
Also used : OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) HashMap(java.util.HashMap) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) TimerTask(java.util.TimerTask) BufferedReader(java.io.BufferedReader) OrientVertexType(com.tinkerpop.blueprints.impls.orient.OrientVertexType) FileReader(java.io.FileReader) File(java.io.File)

Aggregations

OrientEdge (com.tinkerpop.blueprints.impls.orient.OrientEdge)32 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)26 Test (org.junit.Test)17 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)12 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)9 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)9 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)6 Edge (com.tinkerpop.blueprints.Edge)5 HashSet (java.util.HashSet)5 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)4 OrientBaseGraph (com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)4 HashMap (java.util.HashMap)4 OrientGraphNoTx (com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx)3 OMultiCollectionIterator (com.orientechnologies.common.collection.OMultiCollectionIterator)2 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)2 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)2 OCompositeKey (com.orientechnologies.orient.core.index.OCompositeKey)2 OIndex (com.orientechnologies.orient.core.index.OIndex)2 OImmutableClass (com.orientechnologies.orient.core.metadata.schema.OImmutableClass)2 ODirtyManager (com.orientechnologies.orient.core.record.impl.ODirtyManager)2