Skip to content

Commit 694eb95

Browse files
CopilotMte90
andcommitted
Add logging to DB operations and vector operations
Co-authored-by: Mte90 <403283+Mte90@users.noreply.github.com>
1 parent 8dbe465 commit 694eb95

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

db/db_writer.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def __init__(self, database_path, timeout_seconds=30):
2525
self._thread = threading.Thread(target=self._worker, daemon=True, name=f"DBWriter-{database_path}")
2626
self._timeout_seconds = timeout_seconds
2727
self._thread.start()
28+
_LOG.info(f"DBWriter started for database: {database_path}")
2829

2930
def _open_conn(self):
3031
conn = sqlite3.connect(self.database_path, timeout=self._timeout_seconds, check_same_thread=False)
@@ -34,6 +35,7 @@ def _open_conn(self):
3435
conn.execute("PRAGMA busy_timeout = 30000;")
3536
# Optional: balance durability and performance
3637
conn.execute("PRAGMA synchronous = NORMAL;")
38+
_LOG.debug(f"Database connection opened for: {self.database_path}")
3739
return conn
3840

3941
def _worker(self):
@@ -98,11 +100,16 @@ def enqueue_no_wait(self, sql, params):
98100

99101
def stop(self, wait=True):
100102
"""Stop the worker thread. If wait=True, block until thread joins."""
103+
_LOG.info(f"Stopping DBWriter for database: {self.database_path}")
101104
self._stop.set()
102105
# enqueue sentinel for immediate exit
103106
self._q.put(None)
104107
if wait:
105108
self._thread.join(timeout=5.0)
109+
if self._thread.is_alive():
110+
_LOG.warning(f"DBWriter thread for {self.database_path} did not stop within 5s")
111+
else:
112+
_LOG.info(f"DBWriter stopped for database: {self.database_path}")
106113

107114

108115
def get_writer(database_path):

db/vector_operations.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,17 @@ def insert_chunk_vector_with_retry(conn: sqlite3.Connection, file_id: int, path:
156156
dim = len(vector)
157157
if not row:
158158
set_vector_dimension(conn, dim)
159+
logger.info(f"Initialized vector dimension: {dim}")
159160
try:
160161
conn.execute(f"SELECT vector_init('chunks', 'embedding', 'dimension={dim},type=FLOAT32,distance=COSINE')")
162+
logger.debug(f"Vector index initialized for dimension {dim}")
161163
except Exception as e:
164+
logger.error(f"vector_init failed: {e}")
162165
raise RuntimeError(f"vector_init failed: {e}") from e
163166
else:
164167
stored_dim = int(row[0])
165168
if stored_dim != dim:
169+
logger.error(f"Embedding dimension mismatch: stored={stored_dim}, new={dim}")
166170
raise RuntimeError(f"Embedding dimension mismatch: stored={stored_dim}, new={dim}")
167171

168172
q_vec = json.dumps(vector)
@@ -174,17 +178,22 @@ def insert_chunk_vector_with_retry(conn: sqlite3.Connection, file_id: int, path:
174178
cur.execute("INSERT INTO chunks (file_id, path, chunk_index, embedding) VALUES (?, ?, ?, vector_as_f32(?))",
175179
(file_id, path, chunk_index, q_vec))
176180
conn.commit()
177-
return int(cur.lastrowid)
181+
rowid = int(cur.lastrowid)
182+
logger.debug(f"Inserted chunk vector for {path} chunk {chunk_index}, rowid={rowid}")
183+
return rowid
178184
except sqlite3.OperationalError as e:
179185
msg = str(e).lower()
180186
if "database is locked" in msg and attempt < DB_LOCK_RETRY_COUNT:
181187
attempt += 1
182188
delay = DB_LOCK_RETRY_BASE_DELAY * (2 ** (attempt - 1))
189+
logger.warning(f"Database locked, retrying in {delay}s (attempt {attempt}/{DB_LOCK_RETRY_COUNT})")
183190
time.sleep(delay)
184191
continue
185192
else:
193+
logger.error(f"Failed to insert chunk vector after {attempt} retries: {e}")
186194
raise RuntimeError(f"Failed to INSERT chunk vector (vector_as_f32 call): {e}") from e
187195
except Exception as e:
196+
logger.error(f"Failed to insert chunk vector: {e}")
188197
raise RuntimeError(f"Failed to INSERT chunk vector (vector_as_f32 call): {e}") from e
189198

190199

@@ -203,6 +212,7 @@ def search_vectors(database_path: str, q_vector: List[float], top_k: int = 5) ->
203212
Raises:
204213
RuntimeError: If vector search operations fail
205214
"""
215+
logger.debug(f"Searching vectors in database: {database_path}, top_k={top_k}")
206216
conn = connect_db(database_path)
207217
try:
208218
load_sqlite_vector_extension(conn)
@@ -222,7 +232,9 @@ def search_vectors(database_path: str, q_vector: List[float], top_k: int = 5) ->
222232
(q_json, top_k, top_k),
223233
)
224234
rows = cur.fetchall()
235+
logger.debug(f"Vector search returned {len(rows)} results")
225236
except Exception as e:
237+
logger.error(f"Vector search failed: {e}")
226238
raise RuntimeError(f"vector_full_scan call failed: {e}") from e
227239

228240
results: List[Dict[str, Any]] = []

0 commit comments

Comments
 (0)