Skip to content

Commit 4afc53a

Browse files
committed
graph backlog
1 parent f56a6d0 commit 4afc53a

File tree

1 file changed

+50
-6
lines changed

1 file changed

+50
-6
lines changed

code_graph/graph.py

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@ class Graph():
3131

3232
def __init__(self, name: str) -> None:
3333
self.name = name
34-
self.db = FalkorDB(host=os.getenv('FALKORDB_HOST', 'localhost'),
35-
port=os.getenv('FALKORDB_PORT', 6379),
36-
username=os.getenv('FALKORDB_USERNAME', None),
37-
password=os.getenv('FALKORDB_PASSWORD', None))
38-
self.g = self.db.select_graph(name)
34+
self.db = FalkorDB(host=os.getenv('FALKORDB_HOST', 'localhost'),
35+
port=os.getenv('FALKORDB_PORT', 6379),
36+
username=os.getenv('FALKORDB_USERNAME', None),
37+
password=os.getenv('FALKORDB_PASSWORD', None))
38+
self.g = self.db.select_graph(name)
39+
40+
# Initialize the backlog as disabled by default
41+
self.backlog = None
3942

4043
# create indicies
4144

@@ -85,9 +88,50 @@ def delete(self) -> None:
8588
"""
8689
self.g.delete()
8790

91+
def enable_backlog(self) -> None:
92+
"""
93+
Enables the backlog by initializing an empty list.
94+
"""
95+
96+
self.backlog = []
97+
98+
def disable_backlog(self) -> None:
99+
"""
100+
Disables the backlog by setting it to None.
101+
"""
102+
103+
self.backlog = None
88104

89105
def _query(self, q: str, params: dict) -> QueryResult:
90-
return self.g.query(q, params)
106+
"""
107+
Executes a query on the graph database and logs changes to the backlog if any.
108+
109+
Args:
110+
q (str): The query string to execute.
111+
params (dict): The parameters for the query.
112+
113+
Returns:
114+
QueryResult: The result of the query execution.
115+
"""
116+
117+
result_set = self.g.query(q, params)
118+
119+
if self.backlog is not None:
120+
# Check if any change occurred in the query results
121+
change_detected = any(
122+
getattr(result_set, attr) > 0
123+
for attr in [
124+
'relationships_deleted', 'nodes_deleted', 'labels_added',
125+
'labels_removed', 'nodes_created', 'properties_set',
126+
'properties_removed', 'relationships_created'
127+
]
128+
)
129+
130+
# Append the query and parameters to the backlog if changes occurred
131+
if change_detected:
132+
self.backlog.append((q, params))
133+
134+
return result_set
91135

92136
def get_sub_graph(self, l: int) -> dict:
93137

0 commit comments

Comments
 (0)