wedb_embed 0.1.1

Embedded database engine providing Redis-like APIs, built on fjall / 嵌入式数据库引擎,提供类似 Redis 的接口,底层基于 fjall 开发
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
use crate::error::{Error, Result};
use crate::search::encoding::compute_vector_distance;
use crate::search::meta::DistanceMetric;
use hipstr::HipStr;
use rapidhash::{RapidHashMap as HashMap, RapidHashSet as HashSet};
use std::cmp::Ordering;
use std::collections::BinaryHeap;

/// 单个候选向量距离对(用于最小堆/最大堆)
#[derive(Debug, Clone)]
pub struct Candidate {
    pub dist: f64,
    pub doc_id: HipStr<'static>,
}

impl PartialEq for Candidate {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.dist == other.dist && self.doc_id == other.doc_id
    }
}

impl Eq for Candidate {}

impl PartialOrd for Candidate {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Candidate {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        // 用于默认 BinaryHeap(大顶堆:距离大的在堆顶)
        self.dist
            .partial_cmp(&other.dist)
            .unwrap_or(Ordering::Equal)
            .then_with(|| self.doc_id.cmp(&other.doc_id))
    }
}

/// 逆序候选者(用于小顶堆:距离小的在堆顶)
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MinCandidate(pub Candidate);

impl PartialOrd for MinCandidate {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for MinCandidate {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        other.0.cmp(&self.0)
    }
}

/// HNSW 图节点(对标 Apache Kvrocks HnswNode)
#[derive(Debug, Clone)]
pub struct HnswNode {
    pub doc_id: HipStr<'static>,
    pub vector: Vec<f64>,
    pub level: usize,
    /// 每一层的邻居列表,索引为层号 0..=level
    pub neighbors: Vec<Vec<HipStr<'static>>>,
}

/// 内存 HNSW 向量索引图(对标 Apache Kvrocks HnswIndex 与 RediSearch HNSW 实现)
#[derive(Debug, Clone)]
pub struct HnswGraph {
    pub dim: usize,
    pub distance_metric: DistanceMetric,
    pub m: usize,
    pub ef_construction: usize,
    pub ef_runtime: usize,
    pub epsilon: f64,
    pub max_level: usize,
    pub entry_point: Option<HipStr<'static>>,
    pub nodes: HashMap<HipStr<'static>, HnswNode>,
    level_mult: f64,
}

impl Default for HnswGraph {
    fn default() -> Self {
        Self::new(0, DistanceMetric::Cosine, 16, 200, 10, 0.01)
    }
}

impl HnswGraph {
    pub fn new(
        dim: usize,
        distance_metric: DistanceMetric,
        m: usize,
        ef_construction: usize,
        ef_runtime: usize,
        epsilon: f64,
    ) -> Self {
        let m_val = m.max(2);
        let level_mult = 1.0 / (m_val as f64).ln();
        Self {
            dim,
            distance_metric,
            m: m_val,
            ef_construction: ef_construction.max(1),
            ef_runtime: ef_runtime.max(1),
            epsilon,
            max_level: 0,
            entry_point: None,
            nodes: HashMap::default(),
            level_mult,
        }
    }

    /// 返回当前图的层级数(对标 Apache Kvrocks metadata.num_levels)
    #[inline]
    pub fn num_levels(&self) -> u16 {
        if self.nodes.is_empty() {
            0
        } else {
            (self.max_level + 1) as u16
        }
    }

    /// 随机生成新插入节点的层数(对标 Apache Kvrocks HnswIndex::RandomizeLayer)
    #[inline]
    pub fn random_level(&self) -> usize {
        let r: f64 = fastrand::f64();
        let r = r.max(f64::MIN_POSITIVE);
        ((-r.ln()) * self.level_mult).floor() as usize
    }

    /// 计算两向量间距离
    #[inline]
    pub fn dist(&self, v1: &[f64], v2: &[f64]) -> Result<f64> {
        compute_vector_distance(v1, v2, self.distance_metric)
    }

