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 2021/07/13 11:41:17 UTC

[iotdb] branch master updated: Add more builder options (#3552)

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 8a1dd9a  Add more builder options (#3552)
8a1dd9a is described below

commit 8a1dd9ab2c5a6141e4fc35e21cd2877334913b79
Author: J.J. Liu <li...@gmail.com>
AuthorDate: Tue Jul 13 19:40:53 2021 +0800

    Add more builder options (#3552)
    
    * Fix bug
    
    * Add more builder options
---
 docs/UserGuide/API/Programming-Java-Native-API.md  | 13 ++++++++-
 .../UserGuide/API/Programming-Java-Native-API.md   | 13 ++++++++-
 .../java/org/apache/iotdb/session/Session.java     | 29 ++++++++++++++++++++
 .../org/apache/iotdb/session/pool/SessionPool.java |  9 +++++++
 .../java/org/apache/iotdb/session/SessionTest.java | 31 ++++++++++++++++++----
 .../apache/iotdb/session/pool/SessionPoolTest.java | 17 +++++++-----
 6 files changed, 98 insertions(+), 14 deletions(-)

diff --git a/docs/UserGuide/API/Programming-Java-Native-API.md b/docs/UserGuide/API/Programming-Java-Native-API.md
index bfa2079..cac776e 100644
--- a/docs/UserGuide/API/Programming-Java-Native-API.md
+++ b/docs/UserGuide/API/Programming-Java-Native-API.md
@@ -59,11 +59,22 @@ Here we show the commonly used interfaces and their parameters in the Native API
     // use default configuration 
     session = new Session.Builder.build();
 
-    // configure all fields
+    // initialize with a single node
     session = 
         new Session.Builder()
             .host(String host)
             .port(int port)
+            .build();
+
+    // initialize with multiple nodes
+    session = 
+        new Session.Builder()
+            .nodeUrls(List<String> nodeUrls)
+            .build();
+
+    // other configurations
+    session = 
+        new Session.Builder()
             .fetchSize(int fetchSize)
             .username(String username)
             .password(String password)
diff --git a/docs/zh/UserGuide/API/Programming-Java-Native-API.md b/docs/zh/UserGuide/API/Programming-Java-Native-API.md
index 7bc6cab..645305a 100644
--- a/docs/zh/UserGuide/API/Programming-Java-Native-API.md
+++ b/docs/zh/UserGuide/API/Programming-Java-Native-API.md
@@ -62,11 +62,22 @@ mvn clean install -pl session -am -Dmaven.test.skip=true
     // 全部使用默认配置
     session = new Session.Builder.build();
 
-    // 自行配置参数
+    // 指定一个可连接节点
     session = 
         new Session.Builder()
             .host(String host)
             .port(int port)
+            .build();
+
+    // 指定多个可连接节点
+    session = 
+        new Session.Builder()
+            .nodeUrls(List<String> nodeUrls)
+            .build();
+
+    // 其他配置项
+    session = 
+        new Session.Builder()
             .fetchSize(int fetchSize)
             .username(String username)
             .password(String password)
diff --git a/session/src/main/java/org/apache/iotdb/session/Session.java b/session/src/main/java/org/apache/iotdb/session/Session.java
index ca69157..d9fb56b 100644
--- a/session/src/main/java/org/apache/iotdb/session/Session.java
+++ b/session/src/main/java/org/apache/iotdb/session/Session.java
@@ -1914,6 +1914,7 @@ public class Session {
     private int thriftDefaultBufferSize = Config.DEFAULT_INITIAL_BUFFER_CAPACITY;
     private int thriftMaxFrameSize = Config.DEFAULT_MAX_FRAME_SIZE;
     private boolean enableCacheLeader = Config.DEFAULT_CACHE_LEADER_MODE;
+    List<String> nodeUrls = null;
 
     public Builder host(String host) {
       this.host = host;
@@ -1940,6 +1941,11 @@ public class Session {
       return this;
     }
 
+    public Builder zoneId(ZoneId zoneId) {
+      this.zoneId = zoneId;
+      return this;
+    }
+
     public Builder thriftDefaultBufferSize(int thriftDefaultBufferSize) {
       this.thriftDefaultBufferSize = thriftDefaultBufferSize;
       return this;
@@ -1955,7 +1961,30 @@ public class Session {
       return this;
     }
 
+    public Builder nodeUrls(List<String> nodeUrls) {
+      this.nodeUrls = nodeUrls;
+      return this;
+    }
+
     public Session build() {
+      if (nodeUrls != null
+          && (!Config.DEFAULT_HOST.equals(host) || rpcPort != Config.DEFAULT_PORT)) {
+        throw new IllegalArgumentException(
+            "You should specify either nodeUrls or (host + rpcPort), but not both");
+      }
+
+      if (nodeUrls != null) {
+        return new Session(
+            nodeUrls,
+            username,
+            password,
+            fetchSize,
+            zoneId,
+            thriftDefaultBufferSize,
+            thriftMaxFrameSize,
+            enableCacheLeader);
+      }
+
       return new Session(
           host,
           rpcPort,
diff --git a/session/src/main/java/org/apache/iotdb/session/pool/SessionPool.java b/session/src/main/java/org/apache/iotdb/session/pool/SessionPool.java
index fe8cb25..2effaa2 100644
--- a/session/src/main/java/org/apache/iotdb/session/pool/SessionPool.java
+++ b/session/src/main/java/org/apache/iotdb/session/pool/SessionPool.java
@@ -1186,6 +1186,10 @@ public class SessionPool {
     return fetchSize;
   }
 
+  public ZoneId getZoneId() {
+    return zoneId;
+  }
+
   public long getTimeout() {
     return timeout;
   }
@@ -1240,6 +1244,11 @@ public class SessionPool {
       return this;
     }
 
+    public Builder zoneId(ZoneId zoneId) {
+      this.zoneId = zoneId;
+      return this;
+    }
+
     public Builder timeout(long timeout) {
       this.timeout = timeout;
       return this;
diff --git a/session/src/test/java/org/apache/iotdb/session/SessionTest.java b/session/src/test/java/org/apache/iotdb/session/SessionTest.java
index 290c6be..f9dae22 100644
--- a/session/src/test/java/org/apache/iotdb/session/SessionTest.java
+++ b/session/src/test/java/org/apache/iotdb/session/SessionTest.java
@@ -37,7 +37,9 @@ import org.junit.Before;
 import org.junit.Test;
 
 import java.time.ZoneId;
+import java.time.ZoneOffset;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 
@@ -258,13 +260,32 @@ public class SessionTest {
             .thriftDefaultBufferSize(2)
             .thriftMaxFrameSize(3)
             .enableCacheLeader(true)
+            .zoneId(ZoneOffset.UTC)
             .build();
 
-    assertEquals(session.fetchSize, 1);
-    assertEquals(session.username, "abc");
-    assertEquals(session.password, "123456");
-    assertEquals(session.thriftDefaultBufferSize, 2);
-    assertEquals(session.thriftMaxFrameSize, 3);
+    assertEquals(1, session.fetchSize);
+    assertEquals("abc", session.username);
+    assertEquals("123456", session.password);
+    assertEquals(2, session.thriftDefaultBufferSize);
+    assertEquals(3, session.thriftMaxFrameSize);
+    assertEquals(ZoneOffset.UTC, session.zoneId);
     assertTrue(session.enableCacheLeader);
+
+    session = new Session.Builder().nodeUrls(Arrays.asList("aaa.com:12", "bbb.com:12")).build();
+    assertEquals(Arrays.asList("aaa.com:12", "bbb.com:12"), session.nodeUrls);
+
+    try {
+      session =
+          new Session.Builder()
+              .nodeUrls(Arrays.asList("aaa.com:12", "bbb.com:12"))
+              .port(1234)
+              .build();
+      fail("specifying both nodeUrls and (host + port) is not allowed");
+    } catch (IllegalArgumentException e) {
+      assertEquals(
+          "You should specify either nodeUrls or (host + rpcPort), but not both", e.getMessage());
+    } catch (Exception e) {
+      fail();
+    }
   }
 }
diff --git a/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java b/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java
index 9a5c4a3..9116070 100644
--- a/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java
+++ b/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java
@@ -33,6 +33,7 @@ import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.time.ZoneOffset;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -375,16 +376,18 @@ public class SessionPoolTest {
             .timeout(2)
             .enableCacheLeader(true)
             .enableCompression(true)
+            .zoneId(ZoneOffset.UTC)
             .build();
 
-    assertEquals(pool.getHost(), "localhost");
-    assertEquals(pool.getPort(), 1234);
-    assertEquals(pool.getUser(), "abc");
-    assertEquals(pool.getPassword(), "123");
-    assertEquals(pool.getMaxSize(), 10);
-    assertEquals(pool.getFetchSize(), 1);
-    assertEquals(pool.getTimeout(), 2);
+    assertEquals("localhost", pool.getHost());
+    assertEquals(1234, pool.getPort());
+    assertEquals("abc", pool.getUser());
+    assertEquals("123", pool.getPassword());
+    assertEquals(10, pool.getMaxSize());
+    assertEquals(1, pool.getFetchSize());
+    assertEquals(2, pool.getTimeout());
     assertTrue(pool.isEnableCacheLeader());
     assertTrue(pool.isEnableCompression());
+    assertEquals(ZoneOffset.UTC, pool.getZoneId());
   }
 }