kudu-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t...@apache.org
Subject [1/3] kudu git commit: tools: refactor table-printing code a bit
Date Tue, 27 Jun 2017 00:32:39 GMT
Repository: kudu
Updated Branches:
  refs/heads/master 3c3cd3215 -> 78ef92ec8


tools: refactor table-printing code a bit

This changes the table-printing code to use a class and add some extra
safety checks that the added columns have matching numbers of rows.

Change-Id: I7e9556a14b6745f6bd07d1aee111bfa5cd9297fe
Reviewed-on: http://gerrit.cloudera.org:8080/7259
Reviewed-by: Adar Dembo <adar@cloudera.com>
Tested-by: Kudu Jenkins


Project: http://git-wip-us.apache.org/repos/asf/kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/kudu/commit/96ad3b07
Tree: http://git-wip-us.apache.org/repos/asf/kudu/tree/96ad3b07
Diff: http://git-wip-us.apache.org/repos/asf/kudu/diff/96ad3b07

Branch: refs/heads/master
Commit: 96ad3b07cf1dc694ddcfd72405aeb662440199b5
Parents: 3c3cd32
Author: Todd Lipcon <todd@cloudera.com>
Authored: Wed Jun 21 22:32:29 2017 -0700
Committer: Todd Lipcon <todd@apache.org>
Committed: Tue Jun 27 00:16:46 2017 +0000

----------------------------------------------------------------------
 src/kudu/tools/ksck.cc                | 11 ++++++---
 src/kudu/tools/tool_action_common.cc  | 35 +++++++++++++++++++++------
 src/kudu/tools/tool_action_common.h   | 39 +++++++++++++++++++++++++++---
 src/kudu/tools/tool_action_master.cc  |  9 +++----
 src/kudu/tools/tool_action_tserver.cc | 10 +++-----
 5 files changed, 75 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kudu/blob/96ad3b07/src/kudu/tools/ksck.cc
----------------------------------------------------------------------
diff --git a/src/kudu/tools/ksck.cc b/src/kudu/tools/ksck.cc
index f1eec40..9e5acf1 100644
--- a/src/kudu/tools/ksck.cc
+++ b/src/kudu/tools/ksck.cc
@@ -834,8 +834,7 @@ Ksck::CheckResult Ksck::VerifyTablet(const shared_ptr<KsckTablet>&
tablet, int t
     Out() << "The consensus matrix is:" << endl;
 
     // Prepare the header and columns for PrintTable.
-    const vector<string> headers{ "Config source", "Voters", "Current term",
-                                  "Config index", "Committed?" };
+    DataTable table({});
 
     // Seed the columns with the master info.
     vector<string> sources{"master"};
@@ -863,8 +862,12 @@ Ksck::CheckResult Ksck::VerifyTablet(const shared_ptr<KsckTablet>&
tablet, int t
       committed.emplace_back(replica.consensus_state->type == KsckConsensusConfigType::PENDING
?
                           "No" : "Yes");
     }
-    vector<vector<string>> columns{ sources, voters, terms, indexes, committed
};
-    PrintTable(headers, columns, Out());
+    table.AddColumn("Config source", std::move(sources));
+    table.AddColumn("Voters", std::move(voters));
+    table.AddColumn("Current term", std::move(terms));
+    table.AddColumn("Config index", std::move(indexes));
+    table.AddColumn("Committed?", std::move(committed));
+    CHECK_OK(table.PrintTo(Out()));
   }
 
   return result;

http://git-wip-us.apache.org/repos/asf/kudu/blob/96ad3b07/src/kudu/tools/tool_action_common.cc
----------------------------------------------------------------------
diff --git a/src/kudu/tools/tool_action_common.cc b/src/kudu/tools/tool_action_common.cc
index 89531a8..b6ff2d2 100644
--- a/src/kudu/tools/tool_action_common.cc
+++ b/src/kudu/tools/tool_action_common.cc
@@ -480,19 +480,38 @@ void PrintTable(const vector<vector<string>>& columns,
const string& separator,
 
 } // anonymous namespace
 