    /// 插入新向量(对标 Apache Kvrocks HnswIndex::InsertVectorEntry)
    pub fn insert(&mut self, doc_id: HipStr<'static>, vector: Vec<f64>) -> Result<()> {
        if self.dim != 0 && vector.len() != self.dim {
            let dim = self.dim;
            let len = vector.len();
            return Err(Error::invalid_data(format!(
                "vector dimension mismatch: expected {dim}, got {len}"
            )));
        }
        if self.dim == 0 {
            self.dim = vector.len();
        }

        // 如果节点已存在,先删除旧节点
        if self.nodes.contains_key(&doc_id) {
            self.delete(doc_id.as_str());
        }

        let node_level = self.random_level();
        let neighbors = vec![Vec::new(); node_level + 1];

        let new_node = HnswNode {
            doc_id: doc_id.clone(),
            vector: vector.clone(),
            level: node_level,
            neighbors,
        };
        self.nodes.insert(doc_id.clone(), new_node);

        let Some(mut curr_ep) = self.entry_point.clone() else {
            self.entry_point = Some(doc_id);
            self.max_level = node_level;
            return Ok(());
        };
        let max_lvl = self.max_level;

        // 1. 从最高层下行到 node_level + 1 贪心搜索最近入口点
        if max_lvl > node_level {
            for lvl in (node_level + 1..=max_lvl).rev() {
                let mut changed = true;
                while changed {
                    changed = false;
                    let ep_node = match self.nodes.get(&curr_ep) {
                        Some(n) => n,
                        None => break,
                    };
                    let curr_dist = self.dist(&vector, &ep_node.vector)?;
                    let mut closest_ep = curr_ep.clone();
                    let mut min_d = curr_dist;

                    if lvl < ep_node.neighbors.len() {
                        for neighbor_id in &ep_node.neighbors[lvl] {
                            if let Some(neighbor_node) = self.nodes.get(neighbor_id) {
                                let d = self.dist(&vector, &neighbor_node.vector)?;
                                if d < min_d {
                                    min_d = d;
                                    closest_ep = neighbor_id.clone();
                                    changed = true;
                                }
                            }
                        }
                    }
                    if changed {
                        curr_ep = closest_ep;
                    }
                }
            }
        }

        // 2. 从 min(node_level, max_lvl) 到 0 层逐层连边
        let mut curr_eps = vec![curr_ep];
        let insert_top_level = node_level.min(max_lvl);

        for lvl in (0..=insert_top_level).rev() {
            let candidates =
                self.search_layer_internal(&vector, &curr_eps, self.ef_construction, lvl)?;
            let m_max = if lvl == 0 { self.m * 2 } else { self.m };
            let selected = self.select_neighbors(&candidates, m_max);

            // 连接新节点与选取的邻居
            if let Some(node) = self.nodes.get_mut(&doc_id) {
                node.neighbors[lvl] = selected.clone();
            }

            // 双向连边与邻居截断
            for neighbor_id in &selected {
                if let Some(neighbor_node) = self.nodes.get_mut(neighbor_id)
                    && lvl < neighbor_node.neighbors.len()
                {
                    if !neighbor_node.neighbors[lvl].contains(&doc_id) {
                        neighbor_node.neighbors[lvl].push(doc_id.clone());
                    }
                    if neighbor_node.neighbors[lvl].len() > m_max {
                        let n_vec = neighbor_node.vector.clone();
                        let n_candidates: Vec<HipStr<'static>> =
                            neighbor_node.neighbors[lvl].clone();
                        let pruned = self.select_neighbors_from_ids(&n_vec, &n_candidates, m_max);
                        if let Some(n_node_re) = self.nodes.get_mut(neighbor_id) {
                            n_node_re.neighbors[lvl] = pruned;
                        }
                    }
                }
            }

            curr_eps = candidates.into_iter().map(|c| c.doc_id).collect();
        }

        if node_level > self.max_level {
            self.max_level = node_level;
            self.entry_point = Some(doc_id);
        }

        Ok(())
    }

