elek commented on a change in pull request #945:
URL: https://github.com/apache/hadoop-ozone/pull/945#discussion_r428655688



##########
File path: 
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/SCMDBParser.java
##########
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.hadoop.ozone.debug;
+
+import org.apache.hadoop.hdds.utils.db.DBColumnFamilyDefinition;
+import org.rocksdb.*;
+import picocli.CommandLine;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.Callable;
+
+/**
+ * Parser for scm.db file.
+ */
[email protected](
+        name = "scmdbparser",
+        description = "Parse specified metadataTable"
+)
+public class SCMDBParser implements Callable<Void> {
+
+  @CommandLine.Option(names = {"-table"},

Review comment:
       Nit: Can you please use `--table`. It's more standard (or use `-t` and 
`--table` as an alias)

##########
File path: 
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/ListTables.java
##########
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.hadoop.ozone.debug;
+
+import org.rocksdb.Options;
+import org.rocksdb.RocksDB;
+import org.rocksdb.RocksDBException;
+import picocli.CommandLine;
+
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.concurrent.Callable;
+
+/**
+ * List all column Families/Tables in db.
+ */
[email protected](
+        name = "list",
+        description = "list all tables in db."
+)
+public class ListTables implements Callable<Void> {
+
+  @CommandLine.ParentCommand
+  private RDBParser parent;
+
+  @Override
+  public Void call() throws Exception {
+    List<byte[]> cfList = getColumnFamilyList(parent.getDbPath());
+    for (byte[] b : cfList) {
+      System.out.println(new String(b, StandardCharsets.UTF_8));
+    }
+    return null;
+  }
+
+  private static List<byte[]> getColumnFamilyList(String dbPath)

Review comment:
       Nit1: I am not sure if we need a separated method just to call this 
function. 
   
   ```java
   List<byte[]> columnFamilies = RocksDB.listColumnFamilies(new Options(), 
parent.getDbPath);
   ```
   
   Nit2: I tend to agree with Uncle Bob, that we don't need to include the type 
of a variable in the name of the variable:
   
   > Hungarian Notation was considered to be pretty important back in the 
Windows C API, when everything was an integer handle or a long pointer or a 
void pointer, or one of several implementations of “string” (with different 
uses and attributes). The compiler did not check types in those days, so the 
programmers needed a crutch to help them remember the types.
   
   > In modern languages we have much richer type systems, and the compilers 
remember and enforce the types. What’s more, there is a trend toward smaller 
classes and shorter functions so that people can usually see the point of 
declaration of each variable they’re using.
   
   > Java programmers don’t need type encoding. Objects are strongly typed, and 
editing environments have advanced such that they detect a type error long 
before you can run a compile! So nowadays HN and other forms of type encoding 
are simply impediments. They make it harder to change the name or type of a 
variable, function, or class. They make it harder to read the code. And they 
create the possibility that the encoding system will mislead the reader.
   
   (In: Clean Code / Chapter 2. Meaningful name)
   

##########
File path: 
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/SCMDBParser.java
##########
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.hadoop.ozone.debug;
+
+import org.apache.hadoop.hdds.utils.db.DBColumnFamilyDefinition;
+import org.rocksdb.*;
+import picocli.CommandLine;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.Callable;
+
+/**
+ * Parser for scm.db file.
+ */
[email protected](
+        name = "scmdbparser",
+        description = "Parse specified metadataTable"
+)
+public class SCMDBParser implements Callable<Void> {
+
+  @CommandLine.Option(names = {"-table"},
+            description = "Table name")
+  private String tableName;
+
+  @CommandLine.ParentCommand
+  private RDBParser parent;
+
+
+  private static void displayTable(RocksDB rocksDB,
+        DBColumnFamilyDefinition dbColumnFamilyDefinition,
+        List<ColumnFamilyHandle> list) throws IOException {
+    ColumnFamilyHandle columnFamilyHandle = getColumnFamilyHandle(
+            dbColumnFamilyDefinition.getTableName()
+                    .getBytes(StandardCharsets.UTF_8), list);
+    if (columnFamilyHandle==null){
+      throw new IllegalArgumentException("columnFamilyHandle is null");
+    }
+    RocksIterator iterator = rocksDB.newIterator(columnFamilyHandle);
+    iterator.seekToFirst();
+    while (iterator.isValid()){
+      Object o = dbColumnFamilyDefinition.getValueCodec()
+              .fromPersistedFormat(iterator.value());
+      System.out.println(o);
+      iterator.next();
+    }
+  }
+
+  private static ColumnFamilyHandle getColumnFamilyHandle(
+            byte[] name, List<ColumnFamilyHandle> columnFamilyHandles) {
+    return columnFamilyHandles
+            .stream()
+            .filter(
+              handle -> {
+                try {
+                  return Arrays.equals(handle.getName(), name);
+                    } catch (Exception ex) {
+                  throw new RuntimeException(ex);
+                    }
+              })
+            .findAny()
+            .orElse(null);
+  }
+
+  @Override
+  public Void call() throws Exception {
+    List<ColumnFamilyDescriptor> cfs = new ArrayList<>();
+    final List<ColumnFamilyHandle> columnFamilyHandleList =
+            new ArrayList<>();
+    List<byte[]> cfList = null;
+    try {
+      cfList = RocksDB.listColumnFamilies(new Options(),
+                    parent.getDbPath());
+    } catch (Exception e) {
+      e.printStackTrace();

Review comment:
       If there is an exception, we are in trouble anyway. I would throw the 
exception without printing out the problem and GenericCli will handle all the 
remaining parts.




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

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