Skip to main content

sz_orm_vector/
lib.rs

1//! SZ-ORM pgvector 扩展
2//!
3//! 提供 PostgreSQL pgvector 向量相似度搜索能力,支持三种实现:
4//!
5//! - **内存实现**(`InMemoryVectorStore`):纯 Rust 向量计算,不连接数据库,适用于测试和基准
6//! - **Stub 实现**(`StubVectorStore`):所有方法返回 Unsupported,适用于调试占位
7//! - **真实实现**(`RealPgVectorStore`,需启用 `real-pg` feature):通过 tokio-postgres 连接 PostgreSQL + pgvector
8//!
9//! # 快速入门
10//!
11//! ```rust
12//! use sz_orm_vector::{InMemoryVectorStore, PgVectorStore, VectorRecord, VectorMetric};
13//!
14//! # #[tokio::main]
15//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
16//! let store = InMemoryVectorStore::new();
17//! store.create_collection("docs", 3, None).await?;
18//!
19//! let record = VectorRecord::new("doc1", vec![1.0, 0.0, 0.0]);
20//! store.insert("docs", vec![record]).await?;
21//!
22//! let results = store.search("docs", &[1.0, 0.0, 0.0], 5).await?;
23//! println!("found {} results", results.len());
24//! # Ok(())
25//! # }
26//! ```
27
28pub mod error;
29pub mod extensions;
30pub mod memory;
31pub mod stub;
32
33#[cfg(feature = "real-pg")]
34pub mod real_pg;
35
36// v4.0.0 M3:混合搜索(hybrid-search feature gate 隔离)
37#[cfg(feature = "hybrid-search")]
38pub mod hybrid_search;
39
40pub use error::VectorError;
41pub use extensions::{
42    AnnIndexDef, AnnIndexRegistry, AnnIndexType, BatchOpsExt, DimensionValidator, HnswParams,
43    IvfflatParams, MemoryBatchOps, SimilarityAlgorithms, VectorNormalizer, MAX_VECTOR_DIMENSION,
44    MIN_VECTOR_DIMENSION,
45};
46pub use memory::InMemoryVectorStore;
47pub use stub::StubVectorStore;
48
49#[cfg(feature = "real-pg")]
50pub use real_pg::{RealPgConfig, RealPgVectorStore};
51
52use async_trait::async_trait;
53use std::collections::HashMap;
54use std::str::FromStr;
55
56/// M-16 修复:top_k 最大限制
57///
58/// 限制 top_k 上限以防止:
59/// - 大 k 值导致内存爆炸(每个 SearchResult 包含完整向量)
60/// - 数据库/向量引擎执行超大 k 查询的性能问题
61/// - 恶意调用方通过 top_k=usize::MAX 触发 OOM
62pub const MAX_TOP_K: usize = 10_000;
63
64/// M-16 修复:校验 top_k 是否在合理范围内
65///
66/// - `top_k = 0`:返回 `TopKExceeded` 错误(无意义的查询)
67/// - `top_k > MAX_TOP_K`:返回 `TopKExceeded` 错误
68/// - `1 <= top_k <= MAX_TOP_K`:返回 Ok
69pub fn validate_top_k(top_k: usize) -> Result<usize, VectorError> {
70    if top_k == 0 {
71        return Err(VectorError::TopKExceeded {
72            requested: top_k,
73            max: MAX_TOP_K,
74        });
75    }
76    if top_k > MAX_TOP_K {
77        return Err(VectorError::TopKExceeded {
78            requested: top_k,
79            max: MAX_TOP_K,
80        });
81    }
82    Ok(top_k)
83}
84
85/// 向量记录
86#[derive(Debug, Clone)]
87pub struct VectorRecord {
88    pub id: String,
89    pub vector: Vec<f32>,
90    pub score: Option<f32>,
91    pub metadata: Option<HashMap<String, serde_json::Value>>,
92}
93
94impl VectorRecord {
95    pub fn new(id: impl Into<String>, vector: Vec<f32>) -> Self {
96        Self {
97            id: id.into(),
98            vector,
99            score: None,
100            metadata: None,
101        }
102    }
103
104    pub fn with_score(mut self, score: f32) -> Self {
105        self.score = Some(score);
106        self
107    }
108
109    pub fn with_metadata(mut self, metadata: HashMap<String, serde_json::Value>) -> Self {
110        self.metadata = Some(metadata);
111        self
112    }
113}
114
115/// 搜索结果
116#[derive(Debug, Clone)]
117pub struct SearchResult {
118    pub id: String,
119    pub score: f32,
120    pub vector: Vec<f32>,
121    pub text: Option<String>,
122    pub metadata: Option<HashMap<String, serde_json::Value>>,
123}
124
125impl SearchResult {
126    pub fn new(id: impl Into<String>, score: f32, vector: Vec<f32>) -> Self {
127        Self {
128            id: id.into(),
129            score,
130            vector,
131            text: None,
132            metadata: None,
133        }
134    }
135
136    pub fn with_text(mut self, text: impl Into<String>) -> Self {
137        self.text = Some(text.into());
138        self
139    }
140
141    pub fn with_metadata(mut self, metadata: HashMap<String, serde_json::Value>) -> Self {
142        self.metadata = Some(metadata);
143        self
144    }
145}
146
147/// 向量距离度量
148#[derive(Debug, Clone, Copy, Default, PartialEq)]
149pub enum VectorMetric {
150    #[default]
151    Cosine,
152    Euclidean,
153    DotProduct,
154}
155
156impl VectorMetric {
157    /// pgvector 操作符映射
158    pub fn pg_operator(&self) -> &'static str {
159        match self {
160            VectorMetric::Cosine => "<=>",
161            VectorMetric::Euclidean => "<->",
162            VectorMetric::DotProduct => "<#>",
163        }
164    }
165
166    pub fn as_str(&self) -> &'static str {
167        match self {
168            VectorMetric::Cosine => "cosine",
169            VectorMetric::Euclidean => "euclidean",
170            VectorMetric::DotProduct => "dotproduct",
171        }
172    }
173}
174
175impl FromStr for VectorMetric {
176    type Err = String;
177
178    fn from_str(s: &str) -> Result<Self, Self::Err> {
179        match s {
180            "cosine" => Ok(VectorMetric::Cosine),
181            "euclidean" => Ok(VectorMetric::Euclidean),
182            "dotproduct" => Ok(VectorMetric::DotProduct),
183            _ => Err(format!("unknown vector metric: {}", s)),
184        }
185    }
186}
187
188/// Vector Store 核心 trait
189///
190/// 提供向量集合的 CRUD 和相似度搜索能力。
191/// 所有方法均为 async,适用于真实数据库 I/O。
192#[async_trait]
193pub trait PgVectorStore: Send + Sync {
194    /// 创建集合
195    async fn create_collection(
196        &self,
197        name: &str,
198        dimension: usize,
199        metric: Option<VectorMetric>,
200    ) -> Result<(), VectorError>;
201
202    /// 删除集合
203    async fn delete_collection(&self, name: &str) -> Result<(), VectorError>;
204
205    /// 插入向量记录(upsert 语义:相同 id 会覆盖)
206    async fn insert(&self, collection: &str, records: Vec<VectorRecord>)
207        -> Result<(), VectorError>;
208
209    /// 相似度搜索
210    ///
211    /// M-16 修复:`top_k` 必须在 `[1, MAX_TOP_K]` 范围内。
212    /// 实现方应在执行搜索前调用 `validate_top_k(top_k)?` 进行校验。
213    async fn search(
214        &self,
215        collection: &str,
216        query: &[f32],
217        top_k: usize,
218    ) -> Result<Vec<SearchResult>, VectorError>;
219
220    /// 获取单个记录
221    async fn get(&self, collection: &str, id: &str) -> Result<Option<VectorRecord>, VectorError>;
222
223    /// 删除记录
224    async fn delete(&self, collection: &str, ids: Vec<String>) -> Result<u64, VectorError>;
225
226    /// 统计记录数
227    async fn count(&self, collection: &str) -> Result<usize, VectorError>;
228}