    /// 删除节点(对标 Apache Kvrocks HnswIndex::DeleteVectorEntry)
    pub fn delete(&mut self, doc_id: &str) -> bool {
        let doc_key = HipStr::from(doc_id);
        let removed = match self.nodes.remove(&doc_key) {
            Some(n) => n,
            None => return false,
        };

        // 清理所有邻居的反向连边
        for (lvl, n_list) in removed.neighbors.iter().enumerate() {
            for neighbor_id in n_list {
                if let Some(neighbor_node) = self.nodes.get_mut(neighbor_id)
                    && lvl < neighbor_node.neighbors.len()
                {
                    neighbor_node.neighbors[lvl].retain(|id| id.as_str() != doc_id);
                }
            }
        }

        // 若删除的是入口点,更新入口点为剩余节点中层数最高者
        if self.entry_point.as_deref() == Some(doc_id) {
            if self.nodes.is_empty() {
                self.entry_point = None;
                self.max_level = 0;
            } else {
                let mut best_ep = None;
                let mut best_lvl = 0;
                for (id, node) in &self.nodes {
                    if best_ep.is_none() || node.level >= best_lvl {
                        best_ep = Some(id.clone());
                        best_lvl = node.level;
                    }
                }
                self.entry_point = best_ep;
                self.max_level = best_lvl;
            }
        }

        true
    }

    /// 单层 Beam Search 搜索邻近候选集(对标 Apache Kvrocks HnswIndex::SearchLayerInternal)
    pub fn search_layer_internal(
        &self,
        query: &[f64],
        entry_points: &[HipStr<'static>],
        ef: usize,
        level: usize,
    ) -> Result<Vec<Candidate>> {
        let mut visited = HashSet::default();
        let mut explore_heap = BinaryHeap::new(); // 小顶堆 MinCandidate
        let mut result_heap = BinaryHeap::new(); // 大顶堆 Candidate (保持最近的 ef 个)

        for ep in entry_points {
            if let Some(ep_node) = self.nodes.get(ep) {
                let dist = self.dist(query, &ep_node.vector)?;
                let cand = Candidate {
                    dist,
                    doc_id: ep.clone(),
                };
                explore_heap.push(MinCandidate(cand.clone()));
                result_heap.push(cand);
                visited.insert(ep.clone());
            }
        }

        while let Some(MinCandidate(curr)) = explore_heap.pop() {
            if let Some(furthest) = result_heap.peek()
                && curr.dist > furthest.dist
            {
                break;
            }

            if let Some(curr_node) = self.nodes.get(&curr.doc_id)
                && level < curr_node.neighbors.len()
            {
                for neighbor_id in &curr_node.neighbors[level] {
                    if !visited.insert(neighbor_id.clone()) {
                        continue;
                    }
                    if let Some(neighbor_node) = self.nodes.get(neighbor_id) {
                        let dist = self.dist(query, &neighbor_node.vector)?;
                        let cand = Candidate {
                            dist,
                            doc_id: neighbor_id.clone(),
                        };

                        if result_heap.len() < ef
                            || dist < result_heap.peek().map(|f| f.dist).unwrap_or(f64::INFINITY)
                        {
                            explore_heap.push(MinCandidate(cand.clone()));
                            result_heap.push(cand);
                            if result_heap.len() > ef {
                                result_heap.pop();
                            }
                        }
                    }
                }
            }
        }

        let mut res: Vec<Candidate> = result_heap.into_vec();
        res.sort();
        Ok(res)
    }

    /// 邻居启发式筛选(对标 Apache Kvrocks HnswIndex::SelectNeighbors)
    #[inline]
    pub fn select_neighbors(&self, candidates: &[Candidate], m_max: usize) -> Vec<HipStr<'static>> {
        candidates
            .iter()
            .take(m_max)
            .map(|c| c.doc_id.clone())
            .collect()
    }

