This is an automated email from the ASF dual-hosted git repository.

haonan pushed a commit to branch refector_py_table_session
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/refector_py_table_session by 
this push:
     new 2528eb30fb9 builder
2528eb30fb9 is described below

commit 2528eb30fb9987592af3b9c5f096b8ea36ab12be
Author: HTHou <[email protected]>
AuthorDate: Thu Nov 28 12:07:19 2024 +0800

    builder
---
 iotdb-client/client-py/iotdb/table_session.py      | 48 ++++++++++++++-------
 iotdb-client/client-py/iotdb/table_session_pool.py | 49 ++++++++++++++++------
 .../client-py/table_model_session_example.py       | 44 +++++++++----------
 .../client-py/table_model_session_pool_example.py  | 28 +++++++------
 .../tests/integration/test_relational_session.py   | 19 +++++----
 .../tests/integration/test_tablemodel_insert.py    | 32 ++++++--------
 6 files changed, 130 insertions(+), 90 deletions(-)

diff --git a/iotdb-client/client-py/iotdb/table_session.py 
b/iotdb-client/client-py/iotdb/table_session.py
index 5bbc7129a71..747712125d3 100644
--- a/iotdb-client/client-py/iotdb/table_session.py
+++ b/iotdb-client/client-py/iotdb/table_session.py
@@ -23,26 +23,46 @@ from iotdb.utils.SessionDataSet import SessionDataSet
 from iotdb.utils.Tablet import Tablet
 
 
+class TableSessionConfig(object):
+
+    def __init__(
+        self,
+        node_urls: list = None,
+        username: str = Session.DEFAULT_USER,
+        password: str = Session.DEFAULT_PASSWORD,
+        database: str = None,
+        fetch_size: int = 5000,
+        time_zone: str = Session.DEFAULT_ZONE_ID,
+        enable_compression: bool = False,
+    ):
+        if node_urls is None:
+            node_urls = ["localhost:6667"]
+        self.node_urls = node_urls
+        self.username = username
+        self.password = password
+        self.database = database
+        self.fetch_size = fetch_size
+        self.time_zone = time_zone
+        self.enable_compression = enable_compression
+
+
 class TableSession(object):
 
-    def __init__(self, **kwargs):
-        self.__session_pool = kwargs.get("__session_pool", None)
+    def __init__(
+        self, table_session_config: TableSessionConfig = None, 
session_pool=None
+    ):
+        self.__session_pool = session_pool
         if self.__session_pool is None:
-            __node_urls = kwargs.get("node_urls", ["127.0.0.1:6667"])
-            __username = kwargs.get("username", Session.DEFAULT_USER)
-            __password = kwargs.get("password", Session.DEFAULT_PASSWORD)
-            __fetch_size = kwargs.get("fetch_size", 5000)
-            __zone_id = kwargs.get("zone_id", Session.DEFAULT_ZONE_ID)
             self.__session = Session.init_from_node_urls(
-                __node_urls,
-                __username,
-                __password,
-                __fetch_size,
-                __zone_id,
+                table_session_config.node_urls,
+                table_session_config.username,
+                table_session_config.password,
+                table_session_config.fetch_size,
+                table_session_config.time_zone,
             )
             self.__session.sql_dialect = "table"
-            self.__session.database = kwargs.get("database", None)
-            self.__session.open(kwargs.get("enable_rpc_compression", False))
+            self.__session.database = table_session_config.database
+            self.__session.open(table_session_config.enable_compression)
         else:
             self.__session = self.__session_pool.get_session()
 
diff --git a/iotdb-client/client-py/iotdb/table_session_pool.py 
b/iotdb-client/client-py/iotdb/table_session_pool.py
index 79677859fab..a39628e5f42 100644
--- a/iotdb-client/client-py/iotdb/table_session_pool.py
+++ b/iotdb-client/client-py/iotdb/table_session_pool.py
@@ -20,27 +20,50 @@ from iotdb.SessionPool import SessionPool, PoolConfig
 from iotdb.table_session import TableSession
 
 