-Status PrintTable(const vector<string>& headers,
-                  const vector<vector<string>>& columns,
-                  ostream& out) {
+DataTable::DataTable(std::vector<string> col_names)
+    : column_names_(std::move(col_names)),
+      columns_(column_names_.size()) {
+}
+
+void DataTable::AddRow(std::vector<string> row) {
+  CHECK_EQ(row.size(), columns_.size());
+  int i = 0;
+  for (auto& v : row) {
+    columns_[i++].emplace_back(std::move(v));
+  }
+}
+
+void DataTable::AddColumn(string name, vector<string> column) {
+  if (!columns_.empty()) {
+    CHECK_EQ(column.size(), columns_[0].size());
+  }
+  column_names_.emplace_back(std::move(name));
+  columns_.emplace_back(std::move(column));
+}
+
+Status DataTable::PrintTo(ostream& out) const {
   if (boost::iequals(FLAGS_format, "pretty")) {
-    PrettyPrintTable(headers, columns, out);
+    PrettyPrintTable(column_names_, columns_, out);
   } else if (boost::iequals(FLAGS_format, "space")) {
-    PrintTable(columns, " ", out);
+    PrintTable(columns_, " ", out);
   } else if (boost::iequals(FLAGS_format, "tsv")) {
-    PrintTable(columns, "	", out);
+    PrintTable(columns_, "	", out);
   } else if (boost::iequals(FLAGS_format, "csv")) {
-    PrintTable(columns, ",", out);
+    PrintTable(columns_, ",", out);
   } else if (boost::iequals(FLAGS_format, "json")) {
-    JsonPrintTable(headers, columns, out);
+    JsonPrintTable(column_names_, columns_, out);
   } else {
     return Status::InvalidArgument("unknown format (--format)", FLAGS_format);
   }

http://git-wip-us.apache.org/repos/asf/kudu/blob/96ad3b07/src/kudu/tools/tool_action_common.h
----------------------------------------------------------------------
diff --git a/src/kudu/tools/tool_action_common.h b/src/kudu/tools/tool_action_common.h
index 439ba4f..ac232b8 100644
--- a/src/kudu/tools/tool_action_common.h
+++ b/src/kudu/tools/tool_action_common.h
@@ -106,10 +106,41 @@ Status PrintServerTimestamp(const std::string& address, uint16_t
default_port);
 Status SetServerFlag(const std::string& address, uint16_t default_port,
                      const std::string& flag, const std::string& value);
 
-// Prints a table.
-Status PrintTable(const std::vector<std::string>& headers,
-                  const std::vector<std::vector<std::string>>& columns,
-                  std::ostream& out);
+// A table of data to present to the user.
+//
+// Supports formatting based on the --format flag.
+// All data is buffered in memory before being output.
+//
+// Example usage:
+//    DataTable table({"person", "favorite color"});
+//    vector<string> cols(2);
+//    AddTableRow({"joe", "red"}, &cols);
+//    AddTableRow({"bob", "green"}, &cols);
+//    AddTableRow({"alice", "yellow"}, &cols);
+//    PrintTable(headers, cols, cout);
+class DataTable {
+ public:
+  // Construct a table with the given column names.
+  explicit DataTable(std::vector<std::string> col_names);
+
+  // Add a row of data to the table.
+  //
+  // REQUIRES: 'row.size()' matches the number of column names specified
+  // in the constructor.
+  void AddRow(std::vector<std::string> row);
+
+  // Add a column of data to the right side of the table.
+  //
+  // REQUIRES: if any rows have been added already, the length of this column
+  // must match the length of all existing columns.
+  void AddColumn(std::string name, std::vector<std::string> column);
+
+  // Print the table to 'out'.
+  Status PrintTo(std::ostream& out) const WARN_UNUSED_RESULT;
+ private:
+  std::vector<std::string> column_names_;
+  std::vector<std::vector<std::string>> columns_;
+};
 
 // Wrapper around a Kudu client which allows calling proxy methods on the leader
 // master.

http://git-wip-us.apache.org/repos/asf/kudu/blob/96ad3b07/src/kudu/tools/tool_action_master.cc
----------------------------------------------------------------------
diff --git a/src/kudu/tools/tool_action_master.cc b/src/kudu/tools/tool_action_master.cc
index b6d7704..ef71a24 100644
--- a/src/kudu/tools/tool_action_master.cc
+++ b/src/kudu/tools/tool_action_master.cc
@@ -88,8 +88,7 @@ Status ListMasters(const RunnerContext& context) {
     return StatusFromPB(resp.error().status());
   }
 
-  vector<string> headers;
-  vector<vector<string>> columns;
+  DataTable table({});
 
   vector<ServerEntryPB> masters;
   std::copy_if(resp.masters().begin(), resp.masters().end(), std::back_inserter(masters),
@@ -107,7 +106,6 @@ Status ListMasters(const RunnerContext& context) {
   };
 
   for (const auto& column : strings::Split(FLAGS_columns, ",", strings::SkipEmpty()))
{
-    headers.push_back(column.ToString());
     vector<string> values;
     if (boost::iequals(column, "uuid")) {
       for (const auto& master : masters) {
@@ -136,11 +134,10 @@ Status ListMasters(const RunnerContext& context) {
     } else {
       return Status::InvalidArgument("unknown column (--columns)", column);
     }
-
-    columns.emplace_back(std::move(values));
+    table.AddColumn(column.ToString(), std::move(values));
   }
 
-  RETURN_NOT_OK(PrintTable(headers, columns, cout));
+  RETURN_NOT_OK(table.PrintTo(cout));
   return Status::OK();
 }
 

http://git-wip-us.apache.org/repos/asf/kudu/blob/96ad3b07/src/kudu/tools/tool_action_tserver.cc
----------------------------------------------------------------------
diff --git a/src/kudu/tools/tool_action_tserver.cc b/src/kudu/tools/tool_action_tserver.cc
index 4a5efe0..3703103 100644
--- a/src/kudu/tools/tool_action_tserver.cc
+++ b/src/kudu/tools/tool_action_tserver.cc
@@ -91,9 +91,7 @@ Status ListTServers(const RunnerContext& context) {
     return StatusFromPB(resp.error().status());
   }
 
-  vector<string> headers;
-  vector<vector<string>> columns;
-
+  DataTable table({});
   const auto& servers = resp.servers();
 
   auto hostport_to_string = [](const HostPortPB& hostport) {
@@ -101,7 +99,6 @@ Status ListTServers(const RunnerContext& context) {
   };
 
   for (const auto& column : strings::Split(FLAGS_columns, ",", strings::SkipEmpty()))
{
-    headers.emplace_back(column.ToString());
     vector<string> values;
     if (boost::iequals(column, "uuid")) {
       for (const auto& server : servers) {
@@ -134,11 +131,10 @@ Status ListTServers(const RunnerContext& context) {
     } else {
       return Status::InvalidArgument("unknown column (--columns)", column);
     }
-
-    columns.emplace_back(std::move(values));
+    table.AddColumn(column.ToString(), std::move(values));
   }
 
-  RETURN_NOT_OK(PrintTable(headers, columns, cout));
+  RETURN_NOT_OK(table.PrintTo(cout));
   return Status::OK();
 }
 


Mime
View raw message