    pub fn select_neighbors_from_ids(
        &self,
        base_vec: &[f64],
        candidates: &[HipStr<'static>],
        m_max: usize,
    ) -> Vec<HipStr<'static>> {
        let mut scored: Vec<(f64, HipStr<'static>)> = Vec::with_capacity(candidates.len());
        for id in candidates {
            if let Some(node) = self.nodes.get(id)
                && let Ok(d) = self.dist(base_vec, &node.vector)
            {
                scored.push((d, id.clone()));
            }
        }
        scored.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(Ordering::Equal));
        scored.into_iter().take(m_max).map(|(_, id)| id).collect()
    }

    /// 执行 KNN 近邻检索(对标 Apache Kvrocks HnswIndex::KnnSearch)
    pub fn search_knn(
        &self,
        query: &[f64],
        k: usize,
        ef_runtime: Option<usize>,
    ) -> Result<Vec<(f64, HipStr<'static>)>> {
        let Some(mut curr_ep) = self.entry_point.clone() else {
            return Ok(Vec::new());
        };
        if self.nodes.is_empty() {
            return Ok(Vec::new());
        }

        let max_lvl = self.max_level;

        // 1. 上层贪心搜索入口点
        for lvl in (1..=max_lvl).rev() {
            let mut changed = true;
            while changed {
                changed = false;
                let ep_node = match self.nodes.get(&curr_ep) {
                    Some(n) => n,
                    None => break,
                };
                let curr_dist = self.dist(query, &ep_node.vector)?;
                let mut closest_ep = curr_ep.clone();
                let mut min_d = curr_dist;

                if lvl < ep_node.neighbors.len() {
                    for neighbor_id in &ep_node.neighbors[lvl] {
                        if let Some(neighbor_node) = self.nodes.get(neighbor_id) {
                            let d = self.dist(query, &neighbor_node.vector)?;
                            if d < min_d {
                                min_d = d;
                                closest_ep = neighbor_id.clone();
                                changed = true;
                            }
                        }
                    }
                }
                if changed {
                    curr_ep = closest_ep;
                }
            }
        }

        // 2. 底层 0 层 Beam Search
        let ef = ef_runtime.unwrap_or(self.ef_runtime).max(k);
        let candidates = self.search_layer_internal(query, &[curr_ep], ef, 0)?;

        let results = candidates
            .into_iter()
            .take(k)
            .map(|c| (c.dist, c.doc_id))
            .collect();
        Ok(results)
    }

    /// 执行范围检索(对标 Apache Kvrocks VECTOR_RANGE 检索)
    pub fn search_range(
        &self,
        query: &[f64],
        radius: f64,
        epsilon: Option<f64>,
    ) -> Result<Vec<(f64, HipStr<'static>)>> {
        let eps = epsilon.unwrap_or(self.epsilon);
        let effective_radius = radius * (1.0 + eps);
        let knn_candidates = self.search_knn(query, self.nodes.len(), Some(self.ef_runtime * 2))?;
        let filtered: Vec<(f64, HipStr<'static>)> = knn_candidates
            .into_iter()
            .filter(|(d, _)| *d <= effective_radius)
            .collect();
        Ok(filtered)
    }

    /// 扩展搜索范围(对标 Apache Kvrocks HnswIndex::ExpandSearchScope)
    pub fn expand_search_scope(
        &self,
        query: &[f64],
        initial_keys: &[(f64, HipStr<'static>)],
        visited: &mut HashSet<HipStr<'static>>,
    ) -> Result<Vec<(f64, HipStr<'static>)>> {
        let mut result = Vec::new();
        for (_, key) in initial_keys {
            if let Some(node) = self.nodes.get(key)
                && !node.neighbors.is_empty()
            {
                for neighbor_id in &node.neighbors[0] {
                    if !visited.insert(neighbor_id.clone()) {
                        continue;
                    }
                    if let Some(neighbor_node) = self.nodes.get(neighbor_id) {
                        let dist = self.dist(query, &neighbor_node.vector)?;
                        result.push((dist, neighbor_id.clone()));
                    }
                }
            }
        }
        result.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(Ordering::Equal));
        Ok(result)
    }

    /// 清空索引
    #[inline]
    pub fn clear(&mut self) {
        self.nodes.clear();
        self.entry_point = None;
        self.max_level = 0;
    }
}