+class TableSessionPoolConfig(object):
+    def __init__(
+        self,
+        node_urls: list = None,
+        max_pool_size: int = 5,
+        username: str = Session.DEFAULT_USER,
+        password: str = Session.DEFAULT_PASSWORD,
+        database: str = None,
+        fetch_size: int = 5000,
+        time_zone: str = Session.DEFAULT_ZONE_ID,
+        enable_compression: bool = False,
+        wait_timeout_in_ms: int = 10000,
+        max_retry: int = 3,
+    ):
+        if node_urls is None:
+            node_urls = ["localhost:6667"]
+        self.pool_config = PoolConfig(
+            node_urls=node_urls,
+            user_name=username,
+            password=password,
+            fetch_size=fetch_size,
+            time_zone=time_zone,
+            max_retry=max_retry,
+            enable_compression=enable_compression,
+        )
+        self.max_pool_size = max_pool_size
+        self.wait_timeout_in_ms = wait_timeout_in_ms
+        self.database = database
+
+
 class TableSessionPool(object):
 
-    def __init__(self, **kwargs):
-        pool_config = PoolConfig(
-            node_urls=kwargs.get("node_urls", ["localhost:6667"]),
-            user_name=kwargs.get("username", Session.DEFAULT_USER),
-            password=kwargs.get("password", Session.DEFAULT_PASSWORD),
-            fetch_size=kwargs.get("fetch_size", 5000),
-            time_zone=kwargs.get("zone_id", Session.DEFAULT_ZONE_ID),
-            max_retry=3,
-        )
-        max_pool_size = 5
-        wait_timeout_in_ms = 3000
+    def __init__(self, table_session_pool_config: TableSessionPoolConfig):
+        pool_config = table_session_pool_config.pool_config
+        max_pool_size = table_session_pool_config.max_pool_size
+        wait_timeout_in_ms = table_session_pool_config.wait_timeout_in_ms
         self.__session_pool = SessionPool(
             pool_config, max_pool_size, wait_timeout_in_ms
         )
         self.__session_pool.sql_dialect = "table"
-        self.__session_pool.database = kwargs.get("database", None)
+        self.__session_pool.database = table_session_pool_config.database
 
     def get_session(self) -> TableSession:
-        return TableSession(config={"__session_pool": self.__session_pool})
+        return TableSession(None, session_pool=self.__session_pool)
 
     def close(self):
         self.__session_pool.close()
diff --git a/iotdb-client/client-py/table_model_session_example.py 
b/iotdb-client/client-py/table_model_session_example.py
index c25a0c8cc71..108373b55a3 100644
--- a/iotdb-client/client-py/table_model_session_example.py
+++ b/iotdb-client/client-py/table_model_session_example.py
@@ -17,20 +17,20 @@
 #
 import numpy as np
 
-from iotdb.table_session import TableSession
+from iotdb.table_session import TableSession, TableSessionConfig
 from iotdb.utils.IoTDBConstants import TSDataType
 from iotdb.utils.NumpyTablet import NumpyTablet
 from iotdb.utils.Tablet import ColumnType, Tablet
 
 # creating session connection.
 # don't specify database in constructor
-config = {
-    "node_urls": ["localhost:6667"],
-    "username": "root",
-    "password": "root",
-    "time_zone": "UTC+8",
-}
-session = TableSession(**config)
+config = TableSessionConfig(
+    node_urls=["localhost:6667"],
+    username="root",
+    password="root",
+    time_zone="UTC+8",
+)
+session = TableSession(config)
 
 session.execute_non_query_statement("CREATE DATABASE test1")
 session.execute_non_query_statement("CREATE DATABASE test2")
