macrame/vector/registry.rs
1use crate::error::{DbError, Result};
2use crate::schema::ddl;
3use crate::vector::ModelName;
4
5/// Create a model's embedding table and its DiskANN index, if absent (§4.1).
6///
7/// Both objects, or neither. The index is not an optimisation that can be added
8/// later at leisure: until it exists the engine accepts vectors of any length
9/// into the column (see [`ddl::create_embeddings_index`]), so a table without
10/// its index is a table with no dimension enforcement at all. Creating them in
11/// one transaction is what makes "registered" mean "enforcing".
12///
13/// Idempotent, so an application may call it on every start.
14///
15/// # Prefer [`crate::Database::register_model`]
16///
17/// This takes a **bare connection** and is therefore §4.7 invariant 2's third
18/// hole: a write that does not cross the actor's channel. Hidden from the docs
19/// alongside [`crate::Database::raw`] (D-091) so the documented path is the one
20/// that preserves the single-writer property; still public, because
21/// [D-068](../../docs/architecture/s13-decision-register.md#d-068)'s argument
22/// applies unchanged — the file is reachable by any SQLite client, so removing
23/// the supported way to do this would buy the appearance of a guarantee.
24#[doc(hidden)]
25pub async fn register_model(
26 conn: &libsql::Connection,
27 model: &ModelName,
28 dim: usize,
29) -> Result<()> {
30 if dim == 0 {
31 return Err(DbError::DimMismatch {
32 got: 0,
33 expected: 1,
34 model: model.to_string(),
35 });
36 }
37
38 // If it already exists at a different dimension, say so rather than
39 // no-opping through `IF NOT EXISTS` and leaving the caller believing the
40 // dimension they asked for is the one in force.
41 if let Some(existing) = read_declared_dimension(conn, model).await? {
42 if existing != dim {
43 return Err(DbError::DimMismatch {
44 got: dim,
45 expected: existing,
46 model: model.to_string(),
47 });
48 }
49 return Ok(());
50 }
51
52 let tx = conn
53 .transaction_with_behavior(libsql::TransactionBehavior::Immediate)
54 .await?;
55 let res: Result<()> = async {
56 tx.execute(&ddl::create_embeddings_table(model, dim), ())
57 .await?;
58 tx.execute(&ddl::create_embeddings_index(model), ()).await?;
59 Ok(())
60 }
61 .await;
62
63 match res {
64 Ok(()) => {
65 tx.commit().await?;
66 Ok(())
67 }
68 Err(e) => {
69 let _ = tx.rollback().await;
70 Err(e)
71 }
72 }
73}
74
75/// The dimension a registered model's table declares, read from the schema.
76///
77/// The crate keeps no registry of its own. `F32_BLOB(768)` in the column type
78/// *is* the declaration, `PRAGMA table_info` reports it verbatim, and reading it
79/// back means the Rust-side dimension check and the storage-layer one cannot
80/// disagree — there is only one number. Keeping a `HashMap<model, dim>` beside
81/// the database would be the same mistake D-035 fixed in `archive()`: a second
82/// hand-maintained description of a set the schema already defines.
83pub async fn declared_dimension(conn: &libsql::Connection, model: &ModelName) -> Result<usize> {
84 read_declared_dimension(conn, model)
85 .await?
86 .ok_or_else(|| DbError::ModelNotRegistered {
87 model: model.to_string(),
88 table: model.table(),
89 })
90}
91
92/// `Ok(None)` when the model has no table; `Ok(Some(dim))` when it has one.
93async fn read_declared_dimension(
94 conn: &libsql::Connection,
95 model: &ModelName,
96) -> Result<Option<usize>> {
97 // `model` is a ModelName, so this splice is a bare identifier by construction.
98 let mut rows = conn
99 .query(&format!("PRAGMA table_info({})", model.table()), ())
100 .await?;
101
102 while let Some(row) = rows.next().await? {
103 let name: String = row.get(1)?;
104 if name == "embedding" {
105 let declared: String = row.get(2)?;
106 return parse_f32_blob_dim(&declared).map(Some).ok_or_else(|| {
107 DbError::ModelNotRegistered {
108 model: model.to_string(),
109 table: format!(
110 "{} (column `embedding` is declared {declared:?}, not F32_BLOB(n))",
111 model.table()
112 ),
113 }
114 });
115 }
116 }
117 Ok(None)
118}
119
120/// Pull `n` out of a declared type of the form `F32_BLOB(n)`.
121fn parse_f32_blob_dim(declared: &str) -> Option<usize> {
122 let t = declared.trim();
123 if !t.get(..9)?.eq_ignore_ascii_case("F32_BLOB(") || !t.ends_with(')') {
124 return None;
125 }
126 t[9..t.len() - 1].trim().parse::<usize>().ok()
127}
128
129/// Every model registered in this database, in name order.
130///
131/// Derived from `sqlite_master` for the same reason as the dimension: the set of
132/// registered models is a fact about the schema, and asking the schema cannot
133/// drift from it. libSQL's own `libsql_vector_meta_shadow` and `*_shadow` tables
134/// are filtered out — they are engine internals that happen to live in `main`.
135pub async fn registered_models(conn: &libsql::Connection) -> Result<Vec<ModelName>> {
136 let mut rows = conn
137 .query(
138 "SELECT name FROM sqlite_master
139 WHERE type = 'table'
140 AND name LIKE 'embeddings_%'
141 AND name NOT LIKE '%_shadow'
142 ORDER BY name",
143 (),
144 )
145 .await?;
146
147 let mut out = Vec::new();
148 while let Some(row) = rows.next().await? {
149 let table: String = row.get(0)?;
150 if let Some(suffix) = table.strip_prefix("embeddings_") {
151 // A table matching the pattern but not the naming rule was not
152 // created by `register_model`; skipping it is more honest than
153 // reporting a name this crate would refuse to accept back.
154 if let Ok(model) = ModelName::new(suffix) {
155 out.push(model);
156 }
157 }
158 }
159 Ok(out)
160}
161
162#[cfg(test)]
163mod tests {
164 use super::parse_f32_blob_dim;
165
166 #[test]
167 fn reads_the_dimension_out_of_a_declared_type() {
168 assert_eq!(parse_f32_blob_dim("F32_BLOB(768)"), Some(768));
169 assert_eq!(parse_f32_blob_dim("f32_blob(1536)"), Some(1536));
170 assert_eq!(parse_f32_blob_dim(" F32_BLOB( 4 ) "), Some(4));
171 }
172
173 /// Anything that is not a dimensioned vector column must read as absent
174 /// rather than as some default, or a plain BLOB column would silently
175 /// acquire whatever dimension the caller happened to pass.
176 #[test]
177 fn refuses_to_invent_a_dimension() {
178 for bad in [
179 "BLOB",
180 "TEXT",
181 "F32_BLOB",
182 "F32_BLOB()",
183 "F32_BLOB(x)",
184 "F32_BLOB(4",
185 "",
186 ] {
187 assert_eq!(parse_f32_blob_dim(bad), None, "{bad:?} yielded a dimension");
188 }
189 }
190}