Search in sources :

Example 91 with OrientVertex

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

the class AsyncIndexTest method dbClient1.

protected void dbClient1() {
    OrientBaseGraph graph = new OrientGraphNoTx(getLocalURL());
    try {
        graph.command(new OCommandSQL("create class SMS")).execute();
        graph.command(new OCommandSQL("create property SMS.type string")).execute();
        graph.command(new OCommandSQL("create property SMS.lang string")).execute();
        graph.command(new OCommandSQL("create property SMS.source integer")).execute();
        graph.command(new OCommandSQL("create property SMS.content string")).execute();
        graph.command(new OCommandSQL("alter property SMS.lang min 2")).execute();
        graph.command(new OCommandSQL("alter property SMS.lang max 2")).execute();
        graph.command(new OCommandSQL("create index sms_keys ON SMS (type, lang) unique")).execute();
        graph.command(new OCommandSQL("insert into sms (type, lang, source, content) values ( 'notify', 'en', 1, 'This is a test')")).execute();
        try {
            graph.command(new OCommandSQL("insert into sms (type, lang, source, content) values ( 'notify', 'en', 1, 'This is a test')")).execute();
            Assert.fail("violated unique index was not raised");
        } catch (ORecordDuplicatedException e) {
        }
        final Iterable<OrientVertex> result = graph.command(new OSQLSynchQuery<OrientVertex>("select count(*) from SMS")).execute();
        Assert.assertEquals(1, ((Number) result.iterator().next().getProperty("count")).intValue());
    } catch (Throwable e) {
        if (exceptionInThread == null) {
            exceptionInThread = e;
        }
    } finally {
        OLogManager.instance().info(this, "Shutting down db1");
        graph.shutdown();
    }
    // CHECK ON THE OTHER NODE
    OrientBaseGraph graph2 = new OrientGraphNoTx(getLocalURL2());
    try {
        try {
            graph2.command(new OCommandSQL("insert into sms (type, lang, source, content) values ( 'notify', 'en', 1, 'This is a test')")).execute();
            Assert.fail("violated unique index was not raised");
        } catch (ORecordDuplicatedException e) {
        }
        final Iterable<OrientVertex> result = graph2.command(new OSQLSynchQuery<OrientVertex>("select count(*) from SMS")).execute();
        Assert.assertEquals(1, ((Number) result.iterator().next().getProperty("count")).intValue());
    } catch (Throwable e) {
        if (exceptionInThread == null) {
            exceptionInThread = e;
        }
    } finally {
        OLogManager.instance().info(this, "Shutting down db2");
        graph2.shutdown();
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OrientGraphNoTx(com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)

Example 92 with OrientVertex

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

the class OOrientDBLoader method load.

@Override
public void load(OETLPipeline pipeline, final Object input, OCommandContext context) {
    if (input == null)
        return;
    if (dbAutoCreateProperties) {
        autoCreateProperties(pipeline, input);
    }
    if (tx && dbType == DOCUMENT) {
        final ODatabaseDocument documentDatabase = pipeline.getDocumentDatabase();
        if (!documentDatabase.getTransaction().isActive()) {
            documentDatabase.begin();
            documentDatabase.getTransaction().setUsingLog(txUseLog);
        }
    }
    if (input instanceof OrientVertex) {
        final OrientVertex v = (OrientVertex) input;
        try {
            v.save(clusterName);
        } catch (ORecordDuplicatedException e) {
            if (skipDuplicates) {
            } else {
                throw e;
            }
        } finally {
        }
    } else if (input instanceof ODocument) {
        final ODocument doc = (ODocument) input;
        if (className != null) {
            doc.setClassName(className);
        }
        if (clusterName != null) {
            doc.save(clusterName);
        } else {
            doc.save();
        }
    }
    progress.incrementAndGet();
    // DO BATCH COMMIT
    if (batchCommitSize > 0 && batchCounter.get() > batchCommitSize) {
        if (dbType == DOCUMENT) {
            final ODatabaseDocument documentDatabase = pipeline.getDocumentDatabase();
            log(DEBUG, "committing batch");
            documentDatabase.commit();
            documentDatabase.begin();
            documentDatabase.getTransaction().setUsingLog(txUseLog);
        } else {
            log(DEBUG, "committing batch");
            pipeline.getGraphDatabase().commit();
        }
        batchCounter.set(0);
    } else {
        batchCounter.incrementAndGet();
    }
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 93 with OrientVertex

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

the class OEdgeTransformer method executeTransform.

@Override
public Object executeTransform(final Object input) {
    for (Object o : OMultiValue.getMultiValueIterable(input)) {
        // GET JOIN VALUE
        final OrientVertex vertex;
        if (o instanceof OrientVertex)
            vertex = (OrientVertex) o;
        else if (o instanceof OIdentifiable)
            vertex = pipeline.getGraphDatabase().getVertex(o);
        else
            throw new OTransformException(getName() + ": input type '" + o + "' is not supported");
        final Object joinCurrentValue = joinValue != null ? joinValue : vertex.getProperty(joinFieldName);
        if (OMultiValue.isMultiValue(joinCurrentValue)) {
            // RESOLVE SINGLE JOINS
            for (Object ob : OMultiValue.getMultiValueIterable(joinCurrentValue)) {
                final Object r = lookup(ob, true);
                if (createEdge(vertex, ob, r) == null) {
                    if (unresolvedLinkAction == ACTION.SKIP)
                        // RETURN NULL ONLY IN CASE SKIP ACTION IS REQUESTED
                        return null;
                }
            }
        } else {
            final Object result = lookup(joinCurrentValue, true);
            if (createEdge(vertex, joinCurrentValue, result) == null) {
                if (unresolvedLinkAction == ACTION.SKIP)
                    // RETURN NULL ONLY IN CASE SKIP ACTION IS REQUESTED
                    return null;
            }
        }
    }
    return input;
}
Also used : OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Example 94 with OrientVertex

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

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

the class DirtyManagerGraph method testLoopOfNew.

@Test
public void testLoopOfNew() {
    OrientGraph graph = new OrientGraph("memory:" + DirtyManagerGraph.class.getSimpleName());
    try {
        graph.createEdgeType("next");
        OrientVertex vertex = graph.addVertex(null);
        OrientVertex vertex1 = graph.addVertex(null);
        OrientVertex vertex2 = graph.addVertex(null);
        OrientVertex vertex3 = graph.addVertex(null);
        OrientEdge edge1 = (OrientEdge) vertex.addEdge("next", vertex1);
        OrientEdge edge2 = (OrientEdge) vertex1.addEdge("next", vertex2);
        OrientEdge edge3 = (OrientEdge) vertex2.addEdge("next", vertex3);
        OrientEdge edge4 = (OrientEdge) vertex3.addEdge("next", vertex);
        ODocument rec = vertex.getRecord();
        ODirtyManager manager = ORecordInternal.getDirtyManager(rec);
        List<OIdentifiable> pointed = manager.getPointed(vertex.getRecord());
        assertEquals(2, pointed.size());
        assertTrue(pointed.contains(edge1.getRecord()));
        assertTrue(pointed.contains(edge4.getRecord()));
        pointed = manager.getPointed(vertex1.getRecord());
        assertEquals(2, pointed.size());
        assertTrue(pointed.contains(edge1.getRecord()));
        assertTrue(pointed.contains(edge2.getRecord()));
        pointed = manager.getPointed(vertex2.getRecord());
        assertEquals(2, pointed.size());
        assertTrue(pointed.contains(edge2.getRecord()));
        assertTrue(pointed.contains(edge3.getRecord()));
        pointed = manager.getPointed(vertex3.getRecord());
        assertEquals(2, pointed.size());
        assertTrue(pointed.contains(edge3.getRecord()));
        assertTrue(pointed.contains(edge4.getRecord()));
        pointed = manager.getPointed(edge1.getRecord());
        assertEquals(2, pointed.size());
        assertTrue(pointed.contains(vertex.getRecord()));
        assertTrue(pointed.contains(vertex1.getRecord()));
        pointed = manager.getPointed(edge2.getRecord());
        assertEquals(2, pointed.size());
        assertTrue(pointed.contains(vertex1.getRecord()));
        assertTrue(pointed.contains(vertex2.getRecord()));
        pointed = manager.getPointed(edge3.getRecord());
        assertEquals(2, pointed.size());
        assertTrue(pointed.contains(vertex2.getRecord()));
        assertTrue(pointed.contains(vertex3.getRecord()));
        pointed = manager.getPointed(edge4.getRecord());
        assertEquals(2, pointed.size());
        assertTrue(pointed.contains(vertex3.getRecord()));
        assertTrue(pointed.contains(vertex.getRecord()));
    } finally {
        graph.drop();
    }
}
Also used : OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) ODirtyManager(com.orientechnologies.orient.core.record.impl.ODirtyManager) 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) Test(org.junit.Test)

Aggregations

OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)155 Test (org.junit.Test)73 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)42 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)40 OrientBaseGraph (com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)37 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)35 OrientGraphNoTx (com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx)26 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)25 OrientEdge (com.tinkerpop.blueprints.impls.orient.OrientEdge)25 Vertex (com.tinkerpop.blueprints.Vertex)24 HashMap (java.util.HashMap)20 OBasicCommandContext (com.orientechnologies.orient.core.command.OBasicCommandContext)13 OrientGraphFactory (com.tinkerpop.blueprints.impls.orient.OrientGraphFactory)13 OrientVertexType (com.tinkerpop.blueprints.impls.orient.OrientVertexType)13 Edge (com.tinkerpop.blueprints.Edge)12 OConcurrentModificationException (com.orientechnologies.orient.core.exception.OConcurrentModificationException)10 GraphNoTxAbstractTest (com.orientechnologies.orient.graph.GraphNoTxAbstractTest)9 ORID (com.orientechnologies.orient.core.id.ORID)7 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)7 GraphTxAbstractTest (com.orientechnologies.orient.graph.GraphTxAbstractTest)7