Skip to content

Commit c596814

Browse files
fix: add robust handling for getting nodes or edges in kuzudb
1 parent 658397d commit c596814

File tree

1 file changed

+63
-14
lines changed

1 file changed

+63
-14
lines changed

graphgen/models/storage/graph/kuzu_storage.py

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,18 @@ def get_node(self, node_id: str) -> Any:
111111
result = self._conn.execute(
112112
"MATCH (a:Entity {id: $id}) RETURN a.data", {"id": node_id}
113113
)
114-
if result.has_next():
115-
data_str = result.get_next()[0]
116-
return json.loads(data_str) if data_str else {}
117-
return None
114+
if not result.has_next():
115+
return None
116+
117+
data_str = result.get_next()[0]
118+
119+
if not isinstance(data_str, str) or not data_str.strip():
120+
return {}
121+
try:
122+
return json.loads(data_str)
123+
except json.JSONDecodeError as e:
124+
print(f"Error decoding JSON for node {node_id}: {e}")
125+
return None
118126

119127
def update_node(self, node_id: str, node_data: dict[str, str]):
120128
current_data = self.get_node(node_id)
@@ -137,7 +145,13 @@ def get_all_nodes(self) -> Any:
137145
nodes = []
138146
while result.has_next():
139147
row = result.get_next()
140-
nodes.append((row[0], json.loads(row[1])))
148+
node_id, data_str = row[0], row[1]
149+
try:
150+
data = json.loads(data_str) if data_str and data_str.strip() else {}
151+
except json.JSONDecodeError as e:
152+
print(f"Error decoding JSON for node {node_id}: {e}")
153+
continue
154+
nodes.append((node_id, data))
141155
return nodes
142156

143157
def get_edge(self, source_node_id: str, target_node_id: str):
@@ -149,10 +163,21 @@ def get_edge(self, source_node_id: str, target_node_id: str):
149163
result = self._conn.execute(
150164
query, {"src": source_node_id, "dst": target_node_id}
151165
)
152-
if result.has_next():
153-
data_str = result.get_next()[0]
154-
return json.loads(data_str) if data_str else {}
155-
return None
166+
if not result.has_next():
167+
return None
168+
169+
data_str = result.get_next()[0]
170+
171+
if not isinstance(data_str, str) or not data_str.strip():
172+
return {}
173+
174+
try:
175+
return json.loads(data_str)
176+
except json.JSONDecodeError as e:
177+
print(
178+
f"Error decoding JSON for edge {source_node_id}->{target_node_id}: {e}"
179+
)
180+
return None
156181

157182
def update_edge(
158183
self, source_node_id: str, target_node_id: str, edge_data: dict[str, str]
@@ -180,7 +205,13 @@ def get_all_edges(self) -> Any:
180205
edges = []
181206
while result.has_next():
182207
row = result.get_next()
183-
edges.append((row[0], row[1], json.loads(row[2])))
208+
src, dst, data_str = row[0], row[1], row[2]
209+
try:
210+
data = json.loads(data_str) if data_str and data_str.strip() else {}
211+
except json.JSONDecodeError as e:
212+
print(f"Error decoding JSON for edge {src}->{dst}: {e}")
213+
continue
214+
edges.append((src, dst, data))
184215
return edges
185216

186217
def get_node_edges(self, source_node_id: str) -> Any:
@@ -193,7 +224,13 @@ def get_node_edges(self, source_node_id: str) -> Any:
193224
edges = []
194225
while result.has_next():
195226
row = result.get_next()
196-
edges.append((row[0], row[1], json.loads(row[2])))
227+
src, dst, data_str = row[0], row[1], row[2]
228+
try:
229+
data = json.loads(data_str) if data_str and data_str.strip() else {}
230+
except json.JSONDecodeError as e:
231+
print(f"Error decoding JSON for edge {src}->{dst}: {e}")
232+
continue
233+
edges.append((src, dst, data))
197234
return edges
198235

199236
def upsert_node(self, node_id: str, node_data: dict[str, str]):
@@ -250,7 +287,19 @@ def reload(self):
250287
"""For databases that need reloading, KuzuDB auto-manages this."""
251288

252289
def drop(self):
253-
"""Completely remove the database folder."""
254-
if self.db_path and os.path.exists(self.db_path):
290+
if not self.db_path:
291+
return
292+
self._conn = None
293+
self._db = None
294+
295+
if os.path.isdir(self.db_path):
255296
shutil.rmtree(self.db_path)
256-
print(f"Dropped KuzuDB at {self.db_path}")
297+
print(f"Dropped KuzuDB directory at {self.db_path}")
298+
elif os.path.isfile(self.db_path):
299+
os.remove(self.db_path)
300+
print(f"Dropped KuzuDB file at {self.db_path}")
301+
elif os.path.exists(self.db_path):
302+
os.unlink(self.db_path)
303+
print(f"Dropped KuzuDB path at {self.db_path}")
304+
else:
305+
print(f"Database path {self.db_path} does not exist")

0 commit comments

Comments
 (0)