Search in sources :

Example 26 with OrientGraphNoTx

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

the class OHttpGraphResponse method writeRecords.

public void writeRecords(final Object iRecords, final String iFetchPlan, String iFormat, final String accept, final Map<String, Object> iAdditionalProperties, final String mode) throws IOException {
    if (iRecords == null) {
        send(OHttpUtils.STATUS_OK_NOCONTENT_CODE, "", OHttpUtils.CONTENT_TEXT_PLAIN, null, null);
        return;
    }
    if (!mode.equalsIgnoreCase("graph")) {
        super.writeRecords(iRecords, iFetchPlan, iFormat, accept, iAdditionalProperties, mode);
        return;
    }
    if (accept != null && accept.contains("text/csv"))
        throw new IllegalArgumentException("Graph mode cannot accept '" + accept + "'");
    final OrientGraphNoTx graph = (OrientGraphNoTx) OrientGraphFactory.getNoTxGraphImplFactory().getGraph((ODatabaseDocumentTx) ODatabaseRecordThreadLocal.INSTANCE.get());
    try {
        // all the edges in the result-set
        final Set<OrientEdge> rsEdges = new HashSet<OrientEdge>();
        // all the vertices sent
        final Set<OrientVertex> vertices = new HashSet<OrientVertex>();
        final Iterator<Object> iIterator = OMultiValue.getMultiValueIterator(iRecords);
        while (iIterator.hasNext()) {
            Object entry = iIterator.next();
            if (entry == null || !(entry instanceof OIdentifiable))
                // IGNORE IT
                continue;
            entry = ((OIdentifiable) entry).getRecord();
            if (entry == null || !(entry instanceof OIdentifiable))
                // IGNORE IT
                continue;
            if (entry instanceof ODocument) {
                OClass schemaClass = ((ODocument) entry).getSchemaClass();
                if (schemaClass != null && schemaClass.isVertexType())
                    vertices.add(graph.getVertex(entry));
                else if (schemaClass != null && schemaClass.isEdgeType()) {
                    OrientEdge edge = graph.getEdge(entry);
                    rsEdges.add(edge);
                    vertices.add(graph.getVertex(edge.getVertex(Direction.IN)));
                    vertices.add(graph.getVertex(edge.getVertex(Direction.OUT)));
                } else
                    // IGNORE IT
                    continue;
            }
        }
        final StringWriter buffer = new StringWriter();
        final OJSONWriter json = new OJSONWriter(buffer, "");
        json.beginObject();
        json.beginObject("graph");
        // WRITE VERTICES
        json.beginCollection("vertices");
        for (OrientVertex vertex : vertices) {
            json.beginObject();
            json.writeAttribute("@rid", vertex.getIdentity());
            json.writeAttribute("@class", vertex.getRecord().getClassName());
            // ADD ALL THE PROPERTIES
            for (String field : vertex.getPropertyKeys()) {
                final Object v = vertex.getProperty(field);
                if (v != null)
                    json.writeAttribute(field, v);
            }
            json.endObject();
        }
        json.endCollection();
        // WRITE EDGES
        json.beginCollection("edges");
        if (rsEdges.isEmpty()) {
            // no explicit edges in the result-set, let's calculate them from vertices
            Set<ORID> edgeRids = new HashSet<ORID>();
            for (OrientVertex vertex : vertices) {
                for (Edge e : vertex.getEdges(Direction.BOTH)) {
                    OrientEdge edge = (OrientEdge) e;
                    if (edgeRids.contains(((OrientEdge) e).getIdentity())) {
                        continue;
                    }
                    if (!vertices.contains(edge.getVertex(Direction.OUT)) || !vertices.contains(edge.getVertex(Direction.IN)))
                        // ONE OF THE 2 VERTICES ARE NOT PART OF THE RESULT SET: DISCARD IT
                        continue;
                    edgeRids.add(edge.getIdentity());
                    writeEdge(edge, json);
                }
            }
        } else {
            // edges are explicitly in the RS, only send them
            for (OrientEdge edge : rsEdges) {
                if (!vertices.contains(edge.getVertex(Direction.OUT)) || !vertices.contains(edge.getVertex(Direction.IN)))
                    // ONE OF THE 2 VERTICES ARE NOT PART OF THE RESULT SET: DISCARD IT
                    continue;
                writeEdge(edge, json);
            }
        }
        json.endCollection();
        if (iAdditionalProperties != null) {
            for (Map.Entry<String, Object> entry : iAdditionalProperties.entrySet()) {
                final Object v = entry.getValue();
                if (OMultiValue.isMultiValue(v)) {
                    json.beginCollection(-1, true, entry.getKey());
                    formatMultiValue(OMultiValue.getMultiValueIterator(v), buffer, null);
                    json.endCollection(-1, true);
                } else
                    json.writeAttribute(entry.getKey(), v);
                if (Thread.currentThread().isInterrupted())
                    break;
            }
        }
        json.endObject();
        json.endObject();
        send(OHttpUtils.STATUS_OK_CODE, "OK", OHttpUtils.CONTENT_JSON, buffer.toString(), null);
    } finally {
        graph.shutdown();
    }
}
Also used : OJSONWriter(com.orientechnologies.orient.core.serialization.serializer.OJSONWriter) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) StringWriter(java.io.StringWriter) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OrientGraphNoTx(com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx) ORID(com.orientechnologies.orient.core.id.ORID) Edge(com.tinkerpop.blueprints.Edge) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) Map(java.util.Map) HashSet(java.util.HashSet) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 27 with OrientGraphNoTx

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

the class OGraphCommandExecutorSQLFactory method getGraphNoTx.

