smengcl commented on code in PR #4420:
URL: https://github.com/apache/ozone/pull/4420#discussion_r1146899627


##########
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/DBScanner.java:
##########
@@ -136,208 +156,177 @@ public static byte[] getValueObject(
     }
   }
 
-  private static List<Object> displayTable(ManagedRocksIterator iterator,
+  private void displayTable(ManagedRocksIterator iterator,
       DBColumnFamilyDefinition dbColumnFamilyDefinition) throws IOException {
-    List<Object> outputs = new ArrayList<>();
+
+    if (fileName != null) {
+      try (PrintWriter out = new PrintWriter(fileName, UTF_8.name())) {
+        displayTable(iterator, dbColumnFamilyDefinition, out);
+      }
+    } else {
+      displayTable(iterator, dbColumnFamilyDefinition, out());
+    }
+  }
+
+  private void displayTable(ManagedRocksIterator iterator,
+      DBColumnFamilyDefinition dbColumnFamilyDefinition, PrintWriter out)
+      throws IOException {
 
     if (startKey != null) {
       iterator.get().seek(getValueObject(dbColumnFamilyDefinition));
     }
 
-    Writer fileWriter = null;
-    PrintWriter printWriter = null;
-    try {
-      if (fileName != null) {
-        fileWriter = new OutputStreamWriter(
-            new FileOutputStream(fileName), StandardCharsets.UTF_8);
-        printWriter = new PrintWriter(fileWriter);
-      }
+    if (withKey) {
+      // Start JSON object (map)
+      out.print("{ ");
+    } else {
+      // Start JSON array
+      out.print("[ ");
+    }
 
-      boolean schemaV3 = dnDBSchemaVersion != null &&
-          dnDBSchemaVersion.equals("V3");
-      while (iterator.get().isValid()) {
-        StringBuilder result = new StringBuilder();
-        if (withKey) {
-          Object key = dbColumnFamilyDefinition.getKeyCodec()
-              .fromPersistedFormat(iterator.get().key());
-          Gson gson = new GsonBuilder().setPrettyPrinting().create();
-          if (schemaV3) {
-            int index =
-                DatanodeSchemaThreeDBDefinition.getContainerKeyPrefixLength();
-            String cid = key.toString().substring(0, index);
-            String blockId = key.toString().substring(index);
-            result.append(gson.toJson(Longs.fromByteArray(
-                FixedLengthStringUtils.string2Bytes(cid)) + ": " + blockId));
-          } else {
-            result.append(gson.toJson(key));
-          }
-          result.append(" -> ");
-        }
-        Object o = dbColumnFamilyDefinition.getValueCodec()
-            .fromPersistedFormat(iterator.get().value());
-        outputs.add(o);
+    boolean schemaV3 = dnDBSchemaVersion != null &&
+        dnDBSchemaVersion.equalsIgnoreCase(SCHEMA_V3);
+
+    // Count number of keys printed so far
+    long count = 0;
+    while (withinLimit(count) && iterator.get().isValid()) {
+      StringBuilder sb = new StringBuilder();
+      if (withKey) {
+        Object key = dbColumnFamilyDefinition.getKeyCodec()
+            .fromPersistedFormat(iterator.get().key());
         Gson gson = new GsonBuilder().setPrettyPrinting().create();
-        result.append(gson.toJson(o));
-        if (fileName != null) {
-          printWriter.println(result);
+        if (schemaV3) {
+          int index =
+              DatanodeSchemaThreeDBDefinition.getContainerKeyPrefixLength();
+          String cid = key.toString().substring(0, index);
+          String blockId = key.toString().substring(index);
+          sb.append(gson.toJson(Longs.fromByteArray(
+              FixedLengthStringUtils.string2Bytes(cid)) + ": " + blockId));

Review Comment:
   Yes. But Schema V3 block_data table key is not fully composed of ASCII 
characters. The logic to print it is untouched.



##########
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/DBScanner.java:
##########
@@ -136,208 +156,177 @@ public static byte[] getValueObject(
     }
   }
 
-  private static List<Object> displayTable(ManagedRocksIterator iterator,
+  private void displayTable(ManagedRocksIterator iterator,
       DBColumnFamilyDefinition dbColumnFamilyDefinition) throws IOException {
-    List<Object> outputs = new ArrayList<>();
+
+    if (fileName != null) {
+      try (PrintWriter out = new PrintWriter(fileName, UTF_8.name())) {
+        displayTable(iterator, dbColumnFamilyDefinition, out);
+      }
+    } else {
+      displayTable(iterator, dbColumnFamilyDefinition, out());
+    }
+  }
+
+  private void displayTable(ManagedRocksIterator iterator,
+      DBColumnFamilyDefinition dbColumnFamilyDefinition, PrintWriter out)
+      throws IOException {
 
     if (startKey != null) {
       iterator.get().seek(getValueObject(dbColumnFamilyDefinition));
     }
 
-    Writer fileWriter = null;
-    PrintWriter printWriter = null;
-    try {
-      if (fileName != null) {
-        fileWriter = new OutputStreamWriter(
-            new FileOutputStream(fileName), StandardCharsets.UTF_8);
-        printWriter = new PrintWriter(fileWriter);
-      }
+    if (withKey) {
+      // Start JSON object (map)
+      out.print("{ ");
+    } else {
+      // Start JSON array
+      out.print("[ ");
+    }
 
-      boolean schemaV3 = dnDBSchemaVersion != null &&
-          dnDBSchemaVersion.equals("V3");
-      while (iterator.get().isValid()) {
-        StringBuilder result = new StringBuilder();
-        if (withKey) {
-          Object key = dbColumnFamilyDefinition.getKeyCodec()
-              .fromPersistedFormat(iterator.get().key());
-          Gson gson = new GsonBuilder().setPrettyPrinting().create();
-          if (schemaV3) {
-            int index =
-                DatanodeSchemaThreeDBDefinition.getContainerKeyPrefixLength();
-            String cid = key.toString().substring(0, index);
-            String blockId = key.toString().substring(index);
-            result.append(gson.toJson(Longs.fromByteArray(
-                FixedLengthStringUtils.string2Bytes(cid)) + ": " + blockId));
-          } else {
-            result.append(gson.toJson(key));
-          }
-          result.append(" -> ");
-        }
-        Object o = dbColumnFamilyDefinition.getValueCodec()
-            .fromPersistedFormat(iterator.get().value());
-        outputs.add(o);
+    boolean schemaV3 = dnDBSchemaVersion != null &&
+        dnDBSchemaVersion.equalsIgnoreCase(SCHEMA_V3);
+
+    // Count number of keys printed so far
+    long count = 0;
+    while (withinLimit(count) && iterator.get().isValid()) {
+      StringBuilder sb = new StringBuilder();
+      if (withKey) {
+        Object key = dbColumnFamilyDefinition.getKeyCodec()
+            .fromPersistedFormat(iterator.get().key());
         Gson gson = new GsonBuilder().setPrettyPrinting().create();
-        result.append(gson.toJson(o));
-        if (fileName != null) {
-          printWriter.println(result);
+        if (schemaV3) {
+          int index =
+              DatanodeSchemaThreeDBDefinition.getContainerKeyPrefixLength();
+          String cid = key.toString().substring(0, index);
+          String blockId = key.toString().substring(index);
+          sb.append(gson.toJson(Longs.fromByteArray(
+              FixedLengthStringUtils.string2Bytes(cid)) + ": " + blockId));

Review Comment:
   Yes. But Schema V3 block_data table key is not fully composed of ASCII 
characters. The logic to print it is left untouched.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to