1616
1717from .exceptions import ConnectionError as VecConnectionError
1818from .exceptions import TableNotFoundError
19+ from .logger import get_logger
1920from .types import Embeddings , Metadata , Result , Rowids , SimilaritySearchResult , Text
2021from .utils import deserialize_f32 , serialize_f32
2122from .validation import (
2728 validate_top_k ,
2829)
2930
31+ logger = get_logger ()
32+
3033
3134class SQLiteVecClient :
3235 """Manage a text+embedding table and its sqlite-vec index.
@@ -52,15 +55,19 @@ def create_connection(db_path: str) -> sqlite3.Connection:
5255 VecConnectionError: If connection or extension loading fails
5356 """
5457 try :
58+ logger .debug (f"Connecting to database: { db_path } " )
5559 connection = sqlite3 .connect (db_path )
5660 connection .row_factory = sqlite3 .Row
5761 connection .enable_load_extension (True )
5862 sqlite_vec .load (connection )
5963 connection .enable_load_extension (False )
64+ logger .info (f"Successfully connected to database: { db_path } " )
6065 return connection
6166 except sqlite3 .Error as e :
67+ logger .error (f"Failed to connect to database { db_path } : { e } " )
6268 raise VecConnectionError (f"Failed to connect to database: { e } " ) from e
6369 except Exception as e :
70+ logger .error (f"Failed to load sqlite-vec extension: { e } " )
6471 raise VecConnectionError (f"Failed to load sqlite-vec extension: { e } " ) from e
6572
6673 @staticmethod
@@ -89,6 +96,7 @@ def __init__(self, table: str, db_path: str) -> None:
8996 """
9097 validate_table_name (table )
9198 self .table = table
99+ logger .debug (f"Initializing SQLiteVecClient for table: { table } " )
92100 self .connection = self .create_connection (db_path )
93101
94102 def __enter__ (self ) -> SQLiteVecClient :
@@ -119,6 +127,9 @@ def create_table(
119127 ValidationError: If dimension is invalid
120128 """
121129 validate_dimension (dim )
130+ logger .info (
131+ f"Creating table '{ self .table } ' with dim={ dim } , distance={ distance } "
132+ )
122133 self .connection .execute (
123134 f"""
124135 CREATE TABLE IF NOT EXISTS { self .table }
@@ -174,6 +185,7 @@ def create_table(
174185 """
175186 )
176187 self .connection .commit ()
188+ logger .debug (f"Table '{ self .table } ' and triggers created successfully" )
177189
178190 def similarity_search (
179191 self ,
@@ -194,6 +206,7 @@ def similarity_search(
194206 TableNotFoundError: If table doesn't exist
195207 """
196208 validate_top_k (top_k )
209+ logger .debug (f"Performing similarity search with top_k={ top_k } " )
197210 try :
198211 cursor = self .connection .cursor ()
199212 cursor .execute (
@@ -212,9 +225,11 @@ def similarity_search(
212225 [serialize_f32 (embedding ), top_k ],
213226 )
214227 results = cursor .fetchall ()
228+ logger .debug (f"Similarity search returned { len (results )} results" )
215229 return [(row ["rowid" ], row ["text" ], row ["distance" ]) for row in results ]
216230 except sqlite3 .OperationalError as e :
217231 if "no such table" in str (e ).lower ():
232+ logger .error (f"Table '{ self .table } ' not found during similarity search" )
218233 raise TableNotFoundError (
219234 f"Table '{ self .table } ' or '{ self .table } _vec' does not exist. "
220235 "Call create_table() first."
@@ -242,6 +257,7 @@ def add(
242257 TableNotFoundError: If table doesn't exist
243258 """
244259 validate_embeddings_match (texts , embeddings , metadata )
260+ logger .debug (f"Adding { len (texts )} records to table '{ self .table } '" )
245261 try :
246262 max_id = self .connection .execute (
247263 f"SELECT max(rowid) as rowid FROM { self .table } "
@@ -266,9 +282,12 @@ def add(
266282 results = self .connection .execute (
267283 f"SELECT rowid FROM { self .table } WHERE rowid > { max_id } "
268284 )
269- return [row ["rowid" ] for row in results ]
285+ rowids = [row ["rowid" ] for row in results ]
286+ logger .info (f"Added { len (rowids )} records to table '{ self .table } '" )
287+ return rowids
270288 except sqlite3 .OperationalError as e :
271289 if "no such table" in str (e ).lower ():
290+ logger .error (f"Table '{ self .table } ' not found during add operation" )
272291 raise TableNotFoundError (
273292 f"Table '{ self .table } ' does not exist. Call create_table() first."
274293 ) from e
@@ -380,6 +399,7 @@ def update(
380399 embedding : Embeddings | None = None ,
381400 ) -> bool :
382401 """Update fields of a record by rowid; return True if a row changed."""
402+ logger .debug (f"Updating record with rowid={ rowid } " )
383403 sets = []
384404 params : list [Any ] = []
385405 if text is not None :
@@ -400,31 +420,43 @@ def update(
400420 cur = self .connection .cursor ()
401421 cur .execute (sql , params )
402422 self .connection .commit ()
403- return cur .rowcount > 0
423+ updated = cur .rowcount > 0
424+ if updated :
425+ logger .debug (f"Successfully updated record with rowid={ rowid } " )
426+ return updated
404427
405428 def delete_by_id (self , rowid : int ) -> bool :
406429 """Delete a single record by rowid; return True if a row was removed."""
430+ logger .debug (f"Deleting record with rowid={ rowid } " )
407431 cur = self .connection .cursor ()
408432 cur .execute (f"DELETE FROM { self .table } WHERE rowid = ?" , [rowid ])
409433 self .connection .commit ()
410- return cur .rowcount > 0
434+ deleted = cur .rowcount > 0
435+ if deleted :
436+ logger .debug (f"Successfully deleted record with rowid={ rowid } " )
437+ return deleted
411438
412439 def delete_many (self , rowids : list [int ]) -> int :
413440 """Delete multiple records by rowids; return number of rows removed."""
414441 if not rowids :
415442 return 0
443+ logger .debug (f"Deleting { len (rowids )} records" )
416444 placeholders = "," .join (["?" ] * len (rowids ))
417445 cur = self .connection .cursor ()
418446 cur .execute (
419447 f"DELETE FROM { self .table } WHERE rowid IN ({ placeholders } )" ,
420448 rowids ,
421449 )
422450 self .connection .commit ()
423- return cur .rowcount
451+ deleted_count = cur .rowcount
452+ logger .info (f"Deleted { deleted_count } records from table '{ self .table } '" )
453+ return deleted_count
424454
425455 def close (self ) -> None :
426456 """Close the underlying SQLite connection, suppressing close errors."""
427457 try :
458+ logger .debug (f"Closing connection for table '{ self .table } '" )
428459 self .connection .close ()
429- except Exception :
430- pass
460+ logger .info (f"Connection closed for table '{ self .table } '" )
461+ except Exception as e :
462+ logger .warning (f"Error closing connection: { e } " )
0 commit comments