sumitagrawl commented on code in PR #6914:
URL: https://github.com/apache/ozone/pull/6914#discussion_r1677297719


##########
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/ValueSchema.java:
##########
@@ -0,0 +1,179 @@
+/*
+ * 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.cli.SubcommandWithParent;
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import org.apache.hadoop.hdds.server.JsonUtils;
+import org.apache.hadoop.hdds.utils.db.DBColumnFamilyDefinition;
+import org.apache.hadoop.hdds.utils.db.DBDefinition;
+import org.apache.hadoop.ozone.OzoneConsts;
+import org.kohsuke.MetaInfServices;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import picocli.CommandLine;
+
+import java.io.PrintWriter;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.lang.reflect.ParameterizedType;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.Callable;
+import java.util.stream.Collectors;
+
+/**
+ * Get schema of value for scm.db, om.db or container db file.
+ */
[email protected](
+    name = "value-schema",
+    description = "Schema of value in metadataTable"
+)
+@MetaInfServices(SubcommandWithParent.class)
+public class ValueSchema implements Callable<Void>, SubcommandWithParent {
+
+  @CommandLine.ParentCommand
+  private RDBParser parent;
+
+  public static final Logger LOG = LoggerFactory.getLogger(ValueSchema.class);
+
+  @CommandLine.Spec
+  private static CommandLine.Model.CommandSpec spec;
+
+  @CommandLine.Option(names = {"--column_family", "--column-family", "--cf"},
+      required = true,
+      description = "Table name")
+  private String tableName;
+
+  @CommandLine.Option(names = {"--dnSchema", "--dn-schema", "-d"},
+      description = "Datanode DB Schema Version: V1/V2/V3",
+      defaultValue = "V3")
+  private String dnDBSchemaVersion;
+
+  @CommandLine.Option(names = {"--depth"},
+      description = "The level till which the value-schema should be shown, 
negative value shows full depth schema",
+      defaultValue = "-1")
+  private int depth;
+
+  @Override
+  public Void call() throws Exception {
+
+    boolean success = true;
+
+    String dbPath = parent.getDbPath();
+    Map<String, Object> fields = new HashMap<>();
+    success = getValueFields(dbPath, fields);
+
+    out().println(JsonUtils.toJsonStringWithDefaultPrettyPrinter(fields));
+
+    if (!success) {
+      // Trick to set exit code to 1 on error.
+      // TODO: Properly set exit code hopefully by refactoring GenericCli
+      throw new Exception(
+          "Exit code is non-zero. Check the error message above");
+    }
+    return null;
+  }
+
+  private boolean getValueFields(String dbPath, Map<String, Object> 
valueSchema) {
+
+    dbPath = removeTrailingSlashIfNeeded(dbPath);
+    DBDefinitionFactory.setDnDBSchemaVersion(dnDBSchemaVersion);
+    DBDefinition dbDefinition = 
DBDefinitionFactory.getDefinition(Paths.get(dbPath), new OzoneConfiguration());
+    if (dbDefinition == null) {
+      err().println("Error: Incorrect DB Path");
+      return false;
+    }
+    final DBColumnFamilyDefinition<?, ?> columnFamilyDefinition =
+        dbDefinition.getColumnFamily(tableName);
+    if (columnFamilyDefinition == null) {
+      err().print("Error: Table with name '" + tableName + "' not found");
+      return false;
+    }
+
+    Class<?> c = columnFamilyDefinition.getValueType();
+    valueSchema.put(c.getSimpleName(), getFieldsStructure(c, depth));

Review Comment:
   Do avoid any recursion issue, maxDepth can be updated to "10", IMO, no more 
than this depth filter is required



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