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


##########
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/DBScanner.java:
##########
@@ -136,210 +173,192 @@ public static byte[] getValueObject(
     }
   }
 
-  private static List<Object> displayTable(ManagedRocksIterator iterator,
-      DBColumnFamilyDefinition dbColumnFamilyDefinition, boolean schemaV3)
-          throws IOException {
-    List<Object> outputs = new ArrayList<>();
+  private boolean displayTable(ManagedRocksIterator iterator,
+                               DBColumnFamilyDefinition dbColumnFamilyDef,
+                               boolean schemaV3)
+      throws IOException {
 
-    if (startKey != null) {
-      iterator.get().seek(getValueObject(dbColumnFamilyDefinition));
+    if (fileName == null) {
+      // Print to stdout
+      return displayTable(iterator, dbColumnFamilyDef, out(), schemaV3);
     }
 
-    Writer fileWriter = null;
-    PrintWriter printWriter = null;
-    try {
-      if (fileName != null) {
-        fileWriter = new OutputStreamWriter(
-            new FileOutputStream(fileName), StandardCharsets.UTF_8);
-        printWriter = new PrintWriter(fileWriter);
-      }
-
-      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);
-        Gson gson = new GsonBuilder().setPrettyPrinting().create();
-        result.append(gson.toJson(o));
-        if (fileName != null) {
-          printWriter.println(result);
-        } else {
-          System.out.println(result.toString());
-        }
-        limit--;
-        iterator.get().next();
-        if (limit == 0) {
-          break;
-        }
-      }
-    } finally {
-      if (printWriter != null) {
-        printWriter.close();
-      }
-      if (fileWriter != null) {
-        fileWriter.close();
-      }
+    // Write to file output
+    try (PrintWriter out = new PrintWriter(fileName, UTF_8.name())) {
+      return displayTable(iterator, dbColumnFamilyDef, out, schemaV3);
     }
-    return outputs;
   }
 
-  public void setTableName(String tableName) {
-    this.tableName = tableName;
-  }
+  private boolean displayTable(ManagedRocksIterator iterator,
+                               DBColumnFamilyDefinition dbColumnFamilyDef,
+                               PrintWriter out,
+                               boolean schemaV3)
+      throws IOException {
 
-  public RDBParser getParent() {
-    return parent;
-  }
+    if (startKey != null) {
+      iterator.get().seek(getValueObject(dbColumnFamilyDef));
+    }
 
-  public void setParent(RDBParser parent) {
-    this.parent = parent;
-  }
+    if (withKey) {
+      // Start JSON object (map)
+      out.print("{ ");
+    } else {
+      // Start JSON array
+      out.print("[ ");
+    }
 
-  public static void setLimit(int limit) {
-    DBScanner.limit = limit;
-  }
+    // Count number of keys printed so far
+    long count = 0;
+    while (withinLimit(count) && iterator.get().isValid()) {
+      StringBuilder sb = new StringBuilder();
+      if (withKey) {
+        Object key = dbColumnFamilyDef.getKeyCodec()
+            .fromPersistedFormat(iterator.get().key());
+        Gson gson = new GsonBuilder().setPrettyPrinting().create();
+        if (schemaV3) {
+          int index =
+              DatanodeSchemaThreeDBDefinition.getContainerKeyPrefixLength();
+          String keyStr = key.toString();
+          if (index > keyStr.length()) {
+            err().println("Error: Invalid SchemaV3 table key length. "
+                + "Is this a V2 table? Try again with --dn-schema=V2");
+            return false;
+          }
+          String cid = keyStr.substring(0, index);
+          String blockId = keyStr.substring(index);
+          sb.append(gson.toJson(Longs.fromByteArray(
+              FixedLengthStringUtils.string2Bytes(cid)) +
+              keySeparatorSchemaV3 +
+              blockId));
+        } else {
+          sb.append(gson.toJson(key));
+        }
+        sb.append(": ");

Review Comment:
   I believe Schema V1/V2 keys are already printed as is on Line 234. The `: ` 
is a manually constructed JSON separator between a key-value pair, not inserted 
into the DB key.



-- 
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