refactor: keep only count_nodes

This commit is contained in:
Daulet Amirkhanov 2025-10-15 17:20:45 +01:00
parent dede5fa6fd
commit 9e38a30c49
7 changed files with 10 additions and 42 deletions

View file

@ -177,10 +177,9 @@ async def search(
raise DatasetNotFoundError(message="No datasets found.")
graph_engine = await get_graph_engine()
edges_count = await graph_engine.count_edges()
nodes_count = await graph_engine.count_nodes()
if nodes_count == 0 or edges_count == 0:
if nodes_count == 0:
raise SearchOnEmptyGraphError(
message="Knowledge graph is empty, please ensure data is added and cognified."
)

View file

@ -163,10 +163,6 @@ class GraphDBInterface(ABC):
async def count_nodes(self) -> int:
raise NotImplementedError
@abstractmethod
async def count_edges(self) -> int:
raise NotImplementedError
@abstractmethod
async def query(self, query: str, params: dict) -> List[Any]:
"""

View file

@ -185,14 +185,6 @@ class KuzuAdapter(GraphDBInterface):
except FileNotFoundError:
logger.warning(f"Kuzu S3 storage file not found: {self.db_path}")
async def count_edges(self) -> int:
query = """
MATCH ()-[r]->()
RETURN COUNT(r);
"""
query_result = await self.query(query)
return query_result[0][0]
async def count_nodes(self) -> int:
query = """
MATCH (n)

View file

@ -87,14 +87,6 @@ class Neo4jAdapter(GraphDBInterface):
async with self.driver.session(database=self.graph_database_name) as session:
yield session
async def count_edges(self) -> int:
query = """
MATCH ()-[r]->()
RETURN COUNT(r) as total_edges;
"""
query_result = await self.query(query)
return query_result[0]["total_edges"]
async def count_nodes(self) -> int:
query = """
MATCH (n)

View file

@ -51,26 +51,21 @@ async def main():
graph_engine = await get_graph_engine()
edges_count = await graph_engine.count_edges()
nodes_count = await graph_engine.count_nodes()
assert edges_count == 0 and nodes_count == 0, "Kuzu graph database is not empty"
assert nodes_count == 0, "Kuzu graph database is not empty"
await cognee.add([explanation_file_path_quantum], dataset_name)
edges_count = await graph_engine.count_edges()
nodes_count = await graph_engine.count_nodes()
assert edges_count == 0 and nodes_count == 0, (
"Kuzu graph database should be empty before cognify"
)
assert nodes_count == 0, "Kuzu graph database should be empty before cognify"
await cognee.cognify([dataset_name])
edges_count = await graph_engine.count_edges()
nodes_count = await graph_engine.count_nodes()
assert edges_count != 0 and nodes_count != 0, "Kuzu graph database should not be empty"
assert nodes_count != 0, "Kuzu graph database should not be empty"
from cognee.infrastructure.databases.vector import get_vector_engine
@ -136,10 +131,9 @@ async def main():
await cognee.prune.prune_system(metadata=True)
edges_count = await graph_engine.count_edges()
nodes_count = await graph_engine.count_nodes()
assert edges_count == 0 and nodes_count == 0, "Kuzu graph database is not empty"
assert nodes_count == 0, "Kuzu graph database is not empty"
finally:
# Ensure cleanup even if tests fail

View file

@ -39,10 +39,9 @@ async def main():
graph_engine = await get_graph_engine()
edges_count = await graph_engine.count_edges()
nodes_count = await graph_engine.count_nodes()
assert edges_count == 0 and nodes_count == 0, "Graph has to be empty"
assert nodes_count == 0, "Graph has to be empty"
await cognee.add([explanation_file_path_nlp], dataset_name)
@ -51,18 +50,15 @@ async def main():
)
await cognee.add([explanation_file_path_quantum], dataset_name)
edges_count = await graph_engine.count_edges()
nodes_count = await graph_engine.count_nodes()
assert edges_count == 0 and nodes_count == 0, "Graph has to be empty before cognify"
assert nodes_count == 0, "Graph has to be empty before cognify"
await cognee.cognify([dataset_name])
edges_count = await graph_engine.count_edges()
nodes_count = await graph_engine.count_nodes()
assert edges_count != 0 and nodes_count != 0, "Graph shouldn't be empty"
assert nodes_count != 0, "Graph shouldn't be empty"
from cognee.infrastructure.databases.vector import get_vector_engine
@ -136,9 +132,8 @@ async def main():
assert not os.path.isdir(data_root_directory), "Local data files are not deleted"
await cognee.prune.prune_system(metadata=True)
edges_count = await graph_engine.count_edges()
nodes_count = await graph_engine.count_nodes()
assert nodes_count == 0 and edges_count == 0, "Neo4j graph database is not empty"
assert nodes_count == 0, "Neo4j graph database is not empty"
if __name__ == "__main__":

View file

@ -199,7 +199,7 @@ if __name__ == "__main__":
"prune_data": rebuild_kg,
"prune_system": rebuild_kg,
"add_text": rebuild_kg,
"cognify": rebuild_kg,
"cognify": False,
"graph_metrics": rebuild_kg,
"retriever": retrieve,
}