@@ -64,14 +64,14 @@ with session.execute_query_statement("SHOW TABLES FROM 
test1") as session_data_s
 session.close()
 
 # specify database in constructor
-config = {
-    "node_urls": ["localhost:6667"],
-    "username": "root",
-    "password": "root",
-    "time_zone": "UTC+8",
-    "database": "test1",
-}
-session = TableSession(**config)
+config = TableSessionConfig(
+    node_urls=["localhost:6667"],
+    username="root",
+    password="root",
+    database="test1",
+    time_zone="UTC+8",
+)
+session = TableSession(config)
 
 # show tables from current database
 with session.execute_query_statement("SHOW TABLES") as session_data_set:
@@ -92,12 +92,12 @@ with session.execute_query_statement("SHOW TABLES") as 
session_data_set:
 session.close()
 
 # insert data by tablet
-config = {
-    "node_urls": ["localhost:6667"],
-    "username": "root",
-    "password": "root",
-}
-session = TableSession(**config)
+config = TableSessionConfig(
+    node_urls=["localhost:6667"],
+    username="root",
+    password="root",
+)
+session = TableSession(config)
 session.execute_non_query_statement("CREATE DATABASE IF NOT EXISTS db1")
 session.execute_non_query_statement('USE "db1"')
 session.execute_non_query_statement(
diff --git a/iotdb-client/client-py/table_model_session_pool_example.py 
b/iotdb-client/client-py/table_model_session_pool_example.py
index efec7503dfe..18f9417a820 100644
--- a/iotdb-client/client-py/table_model_session_pool_example.py
+++ b/iotdb-client/client-py/table_model_session_pool_example.py
@@ -19,7 +19,7 @@ import threading
 
 import numpy as np
 
-from iotdb.table_session_pool import TableSessionPool
+from iotdb.table_session_pool import TableSessionPool, TableSessionPoolConfig
 from iotdb.utils.IoTDBConstants import TSDataType
 from iotdb.utils.NumpyTablet import NumpyTablet
 from iotdb.utils.Tablet import ColumnType, Tablet
@@ -123,22 +123,24 @@ def delete_data():
     session.close()
 
 
-ip = "127.0.0.1"
-port = "6667"
+# Create a session pool
 username = "root"
 password = "root"
-config = {
-    "node_urls": ["127.0.0.1:6667", "127.0.0.1:6668", "127.0.0.1:6669"],
-    "username": username,
-    "password": password,
-    "fetch_size": 1024,
-    "database": "db1",
-}
+node_urls = ["127.0.0.1:6667", "127.0.0.1:6668", "127.0.0.1:6669"]
+fetch_size = 1024
+database = "db1"
 max_pool_size = 5
 wait_timeout_in_ms = 3000
-
-# Create a session pool
-session_pool = TableSessionPool(**config)
+config = TableSessionPoolConfig(
+    node_urls=node_urls,
+    user_name=username,
+    password=password,
+    database=database,
+    max_pool_size=max_pool_size,
+    fetch_size=fetch_size,
+    wait_timeout_in_ms=wait_timeout_in_ms,
+)
+session_pool = TableSessionPool(config)
 
 prepare_data()
 
diff --git 
a/iotdb-client/client-py/tests/integration/test_relational_session.py 
b/iotdb-client/client-py/tests/integration/test_relational_session.py
index 8de87aa202a..bcbb85c5b4f 100644
--- a/iotdb-client/client-py/tests/integration/test_relational_session.py
+++ b/iotdb-client/client-py/tests/integration/test_relational_session.py
@@ -18,8 +18,8 @@
 
 import numpy as np
 
-from iotdb.table_session import TableSession
-from iotdb.table_session_pool import TableSessionPool
+from iotdb.table_session import TableSession, TableSessionConfig
+from iotdb.table_session_pool import TableSessionPool, TableSessionPoolConfig
 from iotdb.utils.IoTDBConstants import TSDataType
 from iotdb.utils.NumpyTablet import NumpyTablet
 from iotdb.utils.Tablet import Tablet, ColumnType
@@ -37,16 +37,17 @@ def test_session_pool():
 def session_test(use_session_pool=False):
     with IoTDBContainer("iotdb:dev") as db:
         db: IoTDBContainer
-        config = {
-            "node_urls": [
-                f"{db.get_container_host_ip()}:{db.get_exposed_port(6667)}",
-            ]
-        }
         if use_session_pool:
-            session_pool = TableSessionPool(**config)
+            config = TableSessionPoolConfig(
+                
node_urls=[f"{db.get_container_host_ip()}:{db.get_exposed_port(6667)}"]
+            )
+            session_pool = TableSessionPool(config)
             session = session_pool.get_session()
         else:
-            session = TableSession(**config)
+            config = TableSessionConfig(
+                
node_urls=[f"{db.get_container_host_ip()}:{db.get_exposed_port(6667)}"]
+            )
+            session = TableSession(config)
 
         session.execute_non_query_statement("CREATE DATABASE IF NOT EXISTS 
db1")
         session.execute_non_query_statement('USE "db1"')
diff --git a/iotdb-client/client-py/tests/integration/test_tablemodel_insert.py 
b/iotdb-client/client-py/tests/integration/test_tablemodel_insert.py
index 52fc111c885..037a876a73e 100644
--- a/iotdb-client/client-py/tests/integration/test_tablemodel_insert.py
+++ b/iotdb-client/client-py/tests/integration/test_tablemodel_insert.py
@@ -17,7 +17,7 @@
 #
 
 import numpy as np
-from iotdb.table_session import TableSession
+from iotdb.table_session import TableSession, TableSessionConfig
 from iotdb.utils.BitMap import BitMap
 from iotdb.utils.IoTDBConstants import TSDataType
 from iotdb.utils.Tablet import Tablet, ColumnType
@@ -30,12 +30,10 @@ from .iotdb_container import IoTDBContainer
 def test_insert_use_tablet():
     with IoTDBContainer("iotdb:dev") as db:
         db: IoTDBContainer
-        config = {
-            "node_urls": [
-                f"{db.get_container_host_ip()}:{db.get_exposed_port(6667)}",
-            ]
-        }
-        session = TableSession(**config)
+        config = TableSessionConfig(
+            
node_urls=[f"{db.get_container_host_ip()}:{db.get_exposed_port(6667)}"]
+        )
+        session = TableSession(config)
 
         # Preparation before testing
         session.execute_non_query_statement(
@@ -622,12 +620,10 @@ def test_insert_use_tablet():
 def test_insert_relational_tablet_use_numpy_tablet():
     with IoTDBContainer("iotdb:dev") as db:
         db: IoTDBContainer
-        config = {
-            "node_urls": [
-                f"{db.get_container_host_ip()}:{db.get_exposed_port(6667)}",
-            ]
-        }
-        session = TableSession(**config)
+        config = TableSessionConfig(
+            
node_urls=[f"{db.get_container_host_ip()}:{db.get_exposed_port(6667)}"]
+        )
+        session = TableSession(config)
 
         # Preparation before testing
         session.execute_non_query_statement(
@@ -1106,12 +1102,10 @@ def test_insert_relational_tablet_use_numpy_tablet():
 def test_insert_relational_tablet_auto_create():
     with IoTDBContainer("iotdb:dev") as db:
         db: IoTDBContainer
-        config = {
-            "node_urls": [
-                f"{db.get_container_host_ip()}:{db.get_exposed_port(6667)}",
-            ]
-        }
-        session = TableSession(**config)
+        config = TableSessionConfig(
+            
node_urls=[f"{db.get_container_host_ip()}:{db.get_exposed_port(6667)}"]
+        )
+        session = TableSession(config)
 
         # Preparation before testing
         session.execute_non_query_statement(

Reply via email to