Permalink
Browse files

Fixes jackson handling of Writer.close(), updates small-*.json data

  • Loading branch information...
1 parent 59a347a commit a459d2a58feb082680b06dc2d8d6d2351fa48dd0 @sagemintblue sagemintblue committed Apr 4, 2013
@@ -32,6 +32,8 @@
import java.util.SortedMap;
import java.util.concurrent.ConcurrentSkipListMap;
+import com.google.common.collect.Maps;
+
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -58,9 +60,8 @@
private static final String DUMP_DAG_FILE_PARAM = "ambrose.write.dag.file";
private static final String DUMP_EVENTS_FILE_PARAM = "ambrose.write.events.file";
- private Map<String, DAGNode<Job>> dagNodeNameMap = new HashMap<String, DAGNode<Job>>();
- private SortedMap<Integer, Event> eventMap =
- new ConcurrentSkipListMap<Integer, Event>();
+ private Map<String, DAGNode<Job>> dagNodeNameMap = Maps.newHashMap();
+ private SortedMap<Integer, Event> eventMap = new ConcurrentSkipListMap<Integer, Event>();
private Writer dagWriter = null;
private Writer eventsWriter = null;
@@ -115,26 +116,23 @@ public synchronized void pushEvent(String workflowId, Event event) throws IOExce
private void writeJsonDagNodenameMapToDisk(Map<String, DAGNode<Job>> dagNodeNameMap) throws IOException {
if (dagWriter != null && dagNodeNameMap != null) {
- Collection<DAGNode<Job>> nodes = dagNodeNameMap.values();
- JSONUtil.writeJson(dagWriter, nodes.toArray(new DAGNode[dagNodeNameMap.size()]));
+ JSONUtil.writeJson(dagWriter, dagNodeNameMap.values());
}
}
private void writeJsonEventToDisk(Event event) throws IOException {
if (eventsWriter != null && event != null) {
- eventsWriter.append(!eventWritten ? "[ " : ", ");
+ eventsWriter.write(!eventWritten ? "[ " : ", ");
JSONUtil.writeJson(eventsWriter, event);
eventsWriter.flush();
eventWritten = true;
}
}
public void flushJsonToDisk() throws IOException {
-
if (dagWriter != null) { dagWriter.close(); }
-
if (eventsWriter != null) {
- if (eventWritten) { eventsWriter.append("]\n"); }
+ if (eventWritten) { eventsWriter.write(" ]\n"); }
eventsWriter.close();
}
}
@@ -25,6 +25,7 @@
import java.nio.charset.Charset;
import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -37,14 +38,6 @@
* @author billg
*/
public class JSONUtil {
- private static final ObjectMapper mapper = new ObjectMapper();
- static {
- mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
- mapper.enable(SerializationFeature.INDENT_OUTPUT);
- mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
- mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
- }
-
/**
* Writes object to the writer as JSON using Jackson and adds a new-line before flushing.
*
@@ -54,12 +47,15 @@
*/
public static void writeJson(Writer writer, Object object) throws IOException {
mapper.writeValue(writer, object);
- writer.write("\n");
- writer.flush();
}
public static void writeJson(String fileName, Object object) throws IOException {
- JSONUtil.writeJson(new PrintWriter(fileName), object);
+ Writer writer = new PrintWriter(fileName);
+ try {
+ JSONUtil.writeJson(writer, object);
+ } finally {
+ writer.close();
+ }
}
public static Object readJson(String json, TypeReference<?> type) throws IOException {
@@ -76,4 +72,17 @@ public static String readFile(String path) throws IOException {
stream.close();
}
}
+
+ private static final ObjectMapper mapper = new ObjectMapper();
+
+ static {
+ mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
+ mapper.enable(SerializationFeature.INDENT_OUTPUT);
+ mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
+ mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT, false);
+ mapper.disable(SerializationFeature.FLUSH_AFTER_WRITE_VALUE);
+ mapper.disable(SerializationFeature.CLOSE_CLOSEABLE);
+ mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
+ mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
+ }
}
@@ -1,25 +1,25 @@
[ {
- "name" : "scope-44",
+ "name" : "scope-148",
"job" : {
- "aliases" : [ "JOINED", "L", "R" ],
- "features" : [ "HASH_JOIN" ],
- "runtime" : "pig"
+ "runtime" : "pig",
+ "aliases" : [ "1-115", "1-116", "enemies_users_histogram", "friends_users_histogram", "masochists", "narcissists", "user", "user_counts" ],
+ "features" : [ "MULTI_QUERY", "COMBINER" ]
},
- "successorNames" : [ "scope-48" ]
+ "successorNames" : [ "scope-168" ]
}, {
- "name" : "scope-48",
+ "name" : "scope-168",
"job" : {
- "aliases" : [ "ORDERED" ],
- "features" : [ "SAMPLER" ],
- "runtime" : "pig"
+ "runtime" : "pig",
+ "aliases" : [ "1-117", "user_enemy", "user_enemy2", "user_enemy_enemy" ],
+ "features" : [ "HASH_JOIN" ]
},
- "successorNames" : [ "scope-63" ]
+ "successorNames" : [ "scope-172" ]
}, {
- "name" : "scope-63",
+ "name" : "scope-172",
"job" : {
- "aliases" : [ "ORDERED" ],
- "features" : [ "ORDER_BY" ],
- "runtime" : "pig"
+ "runtime" : "pig",
+ "aliases" : [ "1-118", "1-119", "1-120", "name", "user_enemies_of_enemies" ],
+ "features" : [ "GROUP_BY", "DISTINCT" ]
},
"successorNames" : [ ]
-} ]
+} ]
Oops, something went wrong.

0 comments on commit a459d2a

Please sign in to comment.