//Logger.getLogger(getClass()).debug("serializeNode: " + node + " with id: " + parent_id);
this.id_counter++;
final int edge_id = node.getId();
final BasicOperator op = node.getOperator();
boolean newEntry = false;
/*
* store all Operators in our new map!
*/
if (!map.containsKey(op)) {
map.put(op, id_counter);
/*
* this is a new entry! (so no re-visit node, that is just serialized)
*/
newEntry = true;
}
/*
* for our forward-connections in the operator graph (we stored an edge to a not known node, that is
* now added, so we know its new id and can serialize the edge!)
*/
if (addLater.containsKey(op)) {
/*
* an object is now serialized, that is used before! now we know its
* id, so we can add the edge from (op_id) to (already stored
* succeeding id)
*/
Tuple<Integer,Integer> data = addLater.get(op);
JSONObject edge = new JSONObject();
try {
edge.put("from", id_counter);
edge.put("to", data.getFirst());
edge.put("edge_id", data.getSecond());
addEdgeTo(edgesJSON, edge);
} catch (Exception e) {
propagate(e);
}
}
if (parent_id > 0) {
final JSONObject edge = new JSONObject();
try {
int counterID = this.id_counter;
if (map.containsKey(op)) {
/*
* get the node id of the operator
*/
counterID = map.get(op);
if (op.getPrecedingOperators().size() == 0) {
//if there is no preceding, it should be the root (node_id = 1)
edge.put("from", 1);
edge.put("to", counterID);
edge.put("edge_id", edge_id);
addEdgeTo(edgesJSON, edge);
} else {
//otherwise create edges to the current operator by
//its preceding operators
for (BasicOperator is : op.getPrecedingOperators()) {
/*
* if the preceding is already known (an node_is is set),
* add the edge, otherwise store the information
* to be added, if the not known operator is stored!
*/
if (map.containsKey(is)) {
edge.put("from", map.get(is));
edge.put("to", counterID);
edge.put("edge_id", edge_id);
addEdgeTo(edgesJSON, edge);
} else {
/*
* we found an operator that is still not known! but we have
* to store the edge to the future serialized node!
*/
int tmpEdge_id = is.getOperatorIDTuple(op).getId();
addLater.put(is, new Tuple<Integer,Integer>(counterID,tmpEdge_id));
}
}
}
}
} catch (final JSONException e) {
e.printStackTrace();
}
}
OperatorFormatter serializer;
if (op instanceof BasicIndexScan) {
serializer = new IndexScanFormatter();
} else if (op instanceof Root) {
serializer = new RootFormatter();
} else if (op instanceof Result) {
serializer = new ResultFormatter();
} else if (op instanceof Filter) {
serializer = new FilterFormatter();
} else if (op instanceof Join) {
/*
* added the Join formatter for joins in subgraph-containers
*/
serializer = new JoinFormatter();
} else if (op instanceof SubgraphContainer) {
/*
* added the formatter for an included subgraph in the subgraph
*/
serializer = new SubSubgraphContainerFormatter();
} else {
throw new RuntimeException("Something is wrong here. Forgot case?");
}
try {
/*
* only add new entry, because otherwise we serialize an node twice ;(
*/
if (newEntry) {
nodesJSON.add(serializer.serialize(op, this.id_counter));
}
} catch (final NullPointerException e) {
throw new IllegalArgumentException(
"This operator is not serializable", e);
} catch (final JSONException e) {
throw propagate(e);
}
for (final OperatorIDTuple successor : op.getSucceedingOperators()) {
this.serializeNode(successor, nodesJSON, edgesJSON, this.id_counter);
}
}