reflex/cache/l2/
backend.rs

1use crate::vectordb::bq::BqClient;
2#[cfg(any(test, feature = "mock"))]
3use crate::vectordb::bq::MockBqClient;
4use crate::vectordb::{SearchResult, VectorDbError, VectorPoint, WriteConsistency};
5
6/// Backend required by the L2 cache for vector search + upsert.
7pub trait BqSearchBackend: Send + Sync {
8    /// Returns `true` if the backend is ready for requests.
9    fn is_ready(&self) -> impl std::future::Future<Output = bool> + Send;
10
11    /// Creates the collection if it doesn't exist.
12    fn ensure_collection(
13        &self,
14        name: &str,
15        vector_size: u64,
16    ) -> impl std::future::Future<Output = Result<(), VectorDbError>> + Send;
17
18    /// Performs a search against the binary-quantized index.
19    fn search_bq(
20        &self,
21        collection: &str,
22        query: Vec<f32>,
23        limit: u64,
24        tenant_filter: Option<u64>,
25    ) -> impl std::future::Future<Output = Result<Vec<SearchResult>, VectorDbError>> + Send;
26
27    /// Upserts points into the collection.
28    fn upsert_points(
29        &self,
30        collection: &str,
31        points: Vec<VectorPoint>,
32        consistency: WriteConsistency,
33    ) -> impl std::future::Future<Output = Result<(), VectorDbError>> + Send;
34}
35
36impl BqSearchBackend for BqClient {
37    async fn is_ready(&self) -> bool {
38        self.health_check().await.is_ok()
39    }
40
41    async fn ensure_collection(&self, name: &str, vector_size: u64) -> Result<(), VectorDbError> {
42        self.ensure_bq_collection(name, vector_size).await
43    }
44
45    async fn search_bq(
46        &self,
47        collection: &str,
48        query: Vec<f32>,
49        limit: u64,
50        tenant_filter: Option<u64>,
51    ) -> Result<Vec<SearchResult>, VectorDbError> {
52        self.search_bq(collection, query, limit, tenant_filter)
53            .await
54    }
55
56    async fn upsert_points(
57        &self,
58        collection: &str,
59        points: Vec<VectorPoint>,
60        consistency: WriteConsistency,
61    ) -> Result<(), VectorDbError> {
62        self.upsert_points(collection, points, consistency).await
63    }
64}
65
66#[cfg(any(test, feature = "mock"))]
67impl BqSearchBackend for MockBqClient {
68    async fn is_ready(&self) -> bool {
69        true
70    }
71
72    async fn ensure_collection(&self, name: &str, vector_size: u64) -> Result<(), VectorDbError> {
73        self.ensure_bq_collection(name, vector_size).await
74    }
75
76    async fn search_bq(
77        &self,
78        collection: &str,
79        query: Vec<f32>,
80        limit: u64,
81        tenant_filter: Option<u64>,
82    ) -> Result<Vec<SearchResult>, VectorDbError> {
83        self.search_bq(collection, query, limit, tenant_filter)
84            .await
85    }
86
87    async fn upsert_points(
88        &self,
89        collection: &str,
90        points: Vec<VectorPoint>,
91        consistency: WriteConsistency,
92    ) -> Result<(), VectorDbError> {
93        self.upsert_points(collection, points, consistency).await
94    }
95}