A zero-dependency Python 3.11+ stdlib (sqlite3) shadow-build indexer (CI-verified on Ubuntu 3.11 and 3.12) and deterministic read-only search engine for operational incident logs, post-mortems, and runbook archives.
Rebuilding an FTS5 search index directly inside a live SQLite database introduces three operational hazards:
- Write-Amplification & Lock Contention: Maintaining a rollback journal or WAL across tens of thousands of inserts plus FTS5 inverted-index merges triggers repeated fsync flushes and
SQLITE_BUSYcontention for active readers. - Torn Rebuilds on Crash: An unhandled exception, OOM kill, or disk full mid-rebuild leaves partial
documentsrows or corrupted FTS5 shadow tables in production. - Sidecar Drift & Windows File Locks: Swapping a
.dbfile next to stale-wal/-shmsidecars corrupts visibility, and on Windows any unclosed SQLite handle breaks atomic file replacement (os.replace).
atomic-sqlite-fts5-engine separates offline shadow building (<target>.<pid>.<uuid>.tmp with PRAGMA journal_mode=OFF and deferred secondary indexes) from strict read-only serving (?mode=ro + PRAGMA query_only=ON).
The atomic shadow-build lifecycle, explicit connection cleanup for Windows, and deterministic two-phase FTS5 + regex query engine were extracted and rewritten from a private high-volume local archive indexing pipeline. All private message tables, personal lifelog schemas, and user data were removed and replaced with a synthetic incident, post-mortem, and runbook schema.
This engine is intentionally optimized for immutable or periodic batch snapshots (CI builds, daily log archives, local CLI runbook indexes).
It does not:
- handle continuous multi-writer single-row streaming mutations in place
- shard indexes across multiple hosts or replace Elasticsearch/OpenSearch for multi-terabyte telemetry lakes
JSONL / Iterator
|
v (1) Disposable Shadow Build on <target>.<pid>.<uuid>.tmp
PRAGMA journal_mode=OFF; synchronous=OFF; temp_store=MEMORY; cache_size=-128000;
|
v (2) Chunked executemany() + keyset FTS5 sync + deferred B-tree indexes
|
v (3) ANALYZE + PRAGMA optimize;
|
v (4) Durable mode transition (DELETE or WAL + wal_checkpoint(TRUNCATE))
|
v (5) PRAGMA integrity_check; + FTS5 'integrity-check'
|
v (6) Explicit cursor & Connection.close() + sidecar cleanup + _fsync_file(.tmp)
|
v (7) os.replace(<target>.<pid>.<uuid>.tmp, <target>.db) + _fsync_parent_dir()
Live Archive (atomic swap)
- Process-Crash Isolation vs. Power-Loss Durability: Each build writes to a unique PID + UUID shadow path (
<target>.<pid>.<uuid>.tmp) with secondary B-tree index creation deferred until after bulk ingestion and FTS5 sync (avoiding B-tree page splits during bulk load). Any Python exception or process kill during steps(1)-(6)cleans up shadow artifacts and leaves any existing<target>.dbbyte-for-byte untouched (process-crash isolation). For power-loss durability on the final commit boundary,_fsync_file()opens the closed.tmpdatabase in"rb+"mode and callsos.fsync()beforeos.replace(), followed by_fsync_parent_dir()on POSIX to persist the updated directory entry. - Windows File-Lock Hygiene: Python's
with sqlite3.connect(...)context manager only commits/rolls back transactions and does not close the connection. Every build path explicitly callsclose()intry...finallybefore unlinking sidecars or invokingos.replace().
AtomicSearchEngine opens databases via URI (file:///...?mode=ro) and immediately applies PRAGMA query_only = ON;. Any accidental INSERT, UPDATE, DELETE, CREATE, or DROP raises sqlite3.OperationalError at both VFS and VM layers.
BuildConfig.final_journal_mode defaults to "DELETE" so the swapped artifact needs zero -wal or -shm sidecars and opens cleanly via ?mode=ro even on read-only mounted filesystems or immutable container layers ("WAL" with wal_checkpoint(TRUNCATE) is also supported when post-swap concurrency requires it).
| Dimension | atomic-sqlite-fts5-engine |
Distributed Engines (Elasticsearch / OpenSearch) |
|---|---|---|
| Operational Footprint | Zero external deps, zero daemon/JVM, single .db artifact |
Multi-node cluster, JVM memory tuning, shard/replica ops |
| Local Read Latency | Low-tens-of-milliseconds in-process C library reads (measured 2.301 ms min / 23.280 ms mixed P50 on 5k synthetic docs; pure FTS5 BM25 P50 23.265 ms across ~625 dense matches/query + snippet() vs regex/fallback P50 30.108 ms; zero network hop) |
Network hop + coordinator scatter-gather overhead |
| Hybrid Structured + Text | Single SQLite query joining documents_fts BM25 with composite B-tree indexes |
Requires denormalized duplication or costly cross-index joins |
| Write Pattern Fit | Batch / periodic shadow rebuilds (archives, post-mortems, runbooks) | Continuous high-QPS real-time writes across concurrent writers |
| Scale Ceiling | Single-node storage/RAM envelope | Multi-terabyte horizontal sharding across clusters |
documents: Canonical table storingdoc_uid(UNIQUE),doc_type(incident,postmortem,runbook),service_key,severity,timestamp_ms,occurred_at,author,title,body,tags, andmetadata_json.- Composite Indexes:
idx_documents_service_ts (service_key, timestamp_ms),idx_documents_severity_ts (severity, timestamp_ms),idx_documents_ts (timestamp_ms),idx_documents_type_ts (doc_type, timestamp_ms). documents_fts: FTS5 virtual table (tokenize='unicode61 remove_diacritics 2') synced 1:1 viarowid == documents.id, supporting accent-insensitive Unicode lookup ("exaustao critica"matches"Exaustão crítica").- Two-Phase BM25 & Deferred
snippet(): Top-K ranking executes first;snippet()evaluates only on the returnedLIMITrowids. - FTS5 Pre-Filter + Regex Refinement: Combines FTS5 candidate narrowing with compiled
re.Patternfiltering and safely falls back to metadata scan when alternations|or optional quantifiers make token extraction unsound.
Requires Python 3.11+ (CI-verified on Ubuntu 3.11 and 3.12, pytest for tests).
python -m pip install -e ".[dev]"
# 1. Atomically build (or rebuild) an archive from JSONL / JSONL.GZ files
atomic-fts build --input incidents.jsonl runbooks.jsonl.gz --db ops_archive.db
# 2. BM25 search with service, severity, and UTC date filters
atomic-fts search "HikariPool timeout" --db ops_archive.db --service payments-gateway --severity SEV1 --start 2026-01-01 --end 2026-03-31
# 3. FTS5 pre-filter + regex refinement (JSON output with elapsed_ms telemetry)
atomic-fts search "HikariPool" --regex "HikariPool-\d+ exhausted" --db ops_archive.db --json
# 4. Inspect a single record by doc_uid or check archive cardinality
atomic-fts show INC-2026-000142 --db ops_archive.db
atomic-fts stats --db ops_archive.dbpython scripts/benchmark_build.py --docs 5000 --queries 50
pytest -vOutput from python scripts/benchmark_build.py --docs 5000 --queries 50 (with pre-swap _fsync_file enabled):
| Metric | Measured Value |
|---|---|
| Documents & FTS5 Rows Indexed | 5,000 (5,000 FTS5 rows) |
| Final Journal / Integrity Check | DELETE / integrity=ok |
| Shadow Build + Fsync + Swap Time | 1.1712 s (4,269.14 docs/sec) |
| Final Archive Size | 5.863 MB (6,148,096 bytes) |
| Queries Executed | 50 (1,000 total hits across 35 pure FTS5 BM25, 10 regex/fallback, 5 metadata-only queries) |
Mixed Query Latency (min / P50 / P95 / max / mean) |
2.301 ms / 23.280 ms / 40.836 ms / 56.236 ms / 23.720 ms |
Pure FTS5 BM25 Breakdown (count=35: P50 / P95 / mean) |
23.265 ms / 42.608 ms / 24.658 ms |
Regex / Scan Fallback Breakdown (count=10: P50 / P95 / mean) |
30.108 ms / 40.836 ms / 30.613 ms |
Because the 5,000 synthetic records cycle through 8 dense scenario templates (~625 matching rows per FTS5 query before ORDER BY bm25_score + LIMIT 20 + snippet()), pure FTS5 BM25 median latency is 23.265 ms (dropping to 2.301 ms min on narrower filtered queries), while regex post-filtering and alternation scan fallbacks raise the regex breakdown to 30.108 ms P50.
MIT - see LICENSE.