You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ha...@apache.org on 2023/06/30 05:29:04 UTC
[iotdb] branch master updated: Fix sonar bugs and code smells in iotdb-cli(tool) (#10394)
This is an automated email from the ASF dual-hosted git repository.
haonan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/master by this push:
new 8447fa2b0c1 Fix sonar bugs and code smells in iotdb-cli(tool) (#10394)
8447fa2b0c1 is described below
commit 8447fa2b0c1adacd3ecdf723b4757495770eb7c2
Author: CritasWang <cr...@outlook.com>
AuthorDate: Fri Jun 30 13:28:59 2023 +0800
Fix sonar bugs and code smells in iotdb-cli(tool) (#10394)
---
.../main/java/org/apache/iotdb/tool/ExportCsv.java | 20 +++++++++---------
.../java/org/apache/iotdb/tool/ExportTsFile.java | 24 +++++++++++++---------
.../main/java/org/apache/iotdb/tool/ImportCsv.java | 10 ++++++---
3 files changed, 31 insertions(+), 23 deletions(-)
diff --git a/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/ExportCsv.java b/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/ExportCsv.java
index 2691d4271c9..c2eb3c9628d 100644
--- a/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/ExportCsv.java
+++ b/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/ExportCsv.java
@@ -92,7 +92,10 @@ public class ExportCsv extends AbstractCsvTool {
private static long timeout = -1;
- @SuppressWarnings("squid:S3776") // Suppress high Cognitive Complexity warning
+ @SuppressWarnings({
+ "squid:S3776",
+ "squid:S2093"
+ }) // Suppress high Cognitive Complexity warning, ignore try-with-resources
/** main function of export csv tool. */
public static void main(String[] args) {
Options options = createOptions();
@@ -125,13 +128,7 @@ public class ExportCsv extends AbstractCsvTool {
if (!checkTimeFormat()) {
System.exit(CODE_ERROR);
}
- } catch (ArgsErrorException e) {
- IoTPrinter.println("Invalid args: " + e.getMessage());
- exitCode = CODE_ERROR;
- }
-
- try (Session sessionNew = new Session(host, Integer.parseInt(port), username, password)) {
- session = sessionNew;
+ session = new Session(host, Integer.parseInt(port), username, password);
session.open(false);
timestampPrecision = session.getTimestampPrecision();
setTimeZone();
@@ -158,6 +155,9 @@ public class ExportCsv extends AbstractCsvTool {
} catch (IOException e) {
IoTPrinter.println("Failed to operate on file, because " + e.getMessage());
exitCode = CODE_ERROR;
+ } catch (ArgsErrorException e) {
+ IoTPrinter.println("Invalid args: " + e.getMessage());
+ exitCode = CODE_ERROR;
} catch (IoTDBConnectionException | StatementExecutionException e) {
IoTPrinter.println("Connect failed because " + e.getMessage());
exitCode = CODE_ERROR;
@@ -307,8 +307,8 @@ public class ExportCsv extends AbstractCsvTool {
/**
* This method will be called, if the query commands are written in a sql file.
*
- * @param filePath
- * @throws IOException
+ * @param filePath sql file path
+ * @throws IOException exception
*/
private static void dumpFromSqlFile(String filePath) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
diff --git a/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/ExportTsFile.java b/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/ExportTsFile.java
index 881854dbb82..0240132b253 100644
--- a/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/ExportTsFile.java
+++ b/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/ExportTsFile.java
@@ -77,7 +77,10 @@ public class ExportTsFile extends AbstractTsFileTool {
private static long timeout = -1;
- @SuppressWarnings("squid:S3776") // Suppress high Cognitive Complexity warning
+ @SuppressWarnings({
+ "squid:S3776",
+ "squid:S2093"
+ }) // Suppress high Cognitive Complexity warning, ignore try-with-resources
/** main function of export tsFile tool. */
public static void main(String[] args) {
Options options = createOptions();
@@ -110,13 +113,7 @@ public class ExportTsFile extends AbstractTsFileTool {
parseBasicParams(commandLine);
parseSpecialParams(commandLine);
- } catch (ArgsErrorException e) {
- IoTPrinter.println("Invalid args: " + e.getMessage());
- exitCode = CODE_ERROR;
- }
-
- try (Session sessionNew = new Session(host, Integer.parseInt(port), username, password)) {
- session = sessionNew;
+ session = new Session(host, Integer.parseInt(port), username, password);
session.open(false);
if (queryCommand == null) {
@@ -144,6 +141,9 @@ public class ExportTsFile extends AbstractTsFileTool {
} catch (IOException e) {
IoTPrinter.println("Failed to operate on file, because " + e.getMessage());
exitCode = CODE_ERROR;
+ } catch (ArgsErrorException e) {
+ IoTPrinter.println("Invalid args: " + e.getMessage());
+ exitCode = CODE_ERROR;
} catch (IoTDBConnectionException e) {
IoTPrinter.println("Connect failed because " + e.getMessage());
exitCode = CODE_ERROR;
@@ -293,7 +293,10 @@ public class ExportTsFile extends AbstractTsFileTool {
}
}
- @SuppressWarnings("squid:S3776") // Suppress high Cognitive Complexity warning
+ @SuppressWarnings({
+ "squid:S3776",
+ "squid:S6541"
+ }) // Suppress high Cognitive Complexity warning, Suppress many task in one method warning
public static void writeTsFileFile(SessionDataSet sessionDataSet, String filePath)
throws IOException, IoTDBConnectionException, StatementExecutionException,
WriteProcessException {
@@ -352,7 +355,8 @@ public class ExportTsFile extends AbstractTsFileTool {
while (sessionDataSet.hasNext()) {
RowRecord rowRecord = sessionDataSet.next();
List<Field> fields = rowRecord.getFields();
- for (int i = 0; i < fields.size(); ) {
+ int i = 0;
+ while (i < fields.size()) {
for (Tablet tablet : tabletList) {
int rowIndex = tablet.rowSize++;
tablet.addTimestamp(rowIndex, rowRecord.getTimestamp());
diff --git a/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/ImportCsv.java b/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/ImportCsv.java
index 43e4e5aaa9d..838193a4f0a 100644
--- a/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/ImportCsv.java
+++ b/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/ImportCsv.java
@@ -363,11 +363,12 @@ public class ImportCsv extends AbstractCsvTool {
* @return the status code
* @throws IoTDBConnectionException
*/
+ @SuppressWarnings({"squid:S2093"}) // ignore try-with-resources
public static int importFromTargetPath(
String host, int port, String username, String password, String targetPath, String timeZone)
throws IoTDBConnectionException {
- try (Session sessionNew = new Session(host, port, username, password, false); ) {
- session = sessionNew;
+ try {
+ session = new Session(host, port, username, password, false);
session.open(false);
timeZoneID = timeZone;
setTimeZone();
@@ -477,7 +478,7 @@ public class ImportCsv extends AbstractCsvTool {
records.forEach(
recordObj -> {
- if (hasStarted.get()) {
+ if (Boolean.TRUE.equals(hasStarted.get())) {
hasStarted.set(true);
} else if (pointSize.get() >= batchPointSize) {
writeAndEmptyDataSet(deviceIds, times, typesList, valuesList, measurementsList, 3);
@@ -789,6 +790,9 @@ public class ImportCsv extends AbstractCsvTool {
* @param headerTypeMap
* @param headerNameMap
*/
+ @SuppressWarnings(
+ "squid:S135") // ignore for loops should not contain more than a single "break" or "continue"
+ // statement
private static void parseHeaders(
List<String> headerNames,
@Nullable HashMap<String, List<String>> deviceAndMeasurementNames,