2020 create_communities ,
2121)
2222from graphrag_storage .tables .csv_table import CSVTable
23+ from graphrag_storage .tables .table import Table
2324
2425
2526class FakeTable (CSVTable ):
@@ -33,15 +34,55 @@ async def write(self, row: dict[str, Any]) -> None:
3334 self .rows .append (row )
3435
3536
37+ class FakeEntitiesTable (Table ):
38+ """In-memory read-only table that supports async iteration."""
39+
40+ def __init__ (self , rows : list [dict [str , Any ]]) -> None :
41+ self ._rows = rows
42+ self ._index = 0
43+
44+ def __aiter__ (self ):
45+ """Return an async iterator over the rows."""
46+ self ._index = 0
47+ return self
48+
49+ async def __anext__ (self ) -> dict [str , Any ]:
50+ """Yield the next row or stop."""
51+ if self ._index >= len (self ._rows ):
52+ raise StopAsyncIteration
53+ row = self ._rows [self ._index ]
54+ self ._index += 1
55+ return row
56+
57+ async def length (self ) -> int :
58+ """Return number of rows."""
59+ return len (self ._rows )
60+
61+ async def has (self , row_id : str ) -> bool :
62+ """Check if a row with the given ID exists."""
63+ return any (r .get ("id" ) == row_id for r in self ._rows )
64+
65+ async def write (self , row : dict [str , Any ]) -> None :
66+ """Not supported for read-only table."""
67+ raise NotImplementedError
68+
69+ async def close (self ) -> None :
70+ """No-op."""
71+
72+
3673async def _run_create_communities (
3774 title_to_entity_id : dict [str , str ],
3875 relationships : pd .DataFrame ,
3976 ** kwargs : Any ,
4077) -> pd .DataFrame :
41- """Helper that runs create_communities with a FakeTable and returns all rows as a DataFrame."""
42- table = FakeTable ()
43- await create_communities (table , title_to_entity_id , relationships , ** kwargs )
44- return pd .DataFrame (table .rows )
78+ """Helper that runs create_communities with fake tables and returns all rows as a DataFrame."""
79+ communities_table = FakeTable ()
80+ entity_rows = [
81+ {"id" : eid , "title" : title } for title , eid in title_to_entity_id .items ()
82+ ]
83+ entities_table = FakeEntitiesTable (entity_rows )
84+ await create_communities (communities_table , entities_table , relationships , ** kwargs )
85+ return pd .DataFrame (communities_table .rows )
4586
4687
4788def _make_title_to_entity_id (
0 commit comments