Search in sources :

Example 26 with ORecordDuplicatedException

use of com.orientechnologies.orient.core.storage.ORecordDuplicatedException in project orientdb by orientechnologies.

the class OEdgeTransformer method createEdge.

private List<OrientEdge> createEdge(final OrientVertex vertex, final Object joinCurrentValue, Object result) {
    log(OETLProcessor.LOG_LEVELS.DEBUG, "joinCurrentValue=%s, lookupResult=%s", joinCurrentValue, result);
    if (result == null) {
        // APPLY THE STRATEGY DEFINED IN unresolvedLinkAction
        switch(unresolvedLinkAction) {
            case CREATE:
                // Don't try to create a Vertex with a null value
                if (joinCurrentValue != null) {
                    if (lookup != null) {
                        final String[] lookupParts = lookup.split("\\.");
                        final OrientVertex linkedV = pipeline.getGraphDatabase().addTemporaryVertex(lookupParts[0]);
                        linkedV.setProperty(lookupParts[1], joinCurrentValue);
                        if (targetVertexFields != null) {
                            for (String f : targetVertexFields.fieldNames()) linkedV.setProperty(f, resolve(targetVertexFields.field(f)));
                        }
                        linkedV.save();
                        log(OETLProcessor.LOG_LEVELS.DEBUG, "created new vertex=%s", linkedV.getRecord());
                        result = linkedV.getIdentity();
                    } else {
                        throw new OConfigurationException("Cannot create linked document because target class is unknown. Use 'lookup' field");
                    }
                }
                break;
            case ERROR:
                processor.getStats().incrementErrors();
                log(OETLProcessor.LOG_LEVELS.ERROR, "%s: ERROR Cannot resolve join for value '%s'", getName(), joinCurrentValue);
                break;
            case WARNING:
                processor.getStats().incrementWarnings();
                log(OETLProcessor.LOG_LEVELS.INFO, "%s: WARN Cannot resolve join for value '%s'", getName(), joinCurrentValue);
                break;
            case SKIP:
                return null;
            case HALT:
                throw new OETLProcessHaltedException("Cannot resolve join for value '" + joinCurrentValue + "'");
            case NOTHING:
            default:
                return null;
        }
    }
    if (result != null) {
        final List<OrientEdge> edges;
        if (OMultiValue.isMultiValue(result)) {
            final int size = OMultiValue.getSize(result);
            if (size == 0)
                // NO EDGES
                return null;
            edges = new ArrayList<OrientEdge>(size);
        } else
            edges = new ArrayList<OrientEdge>(1);
        for (Object o : OMultiValue.getMultiValueIterable(result)) {
            OIdentifiable oid = (OIdentifiable) o;
            final OrientVertex targetVertex = pipeline.getGraphDatabase().getVertex(oid);
            try {
                // CREATE THE EDGE
                final OrientEdge edge;
                if (directionOut)
                    edge = (OrientEdge) vertex.addEdge(edgeClass, targetVertex);
                else
                    edge = (OrientEdge) targetVertex.addEdge(edgeClass, vertex);
                if (edgeFields != null) {
                    for (String f : edgeFields.fieldNames()) edge.setProperty(f, resolve(edgeFields.field(f)));
                }
                edges.add(edge);
                log(OETLProcessor.LOG_LEVELS.DEBUG, "created new edge=%s", edge);
            } catch (ORecordDuplicatedException e) {
                if (skipDuplicates) {
                    log(OETLProcessor.LOG_LEVELS.DEBUG, "skipped creation of new edge because already exists");
                    continue;
                } else {
                    log(OETLProcessor.LOG_LEVELS.ERROR, "error on creation of new edge because it already exists (skipDuplicates=false)");
                    throw e;
                }
            }
        }
        return edges;
    }
    // NO EDGES
    return null;
}
Also used : 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) OETLProcessHaltedException(com.orientechnologies.orient.etl.OETLProcessHaltedException) OConfigurationException(com.orientechnologies.orient.core.exception.OConfigurationException) ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException)

Example 27 with ORecordDuplicatedException

use of com.orientechnologies.orient.core.storage.ORecordDuplicatedException in project orientdb by orientechnologies.

the class TestGraphTransactionOnBatch method testDuplicateEdgeAlreadyPresentRollback.

