nvidia-nat-mysql 1.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
File without changes
@@ -0,0 +1,211 @@
1
+ # SPDX-FileCopyrightText: Copyright (c) 2024-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ import logging
17
+
18
+ import aiomysql
19
+ from aiomysql.pool import Pool
20
+
21
+ from nat.data_models.object_store import KeyAlreadyExistsError
22
+ from nat.data_models.object_store import NoSuchKeyError
23
+ from nat.object_store.interfaces import ObjectStore
24
+ from nat.object_store.models import ObjectStoreItem
25
+ from nat.plugins.mysql.object_store import MySQLObjectStoreClientConfig
26
+ from nat.utils.type_utils import override
27
+
28
+ logger = logging.getLogger(__name__)
29
+
30
+
31
+ class MySQLObjectStore(ObjectStore):
32
+ """
33
+ Implementation of ObjectStore that stores objects in a MySQL database.
34
+ """
35
+
36
+ def __init__(self, config: MySQLObjectStoreClientConfig):
37
+
38
+ super().__init__()
39
+
40
+ self._config = config
41
+ self._conn_pool: Pool | None = None
42
+
43
+ self._schema = f"`bucket_{self._config.bucket_name}`"
44
+
45
+ async def __aenter__(self):
46
+
47
+ if self._conn_pool is not None:
48
+ raise RuntimeError("Connection already established")
49
+
50
+ self._conn_pool = await aiomysql.create_pool(
51
+ host=self._config.host,
52
+ port=self._config.port,
53
+ user=self._config.username,
54
+ password=self._config.password,
55
+ autocommit=False, # disable autocommit for transactions
56
+ )
57
+ assert self._conn_pool is not None
58
+
59
+ logger.info("Created connection pool for %s at %s:%s",
60
+ self._config.bucket_name,
61
+ self._config.host,
62
+ self._config.port)
63
+
64
+ async with self._conn_pool.acquire() as conn:
65
+ async with conn.cursor() as cur:
66
+
67
+ # Create schema (database) if doesn't exist
68
+ await cur.execute(f"CREATE SCHEMA IF NOT EXISTS {self._schema} DEFAULT CHARACTER SET utf8mb4;")
69
+ await cur.execute(f"USE {self._schema};")
70
+
71
+ # Create metadata table_schema
72
+ await cur.execute("""
73
+ CREATE TABLE IF NOT EXISTS object_meta (
74
+ id INT AUTO_INCREMENT PRIMARY KEY,
75
+ path VARCHAR(768) NOT NULL UNIQUE,
76
+ size BIGINT NOT NULL,
77
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
78
+ ) ENGINE=InnoDB;
79
+ """)
80
+
81
+ # Create blob data table
82
+ await cur.execute("""
83
+ CREATE TABLE IF NOT EXISTS object_data (
84
+ id INT PRIMARY KEY,
85
+ data LONGBLOB NOT NULL,
86
+ FOREIGN KEY (id) REFERENCES object_meta(id) ON DELETE CASCADE
87
+ ) ENGINE=InnoDB ROW_FORMAT=DYNAMIC;
88
+ """)
89
+
90
+ await conn.commit()
91
+
92
+ logger.info("Created schema and tables for %s at %s:%s",
93
+ self._config.bucket_name,
94
+ self._config.host,
95
+ self._config.port)
96
+
97
+ return self
98
+
99
+ async def __aexit__(self, exc_type, exc_value, traceback):
100
+
101
+ if not self._conn_pool:
102
+ raise RuntimeError("Connection not established")
103
+
104
+ # Trigger the non-async close method then wait for the pool to close
105
+ self._conn_pool.close()
106
+
107
+ await self._conn_pool.wait_closed()
108
+
109
+ self._conn_pool = None
110
+
111
+ @override
112
+ async def put_object(self, key: str, item: ObjectStoreItem):
113
+
114
+ if not self._conn_pool:
115
+ raise RuntimeError("Connection not established")
116
+
117
+ async with self._conn_pool.acquire() as conn:
118
+ async with conn.cursor() as cur:
119
+ await cur.execute(f"USE {self._schema};")
120
+ try:
121
+ await cur.execute("START TRANSACTION;")
122
+ await cur.execute("INSERT IGNORE INTO object_meta (path, size) VALUES (%s, %s)",
123
+ (key, len(item.data)))
124
+ if cur.rowcount == 0:
125
+ raise KeyAlreadyExistsError(
126
+ key=key, additional_message=f"MySQL table {self._config.bucket_name} already has key {key}")
127
+ await cur.execute("SELECT id FROM object_meta WHERE path=%s FOR UPDATE;", (key, ))
128
+ (obj_id, ) = await cur.fetchone()
129
+ blob = item.model_dump_json()
130
+ await cur.execute("INSERT INTO object_data (id, data) VALUES (%s, %s)", (obj_id, blob))
131
+ await conn.commit()
132
+ except Exception:
133
+ await conn.rollback()
134
+ raise
135
+
136
+ @override
137
+ async def upsert_object(self, key: str, item: ObjectStoreItem):
138
+
139
+ if not self._conn_pool:
140
+ raise RuntimeError("Connection not established")
141
+
142
+ async with self._conn_pool.acquire() as conn:
143
+ async with conn.cursor() as cur:
144
+ await cur.execute(f"USE {self._schema};")
145
+ try:
146
+ await cur.execute("START TRANSACTION;")
147
+ await cur.execute(
148
+ """
149
+ INSERT INTO object_meta (path, size)
150
+ VALUES (%s, %s)
151
+ ON DUPLICATE KEY UPDATE size=VALUES(size), created_at=CURRENT_TIMESTAMP
152
+ """, (key, len(item.data)))
153
+ await cur.execute("SELECT id FROM object_meta WHERE path=%s FOR UPDATE;", (key, ))
154
+ (obj_id, ) = await cur.fetchone()
155
+
156
+ blob = item.model_dump_json()
157
+ await cur.execute("REPLACE INTO object_data (id, data) VALUES (%s, %s)", (obj_id, blob))
158
+ await conn.commit()
159
+ except Exception:
160
+ await conn.rollback()
161
+ raise
162
+
163
+ @override
164
+ async def get_object(self, key: str) -> ObjectStoreItem:
165
+
166
+ if not self._conn_pool:
167
+ raise RuntimeError("Connection not established")
168
+
169
+ async with self._conn_pool.acquire() as conn:
170
+ async with conn.cursor() as cur:
171
+ await cur.execute(f"USE {self._schema};")
172
+ await cur.execute(
173
+ """
174
+ SELECT d.data
175
+ FROM object_data d
176
+ JOIN object_meta m USING(id)
177
+ WHERE m.path=%s
178
+ """, (key, ))
179
+ row = await cur.fetchone()
180
+ if not row:
181
+ raise NoSuchKeyError(
182
+ key=key, additional_message=f"MySQL table {self._config.bucket_name} does not have key {key}")
183
+ return ObjectStoreItem.model_validate_json(row[0].decode("utf-8"))
184
+
185
+ @override
186
+ async def delete_object(self, key: str):
187
+
188
+ if not self._conn_pool:
189
+ raise RuntimeError("Connection not established")
190
+
191
+ async with self._conn_pool.acquire() as conn:
192
+ async with conn.cursor() as cur:
193
+ try:
194
+ await cur.execute(f"USE {self._schema};")
195
+ await cur.execute(
196
+ """
197
+ DELETE m, d
198
+ FROM object_meta m
199
+ JOIN object_data d USING(id)
200
+ WHERE m.path=%s
201
+ """, (key, ))
202
+
203
+ if cur.rowcount == 0:
204
+ raise NoSuchKeyError(
205
+ key=key,
206
+ additional_message=f"MySQL table {self._config.bucket_name} does not have key {key}")
207
+
208
+ await conn.commit()
209
+ except Exception:
210
+ await conn.rollback()
211
+ raise
@@ -0,0 +1,66 @@
1
+ # SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ import os
17
+ from typing import ClassVar
18
+
19
+ from pydantic import Field
20
+
21
+ from nat.builder.builder import Builder
22
+ from nat.cli.register_workflow import register_object_store
23
+ from nat.data_models.object_store import ObjectStoreBaseConfig
24
+
25
+
26
+ class MySQLObjectStoreClientConfig(ObjectStoreBaseConfig, name="mysql"):
27
+ """
28
+ Object store that stores objects in a MySQL database.
29
+ """
30
+
31
+ DEFAULT_HOST: ClassVar[str] = "localhost"
32
+ DEFAULT_PORT: ClassVar[int] = 3306
33
+
34
+ HOST_ENV: ClassVar[str] = "NAT_MYSQL_OBJECT_STORE_HOST"
35
+ PORT_ENV: ClassVar[str] = "NAT_MYSQL_OBJECT_STORE_PORT"
36
+ USERNAME_ENV: ClassVar[str] = "NAT_MYSQL_OBJECT_STORE_USERNAME"
37
+ PASSWORD_ENV: ClassVar[str] = "NAT_MYSQL_OBJECT_STORE_PASSWORD"
38
+
39
+ bucket_name: str = Field(description="The name of the bucket to use for the object store")
40
+ host: str = Field(
41
+ default=os.environ.get(HOST_ENV, DEFAULT_HOST),
42
+ description="The host of the MySQL server"
43
+ " (uses {HOST_ENV} if unspecified; falls back to {DEFAULT_HOST})",
44
+ )
45
+ port: int = Field(
46
+ default=int(os.environ.get(PORT_ENV, DEFAULT_PORT)),
47
+ description="The port of the MySQL server"
48
+ " (uses {PORT_ENV} if unspecified; falls back to {DEFAULT_PORT})",
49
+ )
50
+ username: str | None = Field(
51
+ default=os.environ.get(USERNAME_ENV),
52
+ description=f"The username used to connect to the MySQL server (uses {USERNAME_ENV} if unspecifed)",
53
+ )
54
+ password: str | None = Field(
55
+ default=os.environ.get(PASSWORD_ENV),
56
+ description="The password used to connect to the MySQL server (uses {PASSWORD_ENV} if unspecifed)",
57
+ )
58
+
59
+
60
+ @register_object_store(config_type=MySQLObjectStoreClientConfig)
61
+ async def mysql_object_store_client(config: MySQLObjectStoreClientConfig, builder: Builder):
62
+
63
+ from .mysql_object_store import MySQLObjectStore
64
+
65
+ async with MySQLObjectStore(config) as store:
66
+ yield store
@@ -0,0 +1,22 @@
1
+ # SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ # pylint: disable=unused-import
17
+ # flake8: noqa
18
+ # isort:skip_file
19
+
20
+ # Import any providers which need to be automatically registered here
21
+
22
+ from . import object_store
@@ -0,0 +1,10 @@
1
+ Metadata-Version: 2.4
2
+ Name: nvidia-nat-mysql
3
+ Version: 1.2.0
4
+ Summary: Subpackage for MySQL integration in NeMo Agent toolkit
5
+ Keywords: ai,agents,memory,data store
6
+ Classifier: Programming Language :: Python
7
+ Requires-Python: <3.13,>=3.11
8
+ Description-Content-Type: text/markdown
9
+ Requires-Dist: nvidia-nat==v1.2.0
10
+ Requires-Dist: aiomysql>=0.2.0
@@ -0,0 +1,9 @@
1
+ nat/plugins/mysql/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ nat/plugins/mysql/mysql_object_store.py,sha256=wgsX797j21gw9ZFt9ZCrRHRT5GDrqPHywwFmc1HxdD4,8178
3
+ nat/plugins/mysql/object_store.py,sha256=IWMJFKBhn0lH7UGHuYkYht82TwIe5KXS7VUhQrlNRyI,2600
4
+ nat/plugins/mysql/register.py,sha256=7gqnwyDrYttIlEaa7lo9AASYt-2GrZJE0YT2jpKjepo,845
5
+ nvidia_nat_mysql-1.2.0.dist-info/METADATA,sha256=3wlxOk3yKivCZ-ztR0_sr9BYKN83HNP3tS-Yoe7tW1A,340
6
+ nvidia_nat_mysql-1.2.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
7
+ nvidia_nat_mysql-1.2.0.dist-info/entry_points.txt,sha256=ZI7ielmDX-k_OPXGvpRB5tKFVR5kMCHmHGwLWqxRKh0,56
8
+ nvidia_nat_mysql-1.2.0.dist-info/top_level.txt,sha256=8-CJ2cP6-f0ZReXe5Hzqp-5pvzzHz-5Ds5H2bGqh1-U,4
9
+ nvidia_nat_mysql-1.2.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.9.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [nat.components]
2
+ nat_mysql = nat.plugins.mysql.register
@@ -0,0 +1 @@
1
+ nat