1use std::{path::Path, sync::Arc};
10
11use crate::{ManagedConnection, Result as SQLiteResult};
12use uqa_storage::mvcc::{StorageTransactionId, VersionedKeyValueStore, VersionedSessionOptions};
13use uqa_storage::read_control::{KeyValueReadVisitor, StorageReadControl, ValueReadVisitor};
14use uqa_storage::{
15 CatalogFacade, KeyValueBatch, KeyValueCatalog, KeyValueStorageBackend, KeyValueStore,
16 PersistentStorageBackend, PersistentStorageIdentity, PersistentStorageProvider,
17 PersistentStorageSession, StorageBackendResult,
18};
19
20#[derive(Clone)]
22pub struct SQLiteKeyValueStore {
23 conn: ManagedConnection,
24 records: Arc<VersionedKeyValueStore>,
25}
26
27impl SQLiteKeyValueStore {
28 pub fn open(path: &Path) -> SQLiteResult<Self> {
29 Self::new(ManagedConnection::open(path)?)
30 }
31
32 pub fn open_in_memory() -> SQLiteResult<Self> {
33 Self::new(ManagedConnection::open_in_memory()?)
34 }
35
36 pub fn new(conn: ManagedConnection) -> SQLiteResult<Self> {
37 Self::with_options(conn, VersionedSessionOptions::default())
38 }
39
40 pub fn with_options(
42 conn: ManagedConnection,
43 options: VersionedSessionOptions,
44 ) -> SQLiteResult<Self> {
45 let records = conn.bind_records(options)?;
46 Ok(Self { conn, records })
47 }
48
49 pub fn connection(&self) -> ManagedConnection {
50 self.conn.clone()
51 }
52
53 pub fn new_session(&self) -> Self {
54 self.new_session_with_cancellation(&uqa_core::CancellationToken::new())
55 }
56
57 pub fn new_session_with_cancellation(
58 &self,
59 cancellation: &uqa_core::CancellationToken,
60 ) -> Self {
61 let conn = self.conn.new_session_with_cancellation(cancellation);
62 let records = conn
63 .bind_records(self.records.options())
64 .expect("a fresh connection inherits the same logical session configuration");
65 Self { conn, records }
66 }
67
68 pub fn pending_commit(&self) -> Option<StorageTransactionId> {
69 self.records.pending_commit()
70 }
71}
72
73impl uqa_storage::mvcc::IdentifierAllocator for SQLiteKeyValueStore {
74 fn identifier_watermark(&self, namespace: &[u8]) -> StorageBackendResult<Option<u64>> {
75 self.conn
76 .with_records(|store| store.identifier_watermark(namespace))
77 }
78
79 fn allocate_identifiers(
80 &self,
81 namespace: &[u8],
82 request: uqa_storage::mvcc::IdentifierRequest,
83 ) -> StorageBackendResult<uqa_storage::mvcc::IdentifierAllocation> {
84 self.conn
85 .with_records(|store| store.allocate_identifiers(namespace, request))
86 }
87}
88
89impl KeyValueStore for SQLiteKeyValueStore {
90 fn auxiliary_encryption_key(&self) -> Option<uqa_storage::StorageEncryptionKey> {
91 self.conn.auxiliary_encryption_key()
92 }
93
94 fn retention_control(&self) -> Option<uqa_storage::read_control::StorageReadControl> {
95 Some(self.records.retention_control())
96 }
97
98 fn notification_publications(
99 &self,
100 ) -> Option<&dyn uqa_storage::notifications::NotificationPublicationStore> {
101 Some(&self.conn)
102 }
103
104 fn serializable_session(&self) -> Option<&dyn uqa_storage::mvcc::SerializableSession> {
105 Some(&self.conn)
106 }
107
108 fn vacuum(&self) -> StorageBackendResult<()> {
109 self.conn.vacuum().map_err(Into::into)
110 }
111
112 fn write_cancellation(&self) -> Option<uqa_core::CancellationToken> {
113 self.records.write_cancellation()
114 }
115
116 fn open_session_with_cancellation(
117 &self,
118 cancellation: &uqa_core::CancellationToken,
119 ) -> StorageBackendResult<Arc<dyn KeyValueStore>> {
120 cancellation.check()?;
121 Ok(Arc::new(self.new_session_with_cancellation(cancellation)))
122 }
123
124 fn open_retained_read_session(
125 &self,
126 cancellation: &uqa_core::CancellationToken,
127 ) -> StorageBackendResult<Arc<dyn KeyValueStore>> {
128 let conn = self.conn.new_retained_read_session(cancellation)?;
129 Ok(Arc::new(Self::with_options(conn, self.records.options())?))
130 }
131
132 fn transaction_model(&self) -> uqa_storage::StorageTransactionModel {
133 self.records.transaction_model()
134 }
135
136 fn refresh_transaction_snapshot(
137 &self,
138 cancellation: &uqa_core::CancellationToken,
139 ) -> StorageBackendResult<()> {
140 self.conn
141 .with_records(|store| store.refresh_transaction_snapshot(cancellation))
142 }
143
144 fn identifier_allocator(&self) -> Option<&dyn uqa_storage::mvcc::IdentifierAllocator> {
145 Some(self)
146 }
147
148 fn with_read_view(
149 &self,
150 read: &mut uqa_storage::key_value::KeyValueReadScope<'_>,
151 ) -> StorageBackendResult<()> {
152 self.conn.with_records(|store| store.with_read_view(read))
153 }
154
155 fn with_mutation(
156 &self,
157 mutate: &mut uqa_storage::key_value::KeyValueMutation<'_>,
158 ) -> StorageBackendResult<()> {
159 self.conn.with_records(|store| store.with_mutation(mutate))
160 }
161
162 fn transaction_affinity(&self) -> Option<uqa_storage::StorageSessionAffinity> {
163 Some(self.records.session_affinity())
164 }
165
166 fn storage_identity(&self) -> StorageBackendResult<Option<PersistentStorageIdentity>> {
167 self.records.storage_identity()
168 }
169
170 fn open_session(&self) -> StorageBackendResult<Arc<dyn KeyValueStore>> {
171 Ok(Arc::new(self.new_session()))
172 }
173
174 fn get(&self, key: &[u8]) -> StorageBackendResult<Option<Vec<u8>>> {
175 self.conn.with_records(|store| store.get(key))
176 }
177
178 fn contains_key(&self, key: &[u8]) -> StorageBackendResult<bool> {
179 self.conn.with_records(|store| store.contains_key(key))
180 }
181
182 fn contains_prefix_budgeted(
183 &self,
184 prefix: &[u8],
185 control: &StorageReadControl,
186 ) -> StorageBackendResult<bool> {
187 self.conn
188 .with_records(|store| store.contains_prefix_budgeted(prefix, control))
189 }
190
191 fn visit_value(
192 &self,
193 key: &[u8],
194 control: &StorageReadControl,
195 visit: &mut ValueReadVisitor<'_>,
196 ) -> StorageBackendResult<()> {
197 self.conn
198 .with_records(|store| store.visit_value(key, control, visit))
199 }
200
201 fn visit_prefix_after(
202 &self,
203 prefix: &[u8],
204 after: Option<&[u8]>,
205 limit: usize,
206 control: &StorageReadControl,
207 visit: &mut KeyValueReadVisitor<'_>,
208 ) -> StorageBackendResult<()> {
209 self.conn
210 .with_records(|store| store.visit_prefix_after(prefix, after, limit, control, visit))
211 }
212
213 fn put(&self, key: &[u8], value: &[u8]) -> StorageBackendResult<()> {
214 self.conn.with_records(|store| store.put(key, value))
215 }
216
217 fn delete(&self, key: &[u8]) -> StorageBackendResult<()> {
218 self.conn.with_records(|store| store.delete(key))
219 }
220
221 fn delete_prefix(&self, prefix: &[u8]) -> StorageBackendResult<usize> {
222 self.conn.with_records(|store| store.delete_prefix(prefix))
223 }
224
225 fn scan_prefix(&self, prefix: &[u8]) -> StorageBackendResult<Vec<(Vec<u8>, Vec<u8>)>> {
226 self.conn.with_records(|store| store.scan_prefix(prefix))
227 }
228
229 fn scan_prefix_after(
230 &self,
231 prefix: &[u8],
232 after: Option<&[u8]>,
233 limit: usize,
234 ) -> StorageBackendResult<Vec<(Vec<u8>, Vec<u8>)>> {
235 self.conn
236 .with_records(|store| store.scan_prefix_after(prefix, after, limit))
237 }
238
239 fn scan_prefix_keys_after(
240 &self,
241 prefix: &[u8],
242 after: Option<&[u8]>,
243 limit: usize,
244 ) -> StorageBackendResult<Vec<Vec<u8>>> {
245 self.conn
246 .with_records(|store| store.scan_prefix_keys_after(prefix, after, limit))
247 }
248
249 fn first_prefix_after(
250 &self,
251 prefix: &[u8],
252 after: Option<&[u8]>,
253 ) -> StorageBackendResult<Option<(Vec<u8>, Vec<u8>)>> {
254 self.conn
255 .with_records(|store| store.first_prefix_after(prefix, after))
256 }
257
258 fn batch(&self) -> Box<dyn KeyValueBatch + '_> {
259 Box::new(SQLiteKeyValueBatch {
260 connection: &self.conn,
261 batch: self.records.batch(),
262 })
263 }
264
265 fn begin_transaction(&self) -> StorageBackendResult<()> {
266 self.conn.begin_transaction().map_err(Into::into)
267 }
268
269 fn begin_read_transaction(&self) -> StorageBackendResult<()> {
270 self.conn.begin_record_read().map_err(Into::into)
271 }
272
273 fn begin_upgradeable_transaction(&self) -> StorageBackendResult<()> {
274 self.conn.begin_deferred_transaction().map_err(Into::into)
275 }
276
277 fn in_transaction(&self) -> bool {
278 self.conn.in_transaction()
279 }
280
281 fn transaction_has_written(&self) -> StorageBackendResult<bool> {
282 self.conn.transaction_has_written().map_err(Into::into)
283 }
284
285 fn change_version(&self) -> StorageBackendResult<Option<u64>> {
286 self.conn.data_version().map_err(Into::into)
287 }
288
289 fn change_version_monitor_is_nonblocking(&self) -> StorageBackendResult<bool> {
290 Ok(true)
291 }
292
293 fn pin_transaction_snapshot(&self) -> StorageBackendResult<()> {
294 self.conn.pin_transaction_snapshot().map_err(Into::into)
295 }
296
297 fn commit_transaction(&self) -> StorageBackendResult<()> {
298 self.conn.commit_transaction().map_err(Into::into)
299 }
300
301 fn rollback_transaction(&self) -> StorageBackendResult<()> {
302 self.conn.rollback_transaction().map_err(Into::into)
303 }
304
305 fn savepoint(&self, name: &str) -> StorageBackendResult<()> {
306 self.conn.savepoint(name).map_err(Into::into)
307 }
308
309 fn release_savepoint(&self, name: &str) -> StorageBackendResult<()> {
310 self.conn.release_savepoint(name).map_err(Into::into)
311 }
312
313 fn rollback_to_savepoint(&self, name: &str) -> StorageBackendResult<()> {
314 self.conn.rollback_to_savepoint(name).map_err(Into::into)
315 }
316}
317
318struct SQLiteKeyValueBatch<'a> {
319 connection: &'a ManagedConnection,
320 batch: Box<dyn KeyValueBatch + 'a>,
321}
322
323impl KeyValueBatch for SQLiteKeyValueBatch<'_> {
324 fn serializable_participant(&self) -> Option<uqa_storage::mvcc::SerializableTransactionId> {
325 self.batch.serializable_participant()
326 }
327 fn observe_serializable_write(
328 &mut self,
329 predicate: uqa_storage::mvcc::SerializablePredicate<'_>,
330 ) -> StorageBackendResult<()> {
331 self.batch.observe_serializable_write(predicate)
332 }
333 fn require_unchanged(&mut self, key: &[u8]) -> StorageBackendResult<()> {
334 self.batch.require_unchanged(key)
335 }
336 fn touch_marker(&mut self, key: &[u8], value: &[u8]) -> StorageBackendResult<()> {
337 self.batch.touch_marker(key, value)
338 }
339 fn observe_identifier(&mut self, namespace: &[u8], value: u64) -> StorageBackendResult<()> {
340 self.batch.observe_identifier(namespace, value)
341 }
342 fn inherit_identifiers(&mut self, from: &[u8], to: &[u8]) -> StorageBackendResult<()> {
343 self.batch.inherit_identifiers(from, to)
344 }
345 fn ivf_mutation(
346 &mut self,
347 metadata: &[u8],
348 mutation: uqa_storage::ivf_index::IVFMutation<'_>,
349 ) -> StorageBackendResult<()> {
350 self.batch.ivf_mutation(metadata, mutation)
351 }
352 fn preview_ivf_record(&mut self, key: &[u8], value: Option<&[u8]>) -> StorageBackendResult<()> {
353 self.batch.preview_ivf_record(key, value)
354 }
355 fn preview_ivf_prefix(&mut self, prefix: &[u8]) -> StorageBackendResult<()> {
356 self.batch.preview_ivf_prefix(prefix)
357 }
358 fn fence_ivf_prefix(&mut self, prefix: &[u8]) -> StorageBackendResult<()> {
359 self.batch.fence_ivf_prefix(prefix)
360 }
361 fn hnsw_mutation(
362 &mut self,
363 metadata: &[u8],
364 mutation: uqa_storage::hnsw_index::HNSWMutation<'_>,
365 ) -> StorageBackendResult<()> {
366 self.batch.hnsw_mutation(metadata, mutation)
367 }
368 fn preview_hnsw_record(
369 &mut self,
370 key: &[u8],
371 value: Option<&[u8]>,
372 ) -> StorageBackendResult<()> {
373 self.batch.preview_hnsw_record(key, value)
374 }
375 fn preview_hnsw_prefix(&mut self, prefix: &[u8]) -> StorageBackendResult<()> {
376 self.batch.preview_hnsw_prefix(prefix)
377 }
378 fn fence_hnsw_prefix(&mut self, prefix: &[u8]) -> StorageBackendResult<()> {
379 self.batch.fence_hnsw_prefix(prefix)
380 }
381 fn put(&mut self, key: &[u8], value: &[u8]) -> StorageBackendResult<()> {
382 self.batch.put(key, value)
383 }
384 fn replace_statistics_maintenance(
385 &mut self,
386 key: &[u8],
387 value: &[u8],
388 ) -> StorageBackendResult<()> {
389 self.batch.replace_statistics_maintenance(key, value)
390 }
391 fn delete(&mut self, key: &[u8]) -> StorageBackendResult<()> {
392 self.batch.delete(key)
393 }
394 fn delete_prefix(&mut self, prefix: &[u8]) -> StorageBackendResult<()> {
395 self.batch.delete_prefix(prefix)
396 }
397 fn replace_occurrence_record(
398 &mut self,
399 key: &[u8],
400 value: Option<&[u8]>,
401 ) -> StorageBackendResult<()> {
402 self.batch.replace_occurrence_record(key, value)
403 }
404 fn invalidate_occurrence_prefix(&mut self, prefix: &[u8]) -> StorageBackendResult<()> {
405 self.batch.invalidate_occurrence_prefix(prefix)
406 }
407 fn occurrence_document(&mut self, table: &str, document: u64) -> StorageBackendResult<()> {
408 self.batch.occurrence_document(table, document)
409 }
410 fn reset_occurrences(&mut self, table: &str) -> StorageBackendResult<()> {
411 self.batch.reset_occurrences(table)
412 }
413 fn fence_record(&mut self, key: &[u8]) -> StorageBackendResult<()> {
414 self.batch.fence_record(key)
415 }
416 fn graph_mutation(
417 &mut self,
418 mutation: uqa_storage::mvcc::GraphMutation<'_>,
419 ) -> StorageBackendResult<()> {
420 self.batch.graph_mutation(mutation)
421 }
422 fn preview_graph_invalidation(
423 &mut self,
424 key: &[u8],
425 value: Option<&[u8]>,
426 ) -> StorageBackendResult<()> {
427 self.batch.preview_graph_invalidation(key, value)
428 }
429 fn replace_graph_cache(
430 &mut self,
431 key: &[u8],
432 value: Option<&[u8]>,
433 ) -> StorageBackendResult<()> {
434 self.batch.replace_graph_cache(key, value)
435 }
436 fn commit(self: Box<Self>) -> StorageBackendResult<()> {
437 let Self { connection, batch } = *self;
438 connection.with_records(|_| batch.commit())
439 }
440}
441
442#[cfg(test)]
443#[path = "key_value/controlled/tests.rs"]
444mod controlled_tests;
445
446#[derive(Clone)]
448pub struct SQLiteKeyValueStorage {
449 store: Arc<SQLiteKeyValueStore>,
450}
451
452pub type SQLiteKeyValueCatalog = KeyValueCatalog;
453pub type SQLiteKeyValueStorageBackend = KeyValueStorageBackend;
454
455impl SQLiteKeyValueStorage {
456 pub fn open(path: &Path) -> SQLiteResult<Self> {
457 Self::open_with_options(path, VersionedSessionOptions::default())
458 }
459
460 pub fn open_with_options(path: &Path, options: VersionedSessionOptions) -> SQLiteResult<Self> {
461 Self::from_connection_with_options(ManagedConnection::open(path)?, options)
462 }
463
464 pub fn open_in_memory() -> SQLiteResult<Self> {
465 Ok(Self {
466 store: Arc::new(SQLiteKeyValueStore::open_in_memory()?),
467 })
468 }
469
470 pub fn from_connection(conn: ManagedConnection) -> SQLiteResult<Self> {
471 Self::from_connection_with_options(conn, VersionedSessionOptions::default())
472 }
473
474 pub fn from_connection_with_options(
475 conn: ManagedConnection,
476 options: VersionedSessionOptions,
477 ) -> SQLiteResult<Self> {
478 Ok(Self {
479 store: Arc::new(SQLiteKeyValueStore::with_options(conn, options)?),
480 })
481 }
482
483 pub fn store(&self) -> Arc<SQLiteKeyValueStore> {
484 Arc::clone(&self.store)
485 }
486
487 pub fn catalog(&self) -> SQLiteKeyValueCatalog {
488 let store: Arc<dyn KeyValueStore> = self.store.clone();
489 KeyValueCatalog::new(store)
490 }
491
492 pub fn backend(&self) -> SQLiteKeyValueStorageBackend {
493 let store: Arc<dyn KeyValueStore> = self.store.clone();
494 KeyValueStorageBackend::new(store)
495 }
496}
497
498impl PersistentStorageProvider for SQLiteKeyValueStorage {
499 fn auxiliary_encryption_key(&self) -> Option<uqa_storage::StorageEncryptionKey> {
500 self.store.conn.auxiliary_encryption_key()
501 }
502
503 fn open_session(&self) -> StorageBackendResult<PersistentStorageSession> {
504 self.open_session_with_cancellation(&uqa_core::CancellationToken::new())
505 }
506
507 fn open_session_with_cancellation(
508 &self,
509 cancellation: &uqa_core::CancellationToken,
510 ) -> StorageBackendResult<PersistentStorageSession> {
511 cancellation.check()?;
512 let store: Arc<dyn KeyValueStore> =
513 Arc::new(self.store.new_session_with_cancellation(cancellation));
514 let catalog: Arc<dyn CatalogFacade> = Arc::new(KeyValueCatalog::new(Arc::clone(&store)));
515 let backend: Arc<dyn PersistentStorageBackend> =
516 Arc::new(KeyValueStorageBackend::new(store));
517 Ok(PersistentStorageSession::new(catalog, backend))
518 }
519
520 fn storage_identity(
521 &self,
522 ) -> StorageBackendResult<Option<uqa_storage::PersistentStorageIdentity>> {
523 self.store.connection().storage_identity()
524 }
525}
526
527#[cfg(test)]
528mod tests {
529 use std::collections::BTreeMap;
530
531 use uqa_analysis::standard_analyzer;
532 use uqa_core::Value;
533 use uqa_storage::catalog::{ColumnStatsInput, TableSchema};
534 use uqa_storage::{
535 CatalogFacade, PersistentStorageBackend, VectorIndexOpenMode, VectorIndexSpec,
536 };
537
538 use super::*;
539
540 #[test]
541 fn sqlite_key_value_store_round_trips_and_reopens() {
542 let dir = tempfile::tempdir().unwrap();
543 let path = dir.path().join("keyvalue.sqlite3");
544 {
545 let store = SQLiteKeyValueStore::open(&path).unwrap();
546 store.put(b"apple/1", b"red").unwrap();
547 store.put(b"apple/2", b"green").unwrap();
548 store.put(b"banana/1", b"yellow").unwrap();
549 store.put(&[0x10, 0xff, 0x01], b"binary-prefix").unwrap();
550 store.put(&[0x11, 0x00], b"binary-neighbour").unwrap();
551 assert_eq!(store.get(b"apple/1").unwrap().as_deref(), Some(&b"red"[..]));
552 assert_eq!(store.scan_prefix(b"apple/").unwrap().len(), 2);
553 assert_eq!(
554 store
555 .scan_prefix_keys_after(b"apple/", Some(b"apple/1"), 1)
556 .unwrap(),
557 vec![b"apple/2".to_vec()]
558 );
559 assert_eq!(
560 store
561 .scan_prefix_keys_after(b"apple/", Some(b"a"), 2)
562 .unwrap(),
563 vec![b"apple/1".to_vec(), b"apple/2".to_vec()]
564 );
565 assert!(store
566 .scan_prefix_keys_after(b"apple/", Some(b"z"), 2)
567 .unwrap()
568 .is_empty());
569 assert_eq!(
570 store.first_prefix_after(b"apple/", Some(b"a")).unwrap(),
571 Some((b"apple/1".to_vec(), b"red".to_vec()))
572 );
573 assert!(store
574 .scan_prefix_keys_after(b"apple/", None, 0)
575 .unwrap()
576 .is_empty());
577 assert_eq!(store.scan_prefix(&[0x10, 0xff]).unwrap().len(), 1);
578 store.delete_prefix(&[0x10, 0xff]).unwrap();
579 assert_eq!(
580 store.get(&[0x11, 0x00]).unwrap().as_deref(),
581 Some(&b"binary-neighbour"[..])
582 );
583 }
584 {
585 let store = SQLiteKeyValueStore::open(&path).unwrap();
586 assert_eq!(
587 store.get(b"apple/2").unwrap().as_deref(),
588 Some(&b"green"[..])
589 );
590 store.delete_prefix(b"apple/").unwrap();
591 assert!(store.get(b"apple/1").unwrap().is_none());
592 assert_eq!(
593 store.get(b"banana/1").unwrap().as_deref(),
594 Some(&b"yellow"[..])
595 );
596 }
597 }
598
599 #[test]
600 fn sqlite_store_passes_the_reusable_backend_contract() {
601 let directory = tempfile::tempdir().unwrap();
602 let reader =
603 SQLiteKeyValueStore::open(&directory.path().join("conformance.sqlite3")).unwrap();
604 let writer = reader.new_session();
605 uqa_storage::key_value::conformance::verify_store(&reader).unwrap();
606 uqa_storage::key_value::conformance::verify_session_isolation(&reader, &writer).unwrap();
607 }
608
609 #[test]
610 fn sqlite_key_value_batch_is_atomic() {
611 let store = SQLiteKeyValueStore::open_in_memory().unwrap();
612 let mut batch = store.batch();
613 batch.put(b"k1", b"v1").unwrap();
614 batch.put(b"k2", b"v2").unwrap();
615 batch.commit().unwrap();
616 assert_eq!(store.get(b"k1").unwrap().as_deref(), Some(&b"v1"[..]));
617
618 store.begin_transaction().unwrap();
619 store.put(b"k3", b"v3").unwrap();
620 store.rollback_transaction().unwrap();
621 assert!(store.get(b"k3").unwrap().is_none());
622 }
623
624 #[test]
625 fn sqlite_key_value_storage_supports_existing_store_contracts() {
626 let storage = SQLiteKeyValueStorage::open_in_memory().unwrap();
627 let backend = storage.backend();
628
629 let mut docs = backend.document_store("articles");
630 docs.put(
631 1,
632 BTreeMap::from([("title".to_string(), Value::Str("rust search".into()))]),
633 )
634 .unwrap();
635 assert_eq!(
636 docs.get_field(1, "title").unwrap(),
637 Some(Value::Str("rust search".into()))
638 );
639
640 let mut index = backend.inverted_index("articles", standard_analyzer("english"));
641 index
642 .add_document(1, BTreeMap::from([("title".into(), "rust search".into())]))
643 .unwrap();
644 assert_eq!(index.doc_freq("title", "rust").unwrap(), 1);
645
646 let mut vectors = backend
647 .vector_index(
648 "articles",
649 "embedding",
650 2,
651 VectorIndexSpec::BruteForce,
652 VectorIndexOpenMode::Create,
653 )
654 .unwrap();
655 vectors.add(1, vec![1.0, 0.0]).unwrap();
656 vectors.add(2, vec![0.0, 1.0]).unwrap();
657 let hits = vectors.search_knn(&[1.0, 0.0], 1).unwrap();
658 assert_eq!(hits.entries()[0].doc_id, 1);
659 }
660
661 #[test]
662 fn sqlite_key_value_catalog_supports_existing_registry_contracts() {
663 let storage = SQLiteKeyValueStorage::open_in_memory().unwrap();
664 let catalog = storage.catalog();
665 catalog.set_metadata("schema_version", "keyvalue").unwrap();
666 catalog.save_schema("public").unwrap();
667 catalog
668 .save_table(&TableSchema {
669 relation: uqa_storage::RelationIdentity::new("public", "docs"),
670 security: uqa_storage::RelationSecurityRow::legacy("uqa"),
671 object_id: [1; 16],
672 storage_generation: [1; 16],
673 analyzer_json: "{}".into(),
674 fts_fields: vec!["title".into()],
675 vector_fields: Vec::new(),
676 columns_json: "[]".into(),
677 constraints_json: String::new(),
678 })
679 .unwrap();
680 catalog
681 .save_analyzer("ko", "{\"name\":\"standard\"}")
682 .unwrap();
683 catalog
684 .save_table_field_analyzer("docs", "title", "index", "ko")
685 .unwrap();
686 catalog
687 .save_foreign_server("fs", "memory", "{\"root\":\"/tmp\"}")
688 .unwrap();
689 catalog
690 .save_catalog_index(
691 &uqa_storage::RelationIdentity::new("public", "idx_docs_title"),
692 "gin",
693 "public.docs",
694 "[\"title\"]",
695 "{}",
696 )
697 .unwrap();
698 catalog
699 .save_column_stats(ColumnStatsInput::basic(
700 "docs",
701 "title",
702 4,
703 0,
704 Some("a"),
705 Some("z"),
706 10,
707 ))
708 .unwrap();
709
710 assert_eq!(
711 catalog.get_metadata("schema_version").unwrap().as_deref(),
712 Some("keyvalue")
713 );
714 assert_eq!(
715 catalog.load_tables().unwrap()[0].relation.qualified_name(),
716 "public.docs"
717 );
718 assert_eq!(catalog.load_analyzers().unwrap()[0].0, "ko");
719 assert_eq!(catalog.load_foreign_servers().unwrap()[0].0, "fs");
720 assert_eq!(
721 catalog.load_catalog_indexes().unwrap()[0]
722 .relation
723 .qualified_name(),
724 "public.idx_docs_title"
725 );
726 assert_eq!(
727 catalog.load_column_stats("docs").unwrap()[0].distinct_count,
728 4
729 );
730 }
731}