asterixdb-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Michael Blow (Code Review)" <do-not-re...@asterixdb.incubator.apache.org>
Subject Change in asterixdb[master]: Better indentation of results.
Date Fri, 17 Mar 2017 02:49:11 GMT
Michael Blow has submitted this change and it was merged.

Change subject: Better indentation of results.
......................................................................


Better indentation of results.

Change-Id: Iab09135b0b08b43346244269e0bc252a24525f15
Reviewed-on: https://asterix-gerrit.ics.uci.edu/1592
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Yingyi Bu <buyingyi@gmail.com>
BAD: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
---
M asterixdb/asterix-app/src/main/java/org/apache/asterix/app/result/ResultPrinter.java
1 file changed, 31 insertions(+), 12 deletions(-)

Approvals:
  Yingyi Bu: Looks good to me, approved
  Jenkins: Verified; No violations found; No violations found; Verified



diff --git a/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/result/ResultPrinter.java
b/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/result/ResultPrinter.java
index 088d153..61d0eed 100644
--- a/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/result/ResultPrinter.java
+++ b/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/result/ResultPrinter.java
@@ -24,10 +24,6 @@
 import java.io.StringWriter;
 import java.nio.ByteBuffer;
 
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.SerializationFeature;
-import com.fasterxml.jackson.databind.node.ObjectNode;
-
 import org.apache.asterix.common.utils.JSONUtil;
 import org.apache.asterix.om.types.ARecordType;
 import org.apache.asterix.translator.IStatementExecutor.Stats;
@@ -39,6 +35,12 @@
 import org.apache.hyracks.api.comm.VSizeFrame;
 import org.apache.hyracks.api.exceptions.HyracksDataException;
 import org.apache.hyracks.control.nc.resources.memory.FrameManager;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.core.PrettyPrinter;
+import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.ObjectWriter;
 
 public class ResultPrinter {
 
@@ -57,12 +59,35 @@
     // Whether this is the first instance being output
     private boolean notFirst = false;
 
+    private ObjectMapper om;
+    private ObjectWriter ow;
+
     public ResultPrinter(SessionConfig conf, Stats stats, ARecordType recordType) {
         this.conf = conf;
         this.stats = stats;
         this.recordType = recordType;
         this.indentJSON = conf.is(SessionConfig.FORMAT_INDENT_JSON);
         this.quoteRecord = conf.is(SessionConfig.FORMAT_QUOTE_RECORD);
+        if (indentJSON) {
+            this.om = new ObjectMapper();
+            DefaultPrettyPrinter.Indenter i = new DefaultPrettyPrinter.Indenter() {
+
+                @Override
+                public void writeIndentation(JsonGenerator jsonGenerator, int i) throws IOException
{
+                    jsonGenerator.writeRaw('\n');
+                    for (int j = 0; j < i + 1; ++j) {
+                        jsonGenerator.writeRaw('\t');
+                    }
+                }
+
+                @Override
+                public boolean isInline() {
+                    return false;
+                }
+            };
+            PrettyPrinter pp = new DefaultPrettyPrinter().withObjectIndenter(i).withArrayIndenter(i);
+            this.ow = om.writer(pp);
+        }
     }
 
     private static void appendCSVHeader(Appendable app, ARecordType recordType) throws HyracksDataException
{
@@ -134,13 +159,11 @@
     }
 
     private void displayRecord(String result) throws HyracksDataException {
-        ObjectMapper om = new ObjectMapper();
-        om.enable(SerializationFeature.INDENT_OUTPUT);
         String record = result;
         if (indentJSON) {
             // TODO(tillw): this is inefficient - do this during record generation
             try {
-                record = om.writerWithDefaultPrettyPrinter().writeValueAsString(om.readValue(result,
Object.class));
+                record = ow.writeValueAsString(om.readValue(result, Object.class));
             } catch (IOException e) {
                 throw new HyracksDataException(e);
             }
@@ -190,11 +213,7 @@
                     conf.out().print(", ");
                 }
                 notFirst = true;
-                try {
-                    displayRecord(result);
-                } catch (IOException e) {
-                    throw new HyracksDataException(e);
-                }
+                displayRecord(result);
             }
             frameBuffer.clear();
         }

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/1592
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Iab09135b0b08b43346244269e0bc252a24525f15
Gerrit-PatchSet: 2
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Till Westmann <tillw@apache.org>
Gerrit-Reviewer: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Gerrit-Reviewer: Michael Blow <mblow@apache.org>
Gerrit-Reviewer: Yingyi Bu <buyingyi@gmail.com>

Mime
View raw message