Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

Substantially increate performance of encrypted DBs.

Daniel Petti9c40d8fb9028
diff --git a/src/local_deep_research/database/encrypted_db.py b/src/local_deep_research/database/encrypted_db.py
index 9720c8909b..98f74c793b 100644
--- a/src/local_deep_research/database/encrypted_db.py
+++ b/src/local_deep_research/database/encrypted_db.py
@@ -10,7 +10,7 @@ from loguru import logger
from sqlalchemy import create_engine, event, text
from sqlalchemy.engine import Engine
from sqlalchemy.orm import Session, sessionmaker
-from sqlalchemy.pool import NullPool
+from sqlalchemy.pool import QueuePool, NullPool
from ..config.paths import get_data_directory, get_user_database_filename
from .sqlcipher_utils import (
@@ -317,7 +317,7 @@ class DatabaseManager:
def create_sqlcipher_connection():
"""Create a properly initialized SQLCipher connection."""
- conn = sqlcipher3.connect(str(db_path))
+ conn = sqlcipher3.connect(str(db_path), check_same_thread=False)
cursor = conn.cursor()
# Use centralized SQLCipher setup
@@ -338,10 +338,7 @@ class DatabaseManager:
engine = create_engine(
"sqlite://",
creator=create_sqlcipher_connection,
- poolclass=NullPool, # Use NullPool to avoid thread issues
- connect_args={
- "check_same_thread": False,
- },
+ poolclass=QueuePool,
echo=False,
query_cache_size=1000, # Increased for complex queries with SQLCipher
)
@@ -353,7 +350,7 @@ class DatabaseManager:
engine = create_engine(
f"sqlite:///{db_path}",
connect_args={"check_same_thread": False},
- poolclass=NullPool,
+ poolclass=QueuePool,
echo=False,
query_cache_size=1000, # Same optimization for unencrypted
)
@@ -558,7 +555,6 @@ class DatabaseManager:
logger.warning("Creating unencrypted thread session for metrics")
engine = create_engine(
f"sqlite:///{db_path}",
- connect_args={"check_same_thread": False},
poolclass=NullPool,
echo=False,
)
diff --git a/src/local_deep_research/database/sqlcipher_utils.py b/src/local_deep_research/database/sqlcipher_utils.py
index d66b96a8fe..b6eb0320ac 100644
--- a/src/local_deep_research/database/sqlcipher_utils.py
+++ b/src/local_deep_research/database/sqlcipher_utils.py
@@ -6,9 +6,37 @@ consistent password handling and PRAGMA settings across the codebase.
"""
from typing import Any, Optional
+from hashlib import pbkdf2_hmac
+from functools import cache
+
from loguru import logger
+@cache
+def _get_key_from_password(password: str) -> bytes:
+ """
+ Generates an encryption key from the user's password.
+
+ Args:
+ password: The password.
+
+ Returns:
+ The generated key.
+
+ """
+ # Generate a secure key based on the password.
+ settings = get_sqlcipher_settings()
+ logger.info("Generating DB encryption key...")
+ # The salt we use here doesn't actually matter because sqlcipher
+ # automatically salts the encryption key.
+ key = pbkdf2_hmac(
+ "sha512", password.encode(), b"no salt", settings["kdf_iterations"]
+ )
+ logger.info("Generated DB encryption key.")
+
+ return key
+
+
def set_sqlcipher_key(cursor_or_conn: Any, password: str) -> None:
"""
Set the SQLCipher encryption key using hexadecimal encoding.
@@ -18,9 +46,10 @@ def set_sqlcipher_key(cursor_or_conn: Any, password: str) -> None:
Args:
cursor_or_conn: SQLCipher cursor or connection object
password: The password to use for encryption
+
"""
- hex_password = password.encode("utf-8").hex()
- cursor_or_conn.execute(f"PRAGMA key = \"x'{hex_password}'\"")
+ key = _get_key_from_password(password)
+ cursor_or_conn.execute(f"PRAGMA key = \"x'{key.hex()}'\"")
def set_sqlcipher_rekey(cursor_or_conn: Any, new_password: str) -> None:
@@ -53,7 +82,7 @@ def set_sqlcipher_rekey(cursor_or_conn: Any, new_password: str) -> None:
# Default SQLCipher configuration (can be overridden by settings)
# Reduced for testing - in production use higher values
-DEFAULT_KDF_ITERATIONS = 4000 # Reduced for testing (was 256000)
+DEFAULT_KDF_ITERATIONS = 256000 # Reduced for testing (was 256000)
DEFAULT_PAGE_SIZE = 16384 # 16KB pages for maximum performance with caching
DEFAULT_HMAC_ALGORITHM = "HMAC_SHA512"
DEFAULT_KDF_ALGORITHM = "PBKDF2_HMAC_SHA512"
@@ -109,13 +138,9 @@ def apply_sqlcipher_pragmas(
# Core settings needed for both creation and access
cursor_or_conn.execute(f"PRAGMA cipher_page_size = {settings['page_size']}")
- cursor_or_conn.execute(f"PRAGMA kdf_iter = {settings['kdf_iterations']}")
cursor_or_conn.execute(
f"PRAGMA cipher_hmac_algorithm = {settings['hmac_algorithm']}"
)
- cursor_or_conn.execute(
- f"PRAGMA cipher_kdf_algorithm = {settings['kdf_algorithm']}"
- )
if creation_mode:
# Additional settings only needed during creation