incubator-blur-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amccu...@apache.org
Subject [2/6] git commit: Changing the trace logic to use a json object api instead of building string by hand.
Date Thu, 10 Jul 2014 00:56:18 GMT
Changing the trace logic to use a json object api instead of building string by hand.


Project: http://git-wip-us.apache.org/repos/asf/incubator-blur/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-blur/commit/83e8711c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-blur/tree/83e8711c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-blur/diff/83e8711c

Branch: refs/heads/apache-blur-0.2
Commit: 83e8711c471f53dade68fe6bd617aafa34c59583
Parents: 7ffeba2
Author: Aaron McCurry <amccurry@gmail.com>
Authored: Fri Jul 4 16:05:23 2014 -0400
Committer: Aaron McCurry <amccurry@gmail.com>
Committed: Fri Jul 4 16:05:23 2014 -0400

----------------------------------------------------------------------
 .../blur/trace/hdfs/HdfsTraceStorage.java       | 25 +++++++---
 .../apache/blur/manager/IndexManagerTest.java   |  7 ++-
 .../writer/BlurIndexSimpleWriterTest.java       |  7 ++-
 .../blur/trace/hdfs/HdfsTraceStorageTest.java   | 14 +++---
 .../org/apache/blur/trace/LogTraceStorage.java  | 12 +++--
 .../main/java/org/apache/blur/trace/Trace.java  | 10 +++-
 .../org/apache/blur/trace/TraceCollector.java   | 27 ++++++-----
 .../java/org/apache/blur/trace/TracerImpl.java  | 51 ++++++++------------
 .../java/org/apache/blur/trace/TraceTest.java   |  7 ++-
 9 files changed, 98 insertions(+), 62 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/83e8711c/blur-core/src/main/java/org/apache/blur/trace/hdfs/HdfsTraceStorage.java
----------------------------------------------------------------------
diff --git a/blur-core/src/main/java/org/apache/blur/trace/hdfs/HdfsTraceStorage.java b/blur-core/src/main/java/org/apache/blur/trace/hdfs/HdfsTraceStorage.java
index 5cd9bf2..ad069e0 100644
--- a/blur-core/src/main/java/org/apache/blur/trace/hdfs/HdfsTraceStorage.java
+++ b/blur-core/src/main/java/org/apache/blur/trace/hdfs/HdfsTraceStorage.java
@@ -19,6 +19,7 @@ package org.apache.blur.trace.hdfs;
 import static org.apache.blur.utils.BlurConstants.BLUR_HDFS_TRACE_PATH;
 
 import java.io.IOException;
+import java.io.OutputStreamWriter;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Random;
@@ -38,11 +39,11 @@ import org.apache.hadoop.fs.FSDataOutputStream;
 import org.apache.hadoop.fs.FileStatus;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
+import org.json.JSONException;
+import org.json.JSONObject;
 
 public class HdfsTraceStorage extends TraceStorage {
 
-  private static final String UTF_8 = "UTF-8";
-
   private final static Log LOG = LogFactory.getLog(HdfsTraceStorage.class);
 
   private final Path _storePath;
@@ -97,18 +98,28 @@ public class HdfsTraceStorage extends TraceStorage {
       requestId = "";
     }
     Path tracePath = getTracePath(storeId);
-    String json = collector.toJson();
-    storeJson(new Path(tracePath, getRequestIdPathName(requestId, random)), json);
+    JSONObject jsonObject;
+    try {
+      jsonObject = collector.toJsonObject();
+    } catch (JSONException e) {
+      throw new IOException(e);
+    }
+    storeJson(new Path(tracePath, getRequestIdPathName(requestId, random)), jsonObject);
   }
 
   private String getRequestIdPathName(String requestId, Random random) {
     return requestId + "_" + random.nextLong();
   }
 
-  public void storeJson(Path storePath, String json) throws IOException {
+  public void storeJson(Path storePath, JSONObject jsonObject) throws IOException {
     FSDataOutputStream outputStream = _fileSystem.create(storePath, false);
-    outputStream.write(json.getBytes(UTF_8));
-    outputStream.close();
+    try {
+      OutputStreamWriter writer = new OutputStreamWriter(outputStream);
+      jsonObject.write(writer);
+      writer.close();
+    } catch (JSONException e) {
+      throw new IOException(e);
+    }
   }
 
   private Path getTracePath(String traceId) {

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/83e8711c/blur-core/src/test/java/org/apache/blur/manager/IndexManagerTest.java
----------------------------------------------------------------------
diff --git a/blur-core/src/test/java/org/apache/blur/manager/IndexManagerTest.java b/blur-core/src/test/java/org/apache/blur/manager/IndexManagerTest.java
index 5eb437b..1258bed 100644
--- a/blur-core/src/test/java/org/apache/blur/manager/IndexManagerTest.java
+++ b/blur-core/src/test/java/org/apache/blur/manager/IndexManagerTest.java
@@ -81,6 +81,7 @@ import org.apache.lucene.index.Term;
 import org.apache.lucene.search.Filter;
 import org.apache.lucene.search.QueryWrapperFilter;
 import org.apache.lucene.search.TermQuery;
+import org.json.JSONException;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -297,7 +298,11 @@ public class IndexManagerTest {
 
       @Override
       public void store(TraceCollector collector) {
-        System.out.println(collector.toJson());
+        try {
+          System.out.println(collector.toJsonObject());
+        } catch (JSONException e) {
+          e.printStackTrace();
+        }
       }
     });
 

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/83e8711c/blur-core/src/test/java/org/apache/blur/manager/writer/BlurIndexSimpleWriterTest.java
----------------------------------------------------------------------
diff --git a/blur-core/src/test/java/org/apache/blur/manager/writer/BlurIndexSimpleWriterTest.java
b/blur-core/src/test/java/org/apache/blur/manager/writer/BlurIndexSimpleWriterTest.java
index 4b9927c..4808911 100644
--- a/blur-core/src/test/java/org/apache/blur/manager/writer/BlurIndexSimpleWriterTest.java
+++ b/blur-core/src/test/java/org/apache/blur/manager/writer/BlurIndexSimpleWriterTest.java
@@ -54,6 +54,7 @@ import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
 import org.apache.lucene.index.IndexReader;
 import org.apache.lucene.store.FSDirectory;
+import org.json.JSONException;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -189,7 +190,11 @@ public class BlurIndexSimpleWriterTest {
 
       @Override
       public void store(TraceCollector collector) {
-        System.out.println(collector.toJson());
+        try {
+          System.out.println(collector.toJsonObject());
+        } catch (JSONException e) {
+          e.printStackTrace();
+        }
       }
     });
     Trace.setupTrace("test");

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/83e8711c/blur-core/src/test/java/org/apache/blur/trace/hdfs/HdfsTraceStorageTest.java
----------------------------------------------------------------------
diff --git a/blur-core/src/test/java/org/apache/blur/trace/hdfs/HdfsTraceStorageTest.java
b/blur-core/src/test/java/org/apache/blur/trace/hdfs/HdfsTraceStorageTest.java
index b357f5e..1d6bb74 100644
--- a/blur-core/src/test/java/org/apache/blur/trace/hdfs/HdfsTraceStorageTest.java
+++ b/blur-core/src/test/java/org/apache/blur/trace/hdfs/HdfsTraceStorageTest.java
@@ -31,6 +31,8 @@ import org.apache.hadoop.fs.LocalFileSystem;
 import org.apache.hadoop.fs.Path;
 import org.apache.hadoop.fs.permission.FsAction;
 import org.apache.hadoop.fs.permission.FsPermission;
+import org.json.JSONException;
+import org.json.JSONObject;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -85,7 +87,7 @@ public class HdfsTraceStorageTest {
   }
 
   @Test
