|
| 1 | +# Batch API Vtab Integration & Benchmark |
| 2 | + |
| 3 | +## Summary |
| 4 | + |
| 5 | +The C-level batch API (`diskann_begin_batch()`/`diskann_end_batch()`) persists a BlobCache across inserts, but it's never activated through the SQL/vtab path. The benchmark runner and all SQL users only use `BEGIN/COMMIT`, which provides transaction atomicity but not cache persistence. Wire up batch mode in the vtab layer so SQL transactions automatically benefit from the persistent cache, then benchmark the actual speedup. |
| 6 | + |
| 7 | +## Current Phase |
| 8 | + |
| 9 | +- [ ] Research & Planning |
| 10 | +- [ ] Test Design |
| 11 | +- [ ] Implementation Design |
| 12 | +- [ ] Test-First Development |
| 13 | +- [ ] Implementation |
| 14 | +- [ ] Integration |
| 15 | +- [ ] Cleanup & Documentation |
| 16 | +- [ ] Final Review |
| 17 | + |
| 18 | +## Required Reading |
| 19 | + |
| 20 | +- `CLAUDE.md` — Project conventions |
| 21 | +- `TDD.md` — Testing methodology |
| 22 | +- `DESIGN-PRINCIPLES.md` — C coding standards |
| 23 | +- `src/diskann.h` — `diskann_begin_batch()` / `diskann_end_batch()` declarations |
| 24 | +- `src/diskann_api.c` — Batch API implementation |
| 25 | +- `src/diskann_insert.c` — How `idx->batch_cache` is used when non-NULL |
| 26 | +- `src/diskann_vtab.c` — Virtual table xUpdate (INSERT path) |
| 27 | +- `src/diskann_cache.h` — BlobCache with `owns_blobs` mode |
| 28 | +- `benchmarks/src/runners/diskann-runner.ts` — Current SQL-level `BEGIN/COMMIT` wrapping |
| 29 | +- `_todo/20260211-serial-batch-insert.md` — Phase 1a design notes (ownership model, cache freshness) |
| 30 | + |
| 31 | +## Description |
| 32 | + |
| 33 | +**Problem:** Phase 1a (persistent BlobCache) is implemented at the C API level but never reaches production use: |
| 34 | + |
| 35 | +- SQL users wrap inserts in `BEGIN/COMMIT` but `diskann_insert()` still creates/destroys a per-insert cache each time |
| 36 | +- Benchmark runner uses SQL `BEGIN/COMMIT` — measured 37% speedup is from per-insert cache, NOT persistent cache |
| 37 | +- Cache hits across inserts are 0% because cache doesn't survive between `diskann_insert()` calls |
| 38 | + |
| 39 | +**Constraints:** |
| 40 | + |
| 41 | +- Vtab API doesn't have explicit "begin batch" / "end batch" hooks from SQLite |
| 42 | +- Must be transparent to SQL users (no new SQL syntax) |
| 43 | +- Must handle errors gracefully (rollback clears cache) |
| 44 | +- Must not break existing tests (204/204 passing) |
| 45 | + |
| 46 | +**Success Criteria:** |
| 47 | + |
| 48 | +- SQL inserts inside `BEGIN/COMMIT` automatically use persistent cache |
| 49 | +- Benchmark shows measurable speedup over current (432s baseline at 25k) |
| 50 | +- All 204 C tests + vtab tests pass |
| 51 | +- ASan + Valgrind clean |
| 52 | + |
| 53 | +## Tribal Knowledge |
| 54 | + |
| 55 | +- `diskann_begin_batch()` sets `idx->batch_cache` (owning BlobCache with capacity 100) |
| 56 | +- `diskann_insert()` uses `idx->batch_cache` if non-NULL, else creates per-insert cache |
| 57 | +- `diskann_end_batch()` frees batch_cache |
| 58 | +- `BlobSpot.is_cached=1` prevents double-free when cache owns BlobSpots |
| 59 | +- Cache data stays fresh after Phase 2 — in-memory buffer is authoritative |
| 60 | +- On SAVEPOINT rollback, cached data may be stale — `diskann_end_batch()` clears cache safely |
| 61 | +- vtab xUpdate is called once per INSERT row — no SQLite hook for "transaction started" |
| 62 | + |
| 63 | +## Solutions |
| 64 | + |
| 65 | +### Option A: Lazy batch start in xUpdate (Recommended) |
| 66 | + |
| 67 | +On first INSERT in a vtab, call `diskann_begin_batch()`. Track state with a flag on the vtab cursor or module-level state. Clean up on xDisconnect or transaction end. |
| 68 | + |
| 69 | +SQLite provides `xBegin`/`xCommit`/`xRollback` hooks on virtual tables for exactly this purpose. Use `xBegin` to call `diskann_begin_batch()` and `xCommit`/`xRollback` to call `diskann_end_batch()`. |
| 70 | + |
| 71 | +**Pros:** Fully transparent, works for all SQL users, hooks already exist in SQLite vtab API |
| 72 | +**Cons:** Requires implementing xBegin/xCommit/xRollback (currently not implemented) |
| 73 | + |
| 74 | +### Option B: Expose via SQL function |
| 75 | + |
| 76 | +Add `SELECT diskann_begin_batch('index_name')` / `SELECT diskann_end_batch('index_name')` SQL functions. |
| 77 | + |
| 78 | +**Pros:** Explicit control, simple implementation |
| 79 | +**Cons:** User must remember to call, error-prone, not transparent |
| 80 | + |
| 81 | +### Option C: TypeScript-only bindings |
| 82 | + |
| 83 | +Expose `beginBatch()`/`endBatch()` in TypeScript layer, call C API via custom SQL. |
| 84 | + |
| 85 | +**Pros:** Works for JS/TS users |
| 86 | +**Cons:** Only helps TS users, not general SQL |
| 87 | + |
| 88 | +**Recommendation:** Option A — use vtab transaction hooks. This is the most robust and transparent approach. |
| 89 | + |
| 90 | +## Tasks |
| 91 | + |
| 92 | +- [ ] Research: Confirm SQLite vtab `xBegin`/`xSync`/`xCommit`/`xRollback` API in SQLite docs |
| 93 | +- [ ] Read existing `diskann_vtab.c` to understand current vtab module structure |
| 94 | +- [ ] Write tests: vtab INSERT inside `BEGIN/COMMIT` verifies cache is active |
| 95 | +- [ ] Write tests: vtab INSERT without explicit transaction still works (autocommit) |
| 96 | +- [ ] Write tests: ROLLBACK properly clears cache |
| 97 | +- [ ] Implement `xBegin` → call `diskann_begin_batch(idx)` |
| 98 | +- [ ] Implement `xCommit` → call `diskann_end_batch(idx)` |
| 99 | +- [ ] Implement `xRollback` → call `diskann_end_batch(idx)` (same cleanup) |
| 100 | +- [ ] Run `make test` — all 204+ tests pass |
| 101 | +- [ ] Run `make asan` — no memory errors |
| 102 | +- [ ] Run `make clean && make valgrind` — no leaks |
| 103 | +- [ ] Run benchmark: `cd benchmarks && npm run bench -- --profile=profiles/medium.json` |
| 104 | +- [ ] Compare build time vs 432s baseline |
| 105 | +- [ ] Document results in `experiments/experiment-005-batch-vtab.md` |
| 106 | + |
| 107 | +**Verification:** |
| 108 | + |
| 109 | +```bash |
| 110 | +make clean && make test # All tests pass |
| 111 | +make asan # No memory errors |
| 112 | +make clean && make valgrind # No leaks |
| 113 | +cd benchmarks && npm run bench -- --profile=profiles/medium.json # Measure speedup |
| 114 | +``` |
| 115 | + |
| 116 | +## Notes |
| 117 | + |
| 118 | +(To be filled during execution) |
0 commit comments