Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix: resolve qodo review issues for sqlite backend (#240)

luffy220862ac59705b3f
diff --git a/semantica/vector_store/sqlite_vec_store.py b/semantica/vector_store/sqlite_vec_store.py
index a359bda0..55c25dae 100644
--- a/semantica/vector_store/sqlite_vec_store.py
+++ b/semantica/vector_store/sqlite_vec_store.py
@@ -48,13 +48,8 @@ from ..utils.exceptions import ProcessingError, ValidationError
from ..utils.logging import get_logger
# Optional sqlite-vec import
-try:
- import sqlite_vec
-
- SQLITE_VEC_AVAILABLE = True
-except (ImportError, OSError):
- SQLITE_VEC_AVAILABLE = False
- sqlite_vec = None
+# (Loaded lazily inside SQLiteVecStore.__init__ to prevent eager loading)
+sqlite_vec = None
class SQLiteVecStore:
@@ -95,12 +90,16 @@ class SQLiteVecStore:
"""
self.logger = get_logger("sqlite_vec_store")
- # Validate dependencies
- if not SQLITE_VEC_AVAILABLE:
- raise ProcessingError(
- "sqlite-vec Python package is not available. "
- "Install with: pip install sqlite-vec"
- )
+ # Validate dependencies and lazy load
+ global sqlite_vec
+ if sqlite_vec is None:
+ try:
+ import sqlite_vec
+ except (ImportError, OSError):
+ raise ProcessingError(
+ "sqlite-vec Python package is not available. "
+ "Install with: pip install sqlite-vec"
+ )
# Validate parameters
if distance_metric.lower() not in self.SUPPORTED_METRICS:
@@ -115,6 +114,11 @@ class SQLiteVecStore:
"Table names must be alphanumeric with underscores/hyphens only and start with a letter/underscore."
)
+ if not isinstance(dimension, int) or dimension <= 0:
+ raise ValidationError(
+ f"Dimension must be a positive integer, got: {dimension}"
+ )
+
self.db_path = db_path
self.table_name = table_name
self.dimension = dimension
@@ -202,13 +206,13 @@ class SQLiteVecStore:
"""
Validate that a string is safe to use as a SQL identifier.
- Only allows alphanumeric characters, underscores, and hyphens.
+ Only allows alphanumeric characters and underscores.
"""
if not isinstance(key, str):
return False
if not key:
return False
- return bool(re.match(r"^[a-zA-Z_][a-zA-Z0-9_-]*$", key))
+ return bool(re.match(r"^[a-zA-Z_][a-zA-Z0-9_]*$", key))
def _ensure_table_exists(self):
"""Ensure the vector table exists."""
@@ -429,9 +433,13 @@ class SQLiteVecStore:
with self._lock, self._get_connection() as conn:
try:
cur = conn.cursor()
- for vec_id in ids:
+ batch_size = 1000
+ for i in range(0, len(ids), batch_size):
+ batch_ids = ids[i : i + batch_size]
+ placeholders = ",".join(["?"] * len(batch_ids))
cur.execute(
- f"DELETE FROM {self.table_name} WHERE id = ?", (vec_id,)
+ f"DELETE FROM {self.table_name} WHERE id IN ({placeholders})",
+ batch_ids,
)
conn.commit()
cur.close()
diff --git a/semantica/vector_store/vector_store.py b/semantica/vector_store/vector_store.py
index 356c73e8..1ecd7d36 100644
--- a/semantica/vector_store/vector_store.py
+++ b/semantica/vector_store/vector_store.py
@@ -692,13 +692,27 @@ class VectorStore:
raise
def update_vectors(
- self, vector_ids: List[str], new_vectors: List[np.ndarray], **options
+ self, vector_ids: List[str], new_vectors: List[np.ndarray], metadata: Optional[List[Dict[str, Any]]] = None, **options
) -> bool:
"""Update existing vectors."""
+ # Delegate to backend store if available
+ if self._backend_store:
+ if hasattr(self._backend_store, 'update'):
+ return self._backend_store.update(ids=vector_ids, vectors=new_vectors, metadata=metadata, **options)
+ elif hasattr(self._backend_store, 'update_vectors'):
+ return self._backend_store.update_vectors(vector_ids, new_vectors, **options)
+ else:
+ raise NotImplementedError(f"Backend store {type(self._backend_store).__name__} does not have update or update_vectors method")
+
for vec_id, new_vec in zip(vector_ids, new_vectors):
if vec_id in self.vectors:
self.vectors[vec_id] = new_vec
+ if metadata:
+ for vec_id, meta in zip(vector_ids, metadata):
+ if vec_id in self.metadata:
+ self.metadata[vec_id] = meta
+
# Rebuild index
self.indexer.create_index(
list(self.vectors.values()), list(self.vectors.keys())
@@ -708,6 +722,15 @@ class VectorStore:
def delete_vectors(self, vector_ids: List[str], **options) -> bool:
"""Delete vectors from store."""
+ # Delegate to backend store if available
+ if self._backend_store:
+ if hasattr(self._backend_store, 'delete'):
+ return self._backend_store.delete(ids=vector_ids, **options)
+ elif hasattr(self._backend_store, 'delete_vectors'):
+ return self._backend_store.delete_vectors(vector_ids, **options)
+ else:
+ raise NotImplementedError(f"Backend store {type(self._backend_store).__name__} does not have delete or delete_vectors method")
+
for vec_id in vector_ids:
self.vectors.pop(vec_id, None)
self.metadata.pop(vec_id, None)