11import sqlite3
2- from dataclasses import asdict , replace
2+ from dataclasses import asdict
33from pathlib import Path
44from typing import Optional
55
@@ -51,30 +51,10 @@ def create(
5151 If no new settings are provided, it uses the default settings or load
5252 the settings used in the last execution."""
5353
54- conn = sqlite3 .connect (db_path )
55- conn .row_factory = sqlite3 .Row
54+ conn = Database .new_connection (db_path )
5655
57- # Load, initialize or update settings
5856 settings_manager = SettingsManager (conn )
59- current_settings = settings_manager .load_settings ()
60- if current_settings :
61- if settings :
62- settings = replace (current_settings , ** asdict (settings ))
63-
64- if settings_manager .has_critical_changes (settings , current_settings ):
65- raise ValueError (
66- "Critical settings changes detected. Please reset the database."
67- )
68- # Update new settings
69- current_settings = settings_manager .store (settings )
70- elif settings :
71- # Store initial settings with customs
72- settings = replace (Settings (), ** asdict (settings ))
73- current_settings = settings_manager .store (settings )
74- else :
75- # Store default settings
76- settings = Settings ()
77- current_settings = settings_manager .store (settings )
57+ current_settings = settings_manager .prepare_settings (settings )
7858
7959 Database .initialize (conn , current_settings )
8060
@@ -97,6 +77,8 @@ def add(
9777
9878 files_to_process = FileReader .collect_files (Path (path ), recursive = recursive )
9979
80+ self ._engine .create_new_context ()
81+
10082 processed = 0
10183 self ._logger .info (f"Processing { len (files_to_process )} files..." )
10284 for file_path in files_to_process :
@@ -138,6 +120,7 @@ def add_text(
138120 self ._ensure_initialized ()
139121
140122 document = Document (content = text , uri = uri , metadata = metadata )
123+ self ._engine .create_new_context ()
141124 document = self ._engine .process (document )
142125
143126 self ._repository .add_document (document )
@@ -177,6 +160,8 @@ def rebuild(self, remove_missing: bool = False) -> dict:
177160 not_found = 0
178161 removed = 0
179162
163+ self ._engine .create_new_context ()
164+
180165 for doc in documents :
181166 doc_id = doc .id or ""
182167
@@ -218,8 +203,8 @@ def rebuild(self, remove_missing: bool = False) -> dict:
218203 except Exception as e :
219204 self ._logger .error (f"Error processing text document { doc .id } : { e } " )
220205
221- if self ._settings .quantize_scan :
222- self ._engine .quantize ()
206+ if self ._settings .quantize_scan :
207+ self ._engine .quantize ()
223208
224209 return {
225210 "total" : total_docs ,
@@ -247,9 +232,13 @@ def reset(self) -> bool:
247232 self ._logger .error (f"Error during database reset: { e } " )
248233 return False
249234
250- def search (self , query : str , top_k : int = 10 ) -> list [DocumentResult ]:
235+ def search (
236+ self , query : str , top_k : int = 10 , new_context : bool = True
237+ ) -> list [DocumentResult ]:
251238 """Search for documents matching the query"""
252239 self ._ensure_initialized ()
240+ if new_context :
241+ self ._engine .create_new_context ()
253242
254243 if self ._settings .quantize_preload :
255244 self ._engine .quantize_preload ()
0 commit comments