-
Notifications
You must be signed in to change notification settings - Fork 49
perf(graph): batch DB operations for entity insertion #624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: staging
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,6 @@ | ||||||||||||||
| import os | ||||||||||||||
| import time | ||||||||||||||
| from collections import defaultdict | ||||||||||||||
| from .entities import * | ||||||||||||||
| from typing import Optional | ||||||||||||||
| from falkordb import FalkorDB, Path, Node, QueryResult | ||||||||||||||
|
|
@@ -267,6 +268,42 @@ def add_entity(self, label: str, name: str, doc: str, path: str, src_start: int, | |||||||||||||
| node = res.result_set[0][0] | ||||||||||||||
| return node.id | ||||||||||||||
|
|
||||||||||||||
| def add_entities_batch(self, entities_data: list) -> None: | ||||||||||||||
| """ | ||||||||||||||
| Batch add entity nodes to the graph database using UNWIND. | ||||||||||||||
| Reduces N individual queries to one query per entity label. | ||||||||||||||
|
|
||||||||||||||
| Args: | ||||||||||||||
| entities_data: list of tuples | ||||||||||||||
| (entity_obj, label, name, doc, path, src_start, src_end, props) | ||||||||||||||
| entity_obj.id will be set after insertion. | ||||||||||||||
| """ | ||||||||||||||
|
|
||||||||||||||
| if not entities_data: | ||||||||||||||
| return | ||||||||||||||
|
|
||||||||||||||
| by_label = defaultdict(list) | ||||||||||||||
| for item in entities_data: | ||||||||||||||
| by_label[item[1]].append(item) | ||||||||||||||
|
|
||||||||||||||
| for label, group in by_label.items(): | ||||||||||||||
| data = [{ | ||||||||||||||
| 'name': item[2], 'doc': item[3], 'path': item[4], | ||||||||||||||
| 'src_start': item[5], 'src_end': item[6], 'props': item[7] | ||||||||||||||
| } for item in group] | ||||||||||||||
|
|
||||||||||||||
| q = f"""UNWIND $entities AS e | ||||||||||||||
| MERGE (c:{label}:Searchable {{name: e['name'], path: e['path'], | ||||||||||||||
| src_start: e['src_start'], | ||||||||||||||
| src_end: e['src_end']}}) | ||||||||||||||
| SET c.doc = e['doc'] | ||||||||||||||
| SET c += e['props'] | ||||||||||||||
| RETURN c""" | ||||||||||||||
|
Comment on lines
+303
to
+309
|
||||||||||||||
|
|
||||||||||||||
| res = self._query(q, {'entities': data}) | ||||||||||||||
| for j, item in enumerate(group): | ||||||||||||||
| item[0].id = res.result_set[j][0].id | ||||||||||||||
|
|
||||||||||||||
| def get_class_by_name(self, class_name: str) -> Optional[Node]: | ||||||||||||||
| q = "MATCH (c:Class) WHERE c.name = $name RETURN c LIMIT 1" | ||||||||||||||
| res = self._query(q, {'name': class_name}).result_set | ||||||||||||||
|
|
@@ -406,6 +443,29 @@ def add_file(self, file: File) -> None: | |||||||||||||
| node = res.result_set[0][0] | ||||||||||||||
| file.id = node.id | ||||||||||||||
|
|
||||||||||||||
| def add_files_batch(self, files: list[File]) -> None: | ||||||||||||||
| """ | ||||||||||||||
| Batch add file nodes to the graph database using UNWIND. | ||||||||||||||
| Reduces N individual queries to a single query. | ||||||||||||||
|
|
||||||||||||||
| Args: | ||||||||||||||
| files: list of File objects. Each file.id will be set after insertion. | ||||||||||||||
| """ | ||||||||||||||
|
|
||||||||||||||
| if not files: | ||||||||||||||
| return | ||||||||||||||
|
|
||||||||||||||
| file_data = [{'path': str(f.path), 'name': f.path.name, 'ext': f.path.suffix} | ||||||||||||||
| for f in files] | ||||||||||||||
|
|
||||||||||||||
| q = """UNWIND $files AS fd | ||||||||||||||
| MERGE (f:File:Searchable {path: fd['path'], name: fd['name'], ext: fd['ext']}) | ||||||||||||||
| RETURN f""" | ||||||||||||||
|
|
||||||||||||||
| res = self._query(q, {'files': file_data}) | ||||||||||||||
| for i, row in enumerate(res.result_set): | ||||||||||||||
| files[i].id = row[0].id | ||||||||||||||
|
|
||||||||||||||
| def delete_files(self, files: list[Path]) -> tuple[str, dict, list[int]]: | ||||||||||||||
| """ | ||||||||||||||
| Deletes file(s) from the graph in addition to any other entity | ||||||||||||||
|
|
@@ -485,6 +545,35 @@ def connect_entities(self, relation: str, src_id: int, dest_id: int, properties: | |||||||||||||
| params = {'src_id': src_id, 'dest_id': dest_id, "properties": properties} | ||||||||||||||
| self._query(q, params) | ||||||||||||||
|
|
||||||||||||||
| def connect_entities_batch(self, relationships: list[tuple[str, int, int, dict]]) -> None: | ||||||||||||||
| """ | ||||||||||||||
| Batch create relationships between entities using UNWIND. | ||||||||||||||
| Reduces K individual queries to one query per relationship type. | ||||||||||||||
|
|
||||||||||||||
| Args: | ||||||||||||||
| relationships: list of (relation, src_id, dest_id, properties) | ||||||||||||||
| """ | ||||||||||||||
|
|
||||||||||||||
| if not relationships: | ||||||||||||||
| return | ||||||||||||||
|
|
||||||||||||||
| by_relation = defaultdict(list) | ||||||||||||||
| for rel in relationships: | ||||||||||||||
| by_relation[rel[0]].append(rel) | ||||||||||||||
|
|
||||||||||||||
| for relation, group in by_relation.items(): | ||||||||||||||
| data = [{'src_id': r[1], 'dest_id': r[2], 'properties': r[3]} | ||||||||||||||
| for r in group] | ||||||||||||||
|
|
||||||||||||||
| q = f"""UNWIND $rels AS r | ||||||||||||||
| MATCH (src), (dest) | ||||||||||||||
| WHERE ID(src) = r['src_id'] AND ID(dest) = r['dest_id'] | ||||||||||||||
|
||||||||||||||
| MATCH (src), (dest) | |
| WHERE ID(src) = r['src_id'] AND ID(dest) = r['dest_id'] | |
| MATCH (src) | |
| WHERE ID(src) = r['src_id'] | |
| MATCH (dest) | |
| WHERE ID(dest) = r['dest_id'] |
Copilot
AI
Mar 20, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
connect_entities_batch always uses MERGE (src)-[e:REL]->(dest) and then SET e += properties. For relationship types where multiple edges between the same pair are meaningful (e.g., multiple CALL sites between the same functions), this will collapse them into a single edge and later rows will overwrite earlier properties. Consider using CREATE for those relation types, or MERGE on a uniqueness key that includes the distinguishing properties (like line/pos).
| MERGE (src)-[e:{relation}]->(dest) | |
| CREATE (src)-[e:{relation}]->(dest) |
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -69,5 +69,75 @@ def test_function_calls_function(self): | |||
| res = self.g.query(query, params).result_set | ||||
| self.assertTrue(res[0][0]) | ||||
|
|
||||
| def test_add_files_batch(self): | ||||
| files = [File(Path(f'/batch/file{i}.py'), None) for i in range(5)] | ||||
| self.graph.add_files_batch(files) | ||||
|
|
||||
| for i, f in enumerate(files): | ||||
| self.assertIsNotNone(f.id) | ||||
| result = self.graph.get_file(f'/batch/file{i}.py', f'file{i}.py', '.py') | ||||
| self.assertIsNotNone(result) | ||||
| self.assertEqual(result.properties['name'], f'file{i}.py') | ||||
|
|
||||
| def test_add_files_batch_empty(self): | ||||
| self.graph.add_files_batch([]) | ||||
|
|
||||
| def test_add_entities_batch(self): | ||||
| from api.entities.entity import Entity | ||||
|
||||
| from api.entities.entity import Entity |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
any([ ... for ... ])builds an intermediate list on every file;any(ig in str(file_path) for ig in ignore)avoids the allocation and is equivalent.