uqa-storage-sqlite 0.5.4

SQLite catalog, indexes, compressed storage, graph and key/value providers
Documentation
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! HNSW query selection with an exact fallback before initial graph build.

use uqa_core::PostingList;

use super::SQLiteHNSWIndex;
use uqa_storage::vector_index::VectorIndex;
use uqa_storage::StorageBackendResult;

impl SQLiteHNSWIndex {
    pub(super) fn search_top_k(
        &self,
        query: &[f32],
        k: usize,
    ) -> StorageBackendResult<PostingList> {
        if let Some(snapshot) = self.native_snapshot()? {
            return match snapshot
                .persistent
                .read_native(|read| snapshot.cached_native_graph(read))?
                .flatten()
            {
                Some(graph) => graph.search_knn(query, k),
                None if self.require_persisted_graph => {
                    Err(super::mutation::missing_metadata(self))
                }
                None => snapshot.persistent.search_knn(query, k),
            };
        }
        if let Some(cached) = self.graph_snapshot()? {
            return cached.graph.search_knn(query, k);
        }
        if self.require_persisted_graph {
            return Err(super::mutation::missing_metadata(self));
        }
        self.persistent.search_knn(query, k)
    }
}