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();
}
}
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);
}
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);
}
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" });
}
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();
}
}
Aggregations