/**
 * @return a Non Transactional OrientGraph implementation from the current database in thread local.
 */
public static OrientGraphNoTx getGraphNoTx(final OModifiableBoolean shouldBeShutDown) {
    final ODatabaseDocument database = ODatabaseRecordThreadLocal.INSTANCE.get();
    final OrientBaseGraph result = OrientBaseGraph.getActiveGraph();
    if (result != null && (result instanceof OrientGraphNoTx)) {
        final ODatabaseDocumentTx graphDb = result.getRawGraph();
        // CHECK IF THE DATABASE + USER IN TL IS THE SAME IN ORDER TO USE IT
        if (canReuseActiveGraph(graphDb, database)) {
            if (!graphDb.isClosed()) {
                ODatabaseRecordThreadLocal.INSTANCE.set(graphDb);
                shouldBeShutDown.setValue(false);
                return (OrientGraphNoTx) result;
            }
        }
    }
    // Set it again on ThreadLocal because the getRawGraph() may have set a closed db in the thread-local
    shouldBeShutDown.setValue(true);
    ODatabaseRecordThreadLocal.INSTANCE.set((ODatabaseDocumentInternal) database);
    return (OrientGraphNoTx) OrientGraphFactory.getNoTxGraphImplFactory().getGraph((ODatabaseDocumentTx) database);
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OrientGraphNoTx(com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)

Example 28 with OrientGraphNoTx

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

the class LuceneListIndexingTest method rname.

@Test
public void rname() throws Exception {
    final OrientGraphNoTx graph = new OrientGraphNoTx(db);
    final OrientVertexType c1 = graph.createVertexType("C1");
    c1.createProperty("p1", OType.STRING);
    final ODocument metadata = new ODocument();
    metadata.field("default", "org.apache.lucene.analysis.en.EnglishAnalyzer");
    c1.createIndex("p1", "FULLTEXT", null, metadata, "LUCENE", new String[] { "p1" });
    final OrientVertex result = graph.addVertex("class:C1");
    result.setProperty("p1", "testing");
    graph.commit();
    final Iterable search = db.command(new OSQLSynchQuery<ODocument>("SELECT from C1 WHERE p1 LUCENE \"tested\"")).execute();
    assertThat(search).hasSize(1);
}
Also used : OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OrientGraphNoTx(com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx) OrientVertexType(com.tinkerpop.blueprints.impls.orient.OrientVertexType) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Example 29 with OrientGraphNoTx

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

the class LuceneTransactionEmbeddedQueryTest method createSchema.

protected void createSchema(ODatabaseDocumentTx db) {
    final OrientVertexType c1 = new OrientGraphNoTx(db).createVertexType("C1");
    c1.createProperty("p1", OType.EMBEDDEDLIST, OType.STRING);
    c1.createIndex("C1.p1", "FULLTEXT", null, null, "LUCENE", new String[] { "p1" });
}
Also used : OrientVertexType(com.tinkerpop.blueprints.impls.orient.OrientVertexType) OrientGraphNoTx(com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx)

Example 30 with OrientGraphNoTx

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

the class LuceneMiscTest method testDoubleLucene.

// TODO Re-enable when removed check syntax on ODB
public void testDoubleLucene() {
    OrientGraphNoTx graph = new OrientGraphNoTx("memory:doubleLucene");
    try {
        ODatabaseDocumentTx db = graph.getRawGraph();
        db.command(new OCommandSQL("create class Test extends V")).execute();
        db.command(new OCommandSQL("create property Test.attr1 string")).execute();
        db.command(new OCommandSQL("create index Test.attr1 on Test (attr1) fulltext engine lucene")).execute();
        db.command(new OCommandSQL("create property Test.attr2 string")).execute();
        db.command(new OCommandSQL("create index Test.attr2 on Test (attr2) fulltext engine lucene")).execute();
        db.command(new OCommandSQL("insert into Test set attr1='foo', attr2='bar'")).execute();
        db.command(new OCommandSQL("insert into Test set attr1='bar', attr2='foo'")).execute();
        List<ODocument> results = db.command(new OCommandSQL("select from Test where attr1 lucene 'foo*' OR attr2 lucene 'foo*'")).execute();
        Assert.assertEquals(results.size(), 2);
        results = db.command(new OCommandSQL("select from Test where attr1 lucene 'bar*' OR attr2 lucene 'bar*'")).execute();
        Assert.assertEquals(results.size(), 2);
        results = db.command(new OCommandSQL("select from Test where attr1 lucene 'foo*' AND attr2 lucene 'bar*'")).execute();
        Assert.assertEquals(results.size(), 1);
        results = db.command(new OCommandSQL("select from Test where attr1 lucene 'bar*' AND attr2 lucene 'foo*'")).execute();
        Assert.assertEquals(results.size(), 1);
    } finally {
        graph.drop();
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OrientGraphNoTx(com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OrientGraphNoTx (com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx)72 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)28 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)23 Test (org.junit.Test)22 OrientGraphFactory (com.tinkerpop.blueprints.impls.orient.OrientGraphFactory)20 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)17 OrientVertexType (com.tinkerpop.blueprints.impls.orient.OrientVertexType)17 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)16 OrientBaseGraph (com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)15 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)7 Vertex (com.tinkerpop.blueprints.Vertex)6 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)6 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)5 OGraphMLReader (com.orientechnologies.orient.graph.graphml.OGraphMLReader)5 OGraphRepair (com.tinkerpop.blueprints.impls.orient.OGraphRepair)5 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)4 Edge (com.tinkerpop.blueprints.Edge)4 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)3 OrientEdge (com.tinkerpop.blueprints.impls.orient.OrientEdge)3 File (java.io.File)3