Merge pull request #136 from igorls/fix/kg-hardening
fix: enable SQLite WAL mode and add consistent LIMIT to KG timeline
This commit is contained in:
@@ -87,7 +87,9 @@ class KnowledgeGraph:
|
|||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
def _conn(self):
|
def _conn(self):
|
||||||
return sqlite3.connect(self.db_path, timeout=10)
|
conn = sqlite3.connect(self.db_path, timeout=10)
|
||||||
|
conn.execute("PRAGMA journal_mode=WAL")
|
||||||
|
return conn
|
||||||
|
|
||||||
def _entity_id(self, name: str) -> str:
|
def _entity_id(self, name: str) -> str:
|
||||||
return name.lower().replace(" ", "_").replace("'", "")
|
return name.lower().replace(" ", "_").replace("'", "")
|
||||||
@@ -284,6 +286,7 @@ class KnowledgeGraph:
|
|||||||
JOIN entities o ON t.object = o.id
|
JOIN entities o ON t.object = o.id
|
||||||
WHERE (t.subject = ? OR t.object = ?)
|
WHERE (t.subject = ? OR t.object = ?)
|
||||||
ORDER BY t.valid_from ASC NULLS LAST
|
ORDER BY t.valid_from ASC NULLS LAST
|
||||||
|
LIMIT 100
|
||||||
""",
|
""",
|
||||||
(eid, eid),
|
(eid, eid),
|
||||||
).fetchall()
|
).fetchall()
|
||||||
|
|||||||
@@ -107,6 +107,23 @@ class TestTimeline:
|
|||||||
tl = kg.timeline()
|
tl = kg.timeline()
|
||||||
assert len(tl) == 100 # LIMIT 100
|
assert len(tl) == 100 # LIMIT 100
|
||||||
|
|
||||||
|
def test_timeline_entity_has_limit(self, kg):
|
||||||
|
# Add > 100 triples all connected to a single entity
|
||||||
|
for i in range(105):
|
||||||
|
kg.add_triple(
|
||||||
|
"hub", "connects_to", f"spoke_{i}", valid_from=f"2025-01-{(i % 28) + 1:02d}"
|
||||||
|
)
|
||||||
|
tl = kg.timeline("hub")
|
||||||
|
assert len(tl) == 100 # LIMIT 100 on entity-filtered branch
|
||||||
|
|
||||||
|
|
||||||
|
class TestWALMode:
|
||||||
|
def test_wal_mode_enabled(self, kg):
|
||||||
|
conn = kg._conn()
|
||||||
|
mode = conn.execute("PRAGMA journal_mode").fetchone()[0]
|
||||||
|
conn.close()
|
||||||
|
assert mode == "wal"
|
||||||
|
|
||||||
|
|
||||||
class TestStats:
|
class TestStats:
|
||||||
def test_stats_empty(self, kg):
|
def test_stats_empty(self, kg):
|
||||||
|
|||||||
Reference in New Issue
Block a user