|
1 | 1 | import redis.asyncio as redis |
2 | 2 | from .cluster import * |
3 | 3 | from .graph import AsyncGraph |
4 | | -from typing import List, Union |
| 4 | +from typing import List, Union, Optional |
5 | 5 |
|
6 | 6 | # config command |
| 7 | +UDF_CMD = "GRAPH.UDF" |
7 | 8 | LIST_CMD = "GRAPH.LIST" |
8 | 9 | CONFIG_CMD = "GRAPH.CONFIG" |
9 | 10 |
|
@@ -197,3 +198,93 @@ async def config_set(self, name: str, value=None) -> None: |
197 | 198 | """ |
198 | 199 |
|
199 | 200 | return await self.connection.execute_command(CONFIG_CMD, "SET", name, value) |
| 201 | + |
| 202 | + # GRAPH.UDF LOAD [REPLACE] <lib> <script> |
| 203 | + async def udf_load(self, name: str, script: str, replace: bool = False): |
| 204 | + """ |
| 205 | + Load a User Defined Function (UDF) library. |
| 206 | +
|
| 207 | + Args: |
| 208 | + name (str): The name of the library to load. |
| 209 | + script (str): The UDF script contents. |
| 210 | + replace (bool, optional): If True, replace an existing library with the same name. |
| 211 | + Defaults to False. |
| 212 | + """ |
| 213 | + |
| 214 | + # prep arguments |
| 215 | + args = [UDF_CMD, "LOAD"] |
| 216 | + if replace: |
| 217 | + args.append("REPLACE") |
| 218 | + args.extend([name, script]) |
| 219 | + |
| 220 | + # propagate command in cluster mode |
| 221 | + if Is_Cluster(self.connection): |
| 222 | + for node in self.connection.get_primaries(): |
| 223 | + # create a direct connection to this node |
| 224 | + client = self.connection.get_redis_connection(node) |
| 225 | + resp = await client.execute_command(*args) |
| 226 | + else: |
| 227 | + resp = await self.connection.execute_command(*args) |
| 228 | + |
| 229 | + return resp |
| 230 | + |
| 231 | + # GRAPH.UDF LIST [LIBRARYNAME] [WITHCODE] |
| 232 | + async def udf_list(self, lib: Optional[str] = None, with_code: bool = False): |
| 233 | + """ |
| 234 | + List User Defined Function (UDF) libraries. |
| 235 | +
|
| 236 | + Args: |
| 237 | + lib (str, optional): If provided, filter the list to this specific library. |
| 238 | + with_code (bool, optional): If True, include the library source code in the result. |
| 239 | + Defaults to False. |
| 240 | +
|
| 241 | + Returns: |
| 242 | + list: A list of UDF libraries and their metadata. |
| 243 | + """ |
| 244 | + |
| 245 | + args = [UDF_CMD, "LIST"] |
| 246 | + if lib is not None: |
| 247 | + args.append(lib) |
| 248 | + |
| 249 | + if with_code: |
| 250 | + args.append("WITHCODE") |
| 251 | + |
| 252 | + return await self.connection.execute_command(*args) |
| 253 | + |
| 254 | + # GRAPH.UDF FLUSH |
| 255 | + async def udf_flush(self): |
| 256 | + """ |
| 257 | + Flush (remove) all User Defined Function (UDF) libraries. |
| 258 | + """ |
| 259 | + |
| 260 | + # propagate command in cluster mode |
| 261 | + if Is_Cluster(self.connection): |
| 262 | + for node in self.connection.get_primaries(): |
| 263 | + # create a direct connection to this node |
| 264 | + client = self.connection.get_redis_connection(node) |
| 265 | + resp = await client.execute_command(UDF_CMD, "FLUSH") |
| 266 | + else: |
| 267 | + resp = await self.connection.execute_command(UDF_CMD, "FLUSH") |
| 268 | + |
| 269 | + return resp |
| 270 | + |
| 271 | + # GRAPH.UDF DELETE <lib> |
| 272 | + async def udf_delete(self, lib: str): |
| 273 | + """ |
| 274 | + Delete a User Defined Function (UDF) library. |
| 275 | +
|
| 276 | + Args: |
| 277 | + lib (str): The name of the library to delete. |
| 278 | + """ |
| 279 | + |
| 280 | + # propagate command in cluster mode |
| 281 | + if Is_Cluster(self.connection): |
| 282 | + for node in self.connection.get_primaries(): |
| 283 | + # create a direct connection to this node |
| 284 | + client = self.connection.get_redis_connection(node) |
| 285 | + resp = await client.execute_command(UDF_CMD, "DELETE", lib) |
| 286 | + else: |
| 287 | + resp = await self.connection.execute_command(UDF_CMD, "DELETE", lib) |
| 288 | + |
| 289 | + return resp |
| 290 | + |
0 commit comments