chore: add test coverage configuration and refactor KnowledgeStore to use persistent connection
This commit is contained in:
@@ -31,13 +31,13 @@ class KnowledgeEntry:
|
||||
class KnowledgeStore:
|
||||
def __init__(self, db_path: str):
|
||||
self.db_path = db_path
|
||||
self.conn = sqlite3.connect(self.db_path, check_same_thread=False)
|
||||
self.semantic_index = SemanticIndex()
|
||||
self._initialize_store()
|
||||
self._load_index()
|
||||
|
||||
def _initialize_store(self):
|
||||
conn = sqlite3.connect(self.db_path, check_same_thread=False)
|
||||
cursor = conn.cursor()
|
||||
cursor = self.conn.cursor()
|
||||
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS knowledge_entries (
|
||||
@@ -62,22 +62,17 @@ class KnowledgeStore:
|
||||
CREATE INDEX IF NOT EXISTS idx_created ON knowledge_entries(created_at DESC)
|
||||
''')
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
self.conn.commit()
|
||||
|
||||
def _load_index(self):
|
||||
conn = sqlite3.connect(self.db_path, check_same_thread=False)
|
||||
cursor = conn.cursor()
|
||||
cursor = self.conn.cursor()
|
||||
|
||||
cursor.execute('SELECT entry_id, content FROM knowledge_entries')
|
||||
for row in cursor.fetchall():
|
||||
self.semantic_index.add_document(row[0], row[1])
|
||||
|
||||
conn.close()
|
||||
|
||||
def add_entry(self, entry: KnowledgeEntry):
|
||||
conn = sqlite3.connect(self.db_path, check_same_thread=False)
|
||||
cursor = conn.cursor()
|
||||
cursor = self.conn.cursor()
|
||||
|
||||
cursor.execute('''
|
||||
INSERT OR REPLACE INTO knowledge_entries
|
||||
@@ -94,14 +89,12 @@ class KnowledgeStore:
|
||||
entry.importance_score
|
||||
))
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
self.conn.commit()
|
||||
|
||||
self.semantic_index.add_document(entry.entry_id, entry.content)
|
||||
|
||||
def get_entry(self, entry_id: str) -> Optional[KnowledgeEntry]:
|
||||
conn = sqlite3.connect(self.db_path, check_same_thread=False)
|
||||
cursor = conn.cursor()
|
||||
cursor = self.conn.cursor()
|
||||
|
||||
cursor.execute('''
|
||||
SELECT entry_id, category, content, metadata, created_at, updated_at, access_count, importance_score
|
||||
@@ -117,9 +110,7 @@ class KnowledgeStore:
|
||||
SET access_count = access_count + 1
|
||||
WHERE entry_id = ?
|
||||
''', (entry_id,))
|
||||
conn.commit()
|
||||
|
||||
conn.close()
|
||||
self.conn.commit()
|
||||
|
||||
return KnowledgeEntry(
|
||||
entry_id=row[0],
|
||||
@@ -132,15 +123,13 @@ class KnowledgeStore:
|
||||
importance_score=row[7]
|
||||
)
|
||||
|
||||
conn.close()
|
||||
return None
|
||||
|
||||
def search_entries(self, query: str, category: Optional[str] = None,
|
||||
top_k: int = 5) -> List[KnowledgeEntry]:
|
||||
search_results = self.semantic_index.search(query, top_k * 2)
|
||||
|
||||
conn = sqlite3.connect(self.db_path, check_same_thread=False)
|
||||
cursor = conn.cursor()
|
||||
cursor = self.conn.cursor()
|
||||
|
||||
entries = []
|
||||
for entry_id, score in search_results:
|
||||
@@ -174,12 +163,10 @@ class KnowledgeStore:
|
||||
if len(entries) >= top_k:
|
||||
break
|
||||
|
||||
conn.close()
|
||||
return entries
|
||||
|
||||
def get_by_category(self, category: str, limit: int = 20) -> List[KnowledgeEntry]:
|
||||
conn = sqlite3.connect(self.db_path, check_same_thread=False)
|
||||
cursor = conn.cursor()
|
||||
cursor = self.conn.cursor()
|
||||
|
||||
cursor.execute('''
|
||||
SELECT entry_id, category, content, metadata, created_at, updated_at, access_count, importance_score
|
||||
@@ -202,12 +189,10 @@ class KnowledgeStore:
|
||||
importance_score=row[7]
|
||||
))
|
||||
|
||||
conn.close()
|
||||
return entries
|
||||
|
||||
def update_importance(self, entry_id: str, importance_score: float):
|
||||
conn = sqlite3.connect(self.db_path, check_same_thread=False)
|
||||
cursor = conn.cursor()
|
||||
cursor = self.conn.cursor()
|
||||
|
||||
cursor.execute('''
|
||||
UPDATE knowledge_entries
|
||||
@@ -215,18 +200,15 @@ class KnowledgeStore:
|
||||
WHERE entry_id = ?
|
||||
''', (importance_score, time.time(), entry_id))
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
self.conn.commit()
|
||||
|
||||
def delete_entry(self, entry_id: str) -> bool:
|
||||
conn = sqlite3.connect(self.db_path, check_same_thread=False)
|
||||
cursor = conn.cursor()
|
||||
cursor = self.conn.cursor()
|
||||
|
||||
cursor.execute('DELETE FROM knowledge_entries WHERE entry_id = ?', (entry_id,))
|
||||
deleted = cursor.rowcount > 0
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
self.conn.commit()
|
||||
|
||||
if deleted:
|
||||
self.semantic_index.remove_document(entry_id)
|
||||
@@ -234,8 +216,7 @@ class KnowledgeStore:
|
||||
return deleted
|
||||
|
||||
def get_statistics(self) -> Dict[str, Any]:
|
||||
conn = sqlite3.connect(self.db_path, check_same_thread=False)
|
||||
cursor = conn.cursor()
|
||||
cursor = self.conn.cursor()
|
||||
|
||||
cursor.execute('SELECT COUNT(*) FROM knowledge_entries')
|
||||
total_entries = cursor.fetchone()[0]
|
||||
@@ -254,8 +235,6 @@ class KnowledgeStore:
|
||||
cursor.execute('SELECT SUM(access_count) FROM knowledge_entries')
|
||||
total_accesses = cursor.fetchone()[0] or 0
|
||||
|
||||
conn.close()
|
||||
|
||||
return {
|
||||
'total_entries': total_entries,
|
||||
'total_categories': total_categories,
|
||||
|
||||
Reference in New Issue
Block a user