-  public void testStorage() throws IOException {
+  public void testStorage() throws IOException, JSONException {
     Random random = new Random();
     createTraceData(random);
     createTraceData(random);
@@ -98,7 +100,7 @@ public class HdfsTraceStorageTest {
       assertEquals(4, requestIds.size());
       for (String requestId : requestIds) {
         String contents = _storage.getRequestContentsJson(traceId, requestId);
-        assertEquals("{" + requestId + "}", contents);
+        assertEquals("{\"id\":" + requestId + "}", contents);
       }
     }
 
@@ -106,22 +108,22 @@ public class HdfsTraceStorageTest {
     assertEquals(2, _storage.getTraceIds().size());
   }
 
-  private void createTraceData(Random random) throws IOException {
+  private void createTraceData(Random random) throws IOException, JSONException {
     String traceId = Long.toString(Math.abs(random.nextLong()));
     Path path = new Path(configuration.get(BLUR_HDFS_TRACE_PATH));
     Path tracePath = new Path(path, traceId);
     Path storePath = new Path(tracePath, traceId + "_" + Long.toString(Math.abs(random.nextLong())));
-    _storage.storeJson(storePath, "{" + traceId + "}");
+    _storage.storeJson(storePath, new JSONObject("{\"id\":" + traceId + "}"));
     writeRequest(random, tracePath);
     writeRequest(random, tracePath);
     writeRequest(random, tracePath);
   }
 
-  private void writeRequest(Random random, Path tracePath) throws IOException {
+  private void writeRequest(Random random, Path tracePath) throws IOException, JSONException
{
     String requestId = Long.toString(random.nextLong());
     Path storePath = new Path(tracePath, requestId + "_" + Long.toString(Math.abs(random.nextLong())));
     System.out.println(storePath);
-    _storage.storeJson(storePath, "{" + requestId + "}");
+    _storage.storeJson(storePath, new JSONObject("{\"id\":" + requestId + "}"));
   }
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/83e8711c/blur-util/src/main/java/org/apache/blur/trace/LogTraceStorage.java
----------------------------------------------------------------------
diff --git a/blur-util/src/main/java/org/apache/blur/trace/LogTraceStorage.java b/blur-util/src/main/java/org/apache/blur/trace/LogTraceStorage.java
index db139bb..845e51d 100644
--- a/blur-util/src/main/java/org/apache/blur/trace/LogTraceStorage.java
+++ b/blur-util/src/main/java/org/apache/blur/trace/LogTraceStorage.java
@@ -21,6 +21,8 @@ import java.io.IOException;
 import org.apache.blur.BlurConfiguration;
 import org.apache.blur.log.Log;
 import org.apache.blur.log.LogFactory;
+import org.json.JSONException;
+import org.json.JSONObject;
 
 public class LogTraceStorage extends BaseTraceStorage {
 
@@ -32,13 +34,17 @@ public class LogTraceStorage extends BaseTraceStorage {
 
   @Override
   public void store(TraceCollector collector) {
-    String json = collector.toJson();
-    LOG.info("Trace Complete [{0}]", json);
+    try {
+      JSONObject jsonObject = collector.toJsonObject();
+      LOG.info("Trace Complete [{0}]", jsonObject.toString());
+    } catch (JSONException e) {
+      LOG.error("Unknown error", e);
+    }
   }
 
   @Override
   public void close() throws IOException {
-    
+
   }
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/83e8711c/blur-util/src/main/java/org/apache/blur/trace/Trace.java
----------------------------------------------------------------------
diff --git a/blur-util/src/main/java/org/apache/blur/trace/Trace.java b/blur-util/src/main/java/org/apache/blur/trace/Trace.java
index 228e51e..c880641 100644
--- a/blur-util/src/main/java/org/apache/blur/trace/Trace.java
+++ b/blur-util/src/main/java/org/apache/blur/trace/Trace.java
@@ -27,6 +27,9 @@ import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 
+import org.json.JSONException;
+import org.json.JSONObject;
+
 public class Trace {
 
   private static final String REQUEST_ID = "requestId";
@@ -48,8 +51,11 @@ public class Trace {
       return _requestId;
     }
 
-    public String toJson() {
-      return "{\"rootId\":\"" + _rootId + "\",\"requestId\":\"" + _requestId + "\"}";
+    public JSONObject toJsonObject() throws JSONException {
+      JSONObject jsonObject = new JSONObject();
+      jsonObject.put("rootId", _rootId);
+      jsonObject.put("requestId", _requestId);
+      return jsonObject;
     }
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/83e8711c/blur-util/src/main/java/org/apache/blur/trace/TraceCollector.java
----------------------------------------------------------------------
diff --git a/blur-util/src/main/java/org/apache/blur/trace/TraceCollector.java b/blur-util/src/main/java/org/apache/blur/trace/TraceCollector.java
index 98f5af3..e686004 100644
--- a/blur-util/src/main/java/org/apache/blur/trace/TraceCollector.java
+++ b/blur-util/src/main/java/org/apache/blur/trace/TraceCollector.java
@@ -23,6 +23,9 @@ import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.blur.trace.Trace.TraceId;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
 
 public class TraceCollector {
 
@@ -60,19 +63,21 @@ public class TraceCollector {
     return "TraceCollector [_id=" + _id + ", _traces=" + _traces + "]";
   }
 
-  public String toJson() {
-    StringBuilder builder = new StringBuilder();
-    boolean first = true;
+  public JSONObject toJsonObject() throws JSONException {
+    JSONArray tracesArray = new JSONArray();
     for (TracerImpl t : _traces) {
-      if (!first) {
-        builder.append(",\n");
-      }
-      builder.append("    ").append(t.toJson());
-      first = false;
+      tracesArray.put(t.toJsonObject());
     }
-    return "{\n  \"id\":" + _id.toJson() + ",\n  \"nodeName\":\"" + (_nodeName == null ?
"unknown" : _nodeName)
-        + "\",\n  \"pid\":\"" + _pid + "\",\n  \"thread\":\"" + _threadName + "\",\n  \"created\":"
+ _now
-        + ",\n  \"totalTime\":" + (_finished - _started) + ",\n  \"traces\":[\n" + builder.toString()
+ "  ]\n}";
+    JSONObject jsonObject = new JSONObject();
+    jsonObject.put("id", _id.toJsonObject());
+    jsonObject.put("nodeName", (_nodeName == null ? "unknown" : _nodeName));
+    jsonObject.put("pid", _pid);
+    jsonObject.put("thread", _threadName);
+    jsonObject.put("created", _now);
+    jsonObject.put("totalTime", (_finished - _started));
+    jsonObject.put("thread", _threadName);
+    jsonObject.put("traces", tracesArray);
+    return jsonObject;
   }
 
   public TraceId getId() {

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/83e8711c/blur-util/src/main/java/org/apache/blur/trace/TracerImpl.java
----------------------------------------------------------------------
diff --git a/blur-util/src/main/java/org/apache/blur/trace/TracerImpl.java b/blur-util/src/main/java/org/apache/blur/trace/TracerImpl.java
index 89a9345..3e37c19 100644
--- a/blur-util/src/main/java/org/apache/blur/trace/TracerImpl.java
+++ b/blur-util/src/main/java/org/apache/blur/trace/TracerImpl.java
@@ -19,6 +19,7 @@ package org.apache.blur.trace;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.blur.trace.Trace.Parameter;
+import org.json.JSONException;
 import org.json.JSONObject;
 
 public class TracerImpl implements Tracer {
@@ -84,46 +85,36 @@ public class TracerImpl implements Tracer {
     return _threadName;
   }
 
-  public String toJson() {
-    StringBuilder builder = new StringBuilder();
-    builder.append("{");
-    builder.append("\"id\":").append(_id).append(",");
-    builder.append("\"scope\":").append(_traceScope).append(",");
-    builder.append("\"name\":\"").append(_name).append("\",");
-    builder.append("\"thread\":\"").append(_threadName).append("\",");
-    builder.append("\"took\":").append((_ended - _start)).append(",");
-    builder.append("\"started\":").append(_start).append(",");
-    builder.append("\"ended\":").append(_ended);
-    if (_parameters != null) {
-      builder.append(",");
-      builder.append("\"parameters\":[").append(getParametersJson()).append("]");
-    }
+  public JSONObject toJsonObject() throws JSONException {
+    JSONObject jsonObject = new JSONObject();
+    jsonObject.put("id", _id);
+    jsonObject.put("scope", _traceScope);
+    jsonObject.put("name", _name);
+    jsonObject.put("thread", _threadName);
+    jsonObject.put("took", (_ended - _start));
+    jsonObject.put("started", _start);
+    jsonObject.put("ended", _ended);
     if (_traceCollector != null) {
-      builder.append(",");
-      builder.append("\"collector\":\n").append(_traceCollector.toJson()).append("");
+      jsonObject.put("collector", _traceCollector.toJsonObject());
     }
-    builder.append("}");
-    return builder.toString();
+    if (_parameters != null) {
+      jsonObject.put("parameters", getParametersJsonObject());
+    }
+    return jsonObject;
   }
 
-  private String getParametersJson() {
-    StringBuilder builder = new StringBuilder();
+  private JSONObject getParametersJsonObject() throws JSONException {
+    JSONObject jsonObject = new JSONObject();
     for (Parameter parameter : _parameters) {
-      if (builder.length() != 0) {
-        builder.append(',');
-      }
-      builder.append("{\"").append(toJsonString(parameter._name)).append("\":\"")
-          .append(toJsonString(parameter._value)).append("\"}");
+      jsonObject.put(toString(parameter._name), parameter._value);
     }
-    return builder.toString();
+    return jsonObject;
   }
 
-  private String toJsonString(Object o) {
+  private String toString(Object o) {
     if (o == null) {
       return null;
     }
-    String str = o.toString();
-    return JSONObject.quote(str);
+    return o.toString();
   }
-
 }

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/83e8711c/blur-util/src/test/java/org/apache/blur/trace/TraceTest.java
----------------------------------------------------------------------
diff --git a/blur-util/src/test/java/org/apache/blur/trace/TraceTest.java b/blur-util/src/test/java/org/apache/blur/trace/TraceTest.java
index 53ef049..ad1e643 100644
--- a/blur-util/src/test/java/org/apache/blur/trace/TraceTest.java
+++ b/blur-util/src/test/java/org/apache/blur/trace/TraceTest.java
@@ -23,6 +23,7 @@ import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.blur.BlurConfiguration;
 import org.apache.blur.trace.Trace.TraceId;
+import org.json.JSONException;
 import org.junit.Test;
 
 public class TraceTest {
@@ -71,7 +72,11 @@ public class TraceTest {
     Trace.setStorage(new BaseTraceStorage(new BlurConfiguration()) {
       @Override
       public void store(TraceCollector collector) {
-        System.out.println(collector.toJson());
+        try {
+          System.out.println(collector.toJsonObject());
+        } catch (JSONException e) {
+          e.printStackTrace();
+        }
         TraceId id = collector.getId();
         assertEquals("test", id.getRootId());
         assertEquals(5, collector.getTraces().size());


Mime
View raw message