@Test
public void testDuplicateEdgeAlreadyPresentRollback() {
    OClass clazz = db.getMetadata().getSchema().createClass("Test");
    clazz.setSuperClass(E);
    clazz.createProperty("aKey", OType.STRING).createIndex(INDEX_TYPE.UNIQUE);
    db.command(new OCommandScript("sql", "BEGIN \n LET a = create vertex V \n LET b = create vertex V \n LET c =create edge Test from $a to $b SET aKey = \"12345\"  \n commit  \n" + " RETURN $c")).execute();
    try {
        db.command(new OCommandScript("sql", "BEGIN \n LET a = create vertex V \n LET b = create vertex V \n LET c =create edge Test from $a to $b SET aKey = \"12345\"\n COMMIT \n" + " RETURN $c")).execute();
        Assert.fail("expected record duplicate exception");
    } catch (ORecordDuplicatedException ex) {
    }
    List<ODocument> res = db.query(new OSQLSynchQuery("select from Test"));
    Assert.assertEquals(1, res.size());
}
Also used : ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException) OCommandScript(com.orientechnologies.orient.core.command.script.OCommandScript) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Example 28 with ORecordDuplicatedException

use of com.orientechnologies.orient.core.storage.ORecordDuplicatedException in project orientdb by orientechnologies.

the class OGraphImporterMTAPITest method main.

public static void main(String[] args) throws IOException, InterruptedException {
    OGlobalConfiguration.ENVIRONMENT_LOCK_MANAGER_CONCURRENCY_LEVEL.setValue(64);
    // String dbUrl = "memory:amazonReviews";
    final 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 OrientGraphNoTx graph = new OrientGraphNoTx(dbUrl, "admin", "admin");
    OrientVertexType user = graph.createVertexType("User", 64);
    user.createProperty("uid", OType.STRING);
    user.createIndex("User.uid", OClass.INDEX_TYPE.UNIQUE.toString(), (OProgressListener) null, (ODocument) null, "AUTOSHARDING", new String[] { "uid" });
    OrientVertexType product = graph.createVertexType("Product", 64);
    product.createProperty("uid", OType.STRING);
    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));
    final AtomicLong retry = new AtomicLong();
    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 retry=%d", row, vertexCount, ((vertexCount - lastVertexCount) * 1000 / 2000), edgeCount, ((edgeCount - lastEdgeCount) * 1000 / 2000), retry.get()));
            lastVertexCount = vertexCount;
            lastEdgeCount = edgeCount;
        }
    }, 2000, 2000);
    final ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(10000);
    final Thread[] threads = new Thread[parallel];
    for (int i = 0; i < parallel; ++i) {
        threads[i] = new Thread() {

            @Override
            public void run() {
                final OrientGraph localGraph = new OrientGraph(dbUrl, "admin", "admin", false);
                final OIndex<?> userIndex = localGraph.getRawGraph().getMetadata().getIndexManager().getIndex("User.uid");
                final OIndex<?> productIndex = localGraph.getRawGraph().getMetadata().getIndexManager().getIndex("Product.uid");
                localGraph.begin();
                for (int i = 0; ; ++i) {
                    final String line;
                    try {
                        line = queue.take();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        return;
                    }
                    final String[] parts = line.split(",");
                    if (parts.length != 4) {
                        // SKIP IT
                        System.out.print("Skipped invalid line " + i + ": " + 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]));
                    for (int localRetry = 0; localRetry < 100; ++localRetry) {
                        try {
                            final Object k1 = userIndex.get(parts[0]);
                            OrientVertex v1;
                            if (k1 == null) {
                                v1 = localGraph.addVertex("class:User", "uid", parts[0]);
                            } else
                                v1 = localGraph.getVertex(k1);
                            final Object k2 = productIndex.get(parts[1]);
                            OrientVertex v2;
                            if (k2 == null) {
                                v2 = localGraph.addVertex("class:Product", "uid", parts[1]);
                            } else
                                v2 = localGraph.getVertex(k2);
                            final OrientEdge edge = localGraph.addEdge(null, v1, v2, "Reviewed");
                            edge.setProperties(properties);
                            if (i % 2 == 0) {
                                localGraph.commit();
                                localGraph.begin();
                            }
                            break;
                        } catch (ONeedRetryException e) {
                            // RETRY
                            retry.incrementAndGet();
                        } catch (ORecordDuplicatedException e) {
                            // RETRY
                            retry.incrementAndGet();
                        }
                    }
                }
            }
        };
    }
    for (int i = 0; i < parallel; ++i) threads[i].start();
    try {
        for (String line; (line = br.readLine()) != null; ) {
            row++;
            queue.put(line);
        }
    } finally {
        br.close();
    }
    for (int i = 0; i < parallel; ++i) threads[i].join();
    graph.shutdown();
    roGraph.shutdown();
}
Also used : OIndex(com.orientechnologies.orient.core.index.OIndex) TimerTask(java.util.TimerTask) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) FileReader(java.io.FileReader) AtomicLong(java.util.concurrent.atomic.AtomicLong) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException) BufferedReader(java.io.BufferedReader) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 29 with ORecordDuplicatedException

