• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

joaoh82 / rust_sqlite / 25079608728

28 Apr 2026 09:52PM UTC coverage: 70.927% (+0.6%) from 70.322%
25079608728

push

github

web-flow
Phase 7d.3: HNSW persistence + DELETE/UPDATE rebuild trigger (#52)

Closes out Phase 7d. Three intertwined pieces:

  1. **HNSW graph persistence** via a new KIND_HNSW (0x05) cell tag.
     New src/sql/pager/hnsw_cell.rs holds the HnswNodeCell encoder
     (varint node_id + per-layer neighbor lists). Each HNSW index
     gets its own page tree parallel to secondary indexes; the tree
     reuses TableLeaf pages because Cell::peek_rowid works
     uniformly across all cell kinds (it just reads the first
     varint after the kind tag, which is node_id here). Slot-
     directory binary search by node_id therefore works without
     HNSW-specific page-level code.

     Save: stage_hnsw_btree → stage_hnsw_leaves emit one cell per
     node, sibling-chained leaves + interior layers as the index
     grows past one page. The rootpage goes into sqlrite_master.

     Open: load_hnsw_nodes walks the leaves, decodes cells,
     hands the (id, layers) tuples to the new
     HnswIndex::from_persisted_nodes constructor — no algorithm
     runs on the load path; the loaded graph is bit-identical to
     what was saved. Compatibility path retained for files saved
     before persistence (rootpage == 0): execute_create_index
     re-walks current rows, same as 7d.2.

  2. **DELETE / UPDATE on HNSW-indexed tables now works.** The 7d.2
     refusals are removed. New `needs_rebuild` flag on
     HnswIndexEntry marks an index as stale when DELETE removes a
     row or UPDATE touches its column. save_database calls
     rebuild_dirty_hnsw_indexes at the top, before any immutable
     borrows: walks the table's current Vec<f32> column, builds a
     fresh HnswIndex, replaces the entry's index, clears the flag.
     Then the staging loop serializes the rebuilt graph.

     Cost: O(N · ef_construction · log N) per dirty index per save.
     Fine for ≤ 10k-row tables, expensive for ≥ 100k-row bulk
     deletes. Matches the trade-off SQLite makes for FTS5
     (rebuild-on-muta... (continued)

251 of 293 new or added lines in 5 files covered. (85.67%)

5 existing lines in 2 files now uncovered.

5211 of 7347 relevant lines covered (70.93%)

1.46 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.39
/src/sql/pager/mod.rs
1
//! On-disk persistence for a `Database`, using fixed-size paged files.
2
//!
3
//! The file is a sequence of 4 KiB pages. Page 0 holds the header
4
//! (magic, version, page count, schema-root pointer). Every other page carries
5
//! a small per-page header (type tag + next-page pointer + payload length)
6
//! followed by a payload of up to 4089 bytes.
7
//!
8
//! **Storage strategy (format version 2, Phase 3c.5).**
9
//!
10
//! - Each `Table`'s rows live as **cells** in a chain of `TableLeaf` pages.
11
//!   Cell layout and slot directory are in `cell.rs` / `table_page.rs`;
12
//!   cells that exceed the inline threshold spill into an overflow chain
13
//!   via `overflow.rs`.
14
//! - The schema catalog is itself a regular table named `sqlrite_master`,
15
//!   with one row per user table:
16
//!       `(name TEXT PRIMARY KEY, sql TEXT NOT NULL,
17
//!         rootpage INTEGER NOT NULL, last_rowid INTEGER NOT NULL)`
18
//!   This is the SQLite-style approach: the schema of `sqlrite_master`
19
//!   itself is hardcoded into the engine so the open path can bootstrap.
20
//! - Page 0's `schema_root_page` field points at the first leaf of
21
//!   `sqlrite_master`.
22
//!
23
//! **Format version.** Version 2 is not compatible with files produced by
24
//! earlier commits. Opening a v1 file returns a clean error — users on
25
//! old files have to regenerate them from CREATE/INSERT, as there's no
26
//! production data to migrate yet.
27

28
// Data-layer modules. Not every helper in these modules is used by save/open
29
// yet — some exist for tests, some for future maintenance operations.
30
// Module-level #[allow(dead_code)] keeps the build quiet without dotting
31
// the modules with per-item attributes.
32
#[allow(dead_code)]
33
pub mod cell;
34
pub mod file;
35
pub mod header;
36
#[allow(dead_code)]
37
pub mod hnsw_cell;
38
#[allow(dead_code)]
39
pub mod index_cell;
40
#[allow(dead_code)]
41
pub mod interior_page;
42
pub mod overflow;
43
pub mod page;
44
pub mod pager;
45
#[allow(dead_code)]
46
pub mod table_page;
47
#[allow(dead_code)]
48
pub mod varint;
49
#[allow(dead_code)]
50
pub mod wal;
51

52
use std::collections::{BTreeMap, HashMap};
53
use std::path::Path;
54
use std::sync::{Arc, Mutex};
55

56
use sqlparser::dialect::SQLiteDialect;
57
use sqlparser::parser::Parser;
58

59
use crate::error::{Result, SQLRiteError};
60
use crate::sql::db::database::Database;
61
use crate::sql::db::secondary_index::{IndexOrigin, SecondaryIndex};
62
use crate::sql::db::table::{Column, DataType, Row, Table, Value};
63
use crate::sql::pager::cell::Cell;
64
use crate::sql::pager::header::DbHeader;
65
use crate::sql::pager::index_cell::IndexCell;
66
use crate::sql::pager::interior_page::{InteriorCell, InteriorPage};
67
use crate::sql::pager::overflow::{
68
    OVERFLOW_THRESHOLD, OverflowRef, PagedEntry, read_overflow_chain, write_overflow_chain,
69
};
70
use crate::sql::pager::page::{PAGE_HEADER_SIZE, PAGE_SIZE, PAYLOAD_PER_PAGE, PageType};
71
use crate::sql::pager::pager::Pager;
72
use crate::sql::pager::table_page::TablePage;
73
use crate::sql::parser::create::CreateQuery;
74

75
// Re-export so callers can spell `sql::pager::AccessMode` without
76
// reaching into the `pager::pager::pager` submodule path.
77
pub use crate::sql::pager::pager::AccessMode;
78

79
/// Name of the internal catalog table. Reserved — user CREATEs of this
80
/// name must be rejected upstream.
81
pub const MASTER_TABLE_NAME: &str = "sqlrite_master";
82

83
/// Opens a database file in read-write mode. Shorthand for
84
/// [`open_database_with_mode`] with [`AccessMode::ReadWrite`].
85
pub fn open_database(path: &Path, db_name: String) -> Result<Database> {
2✔
86
    open_database_with_mode(path, db_name, AccessMode::ReadWrite)
2✔
87
}
88

89
/// Opens a database file in read-only mode. Acquires a shared OS-level
90
/// advisory lock, so other read-only openers coexist but any writer is
91
/// excluded. Attempts to mutate the returned `Database` (e.g. an
92
/// `INSERT`, or a `save_database` call against it) bottom out in a
93
/// `cannot commit: database is opened read-only` error from the Pager.
94
pub fn open_database_read_only(path: &Path, db_name: String) -> Result<Database> {
1✔
95
    open_database_with_mode(path, db_name, AccessMode::ReadOnly)
1✔
96
}
97

98
/// Opens a database file and reconstructs the in-memory `Database`,
99
/// leaving the long-lived `Pager` attached for subsequent auto-save
100
/// (read-write) or consistent-snapshot reads (read-only).
101
pub fn open_database_with_mode(path: &Path, db_name: String, mode: AccessMode) -> Result<Database> {
2✔
102
    let pager = Pager::open_with_mode(path, mode)?;
5✔
103

104
    // 1. Load sqlrite_master from the tree at header.schema_root_page.
105
    let mut master = build_empty_master_table();
2✔
106
    load_table_rows(&pager, &mut master, pager.header().schema_root_page)?;
4✔
107

108
    // 2. Two passes over master rows: first build every user table, then
109
    //    attach secondary indexes. Indexes need their base table to exist
110
    //    before we can populate them. Auto-indexes are created at table
111
    //    build time so we only have to load explicit indexes from disk
112
    //    (but we also reload the auto-index CONTENT because Table::new
113
    //    built it empty).
114
    let mut db = Database::new(db_name);
2✔
115
    let mut index_rows: Vec<IndexCatalogRow> = Vec::new();
2✔
116

117
    for rowid in master.rowids() {
6✔
118
        let ty = take_text(&master, "type", rowid)?;
4✔
119
        let name = take_text(&master, "name", rowid)?;
4✔
120
        let sql = take_text(&master, "sql", rowid)?;
4✔
121
        let rootpage = take_integer(&master, "rootpage", rowid)? as u32;
4✔
122
        let last_rowid = take_integer(&master, "last_rowid", rowid)?;
2✔
123

124
        match ty.as_str() {
2✔
125
            "table" => {
2✔
126
                let (parsed_name, columns) = parse_create_sql(&sql)?;
4✔
127
                if parsed_name != name {
4✔
128
                    return Err(SQLRiteError::Internal(format!(
×
129
                        "sqlrite_master row '{name}' carries SQL for '{parsed_name}' — corrupt catalog?"
130
                    )));
131
                }
132
                let mut table = build_empty_table(&name, columns, last_rowid);
4✔
133
                if rootpage != 0 {
2✔
134
                    load_table_rows(&pager, &mut table, rootpage)?;
4✔
135
                }
136
                if last_rowid > table.last_rowid {
2✔
137
                    table.last_rowid = last_rowid;
×
138
                }
139
                db.tables.insert(name, table);
4✔
140
            }
141
            "index" => {
4✔
142
                index_rows.push(IndexCatalogRow {
4✔
143
                    name,
2✔
144
                    sql,
2✔
145
                    rootpage,
146
                });
147
            }
148
            other => {
×
149
                return Err(SQLRiteError::Internal(format!(
×
150
                    "sqlrite_master row '{name}' has unknown type '{other}'"
151
                )));
152
            }
153
        }
154
    }
155

156
    // Second pass: attach each index to its table. HNSW indexes
157
    // (Phase 7d.2) take a different code path because their persisted
158
    // form is just the CREATE INDEX SQL — the graph itself isn't
159
    // persisted yet (Phase 7d.3). Detect HNSW via the SQL's USING clause
160
    // and route to a graph-rebuild instead of the B-Tree-cell load.
161
    for row in index_rows {
6✔
162
        if create_index_sql_uses_hnsw(&row.sql) {
4✔
163
            rebuild_hnsw_index(&mut db, &pager, &row)?;
2✔
164
        } else {
165
            attach_index(&mut db, &pager, row)?;
4✔
166
        }
167
    }
168

169
    db.source_path = Some(path.to_path_buf());
2✔
170
    db.pager = Some(pager);
2✔
171
    Ok(db)
2✔
172
}
173

174
/// Catalog row for a secondary index — deferred until after every table is
175
/// loaded so the index's base table exists by the time we populate it.
176
struct IndexCatalogRow {
177
    name: String,
178
    sql: String,
179
    rootpage: u32,
180
}
181

182
/// Persists `db` to disk. Same diff-commit behavior as before: only pages
183
/// whose bytes actually changed get written.
184
pub fn save_database(db: &mut Database, path: &Path) -> Result<()> {
2✔
185
    // Phase 7d.3 — rebuild any HNSW index that DELETE / UPDATE-on-vector
186
    // marked dirty. Done up front under the &mut Database borrow we
187
    // already hold, before the immutable iteration loops below need
188
    // their own borrow.
189
    rebuild_dirty_hnsw_indexes(db);
2✔
190

191
    let same_path = db.source_path.as_deref() == Some(path);
2✔
192
    let mut pager = if same_path {
2✔
193
        match db.pager.take() {
2✔
194
            Some(p) => p,
2✔
195
            None if path.exists() => Pager::open(path)?,
4✔
196
            None => Pager::create(path)?,
2✔
197
        }
198
    } else if path.exists() {
2✔
199
        Pager::open(path)?
×
200
    } else {
201
        Pager::create(path)?
2✔
202
    };
203

204
    pager.clear_staged();
2✔
205

206
    // Page 0 is the header; payload pages start at 1.
207
    let mut next_free_page: u32 = 1;
2✔
208

209
    // 1. Stage each user table's B-Tree, collecting master-row info.
210
    //    `kind` is "table" or "index" — master has one row per each.
211
    let mut master_rows: Vec<CatalogEntry> = Vec::new();
2✔
212

213
    let mut table_names: Vec<&String> = db.tables.keys().collect();
4✔
214
    table_names.sort();
4✔
215
    for name in table_names {
4✔
216
        if name == MASTER_TABLE_NAME {
4✔
217
            return Err(SQLRiteError::Internal(format!(
×
218
                "user table cannot be named '{MASTER_TABLE_NAME}' (reserved)"
219
            )));
220
        }
221
        let table = &db.tables[name];
4✔
222
        let (rootpage, new_next) = stage_table_btree(&mut pager, table, next_free_page)?;
2✔
223
        next_free_page = new_next;
2✔
224
        master_rows.push(CatalogEntry {
2✔
225
            kind: "table".into(),
2✔
226
            name: name.clone(),
2✔
227
            sql: table_to_create_sql(table),
2✔
228
            rootpage,
229
            last_rowid: table.last_rowid,
2✔
230
        });
231
    }
232

233
    // 2. Stage each secondary index's B-Tree. Indexes persist in a
234
    //    deterministic order: sorted by (owning_table, index_name).
235
    let mut index_entries: Vec<(&Table, &SecondaryIndex)> = Vec::new();
2✔
236
    for table in db.tables.values() {
4✔
237
        for idx in &table.secondary_indexes {
4✔
238
            index_entries.push((table, idx));
2✔
239
        }
240
    }
241
    index_entries
2✔
242
        .sort_by(|(ta, ia), (tb, ib)| ta.tb_name.cmp(&tb.tb_name).then(ia.name.cmp(&ib.name)));
4✔
243
    for (_table, idx) in index_entries {
4✔
244
        let (rootpage, new_next) = stage_index_btree(&mut pager, idx, next_free_page)?;
4✔
245
        next_free_page = new_next;
2✔
246
        master_rows.push(CatalogEntry {
2✔
247
            kind: "index".into(),
2✔
248
            name: idx.name.clone(),
2✔
249
            sql: idx.synthesized_sql(),
2✔
250
            rootpage,
251
            last_rowid: 0,
252
        });
253
    }
254

255
    // 2b. Phase 7d.3: persist HNSW indexes as their own cell-encoded
256
    //     page trees, with the rootpage recorded in sqlrite_master.
257
    //     Reopen loads the graph back from cells (fast, exact match)
258
    //     instead of rebuilding from rows.
259
    //
260
    //     Dirty indexes (set by DELETE / UPDATE-on-vector-col) are
261
    //     rebuilt from current rows BEFORE staging, so the on-disk
262
    //     graph reflects the current row set.
263
    let mut hnsw_entries: Vec<(&Table, &crate::sql::db::table::HnswIndexEntry)> = Vec::new();
2✔
264
    for table in db.tables.values() {
4✔
265
        for entry in &table.hnsw_indexes {
4✔
266
            hnsw_entries.push((table, entry));
1✔
267
        }
268
    }
269
    hnsw_entries
2✔
270
        .sort_by(|(ta, ea), (tb, eb)| ta.tb_name.cmp(&tb.tb_name).then(ea.name.cmp(&eb.name)));
2✔
271
    for (table, entry) in hnsw_entries {
4✔
272
        let (rootpage, new_next) = stage_hnsw_btree(&mut pager, &entry.index, next_free_page)?;
2✔
273
        next_free_page = new_next;
1✔
274
        master_rows.push(CatalogEntry {
1✔
275
            kind: "index".into(),
1✔
276
            name: entry.name.clone(),
1✔
277
            sql: format!(
2✔
278
                "CREATE INDEX {} ON {} USING hnsw ({})",
279
                entry.name, table.tb_name, entry.column_name
280
            ),
281
            rootpage,
282
            last_rowid: 0,
283
        });
284
    }
285

286
    // 3. Build an in-memory sqlrite_master with one row per table or index,
287
    //    then stage it via the same tree-build path.
288
    let mut master = build_empty_master_table();
2✔
289
    for (i, entry) in master_rows.into_iter().enumerate() {
8✔
290
        let rowid = (i as i64) + 1;
4✔
291
        master.restore_row(
2✔
292
            rowid,
293
            vec![
4✔
294
                Some(Value::Text(entry.kind)),
2✔
295
                Some(Value::Text(entry.name)),
2✔
296
                Some(Value::Text(entry.sql)),
2✔
297
                Some(Value::Integer(entry.rootpage as i64)),
2✔
298
                Some(Value::Integer(entry.last_rowid)),
2✔
299
            ],
300
        )?;
301
    }
302
    let (master_root, master_next) = stage_table_btree(&mut pager, &master, next_free_page)?;
2✔
303
    next_free_page = master_next;
2✔
304

305
    pager.commit(DbHeader {
2✔
306
        page_count: next_free_page,
2✔
307
        schema_root_page: master_root,
308
    })?;
309

310
    if same_path {
4✔
311
        db.pager = Some(pager);
2✔
312
    }
313
    Ok(())
2✔
314
}
315

316
/// Build material for a single row in sqlrite_master.
317
struct CatalogEntry {
318
    kind: String, // "table" or "index"
319
    name: String,
320
    sql: String,
321
    rootpage: u32,
322
    last_rowid: i64,
323
}
324

325
// -------------------------------------------------------------------------
326
// sqlrite_master — hardcoded catalog table schema
327

328
fn build_empty_master_table() -> Table {
3✔
329
    // Phase 3e: `type` is the first column, matching SQLite's convention.
330
    // It distinguishes `'table'` rows from `'index'` rows.
331
    let columns = vec![
4✔
332
        Column::new("type".into(), "text".into(), false, true, false),
4✔
333
        Column::new("name".into(), "text".into(), true, true, true),
4✔
334
        Column::new("sql".into(), "text".into(), false, true, false),
4✔
335
        Column::new("rootpage".into(), "integer".into(), false, true, false),
4✔
336
        Column::new("last_rowid".into(), "integer".into(), false, true, false),
4✔
337
    ];
338
    build_empty_table(MASTER_TABLE_NAME, columns, 0)
2✔
339
}
340

341
/// Reads a required Text column from a known-good catalog row.
342
fn take_text(table: &Table, col: &str, rowid: i64) -> Result<String> {
2✔
343
    match table.get_value(col, rowid) {
2✔
344
        Some(Value::Text(s)) => Ok(s),
2✔
345
        other => Err(SQLRiteError::Internal(format!(
×
346
            "sqlrite_master column '{col}' at rowid {rowid}: expected Text, got {other:?}"
347
        ))),
348
    }
349
}
350

351
/// Reads a required Integer column from a known-good catalog row.
352
fn take_integer(table: &Table, col: &str, rowid: i64) -> Result<i64> {
3✔
353
    match table.get_value(col, rowid) {
2✔
354
        Some(Value::Integer(v)) => Ok(v),
2✔
355
        other => Err(SQLRiteError::Internal(format!(
×
356
            "sqlrite_master column '{col}' at rowid {rowid}: expected Integer, got {other:?}"
357
        ))),
358
    }
359
}
360

361
// -------------------------------------------------------------------------
362
// CREATE-TABLE SQL synthesis and re-parsing
363

364
/// Synthesizes a CREATE TABLE SQL string that recreates the table's schema.
365
/// Deterministic: same schema → same SQL, so diffing commits stay stable.
366
fn table_to_create_sql(table: &Table) -> String {
2✔
367
    let mut parts = Vec::with_capacity(table.columns.len());
2✔
368
    for c in &table.columns {
4✔
369
        // Render the SQL type literally so the round-trip through
370
        // CREATE TABLE re-parsing recreates the same schema. Vector
371
        // carries its dimension inline.
372
        let ty: String = match &c.datatype {
2✔
373
            DataType::Integer => "INTEGER".to_string(),
4✔
374
            DataType::Text => "TEXT".to_string(),
4✔
375
            DataType::Real => "REAL".to_string(),
×
376
            DataType::Bool => "BOOLEAN".to_string(),
×
377
            DataType::Vector(dim) => format!("VECTOR({dim})"),
2✔
378
            DataType::None | DataType::Invalid => "TEXT".to_string(),
×
379
        };
380
        let mut piece = format!("{} {}", c.column_name, ty);
4✔
381
        if c.is_pk {
2✔
382
            piece.push_str(" PRIMARY KEY");
4✔
383
        } else {
384
            if c.is_unique {
2✔
385
                piece.push_str(" UNIQUE");
2✔
386
            }
387
            if c.not_null {
2✔
388
                piece.push_str(" NOT NULL");
2✔
389
            }
390
        }
391
        parts.push(piece);
2✔
392
    }
393
    format!("CREATE TABLE {} ({});", table.tb_name, parts.join(", "))
2✔
394
}
395

396
/// Reverses `table_to_create_sql`: feeds the SQL back through `sqlparser`
397
/// and produces our internal column list. Returns `(table_name, columns)`.
398
fn parse_create_sql(sql: &str) -> Result<(String, Vec<Column>)> {
2✔
399
    let dialect = SQLiteDialect {};
2✔
400
    let mut ast = Parser::parse_sql(&dialect, sql).map_err(SQLRiteError::from)?;
2✔
401
    let stmt = ast.pop().ok_or_else(|| {
4✔
402
        SQLRiteError::Internal("sqlrite_master row held an empty SQL string".to_string())
×
403
    })?;
404
    let create = CreateQuery::new(&stmt)?;
4✔
405
    let columns = create
2✔
406
        .columns
407
        .into_iter()
408
        .map(|pc| Column::new(pc.name, pc.datatype, pc.is_pk, pc.not_null, pc.is_unique))
6✔
409
        .collect();
410
    Ok((create.table_name, columns))
2✔
411
}
412

413
// -------------------------------------------------------------------------
414
// In-memory table (re)construction
415

416
/// Builds an empty in-memory `Table` given the declared columns.
417
fn build_empty_table(name: &str, columns: Vec<Column>, last_rowid: i64) -> Table {
2✔
418
    let rows: Arc<Mutex<HashMap<String, Row>>> = Arc::new(Mutex::new(HashMap::new()));
4✔
419
    let mut secondary_indexes: Vec<SecondaryIndex> = Vec::new();
2✔
420
    {
421
        let mut map = rows.lock().expect("rows mutex poisoned");
4✔
422
        for col in &columns {
6✔
423
            // Mirror the dispatch in `Table::new` so the reconstructed
424
            // table has the same shape it'd have if it were built fresh
425
            // from SQL. Phase 7a adds the Vector arm — without it,
426
            // VECTOR columns silently restore as Row::None and every
427
            // restore_row hits a "storage None vs value Some(Vector(...))"
428
            // type mismatch.
429
            let row = match &col.datatype {
2✔
430
                DataType::Integer => Row::Integer(BTreeMap::new()),
4✔
431
                DataType::Text => Row::Text(BTreeMap::new()),
4✔
432
                DataType::Real => Row::Real(BTreeMap::new()),
×
433
                DataType::Bool => Row::Bool(BTreeMap::new()),
×
434
                DataType::Vector(_dim) => Row::Vector(BTreeMap::new()),
2✔
435
                DataType::None | DataType::Invalid => Row::None,
×
436
            };
437
            map.insert(col.column_name.clone(), row);
4✔
438

439
            // Auto-create UNIQUE/PK indexes so the restored table has the
440
            // same shape Table::new would have built from fresh SQL.
441
            if (col.is_pk || col.is_unique)
2✔
442
                && matches!(col.datatype, DataType::Integer | DataType::Text)
2✔
443
            {
444
                if let Ok(idx) = SecondaryIndex::new(
445
                    SecondaryIndex::auto_name(name, &col.column_name),
2✔
446
                    name.to_string(),
4✔
447
                    col.column_name.clone(),
2✔
448
                    &col.datatype,
449
                    true,
450
                    IndexOrigin::Auto,
451
                ) {
452
                    secondary_indexes.push(idx);
2✔
453
                }
454
            }
455
        }
456
    }
457

458
    let primary_key = columns
4✔
459
        .iter()
460
        .find(|c| c.is_pk)
6✔
461
        .map(|c| c.column_name.clone())
6✔
462
        .unwrap_or_else(|| "-1".to_string());
2✔
463

464
    Table {
465
        tb_name: name.to_string(),
2✔
466
        columns,
467
        rows,
468
        secondary_indexes,
469
        // HNSW indexes (Phase 7d.2) are reconstructed on open by re-
470
        // executing each `CREATE INDEX … USING hnsw` SQL stored in
471
        // `sqlrite_master`. This builder produces the empty shell;
472
        // `replay_create_index_for_hnsw` (in this same module) walks
473
        // sqlrite_master after every table is loaded and rebuilds the
474
        // graph from current row data. Persistence of the graph itself
475
        // (avoiding the on-open rebuild cost) is Phase 7d.3.
476
        hnsw_indexes: Vec::new(),
2✔
477
        last_rowid,
478
        primary_key,
479
    }
480
}
481

482
// -------------------------------------------------------------------------
483
// Leaf-chain read / write
484

485
/// Walks a table's B-Tree from `root_page`, following the leftmost-child
486
/// chain down to the first leaf, then iterating leaves via their sibling
487
/// `next_page` pointers. Every cell is decoded and replayed into `table`.
488
///
489
/// Open-path note: we eagerly materialize the entire table into `Table`'s
490
/// in-memory maps. Phase 5 will introduce a `Cursor` that hits the pager
491
/// on demand so queries can stream through the tree without a full upfront
492
/// load.
493
/// Re-parses `CREATE INDEX` SQL from sqlrite_master and restores the
494
/// index on its base table by walking the tree of index cells at
495
/// `rootpage`. The base table is expected to already be in `db.tables`.
496
fn attach_index(db: &mut Database, pager: &Pager, row: IndexCatalogRow) -> Result<()> {
2✔
497
    let (table_name, column_name, is_unique) = parse_create_index_sql(&row.sql)?;
4✔
498

499
    let table = db.get_table_mut(table_name.clone()).map_err(|_| {
4✔
500
        SQLRiteError::Internal(format!(
×
501
            "index '{}' references unknown table '{table_name}' (sqlrite_master out of sync?)",
502
            row.name
503
        ))
504
    })?;
505
    let datatype = table
6✔
506
        .columns
507
        .iter()
2✔
508
        .find(|c| c.column_name == column_name)
6✔
509
        .map(|c| clone_datatype(&c.datatype))
6✔
510
        .ok_or_else(|| {
2✔
511
            SQLRiteError::Internal(format!(
×
512
                "index '{}' references unknown column '{column_name}' on '{table_name}'",
513
                row.name
514
            ))
515
        })?;
516

517
    // An auto-index on this column may already exist (built by
518
    // build_empty_table for UNIQUE/PK columns). If the names match, reuse
519
    // the slot instead of adding a duplicate entry.
520
    let existing_slot = table
6✔
521
        .secondary_indexes
522
        .iter()
523
        .position(|i| i.name == row.name);
6✔
524
    let idx = match existing_slot {
2✔
525
        Some(i) => {
2✔
526
            // Drain any entries that may have been populated during table
527
            // restore_row calls — we're about to repopulate from the
528
            // persisted tree.
529
            table.secondary_indexes.remove(i)
4✔
530
        }
531
        None => SecondaryIndex::new(
2✔
532
            row.name.clone(),
1✔
533
            table_name.clone(),
2✔
534
            column_name.clone(),
1✔
535
            &datatype,
536
            is_unique,
537
            IndexOrigin::Explicit,
538
        )?,
539
    };
540
    let mut idx = idx;
2✔
541
    // Wipe any stale entries from the auto path so the load is idempotent.
542
    let is_unique_flag = idx.is_unique;
2✔
543
    let origin = idx.origin;
2✔
544
    idx = SecondaryIndex::new(
6✔
545
        idx.name,
2✔
546
        idx.table_name,
2✔
547
        idx.column_name,
2✔
548
        &datatype,
549
        is_unique_flag,
550
        origin,
551
    )?;
552

553
    // Populate from the index tree's cells.
554
    load_index_rows(pager, &mut idx, row.rootpage)?;
2✔
555

556
    table.secondary_indexes.push(idx);
2✔
557
    Ok(())
2✔
558
}
559

560
/// Walks the leaves of an index B-Tree rooted at `root_page` and inserts
561
/// every `(value, rowid)` pair into `idx`.
562
fn load_index_rows(pager: &Pager, idx: &mut SecondaryIndex, root_page: u32) -> Result<()> {
2✔
563
    if root_page == 0 {
2✔
564
        return Ok(());
×
565
    }
566
    let first_leaf = find_leftmost_leaf(pager, root_page)?;
2✔
567
    let mut current = first_leaf;
2✔
568
    while current != 0 {
2✔
569
        let page_buf = pager
2✔
570
            .read_page(current)
2✔
571
            .ok_or_else(|| SQLRiteError::Internal(format!("missing index leaf page {current}")))?;
2✔
572
        if page_buf[0] != PageType::TableLeaf as u8 {
2✔
573
            return Err(SQLRiteError::Internal(format!(
×
574
                "page {current} tagged {} but expected TableLeaf (index)",
575
                page_buf[0]
576
            )));
577
        }
578
        let next_leaf = u32::from_le_bytes(page_buf[1..5].try_into().unwrap());
2✔
579
        let payload: &[u8; PAYLOAD_PER_PAGE] = (&page_buf[PAGE_HEADER_SIZE..])
4✔
580
            .try_into()
2✔
581
            .map_err(|_| SQLRiteError::Internal("index leaf payload size".to_string()))?;
2✔
582
        let leaf = TablePage::from_bytes(payload);
2✔
583

584
        for slot in 0..leaf.slot_count() {
4✔
585
            // Slots on an index page hold KIND_INDEX cells; decode directly.
586
            let offset = leaf.slot_offset_raw(slot)?;
4✔
587
            let (ic, _) = IndexCell::decode(leaf.as_bytes(), offset)?;
2✔
588
            idx.insert(&ic.value, ic.rowid)?;
4✔
589
        }
590
        current = next_leaf;
2✔
591
    }
592
    Ok(())
2✔
593
}
594

595
/// Minimal recognizer for the synthesized-or-user `CREATE INDEX` SQL we
596
/// store in sqlrite_master. Returns `(table_name, column_name, is_unique)`.
597
///
598
/// Uses sqlparser so user-supplied SQL with extra whitespace, case, etc.
599
/// still works; the only shape we accept is single-column indexes.
600
fn parse_create_index_sql(sql: &str) -> Result<(String, String, bool)> {
2✔
601
    use sqlparser::ast::{CreateIndex, Expr, Statement};
602

603
    let dialect = SQLiteDialect {};
2✔
604
    let mut ast = Parser::parse_sql(&dialect, sql).map_err(SQLRiteError::from)?;
2✔
605
    let Some(Statement::CreateIndex(CreateIndex {
4✔
606
        table_name,
2✔
607
        columns,
2✔
608
        unique,
2✔
609
        ..
610
    })) = ast.pop()
6✔
611
    else {
612
        return Err(SQLRiteError::Internal(format!(
×
613
            "sqlrite_master index row's SQL isn't a CREATE INDEX: {sql}"
614
        )));
615
    };
616
    if columns.len() != 1 {
4✔
617
        return Err(SQLRiteError::NotImplemented(
×
618
            "multi-column indexes aren't supported yet".to_string(),
×
619
        ));
620
    }
621
    let col = match &columns[0].column.expr {
4✔
622
        Expr::Identifier(ident) => ident.value.clone(),
4✔
623
        Expr::CompoundIdentifier(parts) => {
×
624
            parts.last().map(|p| p.value.clone()).unwrap_or_default()
×
625
        }
626
        other => {
×
627
            return Err(SQLRiteError::Internal(format!(
×
628
                "unsupported indexed column expression: {other:?}"
629
            )));
630
        }
631
    };
632
    Ok((table_name.to_string(), col, unique))
4✔
633
}
634

635
/// True iff a CREATE INDEX SQL string uses `USING hnsw` (case-insensitive).
636
/// Used by the open path to route HNSW indexes to the graph-rebuild path
637
/// instead of the standard B-Tree cell-load. Pre-Phase-7d.2 indexes
638
/// don't have a USING clause, so they all return false and continue
639
/// taking the existing path.
640
fn create_index_sql_uses_hnsw(sql: &str) -> bool {
2✔
641
    use sqlparser::ast::{CreateIndex, IndexType, Statement};
642

643
    let dialect = SQLiteDialect {};
2✔
644
    let Ok(mut ast) = Parser::parse_sql(&dialect, sql) else {
4✔
645
        return false;
×
646
    };
647
    let Some(Statement::CreateIndex(CreateIndex { using, .. })) = ast.pop() else {
6✔
648
        return false;
×
649
    };
650
    matches!(using, Some(IndexType::Custom(ident)) if ident.value.eq_ignore_ascii_case("hnsw"))
3✔
651
}
652

653
/// Loads (or rebuilds) an HNSW index on database open. Two paths:
654
///
655
///   - **rootpage != 0** (Phase 7d.3 default): the graph is persisted
656
///     as cell-encoded pages. Read every node directly via
657
///     `load_hnsw_nodes` and reconstruct the index — fast, zero
658
///     algorithm runs, exact bit-for-bit reproduction of what was saved.
659
///
660
///   - **rootpage == 0** (compatibility): no on-disk graph, e.g. for
661
///     files saved by Phase 7d.2 before persistence landed. Replay the
662
///     CREATE INDEX SQL through `execute_create_index`, which walks the
663
///     table's current rows and populates a fresh graph. Slower but
664
///     correctness-equivalent on the first save with the new code.
665
fn rebuild_hnsw_index(db: &mut Database, pager: &Pager, row: &IndexCatalogRow) -> Result<()> {
1✔
666
    use crate::sql::db::table::HnswIndexEntry;
667
    use crate::sql::executor::execute_create_index;
668
    use crate::sql::hnsw::{DistanceMetric, HnswIndex};
669
    use sqlparser::ast::Statement;
670

671
    let dialect = SQLiteDialect {};
1✔
672
    let mut ast = Parser::parse_sql(&dialect, &row.sql).map_err(SQLRiteError::from)?;
1✔
673
    let Some(stmt @ Statement::CreateIndex(_)) = ast.pop() else {
3✔
674
        return Err(SQLRiteError::Internal(format!(
×
675
            "sqlrite_master HNSW row's SQL isn't a CREATE INDEX: {}",
676
            row.sql
677
        )));
678
    };
679

680
    if row.rootpage == 0 {
1✔
681
        // Compatibility path — no persisted graph; walk current rows.
NEW
682
        execute_create_index(&stmt, db)?;
×
NEW
683
        return Ok(());
×
684
    }
685

686
    // Persistence path — read the cell tree, deserialize.
687
    let nodes = load_hnsw_nodes(pager, row.rootpage)?;
2✔
688
    let index = HnswIndex::from_persisted_nodes(DistanceMetric::L2, 0xC0FFEE, nodes);
2✔
689

690
    // Parse the CREATE INDEX to know which table + column to attach to
691
    // — same shape as the row-walk path; we just don't execute it.
692
    let (tbl_name, col_name) = parse_hnsw_create_index_sql(&row.sql)?;
2✔
693
    let table_mut = db.get_table_mut(tbl_name.clone()).map_err(|_| {
2✔
NEW
694
        SQLRiteError::Internal(format!(
×
695
            "HNSW index '{}' references unknown table '{tbl_name}'",
696
            row.name
697
        ))
698
    })?;
699
    table_mut.hnsw_indexes.push(HnswIndexEntry {
2✔
700
        name: row.name.clone(),
1✔
701
        column_name: col_name,
1✔
702
        index,
1✔
703
        needs_rebuild: false,
704
    });
705
    Ok(())
1✔
706
}
707

708
/// Phase 7d.3 — Phase-7d.3-side helper: walk every leaf in the HNSW
709
/// page tree at `root_page` and decode each cell as a node. Returns
710
/// the (node_id, layers) tuples in slot-order (already ascending by
711
/// node_id since they were staged that way). The caller hands them to
712
/// `HnswIndex::from_persisted_nodes`.
713
fn load_hnsw_nodes(pager: &Pager, root_page: u32) -> Result<Vec<(i64, Vec<Vec<i64>>)>> {
1✔
714
    use crate::sql::pager::hnsw_cell::HnswNodeCell;
715

716
    let mut nodes: Vec<(i64, Vec<Vec<i64>>)> = Vec::new();
1✔
717
    let first_leaf = find_leftmost_leaf(pager, root_page)?;
2✔
718
    let mut current = first_leaf;
1✔
719
    while current != 0 {
1✔
720
        let page_buf = pager
1✔
721
            .read_page(current)
1✔
722
            .ok_or_else(|| SQLRiteError::Internal(format!("missing HNSW leaf page {current}")))?;
1✔
723
        if page_buf[0] != PageType::TableLeaf as u8 {
1✔
NEW
724
            return Err(SQLRiteError::Internal(format!(
×
725
                "page {current} tagged {} but expected TableLeaf (HNSW)",
NEW
726
                page_buf[0]
×
727
            )));
728
        }
729
        let next_leaf = u32::from_le_bytes(page_buf[1..5].try_into().unwrap());
2✔
730
        let payload: &[u8; PAYLOAD_PER_PAGE] = (&page_buf[PAGE_HEADER_SIZE..])
2✔
731
            .try_into()
1✔
732
            .map_err(|_| SQLRiteError::Internal("HNSW leaf payload size".to_string()))?;
1✔
733
        let leaf = TablePage::from_bytes(payload);
1✔
734
        for slot in 0..leaf.slot_count() {
3✔
735
            let offset = leaf.slot_offset_raw(slot)?;
2✔
736
            let (cell, _) = HnswNodeCell::decode(leaf.as_bytes(), offset)?;
1✔
737
            nodes.push((cell.node_id, cell.layers));
1✔
738
        }
739
        current = next_leaf;
1✔
740
    }
741
    Ok(nodes)
1✔
742
}
743

744
/// Pulls (table_name, column_name) out of a `CREATE INDEX … USING hnsw (col)`
745
/// SQL string. Used by the persistence path on open to know where to
746
/// attach the loaded graph. Same shape as `parse_create_index_sql` for
747
/// regular indexes — only the assertion differs (we don't care about
748
/// UNIQUE for HNSW).
749
fn parse_hnsw_create_index_sql(sql: &str) -> Result<(String, String)> {
1✔
750
    use sqlparser::ast::{CreateIndex, Expr, Statement};
751

752
    let dialect = SQLiteDialect {};
1✔
753
    let mut ast = Parser::parse_sql(&dialect, sql).map_err(SQLRiteError::from)?;
1✔
754
    let Some(Statement::CreateIndex(CreateIndex {
2✔
755
        table_name,
1✔
756
        columns,
1✔
757
        ..
758
    })) = ast.pop()
3✔
759
    else {
NEW
760
        return Err(SQLRiteError::Internal(format!(
×
761
            "sqlrite_master HNSW row's SQL isn't a CREATE INDEX: {sql}"
762
        )));
763
    };
764
    if columns.len() != 1 {
2✔
NEW
765
        return Err(SQLRiteError::NotImplemented(
×
NEW
766
            "multi-column HNSW indexes aren't supported yet".to_string(),
×
767
        ));
768
    }
769
    let col = match &columns[0].column.expr {
2✔
770
        Expr::Identifier(ident) => ident.value.clone(),
2✔
NEW
771
        Expr::CompoundIdentifier(parts) => {
×
NEW
772
            parts.last().map(|p| p.value.clone()).unwrap_or_default()
×
773
        }
NEW
774
        other => {
×
NEW
775
            return Err(SQLRiteError::Internal(format!(
×
776
                "unsupported HNSW indexed column expression: {other:?}"
777
            )));
778
        }
779
    };
780
    Ok((table_name.to_string(), col))
2✔
781
}
782

783
/// Phase 7d.3 — rebuilds in-place any HnswIndexEntry whose
784
/// `needs_rebuild` flag is set (DELETE / UPDATE-on-vector marked it).
785
/// Walks the table's current Vec<f32> column storage and runs the
786
/// HNSW algorithm fresh. Called at the top of `save_database` before
787
/// any immutable borrows of `db` start.
788
///
789
/// Cost: O(N · ef_construction · log N) per dirty index. Fine for
790
/// small tables, expensive for ≥100k-row tables — matches the
791
/// trade-off SQLite makes for FTS5: dirtying-and-rebuilding is the
792
/// MVP, more sophisticated incremental delete strategies (soft-delete
793
/// + tombstones, neighbor reconnection) are future polish.
794
fn rebuild_dirty_hnsw_indexes(db: &mut Database) {
2✔
795
    use crate::sql::hnsw::{DistanceMetric, HnswIndex};
796

797
    for table in db.tables.values_mut() {
5✔
798
        // Snapshot which (index_name, column) pairs need rebuilding,
799
        // before we go grabbing column data — keeps the borrow
800
        // structure simple.
801
        let dirty: Vec<(String, String)> = table
4✔
802
            .hnsw_indexes
803
            .iter()
804
            .filter(|e| e.needs_rebuild)
4✔
805
            .map(|e| (e.name.clone(), e.column_name.clone()))
4✔
806
            .collect();
807
        if dirty.is_empty() {
4✔
808
            continue;
809
        }
810

811
        for (idx_name, col_name) in dirty {
3✔
812
            // Snapshot every (rowid, vec) for this column.
813
            let mut vectors: Vec<(i64, Vec<f32>)> = Vec::new();
1✔
814
            {
815
                let row_data = table.rows.lock().expect("rows mutex poisoned");
2✔
816
                if let Some(Row::Vector(map)) = row_data.get(&col_name) {
3✔
817
                    for (id, v) in map.iter() {
1✔
818
                        vectors.push((*id, v.clone()));
1✔
819
                    }
820
                }
821
            }
822
            // Pre-build a HashMap for the get_vec closure so we don't
823
            // pay O(N) lookup per insert call.
824
            let snapshot: std::collections::HashMap<i64, Vec<f32>> =
1✔
825
                vectors.iter().cloned().collect();
826

827
            let mut new_idx = HnswIndex::new(DistanceMetric::L2, 0xC0FFEE);
2✔
828
            // Sort by id so the rebuild is deterministic across runs.
829
            vectors.sort_by_key(|(id, _)| *id);
4✔
830
            for (id, v) in &vectors {
1✔
831
                new_idx.insert(*id, v, |q| snapshot.get(&q).cloned().unwrap_or_default());
4✔
832
            }
833

834
            // Replace the entry's index + clear the dirty flag.
835
            if let Some(entry) = table.hnsw_indexes.iter_mut().find(|e| e.name == idx_name) {
4✔
836
                entry.index = new_idx;
1✔
837
                entry.needs_rebuild = false;
1✔
838
            }
839
        }
840
    }
841
}
842

843
/// Cheap clone helper — `DataType` doesn't derive `Clone` elsewhere.
844
fn clone_datatype(dt: &DataType) -> DataType {
2✔
845
    match dt {
2✔
846
        DataType::Integer => DataType::Integer,
2✔
847
        DataType::Text => DataType::Text,
1✔
848
        DataType::Real => DataType::Real,
×
849
        DataType::Bool => DataType::Bool,
×
850
        DataType::Vector(dim) => DataType::Vector(*dim),
×
851
        DataType::None => DataType::None,
×
852
        DataType::Invalid => DataType::Invalid,
×
853
    }
854
}
855

856
/// Stages an index's B-Tree at `start_page`. Each leaf cell is a
857
/// `KIND_INDEX` entry carrying `(original_rowid, value)`. Returns
858
/// `(root_page, next_free_page)`.
859
///
860
/// The tree's shape matches a regular table's — leaves chained via
861
/// `next_page`, optional interior layer above. `Cell::peek_rowid` works
862
/// uniformly for index cells (same prefix as local cells), so the
863
/// existing slot directory and binary search carry over.
864
fn stage_index_btree(
2✔
865
    pager: &mut Pager,
866
    idx: &SecondaryIndex,
867
    start_page: u32,
868
) -> Result<(u32, u32)> {
869
    // Build the leaves.
870
    let (leaves, mut next_free_page) = stage_index_leaves(pager, idx, start_page)?;
2✔
871
    if leaves.len() == 1 {
4✔
872
        return Ok((leaves[0].0, next_free_page));
4✔
873
    }
874
    let mut level: Vec<(u32, i64)> = leaves;
1✔
875
    while level.len() > 1 {
4✔
876
        let (next_level, new_next_free) = stage_interior_level(pager, &level, next_free_page)?;
2✔
877
        next_free_page = new_next_free;
1✔
878
        level = next_level;
2✔
879
    }
880
    Ok((level[0].0, next_free_page))
2✔
881
}
882

883
/// Packs the index's (value, rowid) entries into a sibling-chained run
884
/// of `TableLeaf` pages. Iteration order matches `SecondaryIndex::iter_entries`
885
/// (ascending value; rowids in insertion order within a value), which is
886
/// also ascending by the "cell rowid" carried in each IndexCell (the
887
/// original row's rowid) — so Cell::peek_rowid + the slot directory's
888
/// rowid ordering stays consistent.
889
fn stage_index_leaves(
2✔
890
    pager: &mut Pager,
891
    idx: &SecondaryIndex,
892
    start_page: u32,
893
) -> Result<(Vec<(u32, i64)>, u32)> {
894
    let mut leaves: Vec<(u32, i64)> = Vec::new();
2✔
895
    let mut current_leaf = TablePage::empty();
4✔
896
    let mut current_leaf_page = start_page;
2✔
897
    let mut current_max_rowid: Option<i64> = None;
2✔
898
    let mut next_free_page = start_page + 1;
2✔
899

900
    // Sort the entries by original rowid so the in-page slot directory,
901
    // which binary-searches by rowid, stays valid. (iter_entries orders by
902
    // value; we reorder here for B-Tree correctness.)
903
    let mut entries: Vec<(Value, i64)> = idx.iter_entries().collect();
4✔
904
    entries.sort_by_key(|(_, r)| *r);
6✔
905

906
    for (value, rowid) in entries {
4✔
907
        let cell = IndexCell::new(rowid, value);
2✔
908
        let entry_bytes = cell.encode()?;
4✔
909

910
        if !current_leaf.would_fit(entry_bytes.len()) {
4✔
911
            let next_leaf_page_num = next_free_page;
1✔
912
            emit_leaf(pager, current_leaf_page, &current_leaf, next_leaf_page_num);
1✔
913
            leaves.push((current_leaf_page, current_max_rowid.unwrap_or(i64::MIN)));
1✔
914
            current_leaf = TablePage::empty();
1✔
915
            current_leaf_page = next_leaf_page_num;
1✔
916
            next_free_page += 1;
1✔
917

918
            if !current_leaf.would_fit(entry_bytes.len()) {
2✔
919
                return Err(SQLRiteError::Internal(format!(
×
920
                    "index entry of {} bytes exceeds empty-page capacity {}",
921
                    entry_bytes.len(),
×
922
                    current_leaf.free_space()
×
923
                )));
924
            }
925
        }
926
        current_leaf.insert_entry(rowid, &entry_bytes)?;
4✔
927
        current_max_rowid = Some(rowid);
2✔
928
    }
929

930
    emit_leaf(pager, current_leaf_page, &current_leaf, 0);
2✔
931
    leaves.push((current_leaf_page, current_max_rowid.unwrap_or(i64::MIN)));
2✔
932
    Ok((leaves, next_free_page))
2✔
933
}
934

935
/// Phase 7d.3 — stages an HNSW index's page tree at `start_page`.
936
/// Each leaf cell is a `KIND_HNSW` entry carrying one node's
937
/// (node_id, layers). Returns `(root_page, next_free_page)`.
938
///
939
/// Tree shape is identical to `stage_index_btree` — chained leaves +
940
/// optional interior layers. The slot directory binary-searches by
941
/// node_id (which is the cell's "rowid" in `Cell::peek_rowid` terms),
942
/// so reads can locate any node in O(log N) once 7d.4-or-later
943
/// optimizes the load path to lazy-fetch instead of read-all.
944
/// Today, `load_hnsw_nodes` reads the entire tree on open.
945
fn stage_hnsw_btree(
1✔
946
    pager: &mut Pager,
947
    idx: &crate::sql::hnsw::HnswIndex,
948
    start_page: u32,
949
) -> Result<(u32, u32)> {
950
    let (leaves, mut next_free_page) = stage_hnsw_leaves(pager, idx, start_page)?;
1✔
951
    if leaves.len() == 1 {
2✔
952
        return Ok((leaves[0].0, next_free_page));
2✔
953
    }
NEW
954
    let mut level: Vec<(u32, i64)> = leaves;
×
NEW
955
    while level.len() > 1 {
×
NEW
956
        let (next_level, new_next_free) = stage_interior_level(pager, &level, next_free_page)?;
×
NEW
957
        next_free_page = new_next_free;
×
NEW
958
        level = next_level;
×
959
    }
NEW
960
    Ok((level[0].0, next_free_page))
×
961
}
962

963
/// Packs HNSW nodes into a sibling-chained run of `TableLeaf` pages.
964
/// `serialize_nodes` already returns nodes in ascending node_id order,
965
/// so the slot directory's rowid ordering stays valid.
966
fn stage_hnsw_leaves(
1✔
967
    pager: &mut Pager,
968
    idx: &crate::sql::hnsw::HnswIndex,
969
    start_page: u32,
970
) -> Result<(Vec<(u32, i64)>, u32)> {
971
    use crate::sql::pager::hnsw_cell::HnswNodeCell;
972

973
    let mut leaves: Vec<(u32, i64)> = Vec::new();
1✔
974
    let mut current_leaf = TablePage::empty();
2✔
975
    let mut current_leaf_page = start_page;
1✔
976
    let mut current_max_rowid: Option<i64> = None;
1✔
977
    let mut next_free_page = start_page + 1;
1✔
978

979
    let serialized = idx.serialize_nodes();
1✔
980

981
    // Empty index → emit a single empty leaf page so the rootpage
982
    // pointer in sqlrite_master stays nonzero (== "graph is persisted,
983
    // it just happens to be empty"). load_hnsw_nodes is fine with an
984
    // empty leaf — slot_count() returns 0.
985
    for (node_id, layers) in serialized {
2✔
986
        let cell = HnswNodeCell::new(node_id, layers);
1✔
987
        let entry_bytes = cell.encode()?;
2✔
988

989
        if !current_leaf.would_fit(entry_bytes.len()) {
2✔
NEW
990
            let next_leaf_page_num = next_free_page;
×
NEW
991
            emit_leaf(pager, current_leaf_page, &current_leaf, next_leaf_page_num);
×
NEW
992
            leaves.push((current_leaf_page, current_max_rowid.unwrap_or(i64::MIN)));
×
NEW
993
            current_leaf = TablePage::empty();
×
NEW
994
            current_leaf_page = next_leaf_page_num;
×
NEW
995
            next_free_page += 1;
×
996

NEW
997
            if !current_leaf.would_fit(entry_bytes.len()) {
×
NEW
998
                return Err(SQLRiteError::Internal(format!(
×
999
                    "HNSW node {node_id} cell of {} bytes exceeds empty-page capacity {}",
NEW
1000
                    entry_bytes.len(),
×
NEW
1001
                    current_leaf.free_space()
×
1002
                )));
1003
            }
1004
        }
1005
        current_leaf.insert_entry(node_id, &entry_bytes)?;
2✔
1006
        current_max_rowid = Some(node_id);
1✔
1007
    }
1008

1009
    emit_leaf(pager, current_leaf_page, &current_leaf, 0);
1✔
1010
    leaves.push((current_leaf_page, current_max_rowid.unwrap_or(i64::MIN)));
1✔
1011
    Ok((leaves, next_free_page))
1✔
1012
}
1013

1014
fn load_table_rows(pager: &Pager, table: &mut Table, root_page: u32) -> Result<()> {
2✔
1015
    let first_leaf = find_leftmost_leaf(pager, root_page)?;
2✔
1016
    let mut current = first_leaf;
2✔
1017
    while current != 0 {
2✔
1018
        let page_buf = pager
2✔
1019
            .read_page(current)
2✔
1020
            .ok_or_else(|| SQLRiteError::Internal(format!("missing leaf page {current}")))?;
2✔
1021
        if page_buf[0] != PageType::TableLeaf as u8 {
2✔
1022
            return Err(SQLRiteError::Internal(format!(
×
1023
                "page {current} tagged {} but expected TableLeaf",
1024
                page_buf[0]
1025
            )));
1026
        }
1027
        let next_leaf = u32::from_le_bytes(page_buf[1..5].try_into().unwrap());
2✔
1028
        let payload: &[u8; PAYLOAD_PER_PAGE] = (&page_buf[PAGE_HEADER_SIZE..])
4✔
1029
            .try_into()
2✔
1030
            .map_err(|_| SQLRiteError::Internal("leaf payload slice size".to_string()))?;
2✔
1031
        let leaf = TablePage::from_bytes(payload);
2✔
1032

1033
        for slot in 0..leaf.slot_count() {
6✔
1034
            let entry = leaf.entry_at(slot)?;
4✔
1035
            let cell = match entry {
2✔
1036
                PagedEntry::Local(c) => c,
2✔
1037
                PagedEntry::Overflow(r) => {
1✔
1038
                    let body_bytes =
2✔
1039
                        read_overflow_chain(pager, r.first_overflow_page, r.total_body_len)?;
1040
                    let (c, _) = Cell::decode(&body_bytes, 0)?;
2✔
1041
                    c
1✔
1042
                }
1043
            };
1044
            table.restore_row(cell.rowid, cell.values)?;
4✔
1045
        }
1046
        current = next_leaf;
2✔
1047
    }
1048
    Ok(())
2✔
1049
}
1050

1051
/// Descends from `root_page` through `InteriorNode` pages, always taking
1052
/// the leftmost child, until a `TableLeaf` is reached. Returns that leaf's
1053
/// page number. A root that's already a leaf is returned as-is.
1054
fn find_leftmost_leaf(pager: &Pager, root_page: u32) -> Result<u32> {
2✔
1055
    let mut current = root_page;
2✔
1056
    loop {
1057
        let page_buf = pager.read_page(current).ok_or_else(|| {
2✔
1058
            SQLRiteError::Internal(format!("missing page {current} during tree descent"))
×
1059
        })?;
1060
        match page_buf[0] {
1061
            t if t == PageType::TableLeaf as u8 => return Ok(current),
4✔
1062
            t if t == PageType::InteriorNode as u8 => {
2✔
1063
                let payload: &[u8; PAYLOAD_PER_PAGE] =
1✔
1064
                    (&page_buf[PAGE_HEADER_SIZE..]).try_into().map_err(|_| {
1065
                        SQLRiteError::Internal("interior payload slice size".to_string())
×
1066
                    })?;
1067
                let interior = InteriorPage::from_bytes(payload);
1✔
1068
                current = interior.leftmost_child()?;
2✔
1069
            }
1070
            other => {
×
1071
                return Err(SQLRiteError::Internal(format!(
×
1072
                    "unexpected page type {other} during tree descent at page {current}"
1073
                )));
1074
            }
1075
        }
1076
    }
1077
}
1078

1079
/// Stages a table's B-Tree starting at `start_page`. Returns
1080
/// `(root_page, next_free_page)`. Builds bottom-up:
1081
///
1082
/// 1. Pack all row cells into `TableLeaf` pages, chaining them via each
1083
///    leaf's `next_page` sibling pointer (for fast sequential scans).
1084
/// 2. If the table fits in a single leaf, that leaf is the root.
1085
/// 3. Otherwise, group leaves into `InteriorNode` pages; recurse up the
1086
///    tree until one root remains.
1087
///
1088
/// Deterministic: same in-memory rows → same pages at same offsets, so
1089
/// the Pager's diff commit still skips unchanged tables.
1090
fn stage_table_btree(pager: &mut Pager, table: &Table, start_page: u32) -> Result<(u32, u32)> {
2✔
1091
    let (leaves, mut next_free_page) = stage_leaves(pager, table, start_page)?;
2✔
1092
    if leaves.len() == 1 {
4✔
1093
        return Ok((leaves[0].0, next_free_page));
4✔
1094
    }
1095
    let mut level: Vec<(u32, i64)> = leaves;
1✔
1096
    while level.len() > 1 {
4✔
1097
        let (next_level, new_next_free) = stage_interior_level(pager, &level, next_free_page)?;
2✔
1098
        next_free_page = new_next_free;
1✔
1099
        level = next_level;
2✔
1100
    }
1101
    Ok((level[0].0, next_free_page))
2✔
1102
}
1103

1104
/// Packs the table's rows into a sibling-linked chain of `TableLeaf` pages.
1105
/// Returns each leaf's `(page_number, max_rowid)` (used by the next level
1106
/// up to build divider cells) and the first free page after the chain
1107
/// including any overflow pages allocated for oversized cells.
1108
fn stage_leaves(
2✔
1109
    pager: &mut Pager,
1110
    table: &Table,
1111
    start_page: u32,
1112
) -> Result<(Vec<(u32, i64)>, u32)> {
1113
    let mut leaves: Vec<(u32, i64)> = Vec::new();
2✔
1114
    let mut current_leaf = TablePage::empty();
4✔
1115
    let mut current_leaf_page = start_page;
2✔
1116
    let mut current_max_rowid: Option<i64> = None;
2✔
1117
    let mut next_free_page = start_page + 1;
2✔
1118

1119
    for rowid in table.rowids() {
6✔
1120
        let entry_bytes = build_row_entry(pager, table, rowid, &mut next_free_page)?;
4✔
1121

1122
        if !current_leaf.would_fit(entry_bytes.len()) {
4✔
1123
            // Commit the current leaf. Its sibling next_page is the page
1124
            // number where the new leaf will go — which is next_free_page
1125
            // right now (no overflow pages have been allocated between
1126
            // this decision and the new leaf's allocation below).
1127
            let next_leaf_page_num = next_free_page;
1✔
1128
            emit_leaf(pager, current_leaf_page, &current_leaf, next_leaf_page_num);
1✔
1129
            leaves.push((current_leaf_page, current_max_rowid.unwrap_or(i64::MIN)));
1✔
1130
            current_leaf = TablePage::empty();
1✔
1131
            current_leaf_page = next_leaf_page_num;
1✔
1132
            next_free_page += 1;
1✔
1133
            // current_max_rowid is reassigned by the insert below; no need
1134
            // to zero it out here.
1135

1136
            if !current_leaf.would_fit(entry_bytes.len()) {
2✔
1137
                return Err(SQLRiteError::Internal(format!(
×
1138
                    "entry of {} bytes exceeds empty-page capacity {}",
1139
                    entry_bytes.len(),
×
1140
                    current_leaf.free_space()
×
1141
                )));
1142
            }
1143
        }
1144
        current_leaf.insert_entry(rowid, &entry_bytes)?;
4✔
1145
        current_max_rowid = Some(rowid);
2✔
1146
    }
1147

1148
    // Final leaf: sibling next_page = 0 (end of chain).
1149
    emit_leaf(pager, current_leaf_page, &current_leaf, 0);
2✔
1150
    leaves.push((current_leaf_page, current_max_rowid.unwrap_or(i64::MIN)));
2✔
1151
    Ok((leaves, next_free_page))
2✔
1152
}
1153

1154
/// Encodes a single row's on-leaf entry — either the local cell bytes, or
1155
/// an `OverflowRef` pointing at a freshly-allocated overflow chain if the
1156
/// encoded cell exceeded the inline threshold. Advances `next_free_page`
1157
/// past any overflow pages used.
1158
fn build_row_entry(
2✔
1159
    pager: &mut Pager,
1160
    table: &Table,
1161
    rowid: i64,
1162
    next_free_page: &mut u32,
1163
) -> Result<Vec<u8>> {
1164
    let values = table.extract_row(rowid);
2✔
1165
    let local_cell = Cell::new(rowid, values);
2✔
1166
    let local_bytes = local_cell.encode()?;
4✔
1167
    if local_bytes.len() > OVERFLOW_THRESHOLD {
7✔
1168
        let overflow_start = *next_free_page;
1✔
1169
        *next_free_page = write_overflow_chain(pager, &local_bytes, overflow_start)?;
2✔
1170
        Ok(OverflowRef {
2✔
1171
            rowid,
1172
            total_body_len: local_bytes.len() as u64,
1✔
1173
            first_overflow_page: overflow_start,
1174
        }
1175
        .encode())
1✔
1176
    } else {
1177
        Ok(local_bytes)
2✔
1178
    }
1179
}
1180

1181
/// Builds one level of `InteriorNode` pages above the given children.
1182
/// Each interior packs as many dividers as will fit; the last child
1183
/// assigned to an interior becomes its `rightmost_child`. Returns the
1184
/// emitted interior pages as `(page_number, max_rowid_in_subtree)` so the
1185
/// next level can build on top of them.
1186
fn stage_interior_level(
1✔
1187
    pager: &mut Pager,
1188
    children: &[(u32, i64)],
1189
    start_page: u32,
1190
) -> Result<(Vec<(u32, i64)>, u32)> {
1191
    let mut next_level: Vec<(u32, i64)> = Vec::new();
1✔
1192
    let mut next_free_page = start_page;
1✔
1193
    let mut idx = 0usize;
1✔
1194

1195
    while idx < children.len() {
1✔
1196
        let interior_page_num = next_free_page;
1✔
1197
        next_free_page += 1;
2✔
1198

1199
        // Seed the interior with the first unassigned child as its
1200
        // rightmost. As we add more children, the previous rightmost
1201
        // graduates to being a divider and the new arrival takes over
1202
        // as rightmost.
1203
        let (mut rightmost_child_page, mut rightmost_child_max) = children[idx];
2✔
1204
        idx += 1;
2✔
1205
        let mut interior = InteriorPage::empty(rightmost_child_page);
2✔
1206

1207
        while idx < children.len() {
1✔
1208
            let new_divider_cell = InteriorCell {
1209
                divider_rowid: rightmost_child_max,
1210
                child_page: rightmost_child_page,
1211
            };
1212
            let new_divider_bytes = new_divider_cell.encode();
1✔
1213
            if !interior.would_fit(new_divider_bytes.len()) {
2✔
1214
                break;
1215
            }
1216
            interior.insert_divider(rightmost_child_max, rightmost_child_page)?;
2✔
1217
            let (next_child_page, next_child_max) = children[idx];
1✔
1218
            interior.set_rightmost_child(next_child_page);
1✔
1219
            rightmost_child_page = next_child_page;
1✔
1220
            rightmost_child_max = next_child_max;
1✔
1221
            idx += 1;
1✔
1222
        }
1223

1224
        emit_interior(pager, interior_page_num, &interior);
1✔
1225
        next_level.push((interior_page_num, rightmost_child_max));
1✔
1226
    }
1227

1228
    Ok((next_level, next_free_page))
1✔
1229
}
1230

1231
/// Wraps a `TablePage` in the 7-byte page header and hands it to the pager.
1232
fn emit_leaf(pager: &mut Pager, page_num: u32, leaf: &TablePage, next_leaf: u32) {
2✔
1233
    let mut buf = [0u8; PAGE_SIZE];
2✔
1234
    buf[0] = PageType::TableLeaf as u8;
2✔
1235
    buf[1..5].copy_from_slice(&next_leaf.to_le_bytes());
2✔
1236
    // For leaf pages the legacy `payload_len` field isn't used — the slot
1237
    // directory self-describes. Zero it by convention.
1238
    buf[5..7].copy_from_slice(&0u16.to_le_bytes());
2✔
1239
    buf[PAGE_HEADER_SIZE..].copy_from_slice(leaf.as_bytes());
2✔
1240
    pager.stage_page(page_num, buf);
2✔
1241
}
1242

1243
/// Wraps an `InteriorPage` in the 7-byte page header. Interior pages
1244
/// don't use `next_page` (there's no sibling chain between interiors);
1245
/// `payload_len` is also unused (the slot directory self-describes).
1246
fn emit_interior(pager: &mut Pager, page_num: u32, interior: &InteriorPage) {
1✔
1247
    let mut buf = [0u8; PAGE_SIZE];
1✔
1248
    buf[0] = PageType::InteriorNode as u8;
1✔
1249
    buf[1..5].copy_from_slice(&0u32.to_le_bytes());
1✔
1250
    buf[5..7].copy_from_slice(&0u16.to_le_bytes());
1✔
1251
    buf[PAGE_HEADER_SIZE..].copy_from_slice(interior.as_bytes());
1✔
1252
    pager.stage_page(page_num, buf);
1✔
1253
}
1254

1255
#[cfg(test)]
1256
mod tests {
1257
    use super::*;
1258
    use crate::sql::process_command;
1259

1260
    fn seed_db() -> Database {
1✔
1261
        let mut db = Database::new("test".to_string());
1✔
1262
        process_command(
1263
            "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE, age INTEGER);",
1264
            &mut db,
1265
        )
1266
        .unwrap();
1267
        process_command(
1268
            "INSERT INTO users (name, age) VALUES ('alice', 30);",
1269
            &mut db,
1270
        )
1271
        .unwrap();
1272
        process_command("INSERT INTO users (name, age) VALUES ('bob', 25);", &mut db).unwrap();
1✔
1273
        process_command(
1274
            "CREATE TABLE notes (id INTEGER PRIMARY KEY, body TEXT);",
1275
            &mut db,
1276
        )
1277
        .unwrap();
1278
        process_command("INSERT INTO notes (body) VALUES ('hello');", &mut db).unwrap();
1✔
1279
        db
1✔
1280
    }
1281

1282
    fn tmp_path(name: &str) -> std::path::PathBuf {
1✔
1283
        let mut p = std::env::temp_dir();
1✔
1284
        let pid = std::process::id();
2✔
1285
        let nanos = std::time::SystemTime::now()
2✔
1286
            .duration_since(std::time::UNIX_EPOCH)
1✔
1287
            .map(|d| d.as_nanos())
3✔
1288
            .unwrap_or(0);
1289
        p.push(format!("sqlrite-{pid}-{nanos}-{name}.sqlrite"));
1✔
1290
        p
1✔
1291
    }
1292

1293
    /// Phase 4c: every .sqlrite has a `-wal` sidecar now. Delete both so
1294
    /// `/tmp` doesn't accumulate orphan WALs across test runs.
1295
    fn cleanup(path: &std::path::Path) {
1✔
1296
        let _ = std::fs::remove_file(path);
1✔
1297
        let mut wal = path.as_os_str().to_owned();
1✔
1298
        wal.push("-wal");
1✔
1299
        let _ = std::fs::remove_file(std::path::PathBuf::from(wal));
1✔
1300
    }
1301

1302
    #[test]
1303
    fn round_trip_preserves_schema_and_data() {
4✔
1304
        let path = tmp_path("roundtrip");
1✔
1305
        let mut db = seed_db();
1✔
1306
        save_database(&mut db, &path).expect("save");
2✔
1307

1308
        let loaded = open_database(&path, "test".to_string()).expect("open");
1✔
1309
        assert_eq!(loaded.tables.len(), 2);
2✔
1310

1311
        let users = loaded.get_table("users".to_string()).expect("users table");
1✔
1312
        assert_eq!(users.columns.len(), 3);
1✔
1313
        let rowids = users.rowids();
1✔
1314
        assert_eq!(rowids.len(), 2);
2✔
1315
        let names: Vec<String> = rowids
1✔
1316
            .iter()
1317
            .filter_map(|r| match users.get_value("name", *r) {
3✔
1318
                Some(Value::Text(s)) => Some(s),
1✔
1319
                _ => None,
×
1320
            })
1321
            .collect();
1322
        assert!(names.contains(&"alice".to_string()));
2✔
1323
        assert!(names.contains(&"bob".to_string()));
1✔
1324

1325
        let notes = loaded.get_table("notes".to_string()).expect("notes table");
1✔
1326
        assert_eq!(notes.rowids().len(), 1);
1✔
1327

1328
        cleanup(&path);
1✔
1329
    }
1330

1331
    // -----------------------------------------------------------------
1332
    // Phase 7a — VECTOR(N) save / reopen round-trip
1333
    // -----------------------------------------------------------------
1334

1335
    #[test]
1336
    fn round_trip_preserves_vector_column() {
3✔
1337
        let path = tmp_path("vec_roundtrip");
1✔
1338

1339
        // Build, populate, save.
1340
        {
1341
            let mut db = Database::new("test".to_string());
2✔
1342
            process_command(
1343
                "CREATE TABLE docs (id INTEGER PRIMARY KEY, embedding VECTOR(3));",
1344
                &mut db,
1345
            )
1346
            .unwrap();
1347
            process_command(
1348
                "INSERT INTO docs (embedding) VALUES ([0.1, 0.2, 0.3]);",
1349
                &mut db,
1350
            )
1351
            .unwrap();
1352
            process_command(
1353
                "INSERT INTO docs (embedding) VALUES ([1.5, -2.0, 3.5]);",
1354
                &mut db,
1355
            )
1356
            .unwrap();
1357
            save_database(&mut db, &path).expect("save");
1✔
1358
        } // db drops → its exclusive lock releases before reopen.
1✔
1359

1360
        // Reopen and verify schema + data both round-tripped.
1361
        let loaded = open_database(&path, "test".to_string()).expect("open");
1✔
1362
        let docs = loaded.get_table("docs".to_string()).expect("docs table");
2✔
1363

1364
        // Schema preserved: column is still VECTOR(3).
1365
        let embedding_col = docs
3✔
1366
            .columns
1367
            .iter()
1368
            .find(|c| c.column_name == "embedding")
3✔
1369
            .expect("embedding column");
1370
        assert!(
×
1371
            matches!(embedding_col.datatype, DataType::Vector(3)),
1✔
1372
            "expected DataType::Vector(3) after round-trip, got {:?}",
1373
            embedding_col.datatype
1374
        );
1375

1376
        // Data preserved: both vectors still readable bit-for-bit.
1377
        let mut rows: Vec<Vec<f32>> = docs
1✔
1378
            .rowids()
1379
            .iter()
1380
            .filter_map(|r| match docs.get_value("embedding", *r) {
3✔
1381
                Some(Value::Vector(v)) => Some(v),
1✔
1382
                _ => None,
×
1383
            })
1384
            .collect();
1385
        rows.sort_by(|a, b| a[0].partial_cmp(&b[0]).unwrap());
3✔
1386
        assert_eq!(rows.len(), 2);
1✔
1387
        assert_eq!(rows[0], vec![0.1f32, 0.2, 0.3]);
1✔
1388
        assert_eq!(rows[1], vec![1.5f32, -2.0, 3.5]);
1✔
1389

1390
        cleanup(&path);
1✔
1391
    }
1392

1393
    #[test]
1394
    fn round_trip_rebuilds_hnsw_index_from_create_sql() {
3✔
1395
        // Phase 7d.3: HNSW indexes now persist their graph as cell-encoded
1396
        // pages. After save+reopen the index entry reattaches with the
1397
        // same column + same node count, loaded directly from disk
1398
        // instead of re-walking rows.
1399
        let path = tmp_path("hnsw_roundtrip");
1✔
1400

1401
        // Build, populate, index, save.
1402
        {
1403
            let mut db = Database::new("test".to_string());
2✔
1404
            process_command(
1405
                "CREATE TABLE docs (id INTEGER PRIMARY KEY, e VECTOR(2));",
1406
                &mut db,
1407
            )
1408
            .unwrap();
1409
            for v in &[
1✔
1410
                "[1.0, 0.0]",
1411
                "[2.0, 0.0]",
1412
                "[0.0, 3.0]",
1413
                "[1.0, 4.0]",
1414
                "[10.0, 10.0]",
1415
            ] {
1416
                process_command(&format!("INSERT INTO docs (e) VALUES ({v});"), &mut db).unwrap();
2✔
1417
            }
1418
            process_command("CREATE INDEX ix_e ON docs USING hnsw (e);", &mut db).unwrap();
1✔
1419
            save_database(&mut db, &path).expect("save");
1✔
1420
        } // db drops → exclusive lock releases.
1✔
1421

1422
        // Reopen and verify the index reattached, with the same name +
1423
        // column + populated graph.
1424
        let mut loaded = open_database(&path, "test".to_string()).expect("open");
1✔
1425
        {
1426
            let table = loaded.get_table("docs".to_string()).expect("docs");
2✔
1427
            assert_eq!(table.hnsw_indexes.len(), 1, "HNSW index should reattach");
1✔
1428
            let entry = &table.hnsw_indexes[0];
2✔
1429
            assert_eq!(entry.name, "ix_e");
1✔
1430
            assert_eq!(entry.column_name, "e");
1✔
1431
            assert_eq!(entry.index.len(), 5, "loaded graph should hold all 5 rows");
1✔
NEW
1432
            assert!(
×
1433
                !entry.needs_rebuild,
1✔
1434
                "fresh load should not be marked dirty"
1435
            );
1436
        }
1437

1438
        // Quick functional check: KNN query through the loaded index
1439
        // returns results.
1440
        let resp = process_command(
1441
            "SELECT id FROM docs ORDER BY vec_distance_l2(e, [1.0, 0.0]) ASC LIMIT 3;",
1442
            &mut loaded,
1443
        )
1444
        .unwrap();
1445
        assert!(resp.contains("3 rows returned"), "got: {resp}");
2✔
1446

1447
        cleanup(&path);
2✔
1448
    }
1449

1450
    #[test]
1451
    fn delete_then_save_then_reopen_excludes_deleted_node_from_hnsw() {
3✔
1452
        // Phase 7d.3 — DELETE marks HNSW dirty; save rebuilds it from
1453
        // current rows + serializes; reopen loads the post-delete graph.
1454
        // After all that, the deleted rowid must NOT come back from a
1455
        // KNN query.
1456
        let path = tmp_path("hnsw_delete_rebuild");
1✔
1457
        let mut db = Database::new("test".to_string());
2✔
1458
        process_command(
1459
            "CREATE TABLE docs (id INTEGER PRIMARY KEY, e VECTOR(2));",
1460
            &mut db,
1461
        )
1462
        .unwrap();
1463
        for v in &["[1.0, 0.0]", "[2.0, 0.0]", "[3.0, 0.0]", "[4.0, 0.0]"] {
1✔
1464
            process_command(&format!("INSERT INTO docs (e) VALUES ({v});"), &mut db).unwrap();
2✔
1465
        }
1466
        process_command("CREATE INDEX ix_e ON docs USING hnsw (e);", &mut db).unwrap();
1✔
1467

1468
        // Delete row 1 (the closest match to [0.5, 0.0]).
1469
        process_command("DELETE FROM docs WHERE id = 1;", &mut db).unwrap();
1✔
1470
        // Confirm it marked dirty.
1471
        let dirty_before_save = db.tables["docs"].hnsw_indexes[0].needs_rebuild;
1✔
1472
        assert!(dirty_before_save, "DELETE should mark dirty");
1✔
1473

1474
        save_database(&mut db, &path).expect("save");
2✔
1475
        // Confirm save cleared the dirty flag.
1476
        let dirty_after_save = db.tables["docs"].hnsw_indexes[0].needs_rebuild;
1✔
1477
        assert!(!dirty_after_save, "save should clear dirty");
1✔
1478
        drop(db);
1✔
1479

1480
        // Reopen, query for the closest match. Row 1 is gone; row 2
1481
        // (id=2, vector [2.0, 0.0]) should now be the nearest.
1482
        let loaded = open_database(&path, "test".to_string()).expect("open");
1✔
1483
        let docs = loaded.get_table("docs".to_string()).expect("docs");
2✔
1484

1485
        // Row 1 must not appear in any storage anymore.
1486
        assert!(
1✔
1487
            !docs.rowids().contains(&1),
2✔
1488
            "deleted row 1 should not be in row storage"
1489
        );
1490
        assert_eq!(docs.rowids().len(), 3, "should have 3 surviving rows");
1✔
1491

1492
        // The HNSW index must also have shed the deleted node.
1493
        assert_eq!(
1✔
1494
            docs.hnsw_indexes[0].index.len(),
1✔
1495
            3,
1496
            "HNSW graph should have shed the deleted node"
1497
        );
1498

1499
        cleanup(&path);
2✔
1500
    }
1501

1502
    #[test]
1503
    fn round_trip_survives_writes_after_load() {
3✔
1504
        let path = tmp_path("after_load");
1✔
1505
        save_database(&mut seed_db(), &path).unwrap();
2✔
1506

1507
        {
1508
            let mut db = open_database(&path, "test".to_string()).unwrap();
1✔
1509
            process_command(
1510
                "INSERT INTO users (name, age) VALUES ('carol', 40);",
1511
                &mut db,
1512
            )
1513
            .unwrap();
1514
            save_database(&mut db, &path).unwrap();
1✔
1515
        } // db drops → its exclusive lock releases before we reopen below.
1✔
1516

1517
        let db2 = open_database(&path, "test".to_string()).unwrap();
1✔
1518
        let users = db2.get_table("users".to_string()).unwrap();
2✔
1519
        assert_eq!(users.rowids().len(), 3);
1✔
1520

1521
        cleanup(&path);
1✔
1522
    }
1523

1524
    #[test]
1525
    fn open_rejects_garbage_file() {
3✔
1526
        let path = tmp_path("bad");
1✔
1527
        std::fs::write(&path, b"not a sqlrite database, just bytes").unwrap();
2✔
1528
        let result = open_database(&path, "x".to_string());
1✔
1529
        assert!(result.is_err());
2✔
1530
        cleanup(&path);
1✔
1531
    }
1532

1533
    #[test]
1534
    fn many_small_rows_spread_across_leaves() {
3✔
1535
        let path = tmp_path("many_rows");
1✔
1536
        let mut db = Database::new("big".to_string());
2✔
1537
        process_command(
1538
            "CREATE TABLE things (id INTEGER PRIMARY KEY, data TEXT);",
1539
            &mut db,
1540
        )
1541
        .unwrap();
1542
        for i in 0..200 {
1✔
1543
            let body = "x".repeat(200);
1✔
1544
            let q = format!("INSERT INTO things (data) VALUES ('row-{i}-{body}');");
2✔
1545
            process_command(&q, &mut db).unwrap();
2✔
1546
        }
1547
        save_database(&mut db, &path).unwrap();
1✔
1548
        let loaded = open_database(&path, "big".to_string()).unwrap();
1✔
1549
        let things = loaded.get_table("things".to_string()).unwrap();
2✔
1550
        assert_eq!(things.rowids().len(), 200);
1✔
1551
        cleanup(&path);
1✔
1552
    }
1553

1554
    #[test]
1555
    fn huge_row_goes_through_overflow() {
3✔
1556
        let path = tmp_path("overflow_row");
1✔
1557
        let mut db = Database::new("big".to_string());
2✔
1558
        process_command(
1559
            "CREATE TABLE docs (id INTEGER PRIMARY KEY, body TEXT);",
1560
            &mut db,
1561
        )
1562
        .unwrap();
1563
        let body = "A".repeat(10_000);
1✔
1564
        process_command(
1565
            &format!("INSERT INTO docs (body) VALUES ('{body}');"),
2✔
1566
            &mut db,
1567
        )
1568
        .unwrap();
1569
        save_database(&mut db, &path).unwrap();
1✔
1570

1571
        let loaded = open_database(&path, "big".to_string()).unwrap();
1✔
1572
        let docs = loaded.get_table("docs".to_string()).unwrap();
2✔
1573
        let rowids = docs.rowids();
1✔
1574
        assert_eq!(rowids.len(), 1);
2✔
1575
        let stored = docs.get_value("body", rowids[0]);
1✔
1576
        match stored {
1✔
1577
            Some(Value::Text(s)) => assert_eq!(s.len(), 10_000),
1✔
1578
            other => panic!("expected Text, got {other:?}"),
×
1579
        }
1580
        cleanup(&path);
1✔
1581
    }
1582

1583
    #[test]
1584
    fn create_sql_synthesis_round_trips() {
3✔
1585
        // Build a table via CREATE, then verify table_to_create_sql +
1586
        // parse_create_sql reproduce an equivalent column list.
1587
        let mut db = Database::new("x".to_string());
1✔
1588
        process_command(
1589
            "CREATE TABLE t (id INTEGER PRIMARY KEY, tag TEXT UNIQUE, note TEXT NOT NULL);",
1590
            &mut db,
1591
        )
1592
        .unwrap();
1593
        let t = db.get_table("t".to_string()).unwrap();
1✔
1594
        let sql = table_to_create_sql(t);
1✔
1595
        let (name, cols) = parse_create_sql(&sql).unwrap();
2✔
1596
        assert_eq!(name, "t");
2✔
1597
        assert_eq!(cols.len(), 3);
1✔
1598
        assert!(cols[0].is_pk);
1✔
1599
        assert!(cols[1].is_unique);
1✔
1600
        assert!(cols[2].not_null);
1✔
1601
    }
1602

1603
    #[test]
1604
    fn sqlrite_master_is_not_exposed_as_a_user_table() {
3✔
1605
        // After open, the public db.tables map should not list the master.
1606
        let path = tmp_path("no_master");
1✔
1607
        save_database(&mut seed_db(), &path).unwrap();
2✔
1608
        let loaded = open_database(&path, "x".to_string()).unwrap();
1✔
1609
        assert!(!loaded.tables.contains_key(MASTER_TABLE_NAME));
2✔
1610
        cleanup(&path);
2✔
1611
    }
1612

1613
    #[test]
1614
    fn multi_leaf_table_produces_an_interior_root() {
3✔
1615
        // 200 fat rows force the table into multiple leaves, which means
1616
        // save_database must build at least one InteriorNode above them.
1617
        // The test verifies the round-trip works and confirms the root is
1618
        // indeed an interior page (not a leaf) by reading the page type
1619
        // directly out of the open pager.
1620
        let path = tmp_path("multi_leaf_interior");
1✔
1621
        let mut db = Database::new("big".to_string());
2✔
1622
        process_command(
1623
            "CREATE TABLE things (id INTEGER PRIMARY KEY, data TEXT);",
1624
            &mut db,
1625
        )
1626
        .unwrap();
1627
        for i in 0..200 {
1✔
1628
            let body = "x".repeat(200);
1✔
1629
            let q = format!("INSERT INTO things (data) VALUES ('row-{i}-{body}');");
2✔
1630
            process_command(&q, &mut db).unwrap();
2✔
1631
        }
1632
        save_database(&mut db, &path).unwrap();
1✔
1633

1634
        // Confirm the round-trip preserved all 200 rows.
1635
        let loaded = open_database(&path, "big".to_string()).unwrap();
1✔
1636
        let things = loaded.get_table("things".to_string()).unwrap();
2✔
1637
        assert_eq!(things.rowids().len(), 200);
1✔
1638

1639
        // Peek at `things`'s root page via the pager attached to the
1640
        // loaded DB and check it's an InteriorNode, not a leaf.
1641
        let pager = loaded
2✔
1642
            .pager
1643
            .as_ref()
1644
            .expect("loaded DB should have a pager");
1645
        // sqlrite_master's row for `things` holds its root page. Easiest
1646
        // way to find it: walk the leaf chain by using find_leftmost_leaf
1647
        // and then hop one level up. Simpler: read the master, scan for
1648
        // the "things" row, look up rootpage.
1649
        let mut master = build_empty_master_table();
1✔
1650
        load_table_rows(pager, &mut master, pager.header().schema_root_page).unwrap();
2✔
1651
        let things_root = master
1✔
1652
            .rowids()
1653
            .into_iter()
1654
            .find_map(|r| match master.get_value("name", r) {
3✔
1655
                Some(Value::Text(s)) if s == "things" => match master.get_value("rootpage", r) {
3✔
1656
                    Some(Value::Integer(p)) => Some(p as u32),
1✔
1657
                    _ => None,
×
1658
                },
1659
                _ => None,
×
1660
            })
1661
            .expect("things should appear in sqlrite_master");
1662
        let root_buf = pager.read_page(things_root).unwrap();
1✔
1663
        assert_eq!(
1✔
1664
            root_buf[0],
1665
            PageType::InteriorNode as u8,
1666
            "expected a multi-leaf table to have an interior root, got tag {}",
1667
            root_buf[0]
×
1668
        );
1669

1670
        cleanup(&path);
2✔
1671
    }
1672

1673
    #[test]
1674
    fn explicit_index_persists_across_save_and_open() {
3✔
1675
        let path = tmp_path("idx_persist");
1✔
1676
        let mut db = Database::new("idx".to_string());
2✔
1677
        process_command(
1678
            "CREATE TABLE users (id INTEGER PRIMARY KEY, tag TEXT);",
1679
            &mut db,
1680
        )
1681
        .unwrap();
1682
        for i in 1..=5 {
1✔
1683
            let tag = if i % 2 == 0 { "odd" } else { "even" };
2✔
1684
            process_command(
1685
                &format!("INSERT INTO users (tag) VALUES ('{tag}');"),
1✔
1686
                &mut db,
1687
            )
1688
            .unwrap();
1689
        }
1690
        process_command("CREATE INDEX users_tag_idx ON users (tag);", &mut db).unwrap();
1✔
1691
        save_database(&mut db, &path).unwrap();
1✔
1692

1693
        let loaded = open_database(&path, "idx".to_string()).unwrap();
1✔
1694
        let users = loaded.get_table("users".to_string()).unwrap();
2✔
1695
        let idx = users
1✔
1696
            .index_by_name("users_tag_idx")
1697
            .expect("explicit index should survive save/open");
1698
        assert_eq!(idx.column_name, "tag");
1✔
1699
        assert!(!idx.is_unique);
1✔
1700
        // 5 rows: rowids 2, 4 are "odd" (i % 2 == 0 when i is 2 or 4) — 2 entries;
1701
        // rowids 1, 3, 5 are "even" (i % 2 != 0) — 3 entries.
1702
        let even_rowids = idx.lookup(&Value::Text("even".into()));
2✔
1703
        let odd_rowids = idx.lookup(&Value::Text("odd".into()));
1✔
1704
        assert_eq!(even_rowids.len(), 3);
1✔
1705
        assert_eq!(odd_rowids.len(), 2);
1✔
1706

1707
        cleanup(&path);
1✔
1708
    }
1709

1710
    #[test]
1711
    fn auto_indexes_for_unique_columns_survive_save_open() {
3✔
1712
        let path = tmp_path("auto_idx_persist");
1✔
1713
        let mut db = Database::new("a".to_string());
2✔
1714
        process_command(
1715
            "CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT NOT NULL UNIQUE);",
1716
            &mut db,
1717
        )
1718
        .unwrap();
1719
        process_command("INSERT INTO users (email) VALUES ('a@x');", &mut db).unwrap();
1✔
1720
        process_command("INSERT INTO users (email) VALUES ('b@x');", &mut db).unwrap();
1✔
1721
        save_database(&mut db, &path).unwrap();
1✔
1722

1723
        let loaded = open_database(&path, "a".to_string()).unwrap();
1✔
1724
        let users = loaded.get_table("users".to_string()).unwrap();
2✔
1725
        // Every UNIQUE column auto-creates an index; the load path populated
1726
        // it from the persisted entries.
1727
        let auto_name = SecondaryIndex::auto_name("users", "email");
1✔
1728
        let idx = users
1✔
1729
            .index_by_name(&auto_name)
2✔
1730
            .expect("auto index should be restored");
1731
        assert!(idx.is_unique);
1✔
1732
        assert_eq!(idx.lookup(&Value::Text("a@x".into())).len(), 1);
1✔
1733
        assert_eq!(idx.lookup(&Value::Text("b@x".into())).len(), 1);
1✔
1734

1735
        cleanup(&path);
1✔
1736
    }
1737

1738
    #[test]
1739
    fn deep_tree_round_trips() {
3✔
1740
        // Force a 3-level tree by bypassing process_command (which prints
1741
        // the full table on every INSERT, making large bulk loads O(N^2)
1742
        // in I/O). We build the Table directly via restore_row.
1743
        use crate::sql::db::table::Column as TableColumn;
1744

1745
        let path = tmp_path("deep_tree");
1✔
1746
        let mut db = Database::new("deep".to_string());
2✔
1747
        let columns = vec![
3✔
1748
            TableColumn::new("id".into(), "integer".into(), true, true, true),
2✔
1749
            TableColumn::new("s".into(), "text".into(), false, true, false),
2✔
1750
        ];
1751
        let mut table = build_empty_table("t", columns, 0);
1✔
1752
        // ~900-byte rows → ~4 rows per leaf. 6000 rows → ~1500 leaves,
1753
        // which with interior fanout ~400 needs 2 interior levels (3-level
1754
        // tree total, counting leaves).
1755
        for i in 1..=6_000i64 {
2✔
1756
            let body = "q".repeat(900);
1✔
1757
            table
1✔
1758
                .restore_row(
1759
                    i,
1760
                    vec![
3✔
1761
                        Some(Value::Integer(i)),
1✔
1762
                        Some(Value::Text(format!("r-{i}-{body}"))),
2✔
1763
                    ],
1764
                )
1765
                .unwrap();
1766
        }
1767
        db.tables.insert("t".to_string(), table);
1✔
1768
        save_database(&mut db, &path).unwrap();
1✔
1769

1770
        let loaded = open_database(&path, "deep".to_string()).unwrap();
1✔
1771
        let t = loaded.get_table("t".to_string()).unwrap();
2✔
1772
        assert_eq!(t.rowids().len(), 6_000);
1✔
1773

1774
        // Confirm the tree actually grew past 2 levels — i.e., the root's
1775
        // leftmost child is itself an interior page, not a leaf.
1776
        let pager = loaded.pager.as_ref().unwrap();
1✔
1777
        let mut master = build_empty_master_table();
1✔
1778
        load_table_rows(pager, &mut master, pager.header().schema_root_page).unwrap();
2✔
1779
        let t_root = master
1✔
1780
            .rowids()
1781
            .into_iter()
1782
            .find_map(|r| match master.get_value("name", r) {
3✔
1783
                Some(Value::Text(s)) if s == "t" => match master.get_value("rootpage", r) {
3✔
1784
                    Some(Value::Integer(p)) => Some(p as u32),
1✔
1785
                    _ => None,
×
1786
                },
1787
                _ => None,
×
1788
            })
1789
            .expect("t in sqlrite_master");
1790
        let root_buf = pager.read_page(t_root).unwrap();
1✔
1791
        assert_eq!(root_buf[0], PageType::InteriorNode as u8);
1✔
1792
        let root_payload: &[u8; PAYLOAD_PER_PAGE] =
1✔
1793
            (&root_buf[PAGE_HEADER_SIZE..]).try_into().unwrap();
1794
        let root_interior = InteriorPage::from_bytes(root_payload);
1✔
1795
        let child = root_interior.leftmost_child().unwrap();
2✔
1796
        let child_buf = pager.read_page(child).unwrap();
1✔
1797
        assert_eq!(
1✔
1798
            child_buf[0],
1799
            PageType::InteriorNode as u8,
1800
            "expected 3-level tree: root's leftmost child should also be InteriorNode",
1801
        );
1802

1803
        cleanup(&path);
2✔
1804
    }
1805
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc