Skip to main content

uqa_storage_sqlite/
backend.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Session factories and physical stores for the `SQLite` backend.
8
9use crate::{
10    Catalog, ManagedConnection, SQLiteBTreeIndexStore, SQLiteDocumentStore, SQLiteHNSWIndex,
11    SQLiteIVFIndex, SQLiteInvertedIndex, SQLiteVectorIndex,
12};
13use std::{collections::BTreeMap, sync::Arc};
14use uqa_analysis::Analyzer;
15use uqa_core::{DocId, Value};
16use uqa_storage::{
17    CatalogFacade, DocumentStore, InvertedIndex, PersistentStorageBackend,
18    PersistentStorageIdentity, PersistentStorageProvider, PersistentStorageSession,
19    StorageBackendResult, StorageSavepointId, VectorIndex, VectorIndexOpenMode, VectorIndexSpec,
20};
21
22#[derive(Clone)]
23pub struct SQLiteStorageBackend {
24    conn: ManagedConnection,
25    native_initial_restore: bool,
26}
27
28impl SQLiteStorageBackend {
29    pub fn new(conn: ManagedConnection) -> Self {
30        Self {
31            conn,
32            native_initial_restore: false,
33        }
34    }
35
36    pub fn connection(&self) -> ManagedConnection {
37        self.conn.clone()
38    }
39
40    /// Create a backend whose stores use an independent transaction session
41    /// over the same physical `SQLite` pool.
42    #[must_use]
43    pub fn new_session(&self) -> Self {
44        Self::new(self.conn.new_session())
45    }
46}
47
48/// Database-level owner that creates isolated `SQLite` engine sessions.
49#[derive(Clone)]
50pub struct SQLiteStorageProvider {
51    connection: ManagedConnection,
52}
53
54impl SQLiteStorageProvider {
55    pub fn new(connection: ManagedConnection) -> Self {
56        Self { connection }
57    }
58
59    fn native_session(
60        &self,
61        cancellation: &uqa_core::CancellationToken,
62    ) -> StorageBackendResult<ManagedConnection> {
63        if !self.connection.is_native_record_session() {
64            self.connection
65                .bind_native_records(uqa_storage::mvcc::VersionedSessionOptions::default())?;
66        }
67        Ok(self.connection.new_session_with_cancellation(cancellation))
68    }
69}
70
71impl PersistentStorageProvider for SQLiteStorageProvider {
72    fn auxiliary_encryption_key(&self) -> Option<uqa_storage::StorageEncryptionKey> {
73        self.connection.auxiliary_encryption_key()
74    }
75
76    fn open_initial_session(&self) -> StorageBackendResult<PersistentStorageSession> {
77        let connection = self.connection.new_session();
78        let catalog: Arc<dyn CatalogFacade> =
79            Arc::new(Catalog::for_initial_restore(connection.clone()));
80        let backend: Arc<dyn PersistentStorageBackend> = Arc::new(SQLiteStorageBackend {
81            conn: connection,
82            native_initial_restore: true,
83        });
84        Ok(PersistentStorageSession::new(catalog, backend))
85    }
86
87    fn open_session(&self) -> StorageBackendResult<PersistentStorageSession> {
88        self.open_session_with_cancellation(&uqa_core::CancellationToken::new())
89    }
90
91    fn open_session_with_cancellation(
92        &self,
93        cancellation: &uqa_core::CancellationToken,
94    ) -> StorageBackendResult<PersistentStorageSession> {
95        cancellation.check()?;
96        let connection = self.native_session(cancellation)?;
97        let catalog: Arc<dyn CatalogFacade> = Arc::new(Catalog::open(connection.clone())?);
98        let backend: Arc<dyn PersistentStorageBackend> =
99            Arc::new(SQLiteStorageBackend::new(connection));
100        Ok(PersistentStorageSession::new(catalog, backend))
101    }
102
103    fn storage_identity(&self) -> StorageBackendResult<Option<PersistentStorageIdentity>> {
104        self.connection.storage_identity()
105    }
106}
107
108impl uqa_storage::mvcc::IdentifierAllocator for SQLiteStorageBackend {
109    fn identifier_watermark(&self, namespace: &[u8]) -> StorageBackendResult<Option<u64>> {
110        self.conn
111            .native_identifier_watermark(namespace)
112            .map_err(Into::into)
113    }
114
115    fn allocate_identifiers(
116        &self,
117        namespace: &[u8],
118        request: uqa_storage::mvcc::IdentifierRequest,
119    ) -> StorageBackendResult<uqa_storage::mvcc::IdentifierAllocation> {
120        self.conn
121            .allocate_native_identifiers(namespace, request)
122            .map_err(Into::into)
123    }
124}
125
126impl PersistentStorageBackend for SQLiteStorageBackend {
127    fn retention_control(&self) -> Option<uqa_storage::read_control::StorageReadControl> {
128        self.conn.retention_control()
129    }
130
131    fn notification_publications(
132        &self,
133    ) -> Option<&dyn uqa_storage::notifications::NotificationPublicationStore> {
134        Some(&self.conn)
135    }
136
137    fn serializable_session(&self) -> Option<&dyn uqa_storage::mvcc::SerializableSession> {
138        self.conn
139            .is_native_record_session()
140            .then_some(&self.conn as &dyn uqa_storage::mvcc::SerializableSession)
141    }
142
143    fn open_retained_read_session(
144        &self,
145        cancellation: &uqa_core::CancellationToken,
146    ) -> StorageBackendResult<PersistentStorageSession> {
147        let connection = self.conn.new_retained_read_session(cancellation)?;
148        let catalog: Arc<dyn CatalogFacade> = Arc::new(Catalog::open(connection.clone())?);
149        let backend: Arc<dyn PersistentStorageBackend> = Arc::new(Self::new(connection));
150        Ok(PersistentStorageSession::new(catalog, backend))
151    }
152
153    fn write_cancellation(&self) -> Option<uqa_core::CancellationToken> {
154        Some(self.conn.write_cancellation())
155    }
156
157    fn transaction_model(&self) -> uqa_storage::StorageTransactionModel {
158        self.conn.transaction_model()
159    }
160
161    fn identifier_allocator(&self) -> Option<&dyn uqa_storage::mvcc::IdentifierAllocator> {
162        self.conn.is_native_record_session().then_some(self)
163    }
164
165    fn transaction_affinity(&self) -> Option<uqa_storage::StorageSessionAffinity> {
166        Some(self.conn.transaction_affinity())
167    }
168
169    fn auxiliary_encryption_key(&self) -> Option<uqa_storage::StorageEncryptionKey> {
170        self.conn.auxiliary_encryption_key()
171    }
172
173    fn storage_identity(&self) -> StorageBackendResult<Option<PersistentStorageIdentity>> {
174        self.conn.storage_identity()
175    }
176
177    fn open_session(&self) -> StorageBackendResult<PersistentStorageSession> {
178        self.open_session_with_cancellation(&uqa_core::CancellationToken::new())
179    }
180
181    fn open_session_with_cancellation(
182        &self,
183        cancellation: &uqa_core::CancellationToken,
184    ) -> StorageBackendResult<PersistentStorageSession> {
185        cancellation.check()?;
186        let connection = self.conn.new_session_with_cancellation(cancellation);
187        let catalog: Arc<dyn CatalogFacade> = Arc::new(Catalog::open(connection.clone())?);
188        let backend: Arc<dyn PersistentStorageBackend> = Arc::new(Self::new(connection));
189        Ok(PersistentStorageSession::new(catalog, backend))
190    }
191
192    fn supports_concurrent_pinned_read_and_write(&self) -> bool {
193        self.conn.supports_concurrent_pinned_read_and_write()
194    }
195
196    fn document_store(&self, table: &str) -> Box<dyn DocumentStore> {
197        Box::new(SQLiteDocumentStore::new(self.conn.clone(), table))
198    }
199
200    fn inverted_index(&self, table: &str, analyzer: Analyzer) -> Box<dyn InvertedIndex> {
201        Box::new(SQLiteInvertedIndex::new(self.conn.clone(), table, analyzer))
202    }
203
204    fn vector_index(
205        &self,
206        table: &str,
207        field: &str,
208        dimensions: u32,
209        spec: VectorIndexSpec,
210        mode: VectorIndexOpenMode,
211    ) -> StorageBackendResult<Box<dyn VectorIndex>> {
212        let index: Box<dyn VectorIndex> = match spec {
213            VectorIndexSpec::BruteForce => Box::new(SQLiteVectorIndex::new(
214                self.conn.clone(),
215                table,
216                field,
217                dimensions,
218            )),
219            VectorIndexSpec::IVF(params) => {
220                params.validate()?;
221                match mode {
222                    VectorIndexOpenMode::Create => Box::new(SQLiteIVFIndex::with_params(
223                        self.conn.clone(),
224                        table,
225                        field,
226                        dimensions,
227                        params.nlist,
228                        params.nprobe,
229                        params.train_threshold,
230                    )),
231                    VectorIndexOpenMode::Restore => Box::new(SQLiteIVFIndex::open_existing(
232                        self.conn.clone(),
233                        table,
234                        field,
235                        dimensions,
236                        params.nlist,
237                        params.nprobe,
238                        params.train_threshold,
239                    )),
240                }
241            }
242            VectorIndexSpec::HNSW(params) => {
243                params.validate()?;
244                match mode {
245                    VectorIndexOpenMode::Create => Box::new(SQLiteHNSWIndex::with_params(
246                        self.conn.clone(),
247                        table,
248                        field,
249                        dimensions,
250                        params,
251                    )),
252                    VectorIndexOpenMode::Restore => {
253                        let index = SQLiteHNSWIndex::open_existing(
254                            self.conn.clone(),
255                            table,
256                            field,
257                            dimensions,
258                            params,
259                        );
260                        index.validate_existing()?;
261                        Box::new(index)
262                    }
263                }
264            }
265        };
266        Ok(index)
267    }
268
269    fn drop_vector_index_metadata(&self, table: &str, field: &str) -> StorageBackendResult<()> {
270        SQLiteIVFIndex::drop_metadata(&self.conn, table, field)?;
271        SQLiteHNSWIndex::drop_metadata(&self.conn, table, field)?;
272        Ok(())
273    }
274
275    fn persists_btree_indexes(&self) -> bool {
276        true
277    }
278
279    fn load_btree_index(
280        &self,
281        table: &str,
282        field: &uqa_storage::ValueIndexKey,
283    ) -> StorageBackendResult<Option<Vec<(DocId, Value)>>> {
284        Ok(SQLiteBTreeIndexStore::new(self.conn.clone()).load(table, field)?)
285    }
286
287    fn btree_index_fields(
288        &self,
289        table: &str,
290    ) -> StorageBackendResult<Vec<uqa_storage::ValueIndexKey>> {
291        Ok(SQLiteBTreeIndexStore::new(self.conn.clone()).fields(table)?)
292    }
293
294    fn read_btree_index_entry(
295        &self,
296        table: &str,
297        field: &uqa_storage::ValueIndexKey,
298        doc_id: DocId,
299    ) -> StorageBackendResult<uqa_storage::ValueIndexEntry> {
300        Ok(SQLiteBTreeIndexStore::new(self.conn.clone()).read_entry(table, field, doc_id)?)
301    }
302
303    fn btree_index_repairs(
304        &self,
305    ) -> StorageBackendResult<Vec<(String, uqa_storage::ValueIndexKey)>> {
306        Ok(SQLiteBTreeIndexStore::new(self.conn.clone()).repairs()?)
307    }
308
309    fn clear_btree_index_repair(
310        &self,
311        table: &str,
312        field: &uqa_storage::ValueIndexKey,
313    ) -> StorageBackendResult<()> {
314        SQLiteBTreeIndexStore::new(self.conn.clone()).clear_repair(table, field)?;
315        Ok(())
316    }
317
318    fn replace_btree_index(
319        &self,
320        table: &str,
321        field: &uqa_storage::ValueIndexKey,
322        values: &[(DocId, Value)],
323    ) -> StorageBackendResult<()> {
324        SQLiteBTreeIndexStore::new(self.conn.clone()).replace(table, field, values)?;
325        Ok(())
326    }
327
328    fn repair_btree_index(
329        &self,
330        table: &str,
331        field: &uqa_storage::ValueIndexKey,
332        _complete: &[(DocId, Value)],
333        stale_doc_ids: &[DocId],
334        missing: &[(DocId, Value)],
335    ) -> StorageBackendResult<()> {
336        SQLiteBTreeIndexStore::new(self.conn.clone()).repair(
337            table,
338            field,
339            stale_doc_ids,
340            missing,
341        )?;
342        Ok(())
343    }
344
345    fn replace_btree_indexes(
346        &self,
347        table: &str,
348        indexes: &[(&uqa_storage::ValueIndexKey, &[(DocId, Value)])],
349    ) -> StorageBackendResult<()> {
350        SQLiteBTreeIndexStore::new(self.conn.clone()).replace_many(table, indexes)?;
351        Ok(())
352    }
353
354    fn apply_btree_index_write(
355        &self,
356        table: &str,
357        doc_id: DocId,
358        values: Option<&BTreeMap<uqa_storage::ValueIndexKey, Value>>,
359    ) -> StorageBackendResult<()> {
360        SQLiteBTreeIndexStore::new(self.conn.clone()).apply_write(table, doc_id, values)?;
361        Ok(())
362    }
363
364    fn drop_btree_index(
365        &self,
366        table: &str,
367        field: &uqa_storage::ValueIndexKey,
368    ) -> StorageBackendResult<()> {
369        SQLiteBTreeIndexStore::new(self.conn.clone()).drop_index(table, field)?;
370        Ok(())
371    }
372
373    fn clear_btree_indexes(&self, table: &str) -> StorageBackendResult<()> {
374        SQLiteBTreeIndexStore::new(self.conn.clone()).clear_table(table)?;
375        Ok(())
376    }
377
378    fn vacuum(&self) -> StorageBackendResult<()> {
379        self.conn.vacuum()?;
380        Ok(())
381    }
382
383    fn begin_transaction(&self) -> StorageBackendResult<()> {
384        self.conn.begin_transaction()?;
385        Ok(())
386    }
387
388    fn begin_read_transaction(&self) -> StorageBackendResult<()> {
389        self.conn.begin_deferred_transaction()?;
390        Ok(())
391    }
392
393    fn begin_upgradeable_transaction(&self) -> StorageBackendResult<()> {
394        if self.native_initial_restore && !self.conn.is_native_record_session() {
395            self.conn.begin_native_initial_restore()?;
396        } else {
397            self.conn.begin_deferred_transaction()?;
398        }
399        Ok(())
400    }
401
402    fn refresh_transaction_snapshot(
403        &self,
404        cancellation: &uqa_core::CancellationToken,
405    ) -> StorageBackendResult<()> {
406        self.conn.refresh_transaction_snapshot(cancellation)?;
407        Ok(())
408    }
409
410    fn in_transaction(&self) -> bool {
411        self.conn.in_transaction()
412    }
413
414    fn transaction_has_written(&self) -> StorageBackendResult<bool> {
415        Ok(self.conn.transaction_has_written()?)
416    }
417
418    fn change_version(&self) -> StorageBackendResult<Option<u64>> {
419        Ok(self.conn.data_version()?)
420    }
421
422    fn change_version_monitor_is_nonblocking(&self) -> StorageBackendResult<bool> {
423        Ok(self.conn.data_version_monitor_is_nonblocking()?)
424    }
425
426    fn pin_transaction_snapshot(&self) -> StorageBackendResult<()> {
427        self.conn.pin_transaction_snapshot()?;
428        Ok(())
429    }
430
431    fn commit_transaction(&self) -> StorageBackendResult<()> {
432        self.conn.commit_transaction()?;
433        Ok(())
434    }
435
436    fn rollback_transaction(&self) -> StorageBackendResult<()> {
437        self.conn.rollback_transaction()?;
438        Ok(())
439    }
440
441    fn savepoint(&self, id: StorageSavepointId) -> StorageBackendResult<()> {
442        self.conn.savepoint(&id.backend_name())?;
443        Ok(())
444    }
445
446    fn release_savepoint(&self, id: StorageSavepointId) -> StorageBackendResult<()> {
447        self.conn.release_savepoint(&id.backend_name())?;
448        Ok(())
449    }
450
451    fn rollback_to_savepoint(&self, id: StorageSavepointId) -> StorageBackendResult<()> {
452        self.conn.rollback_to_savepoint(&id.backend_name())?;
453        Ok(())
454    }
455}
456
457#[cfg(test)]
458mod tests {
459    use std::collections::BTreeMap;
460
461    use uqa_analysis::analyzer::standard_analyzer;
462    use uqa_core::Value;
463    use uqa_storage::StorageBackendError;
464
465    use super::*;
466    use crate::{Catalog, SQLiteError};
467
468    #[test]
469    fn session_factories_preserve_a_bound_native_sessions_retention_options() {
470        let connection = ManagedConnection::open_in_memory().unwrap();
471        connection
472            .bind_native_records(uqa_storage::mvcc::VersionedSessionOptions {
473                retained_bytes: 8 << 20,
474            })
475            .unwrap();
476        let provider = SQLiteStorageProvider::new(connection);
477        let initial = provider.open_initial_session().unwrap();
478        let sibling = provider.open_session().unwrap();
479        initial.validate_transaction_affinity().unwrap();
480        sibling.validate_transaction_affinity().unwrap();
481        assert!(initial.backend.transaction_model().is_versioned());
482        assert_eq!(
483            initial.backend.transaction_model(),
484            sibling.backend.transaction_model()
485        );
486        assert_ne!(
487            initial.backend.transaction_affinity(),
488            sibling.backend.transaction_affinity()
489        );
490    }
491
492    #[test]
493    fn session_factory_reads_current_catalog_while_a_sibling_holds_a_writer_reservation() {
494        let directory = tempfile::tempdir().unwrap();
495        let connection = ManagedConnection::open_compressed(
496            &directory.path().join("session-writer-reservation.db"),
497            crate::SQLiteCompressionOptions::default(),
498        )
499        .unwrap();
500        let provider = SQLiteStorageProvider::new(connection);
501        let writer = provider.open_session().unwrap();
502        writer.backend.begin_transaction().unwrap();
503        writer
504            .catalog
505            .set_metadata("private-write", "uncommitted")
506            .unwrap();
507
508        let reader = provider.open_session().unwrap();
509        assert_eq!(reader.catalog.get_metadata("private-write").unwrap(), None);
510        writer.backend.commit_transaction().unwrap();
511        assert_eq!(
512            reader
513                .catalog
514                .get_metadata("private-write")
515                .unwrap()
516                .as_deref(),
517            Some("uncommitted")
518        );
519    }
520
521    #[test]
522    fn sqlite_backend_builds_document_index_and_vector_stores() {
523        let conn = ManagedConnection::open_in_memory().unwrap();
524        let _catalog = Catalog::open(conn.clone()).unwrap();
525        let backend = SQLiteStorageBackend::new(conn);
526
527        let mut doc = BTreeMap::new();
528        doc.insert("title".to_string(), Value::Str("rust storage".into()));
529        let mut docs = backend.document_store("articles");
530        docs.put(1, doc).unwrap();
531        assert_eq!(
532            docs.get_field(1, "title").unwrap(),
533            Some(Value::Str("rust storage".into()))
534        );
535
536        let mut inv = backend.inverted_index("articles", standard_analyzer("english"));
537        inv.add_document(
538            1,
539            BTreeMap::from([("title".to_string(), "rust storage".to_string())]),
540        )
541        .unwrap();
542        assert_eq!(inv.doc_freq("title", "rust").unwrap(), 1);
543
544        let mut vectors = backend
545            .vector_index(
546                "articles",
547                "embedding",
548                2,
549                VectorIndexSpec::IVF(uqa_storage::IVFIndexParams {
550                    nlist: 2,
551                    nprobe: 1,
552                    train_threshold: 2,
553                }),
554                VectorIndexOpenMode::Create,
555            )
556            .unwrap();
557        vectors.add(1, vec![1.0, 0.0]).unwrap();
558        let hits = vectors.search_knn(&[1.0, 0.0], 1).unwrap();
559        assert_eq!(hits.entries().len(), 1);
560        assert_eq!(hits.entries()[0].doc_id, 1);
561    }
562
563    #[test]
564    fn sqlite_backend_transaction_rolls_back_cross_store_writes() {
565        let conn = ManagedConnection::open_in_memory().unwrap();
566        let _catalog = Catalog::open(conn.clone()).unwrap();
567        let backend = SQLiteStorageBackend::new(conn);
568        let mut docs = backend.document_store("articles");
569        let mut inv = backend.inverted_index("articles", standard_analyzer("english"));
570
571        backend.begin_transaction().unwrap();
572        docs.put(
573            1,
574            BTreeMap::from([("title".to_string(), Value::Str("rollback".into()))]),
575        )
576        .unwrap();
577        inv.add_document(
578            1,
579            BTreeMap::from([("title".to_string(), "rollback".to_string())]),
580        )
581        .unwrap();
582        backend.rollback_transaction().unwrap();
583
584        assert_eq!(docs.len().unwrap(), 0);
585        assert_eq!(inv.doc_freq("title", "rollback").unwrap(), 0);
586    }
587
588    #[test]
589    fn sqlite_sessions_isolate_and_atomically_commit_cross_store_writes() {
590        let dir = tempfile::tempdir().unwrap();
591        let path = dir.path().join("cross-store-isolation.sqlite3");
592        let conn = ManagedConnection::open(&path).unwrap();
593        let catalog = Catalog::open(conn.clone()).unwrap();
594        let writer = SQLiteStorageBackend::new(conn.clone());
595        let observer_conn = conn.new_session();
596        let observer_catalog = Catalog::open(observer_conn.clone()).unwrap();
597        let observer = SQLiteStorageBackend::new(observer_conn);
598
599        let mut writer_docs = writer.document_store("articles");
600        let mut writer_inv = writer.inverted_index("articles", standard_analyzer("english"));
601        let mut writer_vectors = writer
602            .vector_index(
603                "articles",
604                "embedding",
605                2,
606                VectorIndexSpec::BruteForce,
607                VectorIndexOpenMode::Create,
608            )
609            .unwrap();
610        let observer_docs = observer.document_store("articles");
611        let observer_inv = observer.inverted_index("articles", standard_analyzer("english"));
612        let observer_vectors = observer
613            .vector_index(
614                "articles",
615                "embedding",
616                2,
617                VectorIndexSpec::BruteForce,
618                VectorIndexOpenMode::Restore,
619            )
620            .unwrap();
621
622        writer.begin_transaction().unwrap();
623        writer_docs
624            .put(
625                1,
626                BTreeMap::from([("title".to_string(), Value::Str("atomic rust".into()))]),
627            )
628            .unwrap();
629        writer_inv
630            .add_document(
631                1,
632                BTreeMap::from([("title".to_string(), "atomic rust".to_string())]),
633            )
634            .unwrap();
635        writer_vectors.add(1, vec![1.0, 0.0]).unwrap();
636        catalog
637            .save_scoring_params("transactional", r#"{"alpha":1.0}"#)
638            .unwrap();
639
640        assert_eq!(writer_docs.len().unwrap(), 1);
641        assert_eq!(writer_inv.doc_freq("title", "rust").unwrap(), 1);
642        assert_eq!(writer_vectors.count().unwrap(), 1);
643        assert!(catalog
644            .load_scoring_params("transactional")
645            .unwrap()
646            .is_some());
647
648        assert_eq!(observer_docs.len().unwrap(), 0);
649        assert_eq!(observer_inv.doc_freq("title", "rust").unwrap(), 0);
650        assert_eq!(observer_vectors.count().unwrap(), 0);
651        assert!(observer_catalog
652            .load_scoring_params("transactional")
653            .unwrap()
654            .is_none());
655
656        writer.commit_transaction().unwrap();
657        assert_eq!(observer_docs.len().unwrap(), 1);
658        assert_eq!(observer_inv.doc_freq("title", "rust").unwrap(), 1);
659        assert_eq!(observer_vectors.count().unwrap(), 1);
660        assert!(observer_catalog
661            .load_scoring_params("transactional")
662            .unwrap()
663            .is_some());
664    }
665
666    #[test]
667    fn ignored_legacy_index_error_cannot_commit_partial_document_write() {
668        let dir = tempfile::tempdir().unwrap();
669        let path = dir.path().join("ignored-index-error.sqlite3");
670        let conn = ManagedConnection::open(&path).unwrap();
671        let _catalog = Catalog::open(conn.clone()).unwrap();
672        let backend = SQLiteStorageBackend::new(conn.clone());
673        let observer = conn.new_session();
674        let mut docs = backend.document_store("articles");
675        let mut vectors = backend
676            .vector_index(
677                "articles",
678                "embedding",
679                2,
680                VectorIndexSpec::BruteForce,
681                VectorIndexOpenMode::Create,
682            )
683            .unwrap();
684
685        backend.begin_transaction().unwrap();
686        docs.put(
687            1,
688            BTreeMap::from([("title".to_string(), Value::Str("must roll back".into()))]),
689        )
690        .unwrap();
691        conn.with(|connection| {
692            connection.execute("DROP TABLE _vectors", [])?;
693            Ok(())
694        })
695        .unwrap();
696        // The vector write reports its error directly. Even if a caller
697        // ignores that Result, the managed transaction is poisoned and the
698        // partial document write cannot commit.
699        let ignored = vectors.add(1, vec![1.0, 0.0]);
700        assert!(ignored.is_err());
701        assert!(matches!(
702            backend.commit_transaction(),
703            Err(StorageBackendError::Backend { source, .. })
704                if matches!(source.downcast_ref::<SQLiteError>(), Some(SQLiteError::TransactionAborted(_)))
705        ));
706
707        let stored_docs: i64 = observer
708            .with(|connection| {
709                Ok(connection.query_row(
710                    "SELECT COUNT(*) FROM _documents WHERE table_name = 'articles'",
711                    [],
712                    |row| row.get(0),
713                )?)
714            })
715            .unwrap();
716        let vector_table_exists: i64 = observer
717            .with(|connection| {
718                Ok(connection.query_row(
719                    "SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = '_vectors'",
720                    [],
721                    |row| row.get(0),
722                )?)
723            })
724            .unwrap();
725        assert_eq!(stored_docs, 0);
726        assert_eq!(vector_table_exists, 1);
727    }
728}