Optimize memory management and performance for graph rendering in Studio
resolves internal issue 8628
@@ -22,6 +22,7 @@ package com.orientechnologies.orient.server.network.protocol.http;
import com.orientechnologies.common.collection.OMultiValue;
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
+import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.serialization.serializer.OJSONWriter;
@@ -57,7 +58,6 @@ public class OHttpGraphResponse extends OHttpResponse {
return;
}
-
if (!mode.equalsIgnoreCase("graph")) {
super.writeRecords(iRecords, iFetchPlan, iFormat, accept, iAdditionalProperties, mode);
return;
@@ -72,7 +72,6 @@ public class OHttpGraphResponse extends OHttpResponse {
try {
// DIVIDE VERTICES FROM EDGES
final Set<OrientVertex> vertices = new HashSet<OrientVertex>();
- final Set<OrientEdge> edges = new HashSet<OrientEdge>();
final Iterator<Object> iIterator = OMultiValue.getMultiValueIterator(iRecords);
while (iIterator.hasNext()) {
@@ -95,7 +94,6 @@ public class OHttpGraphResponse extends OHttpResponse {
OrientEdge edge = graph.getEdge(entry);
vertices.add(graph.getVertex(edge.getVertex(Direction.IN)));
vertices.add(graph.getVertex(edge.getVertex(Direction.OUT)));
- edges.add(edge);
} else
// IGNORE IT
continue;
@@ -115,10 +113,6 @@ public class OHttpGraphResponse extends OHttpResponse {
json.writeAttribute("@rid", vertex.getIdentity());
json.writeAttribute("@class", vertex.getRecord().getClassName());
- // ADD ALL THE VERTEX'S EDGES
- for (Edge e : vertex.getEdges(Direction.BOTH))
- edges.add((OrientEdge) e);
-
// ADD ALL THE PROPERTIES
for (String field : vertex.getPropertyKeys()) {
final Object v = vertex.getProperty(field);
@@ -131,11 +125,19 @@ public class OHttpGraphResponse extends OHttpResponse {
// WRITE EDGES
json.beginCollection("edges");
- for (OrientEdge edge : edges) {
+ 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());
+
json.beginObject();
json.writeAttribute("@rid", edge.getIdentity());
json.writeAttribute("@class", edge.getRecord().getClassName());
@@ -151,6 +153,8 @@ public class OHttpGraphResponse extends OHttpResponse {
json.endObject();
}
+ }
+
json.endCollection();
if (iAdditionalProperties != null) {
