|
1 | 1 | from typing import ( |
2 | 2 | Any, |
| 3 | + List, |
3 | 4 | NamedTuple, |
4 | 5 | Optional, |
5 | 6 | ) |
| 7 | +import posixpath |
6 | 8 |
|
7 | 9 | import ydb |
| 10 | +from ydb.retries import retry_operation_async |
| 11 | + |
| 12 | +from .utils import ( |
| 13 | + handle_ydb_errors, |
| 14 | +) |
8 | 15 |
|
9 | 16 | from .errors import ( |
10 | 17 | # InterfaceError, |
@@ -38,6 +45,8 @@ def __init__( |
38 | 45 | "ydb_table_path_prefix", "" |
39 | 46 | ) |
40 | 47 |
|
| 48 | + self.driver: ydb.aio.Driver = self.conn_kwargs.pop("ydb_driver", None) |
| 49 | + |
41 | 50 | self.session_pool: ydb.aio.QuerySessionPool = self.conn_kwargs.pop( |
42 | 51 | "ydb_session_pool", None |
43 | 52 | ) |
@@ -125,6 +134,48 @@ def get_isolation_level(self) -> str: |
125 | 134 | else: |
126 | 135 | raise NotSupportedError(f"{self.tx_mode.name} is not supported") |
127 | 136 |
|
| 137 | + @handle_ydb_errors |
| 138 | + async def describe(self, table_path: str) -> ydb.TableSchemeEntry: |
| 139 | + abs_table_path = posixpath.join( |
| 140 | + self.database, self.table_path_prefix, table_path |
| 141 | + ) |
| 142 | + return self.driver.table_client.describe_table(abs_table_path) |
| 143 | + |
| 144 | + @handle_ydb_errors |
| 145 | + async def check_exists(self, table_path: str) -> ydb.SchemeEntry: |
| 146 | + abs_table_path = posixpath.join( |
| 147 | + self.database, self.table_path_prefix, table_path |
| 148 | + ) |
| 149 | + return await self._check_path_exists(abs_table_path) |
| 150 | + |
| 151 | + @handle_ydb_errors |
| 152 | + async def get_table_names(self) -> List[str]: |
| 153 | + abs_dir_path = posixpath.join(self.database, self.table_path_prefix) |
| 154 | + names = await self._get_table_names(abs_dir_path) |
| 155 | + return [posixpath.relpath(path, abs_dir_path) for path in names] |
| 156 | + |
| 157 | + async def _check_path_exists(self, table_path: str) -> ydb.SchemeEntry: |
| 158 | + try: |
| 159 | + await retry_operation_async( |
| 160 | + self.driver.scheme_client.describe_path, table_path |
| 161 | + ) |
| 162 | + return True |
| 163 | + except ydb.SchemeError: |
| 164 | + return False |
| 165 | + |
| 166 | + async def _get_table_names(self, abs_dir_path: str) -> List[str]: |
| 167 | + directory = await retry_operation_async( |
| 168 | + self.driver.scheme_client.list_directory, abs_dir_path |
| 169 | + ) |
| 170 | + result = [] |
| 171 | + for child in directory.children: |
| 172 | + child_abs_path = posixpath.join(abs_dir_path, child.name) |
| 173 | + if child.is_table(): |
| 174 | + result.append(child_abs_path) |
| 175 | + elif child.is_directory() and not child.name.startswith("."): |
| 176 | + result.extend(self.get_table_names(child_abs_path)) |
| 177 | + return result |
| 178 | + |
128 | 179 |
|
129 | 180 | async def connect() -> Connection: |
130 | 181 | return Connection() |
0 commit comments