Skip to main content

uqa_storage/sqlite/vector_index/hnsw/
mod.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! SQLite-backed HNSW index with a session-local immutable graph generation.
8
9use std::sync::Arc;
10
11use parking_lot::RwLock;
12
13use super::{ManagedConnection, SQLiteResult, SQLiteVectorIndex};
14use crate::hnsw_index::HNSWIndex;
15use crate::vector_index::HNSWIndexParams;
16
17mod consistency;
18mod encoding;
19mod lifecycle;
20mod loading;
21mod mutation;
22mod persistence;
23mod search;
24mod writing;
25
26#[cfg(test)]
27mod tests;
28
29#[derive(Clone)]
30pub struct SQLiteHNSWIndex {
31    pub(super) persistent: SQLiteVectorIndex,
32    pub(super) params: HNSWIndexParams,
33    pub(super) graph: Arc<RwLock<Option<CachedGraph>>>,
34    pub(super) require_persisted_graph: bool,
35}
36
37#[derive(Clone)]
38pub(super) struct CachedGraph {
39    pub(super) revision: u64,
40    pub(super) graph: Arc<HNSWIndex>,
41}
42
43impl SQLiteHNSWIndex {
44    pub fn new(
45        conn: ManagedConnection,
46        table: impl Into<String>,
47        field: impl Into<String>,
48        dimensions: u32,
49    ) -> Self {
50        Self::with_params(conn, table, field, dimensions, HNSWIndexParams::default())
51    }
52
53    pub fn with_params(
54        conn: ManagedConnection,
55        table: impl Into<String>,
56        field: impl Into<String>,
57        dimensions: u32,
58        params: HNSWIndexParams,
59    ) -> Self {
60        Self {
61            persistent: SQLiteVectorIndex::new(conn, table, field, dimensions),
62            params,
63            graph: Arc::new(RwLock::new(None)),
64            require_persisted_graph: false,
65        }
66    }
67
68    pub fn open_existing(
69        conn: ManagedConnection,
70        table: impl Into<String>,
71        field: impl Into<String>,
72        dimensions: u32,
73        params: HNSWIndexParams,
74    ) -> Self {
75        let mut index = Self::with_params(conn, table, field, dimensions, params);
76        index.require_persisted_graph = true;
77        index
78    }
79
80    pub fn drop_metadata(conn: &ManagedConnection, table: &str, field: &str) -> SQLiteResult<()> {
81        writing::drop_metadata(conn, table, field)
82    }
83}