This is an automated email from the ASF dual-hosted git repository.
rong 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 dda533d [IOTDB-2022] SessionDataSet implements AutoCloseable (#4411)
dda533d is described below
commit dda533dfdc6a676714839fa02ffc011bcd1b04f8
Author: Xieqijun <[email protected]>
AuthorDate: Wed Nov 17 13:54:44 2021 +0800
[IOTDB-2022] SessionDataSet implements AutoCloseable (#4411)
Co-authored-by: Steve Yurong Su <[email protected]>
---
.../main/java/org/apache/iotdb/SessionExample.java | 197 ++++++++++-----------
.../org/apache/iotdb/session/SessionDataSet.java | 7 +-
2 files changed, 104 insertions(+), 100 deletions(-)
diff --git a/example/session/src/main/java/org/apache/iotdb/SessionExample.java
b/example/session/src/main/java/org/apache/iotdb/SessionExample.java
index d3ffcc1..565d706 100644
--- a/example/session/src/main/java/org/apache/iotdb/SessionExample.java
+++ b/example/session/src/main/java/org/apache/iotdb/SessionExample.java
@@ -608,13 +608,13 @@ public class SessionExample {
session.executeNonQueryStatement(
"select s1, s2, s3 into into_s1, into_s2, into_s3 from root.sg1.d1");
- SessionDataSet dataSet =
- session.executeQueryStatement("select into_s1, into_s2, into_s3 from
root.sg1.d1");
- System.out.println(dataSet.getColumnNames());
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ try (SessionDataSet dataSet =
+ session.executeQueryStatement("select into_s1, into_s2, into_s3 from
root.sg1.d1")) {
+ System.out.println(dataSet.getColumnNames());
+ while (dataSet.hasNext()) {
+ System.out.println(dataSet.next());
+ }
}
- dataSet.closeOperationHandle();
}
private static void deleteData() throws IoTDBConnectionException,
StatementExecutionException {
@@ -633,85 +633,83 @@ public class SessionExample {
}
private static void query() throws IoTDBConnectionException,
StatementExecutionException {
- SessionDataSet dataSet = session.executeQueryStatement("select * from
root.sg1.d1");
- System.out.println(dataSet.getColumnNames());
- dataSet.setFetchSize(1024); // default is 10000
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ try (SessionDataSet dataSet = session.executeQueryStatement("select * from
root.sg1.d1")) {
+ System.out.println(dataSet.getColumnNames());
+ dataSet.setFetchSize(1024); // default is 10000
+ while (dataSet.hasNext()) {
+ System.out.println(dataSet.next());
+ }
}
-
- dataSet.closeOperationHandle();
}
private static void query4Redirect()
throws IoTDBConnectionException, StatementExecutionException {
String selectPrefix = "select * from root.redirect";
for (int i = 0; i < 6; i++) {
- SessionDataSet dataSet =
- sessionEnableRedirect.executeQueryStatement(selectPrefix + i +
".d1");
- System.out.println(dataSet.getColumnNames());
- dataSet.setFetchSize(1024); // default is 10000
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
+ try (SessionDataSet dataSet =
+ sessionEnableRedirect.executeQueryStatement(selectPrefix + i +
".d1")) {
- dataSet.closeOperationHandle();
+ System.out.println(dataSet.getColumnNames());
+ dataSet.setFetchSize(1024); // default is 10000
+ while (dataSet.hasNext()) {
+ System.out.println(dataSet.next());
+ }
+ }
}
for (int i = 0; i < 6; i++) {
- SessionDataSet dataSet =
+ try (SessionDataSet dataSet =
sessionEnableRedirect.executeQueryStatement(
- selectPrefix + i + ".d1 where time >= 1 and time < 10");
- System.out.println(dataSet.getColumnNames());
- dataSet.setFetchSize(1024); // default is 10000
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
+ selectPrefix + i + ".d1 where time >= 1 and time < 10")) {
- dataSet.closeOperationHandle();
+ System.out.println(dataSet.getColumnNames());
+ dataSet.setFetchSize(1024); // default is 10000
+ while (dataSet.hasNext()) {
+ System.out.println(dataSet.next());
+ }
+ }
}
for (int i = 0; i < 6; i++) {
- SessionDataSet dataSet =
+ try (SessionDataSet dataSet =
sessionEnableRedirect.executeQueryStatement(
- selectPrefix + i + ".d1 where time >= 1 and time < 10 align by
device");
- System.out.println(dataSet.getColumnNames());
- dataSet.setFetchSize(1024); // default is 10000
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
- }
+ selectPrefix + i + ".d1 where time >= 1 and time < 10 align by
device")) {
- dataSet.closeOperationHandle();
+ System.out.println(dataSet.getColumnNames());
+ dataSet.setFetchSize(1024); // default is 10000
+ while (dataSet.hasNext()) {
+ System.out.println(dataSet.next());
+ }
+ }
}
for (int i = 0; i < 6; i++) {
- SessionDataSet dataSet =
+ try (SessionDataSet dataSet =
sessionEnableRedirect.executeQueryStatement(
selectPrefix
+ i
+ ".d1 where time >= 1 and time < 10 and root.redirect"
+ i
- + ".d1.s1 > 1");
- System.out.println(dataSet.getColumnNames());
- dataSet.setFetchSize(1024); // default is 10000
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ + ".d1.s1 > 1")) {
+ System.out.println(dataSet.getColumnNames());
+ dataSet.setFetchSize(1024); // default is 10000
+ while (dataSet.hasNext()) {
+ System.out.println(dataSet.next());
+ }
}
-
- dataSet.closeOperationHandle();
}
}
private static void queryWithTimeout()
throws IoTDBConnectionException, StatementExecutionException {
- SessionDataSet dataSet = session.executeQueryStatement("select * from
root.sg1.d1", 2000);
- System.out.println(dataSet.getColumnNames());
- dataSet.setFetchSize(1024); // default is 10000
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ try (SessionDataSet dataSet =
+ session.executeQueryStatement("select * from root.sg1.d1", 2000)) {
+ System.out.println(dataSet.getColumnNames());
+ dataSet.setFetchSize(1024); // default is 10000
+ while (dataSet.hasNext()) {
+ System.out.println(dataSet.next());
+ }
}
-
- dataSet.closeOperationHandle();
}
private static void rawDataQuery() throws IoTDBConnectionException,
StatementExecutionException {
@@ -722,13 +720,14 @@ public class SessionExample {
long startTime = 10L;
long endTime = 200L;
- SessionDataSet dataSet = session.executeRawDataQuery(paths, startTime,
endTime);
- System.out.println(dataSet.getColumnNames());
- dataSet.setFetchSize(1024);
- while (dataSet.hasNext()) {
- System.out.println(dataSet.next());
+ try (SessionDataSet dataSet = session.executeRawDataQuery(paths,
startTime, endTime)) {
+
+ System.out.println(dataSet.getColumnNames());
+ dataSet.setFetchSize(1024);
+ while (dataSet.hasNext()) {
+ System.out.println(dataSet.next());
+ }
}
- dataSet.closeOperationHandle();
}
private static void lastDataQuery() throws IoTDBConnectionException,
StatementExecutionException {
@@ -736,57 +735,57 @@ public class SessionExample {
paths.add(ROOT_SG1_D1_S1);
paths.add(ROOT_SG1_D1_S2);
paths.add(ROOT_SG1_D1_S3);
- SessionDataSet sessionDataSet = session.executeLastDataQuery(paths, 3);
- System.out.println(sessionDataSet.getColumnNames());
- sessionDataSet.setFetchSize(1024);
- while (sessionDataSet.hasNext()) {
- System.out.println(sessionDataSet.next());
+ try (SessionDataSet sessionDataSet = session.executeLastDataQuery(paths,
3)) {
+ System.out.println(sessionDataSet.getColumnNames());
+ sessionDataSet.setFetchSize(1024);
+ while (sessionDataSet.hasNext()) {
+ System.out.println(sessionDataSet.next());
+ }
}
- sessionDataSet.closeOperationHandle();
}
private static void queryByIterator()
throws IoTDBConnectionException, StatementExecutionException {
- SessionDataSet dataSet = session.executeQueryStatement("select * from
root.sg1.d1");
- DataIterator iterator = dataSet.iterator();
- System.out.println(dataSet.getColumnNames());
- dataSet.setFetchSize(1024); // default is 10000
- while (iterator.next()) {
- StringBuilder builder = new StringBuilder();
- // get time
- builder.append(iterator.getLong(1)).append(",");
- // get second column
- if (!iterator.isNull(2)) {
- builder.append(iterator.getLong(2)).append(",");
- } else {
- builder.append("null").append(",");
- }
+ try (SessionDataSet dataSet = session.executeQueryStatement("select * from
root.sg1.d1")) {
- // get third column
- if (!iterator.isNull(ROOT_SG1_D1_S2)) {
- builder.append(iterator.getLong(ROOT_SG1_D1_S2)).append(",");
- } else {
- builder.append("null").append(",");
- }
+ DataIterator iterator = dataSet.iterator();
+ System.out.println(dataSet.getColumnNames());
+ dataSet.setFetchSize(1024); // default is 10000
+ while (iterator.next()) {
+ StringBuilder builder = new StringBuilder();
+ // get time
+ builder.append(iterator.getLong(1)).append(",");
+ // get second column
+ if (!iterator.isNull(2)) {
+ builder.append(iterator.getLong(2)).append(",");
+ } else {
+ builder.append("null").append(",");
+ }
- // get forth column
- if (!iterator.isNull(4)) {
- builder.append(iterator.getLong(4)).append(",");
- } else {
- builder.append("null").append(",");
- }
+ // get third column
+ if (!iterator.isNull(ROOT_SG1_D1_S2)) {
+ builder.append(iterator.getLong(ROOT_SG1_D1_S2)).append(",");
+ } else {
+ builder.append("null").append(",");
+ }
- // get fifth column
- if (!iterator.isNull(ROOT_SG1_D1_S4)) {
- builder.append(iterator.getObject(ROOT_SG1_D1_S4));
- } else {
- builder.append("null");
- }
+ // get forth column
+ if (!iterator.isNull(4)) {
+ builder.append(iterator.getLong(4)).append(",");
+ } else {
+ builder.append("null").append(",");
+ }
- System.out.println(builder);
- }
+ // get fifth column
+ if (!iterator.isNull(ROOT_SG1_D1_S4)) {
+ builder.append(iterator.getObject(ROOT_SG1_D1_S4));
+ } else {
+ builder.append("null");
+ }
- dataSet.closeOperationHandle();
+ System.out.println(builder);
+ }
+ }
}
private static void nonQuery() throws IoTDBConnectionException,
StatementExecutionException {
diff --git a/session/src/main/java/org/apache/iotdb/session/SessionDataSet.java
b/session/src/main/java/org/apache/iotdb/session/SessionDataSet.java
index 541d6e4..3c1f6c7 100644
--- a/session/src/main/java/org/apache/iotdb/session/SessionDataSet.java
+++ b/session/src/main/java/org/apache/iotdb/session/SessionDataSet.java
@@ -39,7 +39,7 @@ import java.util.Map;
import static org.apache.iotdb.rpc.IoTDBRpcDataSet.START_INDEX;
-public class SessionDataSet {
+public class SessionDataSet implements AutoCloseable {
private final IoTDBRpcDataSet ioTDBRpcDataSet;
@@ -196,6 +196,11 @@ public class SessionDataSet {
return new DataIterator();
}
+ @Override
+ public void close() throws IoTDBConnectionException,
StatementExecutionException {
+ closeOperationHandle();
+ }
+
public class DataIterator {
public boolean next() throws StatementExecutionException,
IoTDBConnectionException {