π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Scaffold vecdb module in ente-ml
laurenspriem19565c830c4a
diff --git a/rust/Cargo.lock b/rust/Cargo.lockindex df81838461..5d534c798a 100644--- a/rust/Cargo.lock+++ b/rust/Cargo.lock@@ -2663,6 +2663,7 @@ version = "0.0.0"dependencies = ["anyhow","ash",+ "crc32fast","ente-assets","ente-image","fast_image_resize",@@ -2671,6 +2672,7 @@ dependencies = ["html-escape","image","imageproc",+ "libc","log","nalgebra 0.33.3","once_cell",@@ -2686,6 +2688,8 @@ dependencies = ["tokio","url","usearch",+ "wide",+ "windows-sys 0.61.2",][[package]]diff --git a/rust/crates/ml/CONTEXT.md b/rust/crates/ml/CONTEXT.mdnew file mode 100644index 0000000000..999ff6ddab--- /dev/null+++ b/rust/crates/ml/CONTEXT.md@@ -0,0 +1,22 @@+# ente-ml β Context++Glossary of terms for the ML crate's domain. Definitions describe meaning, not implementation.++## Vector DB (vecdb)++- **VecDb** β A single persistent collection of embeddings sharing one dimensionality, addressed by a single filesystem path. The app runs many independent VecDb instances (one per embedding space). Intended to become the sole store for embeddings, not a derived cache.+- **Key** β The caller-chosen string identity of an embedding within one VecDb (non-empty UTF-8, β€ 256 bytes): a file ID, face ID, cluster ID, etc., used directly β no integer mapping layer. Adding an existing key replaces its embedding (upsert semantics).+- **Vector Log** β The durable source of truth of a VecDb: the append-only record of every embedding entrusted to it. An acknowledged write survives process kill and power loss.+- **Graph Snapshot** β A persisted copy of the search index (HNSW graph). Purely derived state: losing or corrupting it costs rebuild time, never data.+- **Tombstone** β A logged record marking a key as removed. Tombstoned entries are dead but still occupy log and graph space until compaction.+- **Compaction** β Rewriting the vector log to contain only live entries and rebuilding the graph, triggered when dead entries exceed a threshold.+- **Writer** β The single instance per index file allowed to mutate it, enforced by an OS lock. All other opens are read-only and see a point-in-time view.+- **Flush** β Explicitly persisting the current graph snapshot so the next open needs no replay.+- **Exact search** β Brute-force scan over all live embeddings; results are ground truth.+- **Approximate search** β Index-backed (HNSW) search; may miss neighbours in exchange for speed.+- **Distance** β 1 β inner product of L2-normalized embeddings, in [0, 2]; lower is closer. The only metric vecdb speaks.+- **Embedding space** β A model-specific vector space (e.g. CLIP image 512-d, face 192-d, pet face 128-d). Embeddings from different spaces are never comparable, hence one VecDb per space.++## Legacy++- **VectorDB (usearch)** β The existing usearch-backed engine, kept intact while vecdb is validated by internal users. Same conceptual model, but persists by rewriting the entire index per mutation.diff --git a/rust/crates/ml/Cargo.toml b/rust/crates/ml/Cargo.tomlindex 63838602f8..e70fa5eab1 100644--- a/rust/crates/ml/Cargo.toml+++ b/rust/crates/ml/Cargo.toml@@ -8,6 +8,7 @@ ml-assets = []usearch = ["dep:usearch"][dependencies]+crc32fast = "1.5"ente-assets.workspace = trueente-image.workspace = truefast_image_resize = { version = "5.5.0", features = ["image", "rayon"] }@@ -25,6 +26,17 @@ thiserror.workspace = true# The FRB photos bridge calls the SimSIMD symbol exported by usearch 2.21.x.# Newer usearch releases switched that implementation to numkong symbols.usearch = { version = "=2.21.4", optional = true }+wide = "0.7"++[target.'cfg(unix)'.dependencies]+libc = "0.2"++[target.'cfg(windows)'.dependencies]+windows-sys = { version = "0.61", features = [+ "Win32_Foundation",+ "Win32_Storage_FileSystem",+ "Win32_System_IO",+] }# Cargokit sets ORT_LIB_PATH to Ente's verified iOS archive.[target.'cfg(target_os = "ios")'.dependencies]diff --git a/rust/crates/ml/docs/adr/0001-vecdb-durable-log-plus-derived-graph.md b/rust/crates/ml/docs/adr/0001-vecdb-durable-log-plus-derived-graph.mdnew file mode 100644index 0000000000..258b03ac2c--- /dev/null+++ b/rust/crates/ml/docs/adr/0001-vecdb-durable-log-plus-derived-graph.md@@ -0,0 +1,21 @@+# vecdb: durable append-only vector log + derived HNSW snapshot++We replaced usearch (C++ under the hood, broken on the napi desktop toolchain, rewrites the entire index file on every mutation) with a pure-Rust engine tailored to Ente's on-device scale of 1kβ200k embeddings per index. The persistence architecture splits durability from search: an append-only log of (key, vector) records is the sole source of truth β one checksummed, fsynced append per write operation gives sqlite-grade durability β while the HNSW graph is derived state, persisted as a sidecar snapshot that can always be rebuilt from the log. Losing the snapshot costs rebuild time, never data. The DB is designed to be trustworthy as the *sole* store for embeddings, so the duplicate embedding blobs in sqlite can eventually be dropped.++## Considered options++- **Incremental on-disk graph mutation (true sqlite-style paging/journaling)** β rejected: months of complexity and corruption risk for no functional gain at our scale.+- **Log only, rebuild graph on every open** β rejected: tens of seconds of rebuild at 200kΓ512-d on every app start.+- **Converting existing usearch files** β rejected: would mean maintaining a parser for a foreign versioned binary format for one-off use; instead indexes are refilled from sqlite via the existing migration machinery.++## Consequences++- One caller-visible path maps to two files: `<path>` (log) and `<path>.graph` (snapshot); the Rust layer owns both, consumers never see the sidecar.+- Snapshots are written lazily (mutation threshold + quiet-gap debounce + hard cap), on compaction, on explicit flush, and on open after tail replay β so a killed process pays at most one bounded replay at next open.+- Deletes are tombstones; dead entries are compacted away (log rewrite + graph rebuild) past a ~10% dead ratio with a small absolute floor.+- Exactly one writer instance per index file, enforced by an OS advisory lock; read-only opens hold no lock and no fds, and see a point-in-time view β which is what makes rename-based compaction safe on Windows.+- Single metric (inner product on L2-normalized vectors, distance = 1 β dot) and single scalar (f32) are hardcoded; the file header carries scalar/metric tags and the distance kernel sits behind one internal seam so smaller scalars (f16/i8) can be added without a format break.+- Keys are strings (non-empty UTF-8, β€ 256 bytes), not integers: file/face/cluster IDs are used directly, which retires the four AUTOINCREMENT key-mapping tables the app kept in sqlite solely for usearch's u64 keys. Internally keys are interned to u32 slots; the graph, arena, and log replay speak slots.+- Vectors live on disk as raw little-endian f32 payloads in the framed log records, and in memory in a chunked, 32-byte-aligned arena (chunks of SIMD lanes; free-list slot reuse; growth never reallocates existing chunks). Memory-mapping the log was rejected: it breaks wasm compatibility, fights rename-based compaction on Windows, and framed records give no alignment guarantee.+- vecdb is layered as an IO-free core (graph, arena, SIMD kernels β wasm32-compatible) beneath a native-only storage layer (fs, fsync, locking). The writer lock is hand-rolled (`flock` on unix, `LockFileEx` on Windows) rather than a crate: with `wide`, `crc32fast`, `libc`, and `windows-sys` already in the dependency tree, vecdb adds zero new Cargo.lock entries.+- usearch stays in the tree, feature-gated, while vecdb runs as a parallel engine for internal users; removal is a separate later decision.diff --git a/rust/crates/ml/src/lib.rs b/rust/crates/ml/src/lib.rsindex 317864d02d..d81b1b88d5 100644--- a/rust/crates/ml/src/lib.rs+++ b/rust/crates/ml/src/lib.rs@@ -17,3 +17,4 @@ pub mod scan;pub mod types;#[cfg(feature = "usearch")]pub mod vector_db;+pub mod vecdb;diff --git a/rust/crates/ml/src/vecdb/arena.rs b/rust/crates/ml/src/vecdb/arena.rsnew file mode 100644index 0000000000..e69de29bb2diff --git a/rust/crates/ml/src/vecdb/graph.rs b/rust/crates/ml/src/vecdb/graph.rsnew file mode 100644index 0000000000..e69de29bb2diff --git a/rust/crates/ml/src/vecdb/kernel.rs b/rust/crates/ml/src/vecdb/kernel.rsnew file mode 100644index 0000000000..e69de29bb2diff --git a/rust/crates/ml/src/vecdb/lock.rs b/rust/crates/ml/src/vecdb/lock.rsnew file mode 100644index 0000000000..e69de29bb2diff --git a/rust/crates/ml/src/vecdb/log.rs b/rust/crates/ml/src/vecdb/log.rsnew file mode 100644index 0000000000..e69de29bb2diff --git a/rust/crates/ml/src/vecdb/mod.rs b/rust/crates/ml/src/vecdb/mod.rsnew file mode 100644index 0000000000..1ef5e67c64--- /dev/null+++ b/rust/crates/ml/src/vecdb/mod.rs@@ -0,0 +1,7 @@+mod arena;+mod graph;+mod kernel;+mod lock;+mod log;+mod snapshot;+mod store;diff --git a/rust/crates/ml/src/vecdb/snapshot.rs b/rust/crates/ml/src/vecdb/snapshot.rsnew file mode 100644index 0000000000..e69de29bb2diff --git a/rust/crates/ml/src/vecdb/store.rs b/rust/crates/ml/src/vecdb/store.rsnew file mode 100644index 0000000000..e69de29bb2