You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by do...@apache.org on 2019/02/22 05:11:45 UTC
[incubator-iotdb] branch master updated: refact client to reduce
sonar bug (#55)
This is an automated email from the ASF dual-hosted git repository.
dope pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-iotdb.git
The following commit(s) were added to refs/heads/master by this push:
new ee4a647 refact client to reduce sonar bug (#55)
ee4a647 is described below
commit ee4a64723c37636b16dc32b4d45fb3eb36797bf9
Author: Jiang Tian <jt...@163.com>
AuthorDate: Fri Feb 22 13:11:41 2019 +0800
refact client to reduce sonar bug (#55)
---
.../apache/iotdb/cli/client/AbstractClient.java | 469 +++++++++++++--------
1 file changed, 289 insertions(+), 180 deletions(-)
diff --git a/iotdb-cli/src/main/java/org/apache/iotdb/cli/client/AbstractClient.java b/iotdb-cli/src/main/java/org/apache/iotdb/cli/client/AbstractClient.java
index 05a21df..267355b 100644
--- a/iotdb-cli/src/main/java/org/apache/iotdb/cli/client/AbstractClient.java
+++ b/iotdb-cli/src/main/java/org/apache/iotdb/cli/client/AbstractClient.java
@@ -18,6 +18,7 @@
*/
package org.apache.iotdb.cli.client;
+import java.io.PrintStream;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
@@ -144,6 +145,16 @@ public abstract class AbstractClient {
protected static ServerProperties properties = null;
+ private static boolean printHeader = false;
+ private static int displayCnt = 0;
+
+ private static final PrintStream SCREEN_PRINTER = new PrintStream(System.out);
+ /**
+ * showException is currently fixed to false because the display of exceptions is not elaborate.
+ * We can make it an option in future versions.
+ */
+ private static boolean showException = false;
+
protected static void init() {
keywordSet.add("-" + HOST_ARGS);
keywordSet.add("-" + HELP_ARGS);
@@ -165,9 +176,9 @@ public abstract class AbstractClient {
public static void output(ResultSet res, boolean printToConsole, ZoneId zoneId)
throws SQLException {
int cnt = 0;
- int displayCnt = 0;
boolean printTimestamp = true;
- boolean printHeader = false;
+ displayCnt = 0;
+ printHeader = false;
ResultSetMetaData resultSetMetaData = res.getMetaData();
int colCount = resultSetMetaData.getColumnCount();
@@ -179,57 +190,10 @@ public abstract class AbstractClient {
// Output values
while (res.next()) {
- // Output Labels
- if (printToConsole) {
- if (!printHeader) {
- printBlockLine(printTimestamp, colCount, resultSetMetaData, isShow);
- printName(printTimestamp, colCount, resultSetMetaData, isShow);
- printBlockLine(printTimestamp, colCount, resultSetMetaData, isShow);
- printHeader = true;
- }
-
- if (isShow) { // 'show timeseries <path>' or 'show storage group' metadata results
- System.out.print("|");
- for (int i = 1; i <= colCount; i++) {
- formatValue = "%" + maxValueLengthForShow[i - 1] + "s|";
- System.out.printf(formatValue, String.valueOf(res.getString(i)));
- }
- System.out.println();
- } else { // queried data results
- if (displayCnt < maxPrintRowCount) { // NOTE displayCnt only works on queried data results
- System.out.print("|");
- if (printTimestamp) {
- System.out.printf(formatTime, formatDatetime(res.getLong(TIMESTAMP_STR), zoneId));
- }
- for (int i = 2; i <= colCount; i++) {
- boolean flag = false;
- for (String timeStr : AGGREGRATE_TIME_LIST) {
- if (resultSetMetaData.getColumnLabel(i).toUpperCase()
- .contains(timeStr.toUpperCase())) {
- flag = true;
- break;
- }
- }
- if (flag) {
- try {
- System.out.printf(formatValue, formatDatetime(res.getLong(i), zoneId));
- } catch (Exception e) {
- System.out.printf(formatValue, "null");
- }
- } else {
- System.out.printf(formatValue, String.valueOf(res.getString(i)));
- }
- }
- System.out.println();
- displayCnt++;
- }
- }
- }
-
+ printRow(printTimestamp, colCount, resultSetMetaData, isShow, res, zoneId);
cnt++;
-
if (!printToConsole && cnt % 10000 == 0) {
- System.out.println(cnt);
+ println(cnt);
}
}
@@ -242,21 +206,97 @@ public abstract class AbstractClient {
printBlockLine(printTimestamp, colCount, resultSetMetaData, isShow);
}
if (displayCnt == maxPrintRowCount) {
- System.out.println(String.format("Reach maxPrintRowCount = %s lines", maxPrintRowCount));
+ println(String.format("Reach maxPrintRowCount = %s lines", maxPrintRowCount));
}
}
- System.out.println(StringUtils.repeat('-', DIVIDING_LINE_LENGTH));
+ println(StringUtils.repeat('-', DIVIDING_LINE_LENGTH));
+ printCount(isShow, res, cnt);
+ }
+
+ protected static void printCount(boolean isShow, ResultSet res, int cnt) throws SQLException {
if (isShow) {
int type = res.getType();
if (type == IoTDBMetadataResultSet.MetadataType.STORAGE_GROUP.ordinal()) { // storage group
- System.out.println("Total storage group number = " + cnt);
+ println("Total storage group number = " + cnt);
} else if (type == IoTDBMetadataResultSet.MetadataType.TIMESERIES
.ordinal()) { // show timeseries <path>
- System.out.println("Total timeseries number = " + cnt);
+ println("Total timeseries number = " + cnt);
}
} else {
- System.out.println("Total line number = " + cnt);
+ println("Total line number = " + cnt);
+ }
+ }
+
+ protected static void printRow(boolean printTimestamp, int colCount,
+ ResultSetMetaData resultSetMetaData, boolean isShow, ResultSet res, ZoneId zoneId)
+ throws SQLException {
+ // Output Labels
+ if (!printToConsole) {
+ return;
+ }
+ printHeader(printTimestamp, colCount, resultSetMetaData, isShow);
+
+ if (isShow) { // 'show timeseries <path>' or 'show storage group' metadata results
+ printShow(colCount, res);
+ } else { // queried data results
+ printRowData(printTimestamp, res, zoneId, resultSetMetaData, colCount);
+ }
+ }
+
+ protected static void printHeader(boolean printTimestamp, int colCount,
+ ResultSetMetaData resultSetMetaData, boolean isShow) throws SQLException {
+ if (!printHeader) {
+ printBlockLine(printTimestamp, colCount, resultSetMetaData, isShow);
+ printName(printTimestamp, colCount, resultSetMetaData, isShow);
+ printBlockLine(printTimestamp, colCount, resultSetMetaData, isShow);
+ printHeader = true;
+ }
+ }
+
+ protected static void printShow(int colCount, ResultSet res) throws SQLException {
+ print("|");
+ for (int i = 1; i <= colCount; i++) {
+ formatValue = "%" + maxValueLengthForShow[i - 1] + "s|";
+ printf(formatValue, String.valueOf(res.getString(i)));
+ }
+ println();
+ }
+
+ protected static void printRowData(boolean printTimestamp, ResultSet res, ZoneId zoneId,
+ ResultSetMetaData resultSetMetaData, int colCount)
+ throws SQLException {
+ if (displayCnt < maxPrintRowCount) { // NOTE displayCnt only works on queried data results
+ print("|");
+ if (printTimestamp) {
+ printf(formatTime, formatDatetime(res.getLong(TIMESTAMP_STR), zoneId));
+ }
+ for (int i = 2; i <= colCount; i++) {
+ printColumnData(resultSetMetaData, res, i, zoneId);
+ }
+ println();
+ displayCnt++;
+ }
+ }
+
+ protected static void printColumnData(ResultSetMetaData resultSetMetaData, ResultSet res, int i,
+ ZoneId zoneId) throws SQLException {
+ boolean flag = false;
+ for (String timeStr : AGGREGRATE_TIME_LIST) {
+ if (resultSetMetaData.getColumnLabel(i).toUpperCase().contains(timeStr.toUpperCase())) {
+ flag = true;
+ break;
+ }
+ }
+ if (flag) {
+ try {
+ printf(formatValue, formatDatetime(res.getLong(i), zoneId));
+ } catch (Exception e) {
+ printf(formatValue, "null");
+ handleException(e);
+ }
+ } else {
+ printf(formatValue, String.valueOf(res.getString(i)));
}
}
@@ -320,8 +360,8 @@ public abstract class AbstractClient {
if (isRequired) {
String msg = String
.format("%s: Required values for option '%s' not provided", IOTDB_CLI_PREFIX, name);
- System.out.println(msg);
- System.out.println("Use -help for more information");
+ println(msg);
+ println("Use -help for more information");
throw new ArgsErrorException(msg);
} else if (defaultValue == null) {
String msg = String
@@ -401,28 +441,28 @@ public abstract class AbstractClient {
blockLine.append(StringUtils.repeat('-', maxValueLength)).append("+");
}
}
- System.out.println(blockLine);
+ println(blockLine);
}
protected static void printName(boolean printTimestamp, int colCount,
ResultSetMetaData resultSetMetaData,
boolean isShowTs) throws SQLException {
- System.out.print("|");
+ print("|");
if (isShowTs) {
for (int i = 1; i <= colCount; i++) {
formatValue = "%" + maxValueLengthForShow[i - 1] + "s|";
- System.out.printf(formatValue, resultSetMetaData.getColumnName(i));
+ printf(formatValue, resultSetMetaData.getColumnName(i));
}
} else {
formatValue = "%" + maxValueLength + "s|";
if (printTimestamp) {
- System.out.printf(formatTime, TIMESTAMP_STR);
+ printf(formatTime, TIMESTAMP_STR);
}
for (int i = 2; i <= colCount; i++) {
- System.out.printf(formatValue, resultSetMetaData.getColumnLabel(i));
+ printf(formatValue, resultSetMetaData.getColumnLabel(i));
}
}
- System.out.println();
+ println();
}
protected static String[] removePasswordArgs(String[] args) {
@@ -441,7 +481,7 @@ public abstract class AbstractClient {
}
protected static void displayLogo(String version) {
- System.out.println(" _____ _________ ______ ______ \n"
+ println(" _____ _________ ______ ______ \n"
+ "|_ _| | _ _ ||_ _ `.|_ _ \\ \n"
+ " | | .--.|_/ | | \\_| | | `. \\ | |_) | \n"
+ " | | / .'`\\ \\ | | | | | | | __'. \n"
@@ -454,147 +494,185 @@ public abstract class AbstractClient {
String specialCmd = cmd.toLowerCase().trim();
if (specialCmd.equals(QUIT_COMMAND) || specialCmd.equals(EXIT_COMMAND)) {
- System.out.println(specialCmd + " normally");
+ println(specialCmd + " normally");
return OperationResult.RETURN_OPER;
}
if (specialCmd.equals(HELP)) {
- System.out.println(" <your-sql>\t\t\t execute your sql statment");
- System.out
- .println(String
- .format(" %s\t\t show how many timeseries are in iotdb", SHOW_METADATA_COMMAND));
- System.out
- .println(String.format(" %s=xxx\t eg. long, default, ISO8601, yyyy-MM-dd HH:mm:ss.",
- SET_TIMESTAMP_DISPLAY));
- System.out.println(String.format(" %s\t show time display type", SHOW_TIMESTAMP_DISPLAY));
- System.out.println(String.format(" %s=xxx\t\t eg. +08:00, Asia/Shanghai.", SET_TIME_ZONE));
- System.out.println(String.format(" %s\t\t show cli time zone", SHOW_TIMEZONE));
- System.out.println(
- String.format(" %s=xxx\t\t set fetch size when querying data from server.",
- SET_FETCH_SIZE));
- System.out.println(String.format(" %s\t\t show fetch size", SHOW_FETCH_SIZE));
- System.out.println(
- String.format(" %s=xxx\t eg. set max lines for cli to ouput, -1 equals to unlimited.",
- SET_MAX_DISPLAY_NUM));
+ showHelp();
return OperationResult.CONTINUE_OPER;
}
if (specialCmd.equals(SHOW_METADATA_COMMAND)) {
- try {
- System.out.println(((IoTDBDatabaseMetadata) connection.getMetaData()).getMetadataInJson());
- } catch (SQLException e) {
- System.out.println("Failed to show timeseries because: " + e.getMessage());
- }
+ showMetaData(connection);
return OperationResult.CONTINUE_OPER;
}
-
if (specialCmd.startsWith(SET_TIMESTAMP_DISPLAY)) {
- String[] values = specialCmd.split("=");
- if (values.length != 2) {
- System.out.println(String.format("Time display format error, please input like %s=ISO8601",
- SET_TIMESTAMP_DISPLAY));
- return OperationResult.CONTINUE_OPER;
- }
- try {
- setTimeFormat(cmd.split("=")[1]);
- } catch (Exception e) {
- System.out.println(String.format("time display format error, %s", e.getMessage()));
- return OperationResult.CONTINUE_OPER;
- }
- System.out.println("Time display type has set to " + cmd.split("=")[1].trim());
+ setTimestampDisplay(specialCmd, cmd);
return OperationResult.CONTINUE_OPER;
}
if (specialCmd.startsWith(SET_TIME_ZONE)) {
- String[] values = specialCmd.split("=");
- if (values.length != 2) {
- System.out.println(
- String.format("Time zone format error, please input like %s=+08:00", SET_TIME_ZONE));
- return OperationResult.CONTINUE_OPER;
- }
- try {
- connection.setTimeZone(cmd.split("=")[1].trim());
- } catch (Exception e) {
- System.out.println(String.format("Time zone format error, %s", e.getMessage()));
- return OperationResult.CONTINUE_OPER;
- }
- System.out.println("Time zone has set to " + values[1].trim());
+ setTimeZone(specialCmd, cmd, connection);
return OperationResult.CONTINUE_OPER;
}
if (specialCmd.startsWith(SET_FETCH_SIZE)) {
- String[] values = specialCmd.split("=");
- if (values.length != 2) {
- System.out
- .println(String
- .format("Fetch size format error, please input like %s=10000", SET_FETCH_SIZE));
- return OperationResult.CONTINUE_OPER;
- }
- try {
- setFetchSize(cmd.split("=")[1]);
- } catch (Exception e) {
- System.out.println(String.format("Fetch size format error, %s", e.getMessage()));
- return OperationResult.CONTINUE_OPER;
- }
- System.out.println("Fetch size has set to " + values[1].trim());
+ setFetchSize(specialCmd, cmd);
return OperationResult.CONTINUE_OPER;
}
if (specialCmd.startsWith(SET_MAX_DISPLAY_NUM)) {
- String[] values = specialCmd.split("=");
- if (values.length != 2) {
- System.out
- .println(String.format("Max display number format error, please input like %s = 10000",
- SET_MAX_DISPLAY_NUM));
- return OperationResult.CONTINUE_OPER;
- }
- try {
- setMaxDisplayNumber(cmd.split("=")[1]);
- } catch (Exception e) {
- System.out.println(String.format("Max display number format error, %s", e.getMessage()));
- return OperationResult.CONTINUE_OPER;
- }
- System.out.println("Max display number has set to " + values[1].trim());
+ setMaxDisplaNum(specialCmd, cmd);
return OperationResult.CONTINUE_OPER;
}
if (specialCmd.startsWith(SHOW_TIMEZONE)) {
- try {
- System.out.println("Current time zone: " + connection.getTimeZone());
- } catch (Exception e) {
- System.out.println("Cannot get time zone from server side because: " + e.getMessage());
- }
+
return OperationResult.CONTINUE_OPER;
}
if (specialCmd.startsWith(SHOW_TIMESTAMP_DISPLAY)) {
- System.out.println("Current time format: " + timeFormat);
+ println("Current time format: " + timeFormat);
return OperationResult.CONTINUE_OPER;
}
if (specialCmd.startsWith(SHOW_FETCH_SIZE)) {
- System.out.println("Current fetch size: " + fetchSize);
+ println("Current fetch size: " + fetchSize);
return OperationResult.CONTINUE_OPER;
}
if (specialCmd.startsWith(IMPORT_CMD)) {
- String[] values = specialCmd.split(" ");
- if (values.length != 2) {
- System.out.println(String.format(
- "Please input like: import /User/myfile. "
- + "Noted that your file path cannot contain any space character)"));
- return OperationResult.CONTINUE_OPER;
- }
- try {
- System.out.println(cmd.split(" ")[1]);
- ImportCsv.importCsvFromFile(host, port, username, password, cmd.split(" ")[1],
- connection.getTimeZone());
- } catch (SQLException e) {
- System.out.println(
- String
- .format("Failed to import from %s because %s", cmd.split(" ")[1], e.getMessage()));
- } catch (TException e) {
- System.out.println("Cannot connect to server");
- }
+ importCmd(specialCmd, cmd, connection);
return OperationResult.CONTINUE_OPER;
}
+ executeQuery(connection, cmd);
+ return OperationResult.NO_OPER;
+ }
+
+ protected static void showHelp() {
+ println(" <your-sql>\t\t\t execute your sql statment");
+ println(String.format(" %s\t\t show how many timeseries are in iotdb",
+ SHOW_METADATA_COMMAND));
+ println(String.format(" %s=xxx\t eg. long, default, ISO8601, yyyy-MM-dd HH:mm:ss.",
+ SET_TIMESTAMP_DISPLAY));
+ println(String.format(" %s\t show time display type", SHOW_TIMESTAMP_DISPLAY));
+ println(String.format(" %s=xxx\t\t eg. +08:00, Asia/Shanghai.", SET_TIME_ZONE));
+ println(String.format(" %s\t\t show cli time zone", SHOW_TIMEZONE));
+ println(
+ String.format(" %s=xxx\t\t set fetch size when querying data from server.",
+ SET_FETCH_SIZE));
+ println(String.format(" %s\t\t show fetch size", SHOW_FETCH_SIZE));
+ println(
+ String.format(" %s=xxx\t eg. set max lines for cli to ouput, -1 equals to unlimited.",
+ SET_MAX_DISPLAY_NUM));
+ }
+
+ protected static void showMetaData(IoTDBConnection connection) {
+ try {
+ println(((IoTDBDatabaseMetadata) connection.getMetaData()).getMetadataInJson());
+ } catch (SQLException e) {
+ println("Failed to show timeseries because: " + e.getMessage());
+ handleException(e);
+ }
+ }
+
+ protected static void setTimestampDisplay(String specialCmd, String cmd) {
+ String[] values = specialCmd.split("=");
+ if (values.length != 2) {
+ println(String.format("Time display format error, please input like %s=ISO8601",
+ SET_TIMESTAMP_DISPLAY));
+ return;
+ }
+ try {
+ setTimeFormat(cmd.split("=")[1]);
+ } catch (Exception e) {
+ println(String.format("time display format error, %s", e.getMessage()));
+ handleException(e);
+ return;
+ }
+ println("Time display type has set to " + cmd.split("=")[1].trim());
+ }
+
+ protected static void setTimeZone(String specialCmd, String cmd, IoTDBConnection connection) {
+ String[] values = specialCmd.split("=");
+ if (values.length != 2) {
+ println(
+ String.format("Time zone format error, please input like %s=+08:00", SET_TIME_ZONE));
+ return;
+ }
+ try {
+ connection.setTimeZone(cmd.split("=")[1].trim());
+ } catch (Exception e) {
+ println(String.format("Time zone format error: %s", e.getMessage()));
+ handleException(e);
+ return;
+ }
+ println("Time zone has set to " + values[1].trim());
+ }
+
+ protected static void setFetchSize(String specialCmd, String cmd) {
+ String[] values = specialCmd.split("=");
+ if (values.length != 2) {
+ println(String
+ .format("Fetch size format error, please input like %s=10000", SET_FETCH_SIZE));
+ return;
+ }
+ try {
+ setFetchSize(cmd.split("=")[1]);
+ } catch (Exception e) {
+ println(String.format("Fetch size format error, %s", e.getMessage()));
+ handleException(e);
+ return;
+ }
+ println("Fetch size has set to " + values[1].trim());
+ }
+
+ protected static void setMaxDisplaNum(String specialCmd, String cmd) {
+ String[] values = specialCmd.split("=");
+ if (values.length != 2) {
+ println(String.format("Max display number format error, please input like %s = 10000",
+ SET_MAX_DISPLAY_NUM));
+ return;
+ }
+ try {
+ setMaxDisplayNumber(cmd.split("=")[1]);
+ } catch (Exception e) {
+ println(String.format("Max display number format error, %s", e.getMessage()));
+ handleException(e);
+ return;
+ }
+ println("Max display number has set to " + values[1].trim());
+ }
+
+ protected static void showTimeZone(IoTDBConnection connection) {
+ try {
+ println("Current time zone: " + connection.getTimeZone());
+ } catch (Exception e) {
+ println("Cannot get time zone from server side because: " + e.getMessage());
+ handleException(e);
+ }
+ }
+
+ protected static void importCmd(String specialCmd, String cmd, IoTDBConnection connection) {
+ String[] values = specialCmd.split(" ");
+ if (values.length != 2) {
+ println("Please input like: import /User/myfile. "
+ + "Noted that your file path cannot contain any space character)");
+ return;
+ }
+ try {
+ println(cmd.split(" ")[1]);
+ ImportCsv.importCsvFromFile(host, port, username, password, cmd.split(" ")[1],
+ connection.getTimeZone());
+ } catch (SQLException e) {
+ println(String.format("Failed to import from %s because %s",
+ cmd.split(" ")[1], e.getMessage()));
+ handleException(e);
+ } catch (TException e) {
+ println("Cannot connect to server");
+ handleException(e);
+ }
+ }
+
+ protected static void executeQuery(IoTDBConnection connection, String cmd) {
Statement statement = null;
long startTime = System.currentTimeMillis();
try {
@@ -605,32 +683,63 @@ public abstract class AbstractClient {
if (hasResultSet) {
ResultSet resultSet = statement.getResultSet();
output(resultSet, printToConsole, zoneId);
- if (resultSet != null) {
- try {
- resultSet.close();
- } catch (SQLException e) {
- System.out.println("Cannot close resultSet because: " + e.getMessage());
- }
- }
+
}
- System.out.println("Execute successfully. Type `help` to get more information.");
} catch (Exception e) {
- System.out.println("Msg: " + e.getMessage());
+ println("Msg: " + e.getMessage());
+ handleException(e);
} finally {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
- System.out.println("Cannot close statement because: " + e.getMessage());
+ println("Cannot close statement because: " + e.getMessage());
+ handleException(e);
}
}
}
long costTime = System.currentTimeMillis() - startTime;
- System.out.println(String.format("It costs %.3fs", costTime / 1000.0));
- return OperationResult.NO_OPER;
+ println(String.format("It costs %.3fs", costTime / 1000.0));
+ }
+
+ protected static void closeResultSet(ResultSet resultSet) {
+ if (resultSet != null) {
+ try {
+ resultSet.close();
+ } catch (SQLException e) {
+ println("Cannot close resultSet because: " + e.getMessage());
+ handleException(e);
+ }
+ }
}
enum OperationResult {
RETURN_OPER, CONTINUE_OPER, NO_OPER
}
+
+ private static void printf(String format, Object ... args) {
+ SCREEN_PRINTER.printf(format, args);
+ }
+
+ private static void print(String msg) {
+ SCREEN_PRINTER.println(msg);
+ }
+
+ private static void println() {
+ SCREEN_PRINTER.println();
+ }
+
+ private static void println(String msg) {
+ SCREEN_PRINTER.println(msg);
+ }
+
+ private static void println(Object obj) {
+ SCREEN_PRINTER.println(obj);
+ }
+
+ private static void handleException(Exception e) {
+ if (showException) {
+ e.printStackTrace(SCREEN_PRINTER);
+ }
+ }
}