use of com.orientechnologies.orient.core.storage.ORecordDuplicatedException in project orientdb by orientechnologies.

the class TestGraphTransactionOnBatch method testDuplicateRollback.

@Test
public void testDuplicateRollback() {
    OClass clazz = db.getMetadata().getSchema().createClass("Test");
    clazz.setSuperClass(V);
    clazz.createProperty("id", OType.STRING).createIndex(INDEX_TYPE.UNIQUE);
    try {
        db.command(new OCommandScript("sql", "BEGIN \n LET a = create vertex Test SET id = \"12345678\" \n LET b = create vertex Test SET id = \"4kkrPhGe\" \n LET c =create vertex Test SET id = \"4kkrPhGe\" \n COMMIT \n RETURN $b ")).execute();
        Assert.fail("expected record duplicate exception");
    } catch (ORecordDuplicatedException ex) {
    }
    try {
        db.command(new OCommandScript("sql", "BEGIN \n LET a = create vertex Test content {\"id\": \"12345678\"} \n LET b = create vertex Test content {\"id\": \"4kkrPhGe\"} \n LET c =create vertex Test content { \"id\": \"4kkrPhGe\"} \n COMMIT \n RETURN $b ")).execute();
        Assert.fail("expected record duplicate exception");
    } catch (ORecordDuplicatedException ex) {
    }
    List<ODocument> res = db.query(new OSQLSynchQuery("select from Test"));
    Assert.assertEquals(0, res.size());
}
Also used : ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException) OCommandScript(com.orientechnologies.orient.core.command.script.OCommandScript) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Example 30 with ORecordDuplicatedException

use of com.orientechnologies.orient.core.storage.ORecordDuplicatedException in project orientdb by orientechnologies.

the class EdgeIndexingTest method testOutLinksUniqueness.

/**
   * Test that "in_vertex" has edges to only single "out_vertex" but we may have several edges to single "out_vertex".
   */
@Test
public void testOutLinksUniqueness() {
    final String url = "memory:" + this.getClass().getSimpleName();
    OrientGraph graph = new OrientGraph(url);
    graph.drop();
    graph = new OrientGraph(url);
    graph.setUseLightweightEdges(true);
    graph.createEdgeType("link");
    graph.setAutoStartTx(false);
    OClass outVertexType = graph.createVertexType("IndexedOutVertex");
    outVertexType.createProperty("out_link", OType.LINKBAG);
    outVertexType.createIndex("uniqueLinkIndex", "unique", "out_link");
    graph.setAutoStartTx(true);
    Vertex vertexOutOne = graph.addVertex("class:IndexedOutVertex");
    Vertex vertexInOne = graph.addVertex(null);
    Vertex vertexInTwo = graph.addVertex(null);
    vertexOutOne.addEdge("link", vertexInOne);
    vertexOutOne.addEdge("link", vertexInTwo);
    graph.commit();
    Vertex vertexOutTwo = graph.addVertex("class:IndexedOutVertex");
    vertexOutTwo.addEdge("link", vertexInTwo);
    try {
        graph.commit();
        // in vertex can be linked by only one out vertex.
        Assert.fail();
    } catch (ORecordDuplicatedException e) {
    }
    graph.drop();
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) Test(org.junit.Test)

Aggregations

ORecordDuplicatedException (com.orientechnologies.orient.core.storage.ORecordDuplicatedException)33 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)19 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)17 Test (org.junit.Test)10 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)8 OCommandScript (com.orientechnologies.orient.core.command.script.OCommandScript)6 ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)5 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)5 Vertex (com.tinkerpop.blueprints.Vertex)5 ONeedRetryException (com.orientechnologies.common.concur.ONeedRetryException)4 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)4 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)4 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)4 ArrayList (java.util.ArrayList)4 Test (org.testng.annotations.Test)4 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)3 DatabaseAbstractTest (com.orientechnologies.DatabaseAbstractTest)2 OTransactionException (com.orientechnologies.orient.core.exception.OTransactionException)2 ORecordId (com.orientechnologies.orient.core.id.ORecordId)2 OrientTest (com.orientechnologies.orient.test.database.base.OrientTest)2