Skip to content

Commit 4c72423

Browse files
authored
Merge pull request #141 from FalkorDB/propagate-udf
propagate udf commands
2 parents 65afbdd + 0170a8d commit 4c72423

3 files changed

Lines changed: 132 additions & 6 deletions

File tree

falkordb/asyncio/falkordb.py

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import redis.asyncio as redis
22
from .cluster import *
33
from .graph import AsyncGraph
4-
from typing import List, Union
4+
from typing import List, Union, Optional
55

66
# config command
7+
UDF_CMD = "GRAPH.UDF"
78
LIST_CMD = "GRAPH.LIST"
89
CONFIG_CMD = "GRAPH.CONFIG"
910

@@ -197,3 +198,93 @@ async def config_set(self, name: str, value=None) -> None:
197198
"""
198199

199200
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+

falkordb/falkordb.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from typing import List, Union, Optional
66

77
# config commands
8+
UDF_CMD = "GRAPH.UDF"
89
LIST_CMD = "GRAPH.LIST"
910
CONFIG_CMD = "GRAPH.CONFIG"
10-
UDF_CMD = "GRAPH.UDF"
1111

1212
class FalkorDB:
1313
"""
@@ -249,11 +249,23 @@ def udf_load(self, name: str, script: str, replace: bool = False):
249249
replace (bool, optional): If True, replace an existing library with the same name.
250250
Defaults to False.
251251
"""
252+
253+
# prep arguments
252254
args = [UDF_CMD, "LOAD"]
253255
if replace:
254256
args.append("REPLACE")
255257
args.extend([name, script])
256-
return self.connection.execute_command(*args)
258+
259+
# propagate command in cluster mode
260+
if Is_Cluster(self.connection):
261+
for node in self.connection.get_primaries():
262+
# create a direct connection to this node
263+
client = self.connection.get_redis_connection(node)
264+
resp = client.execute_command(*args)
265+
else:
266+
resp = self.connection.execute_command(*args)
267+
268+
return resp
257269

258270
# GRAPH.UDF LIST [LIBRARYNAME] [WITHCODE]
259271
def udf_list(self, lib: Optional[str] = None, with_code: bool = False):
@@ -268,19 +280,32 @@ def udf_list(self, lib: Optional[str] = None, with_code: bool = False):
268280
Returns:
269281
list: A list of UDF libraries and their metadata.
270282
"""
283+
271284
args = [UDF_CMD, "LIST"]
272285
if lib is not None:
273286
args.append(lib)
287+
274288
if with_code:
275289
args.append("WITHCODE")
290+
276291
return self.connection.execute_command(*args)
277292

278293
# GRAPH.UDF FLUSH
279294
def udf_flush(self):
280295
"""
281296
Flush (remove) all User Defined Function (UDF) libraries.
282297
"""
283-
return self.connection.execute_command(UDF_CMD, "FLUSH")
298+
299+
# propagate command in cluster mode
300+
if Is_Cluster(self.connection):
301+
for node in self.connection.get_primaries():
302+
# create a direct connection to this node
303+
client = self.connection.get_redis_connection(node)
304+
resp = client.execute_command(UDF_CMD, "FLUSH")
305+
else:
306+
resp = self.connection.execute_command(UDF_CMD, "FLUSH")
307+
308+
return resp
284309

285310
# GRAPH.UDF DELETE <lib>
286311
def udf_delete(self, lib: str):
@@ -290,5 +315,15 @@ def udf_delete(self, lib: str):
290315
Args:
291316
lib (str): The name of the library to delete.
292317
"""
293-
return self.connection.execute_command(UDF_CMD, "DELETE", lib)
318+
319+
# propagate command in cluster mode
320+
if Is_Cluster(self.connection):
321+
for node in self.connection.get_primaries():
322+
# create a direct connection to this node
323+
client = self.connection.get_redis_connection(node)
324+
resp = client.execute_command(UDF_CMD, "DELETE", lib)
325+
else:
326+
resp = self.connection.execute_command(UDF_CMD, "DELETE", lib)
327+
328+
return resp
294329

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "FalkorDB"
3-
version = "1.2.1"
3+
version = "1.2.2"
44
description = "Python client for interacting with FalkorDB database"
55
authors = ["FalkorDB inc <info@falkordb.com>"]
66

0 commit comments

Comments
 (0)