Skip to main content

selene_graph/vector_search/
shared_wrappers.rs

1//! Lock-free snapshot wrappers for vector search APIs.
2
3use selene_core::{CancellationChecker, DbString, VectorMetric, VectorValue};
4
5use super::{
6    ApproximateVectorExpansionOptions, ApproximateVectorSearchOptions, VectorCandidateSet,
7    VectorNodeSearchHit, VectorSearchError,
8};
9use crate::error::GraphResult;
10use crate::shared::SharedGraph;
11
12impl SharedGraph {
13    /// Exhaustively rank vector-valued node properties in the current snapshot.
14    ///
15    /// This loads one immutable snapshot and delegates to
16    /// [`crate::SeleneGraph::exact_vector_search_nodes`], so the result is
17    /// lock-free with respect to concurrent writers once the snapshot pointer is
18    /// read.
19    pub fn exact_vector_search_nodes(
20        &self,
21        label: &DbString,
22        property: &DbString,
23        query: &VectorValue,
24        metric: VectorMetric,
25        k: usize,
26    ) -> GraphResult<Vec<VectorNodeSearchHit>> {
27        self.read()
28            .exact_vector_search_nodes(label, property, query, metric, k)
29    }
30
31    /// Exhaustively rank vector-valued node properties with cancellation checks.
32    ///
33    /// This loads one immutable snapshot and delegates to
34    /// [`crate::SeleneGraph::exact_vector_search_nodes_checked`].
35    pub fn exact_vector_search_nodes_checked(
36        &self,
37        label: &DbString,
38        property: &DbString,
39        query: &VectorValue,
40        metric: VectorMetric,
41        k: usize,
42        checker: CancellationChecker<'_>,
43    ) -> Result<Vec<VectorNodeSearchHit>, VectorSearchError> {
44        self.read()
45            .exact_vector_search_nodes_checked(label, property, query, metric, k, checker)
46    }
47
48    /// Lock-free read snapshot wrapper for exact vector batch search.
49    pub fn exact_vector_search_nodes_batch_checked(
50        &self,
51        label: &DbString,
52        property: &DbString,
53        queries: &[VectorValue],
54        metric: VectorMetric,
55        k: usize,
56        checker: CancellationChecker<'_>,
57    ) -> Result<Vec<Vec<VectorNodeSearchHit>>, VectorSearchError> {
58        self.read()
59            .exact_vector_search_nodes_batch_checked(label, property, queries, metric, k, checker)
60    }
61
62    /// Approximately rank vector-valued node properties through an ANN index.
63    ///
64    /// This loads one immutable snapshot and delegates to
65    /// [`crate::SeleneGraph::approximate_vector_search_nodes_checked`].
66    pub fn approximate_vector_search_nodes_checked(
67        &self,
68        label: &DbString,
69        property: &DbString,
70        query: &VectorValue,
71        options: ApproximateVectorSearchOptions,
72        checker: CancellationChecker<'_>,
73    ) -> Result<Vec<VectorNodeSearchHit>, VectorSearchError> {
74        self.read()
75            .approximate_vector_search_nodes_checked(label, property, query, options, checker)
76    }
77
78    /// Lock-free read snapshot wrapper for approximate vector batch search.
79    pub fn approximate_vector_search_nodes_batch_checked(
80        &self,
81        label: &DbString,
82        property: &DbString,
83        queries: &[VectorValue],
84        options: ApproximateVectorSearchOptions,
85        checker: CancellationChecker<'_>,
86    ) -> Result<Vec<Vec<VectorNodeSearchHit>>, VectorSearchError> {
87        self.read().approximate_vector_search_nodes_batch_checked(
88            label, property, queries, options, checker,
89        )
90    }
91
92    /// Lock-free read snapshot wrapper for approximate search within candidates.
93    pub fn approximate_vector_search_candidate_set_checked(
94        &self,
95        label: &DbString,
96        property: &DbString,
97        query: &VectorValue,
98        candidates: &VectorCandidateSet,
99        options: ApproximateVectorSearchOptions,
100        checker: CancellationChecker<'_>,
101    ) -> Result<Vec<VectorNodeSearchHit>, VectorSearchError> {
102        self.read().approximate_vector_search_candidate_set_checked(
103            label, property, query, candidates, options, checker,
104        )
105    }
106
107    /// Lock-free read snapshot wrapper for approximate batch search within candidates.
108    pub fn approximate_vector_search_candidate_sets_batch_checked(
109        &self,
110        label: &DbString,
111        property: &DbString,
112        queries: &[VectorValue],
113        candidate_sets: &[VectorCandidateSet],
114        options: ApproximateVectorSearchOptions,
115        checker: CancellationChecker<'_>,
116    ) -> Result<Vec<Vec<VectorNodeSearchHit>>, VectorSearchError> {
117        self.read()
118            .approximate_vector_search_candidate_sets_batch_checked(
119                label,
120                property,
121                queries,
122                candidate_sets,
123                options,
124                checker,
125            )
126    }
127
128    /// Lock-free read snapshot wrapper for ANN-root graph expansion.
129    pub fn approximate_vector_search_expanded_candidates_checked(
130        &self,
131        label: &DbString,
132        property: &DbString,
133        query: &VectorValue,
134        options: ApproximateVectorExpansionOptions<'_>,
135        checker: CancellationChecker<'_>,
136    ) -> Result<Vec<VectorNodeSearchHit>, VectorSearchError> {
137        self.read()
138            .approximate_vector_search_expanded_candidates_checked(
139                label, property, query, options, checker,
140            )
141    }
142
143    /// Lock-free read snapshot wrapper for batched ANN-root graph expansion.
144    pub fn approximate_vector_search_expanded_candidates_batch_checked(
145        &self,
146        label: &DbString,
147        property: &DbString,
148        queries: &[VectorValue],
149        options: ApproximateVectorExpansionOptions<'_>,
150        checker: CancellationChecker<'_>,
151    ) -> Result<Vec<Vec<VectorNodeSearchHit>>, VectorSearchError> {
152        self.read()
153            .approximate_vector_search_expanded_candidates_batch_checked(
154                label, property, queries, options, checker,
155            )
156    }
157}