Search in sources :

Example 21 with OrientGraph

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

the class DatabaseConflictStrategyAutoMergeTest method dbClient1.

private void dbClient1() {
    sleep(500);
    synchronized (LOCK) {
        OrientBaseGraph graph = new OrientGraph(CLIENT_ORIENT_URL_MAIN);
        try {
            // Create 2 parent vertices.
            OrientVertex parentV1 = graph.addVertex("vertextype1", (String) null);
            graph.commit();
            assertEquals(1, parentV1.getRecord().getVersion());
            parentV1Id = parentV1.getId();
            OrientVertex parentV2 = graph.addVertex("vertextype2", (String) null);
            graph.commit();
            assertEquals(1, parentV2.getRecord().getVersion());
            parentV2Id = parentV2.getId();
            // Create vertices.
            for (int i = 0; i < NUM_OF_LOOP_ITERATIONS; i++) {
                pause();
                if (exceptionInThread != null)
                    break;
                OrientVertex vertex = graph.addVertex("vertextype3", (String) null);
                graph.commit();
                assertEquals(1, vertex.getRecord().getVersion());
                vertex.setProperty("num", i);
                graph.commit();
                assertEquals(2, vertex.getRecord().getVersion());
                parentV1.addEdge("edgetype1", vertex);
                graph.commit();
                assertNotNull(parentV1.getProperty("cnt"), "record " + parentV1.getIdentity() + " has no 'cnt' property");
                boolean edge1Exists = false;
                for (Edge e : parentV1.getEdges(Direction.OUT, "edgetype1")) {
                    if (e.getVertex(Direction.IN).equals(vertex)) {
                        edge1Exists = true;
                        break;
                    }
                }
                assertTrue(edge1Exists);
                boolean edge2Exists = false;
                for (Edge e : vertex.getEdges(Direction.IN, "edgetype1")) {
                    if (e.getVertex(Direction.OUT).equals(parentV1)) {
                        edge2Exists = true;
                        break;
                    }
                }
                assertTrue(edge2Exists);
                assertNotNull(vertex.getProperty("num"));
                parentV2.addEdge("edgetype2", vertex);
                graph.commit();
                assertNotNull(parentV2.getProperty("cnt"));
                edge1Exists = false;
                for (Edge e : parentV2.getEdges(Direction.OUT, "edgetype2")) {
                    if (e.getVertex(Direction.IN).equals(vertex)) {
                        edge1Exists = true;
                        break;
                    }
                }
                assertTrue(edge1Exists);
                edge2Exists = false;
                for (Edge e : vertex.getEdges(Direction.IN, "edgetype2")) {
                    if (e.getVertex(Direction.OUT).equals(parentV2)) {
                        edge2Exists = true;
                        break;
                    }
                }
                assertTrue(edge2Exists);
                assertNotNull(vertex.getProperty("num"));
            }
        } catch (Throwable e) {
            if (exceptionInThread == null) {
                exceptionInThread = e;
            }
        } finally {
            graph.shutdown();
            LOCK.notifyAll();
        }
    }
}
Also used : OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph) Edge(com.tinkerpop.blueprints.Edge)

Example 22 with OrientGraph

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

the class SQLCreateVertexTest method testCreateVertexByContent.

public void testCreateVertexByContent() {
    OrientGraph graph = new OrientGraph(database, false);
    graph.shutdown();
    database.open("admin", "admin");
    OSchema schema = database.getMetadata().getSchema();
    if (!schema.existsClass("CreateVertexByContent")) {
        OClass vClass = schema.createClass("CreateVertexByContent", schema.getClass("V"));
        vClass.createProperty("message", OType.STRING);
    }
    database.command(new OCommandSQL("create vertex CreateVertexByContent content { \"message\": \"(:\"}")).execute();
    database.command(new OCommandSQL("create vertex CreateVertexByContent content { \"message\": \"\\\"‎ה, כן?...‎\\\"\"}")).execute();
    List<ODocument> result = database.query(new OSQLSynchQuery<ODocument>("select from CreateVertexByContent"));
    Assert.assertEquals(result.size(), 2);
    List<String> messages = new ArrayList<String>();
    messages.add("\"‎ה, כן?...‎\"");
    messages.add("(:");
    List<String> resultMessages = new ArrayList<String>();
    for (ODocument document : result) {
        resultMessages.add(document.<String>field("message"));
    }
//issue #1787, works fine locally, not on CI
//    Assert.assertEqualsNoOrder(messages.toArray(), resultMessages.toArray(),
//    "arrays are different: "+toString(messages)+" - "+toString(resultMessages) );
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ArrayList(java.util.ArrayList) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 23 with OrientGraph

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

the class OGremlinConsole method exportDatabase.

@Override
@ConsoleCommand(description = "Export a database", splitInWords = false, onlineHelp = "Console-Command-Export")
public void exportDatabase(@ConsoleParameter(name = "options", description = "Export options") String iText) throws IOException {
    checkForDatabase();
    final List<String> items = OStringSerializerHelper.smartSplit(iText, ' ');
    final String fileName = items.size() <= 1 || items.get(1).charAt(0) == '-' ? null : items.get(1);
    if (fileName != null && (fileName.endsWith(".graphml") || fileName.endsWith(".xml"))) {
        message("\nExporting database in GRAPHML format to " + iText + "...");
        try {
            final OrientGraph g = (OrientGraph) OrientGraphFactory.getTxGraphImplFactory().getGraph(currentDatabase);
            g.setUseLog(false);
            g.setWarnOnForceClosingTx(false);
            // CREATE THE EXPORT FILE IF NOT EXIST YET
            final File f = new File(fileName);
            if (f.getParentFile() != null) {
                f.getParentFile().mkdirs();
            }
            f.createNewFile();
            new GraphMLWriter(g).outputGraph(fileName);
        } catch (ODatabaseImportException e) {
            printError(e);
        }
    } else
        // BASE EXPORT
        super.exportDatabase(iText);
}
Also used : OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) GraphMLWriter(com.tinkerpop.blueprints.util.io.graphml.GraphMLWriter) ODatabaseImportException(com.orientechnologies.orient.core.db.tool.ODatabaseImportException) File(java.io.File) ConsoleCommand(com.orientechnologies.common.console.annotation.ConsoleCommand)

Example 24 with OrientGraph

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

the class OGraphBatchInsertTest method testTraverse.

@Test
public void testTraverse() {
    String dbUrl = "memory:batchinsert_testTraverse";
    OGraphBatchInsert batch = new OGraphBatchInsert(dbUrl, "admin", "admin");
    batch.begin();
    batch.createEdge(0L, 1L, null);
    batch.createEdge(1L, 2L, null);
    batch.createEdge(2L, 3L, null);
    Map<String, Object> vertexProps = new HashMap<String, Object>();
    vertexProps.put("foo", "bar");
    batch.setVertexProperties(3L, vertexProps);
    batch.end();
    OrientGraph g = new OrientGraph(dbUrl, "admin", "admin");
    Iterable<Vertex> result = g.command(new OSQLSynchQuery<Vertex>("select expand(out().in().out().out().in().out()) from V where uid = ?")).execute(1L);
    for (Vertex v : result) {
        assertEquals("bar", v.getProperty("foo"));
    }
    g.shutdown();
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) HashMap(java.util.HashMap) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) Test(org.junit.Test)

Example 25 with OrientGraph

use of com.tinkerpop.blueprints.impls.orient.OrientGraph 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

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