vectune 0.1.1

A lightweight VectorDB with Incremental Indexing, based on FreshVamana.
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

use rustc_hash::FxHashSet;
use std::time::Instant;

use rand::seq::SliceRandom;
use rand::SeedableRng;
use rand::{rngs::SmallRng, Rng};

use parking_lot::RwLock;
use rayon::prelude::*;

use itertools::Itertools;

#[cfg(feature = "progress-bar")]
use indicatif::ProgressBar;
#[cfg(feature = "progress-bar")]
use std::sync::atomic::{self, AtomicUsize};


use crate::PointInterface;
use crate::utils::*;


/// Builder is a structure and implementation for creating a Vamana graph.
///
// - `a` is the threshold for RobustPrune; increasing it results in more long-distance edges and fewer nearby edges.
// - `r` represents the number of edges; increasing it adds complexity to the graph but reduces the number of isolated nodes.
// - `l` is the size of the retention list for greedy-search; increasing it allows for the construction of more accurate graphs, but the computational cost grows exponentially.
// - `seed` is used for initializing random graphs; it allows for the fixation of the random graph, which can be useful for debugging.
///
#[derive(Clone)]
pub struct Builder {
    a: f32,
    r: usize,
    l: usize,
    seed: u64,

    #[cfg(feature = "progress-bar")]
    progress: Option<ProgressBar>,
}

impl Default for Builder {
    fn default() -> Self {
        Self {
            a: 2.0,
            r: 70,
            l: 125,
            seed: rand::random(),
            #[cfg(feature = "progress-bar")]
            progress: None,
        }
    }
}

impl Builder {
    pub fn set_a(mut self, a: f32) -> Self {
        self.a = a;
        self
    }
    pub fn set_r(mut self, r: usize) -> Self {
        self.r = r;
        self
    }
    pub fn set_l(mut self, l: usize) -> Self {
        self.l = l;
        self
    }
    pub fn set_seed(mut self, seed: u64) -> Self {
        self.seed = seed;
        self
    }

    pub fn get_a(&self) -> f32 {
      self.a
    }

    pub fn get_r(&self) -> usize {
        self.r
    }
    pub fn get_l(&self) -> usize {
        self.l
    }
    pub fn get_seed(&self) -> u64 {
        self.seed
    }

    ///
    /// Creates a directed Vamana graph from a Point type that implements the PointInterface.
    ///
    /// Takes a `Vec<P>` as an argument and returns a `Vec<(P, Vec<usize>)>` with edges added.
    ///
    pub fn build<P: PointInterface>(self, points: Vec<P>) -> (Vec<(P, Vec<u32>)>, u32) {
        let ann = Vamana::new(points, self);

        let nodes = ann
            .nodes
            .into_iter()
            .map(|node| {
                (
                    node.p,
                    node.n_out.into_inner().into_iter().sorted().collect(),
                )
            })
            .collect();
        let s = ann.centroid;

        (nodes, s)
    }

    #[cfg(feature = "progress-bar")]
    pub fn progress(mut self, bar: ProgressBar) -> Self {
        self.progress = Some(bar);
        self
    }
}



pub struct Node<P> {
  n_out: RwLock<Vec<u32>>,
  p: P,
}

pub struct Vamana<P> {
  pub nodes: Vec<Node<P>>,
  pub centroid: u32,
  pub builder: Builder,
}

impl<P> Vamana<P>
where
  P: PointInterface,
{
  pub fn new(points: Vec<P>, builder: Builder) -> Self {
      let mut rng = SmallRng::seed_from_u64(builder.seed);
      // println!("seed: {}", builder.seed);

      let start_time = Instant::now();
      let mut ann = Vamana::<P>::random_graph_init(points, builder, &mut rng);

      // Prune Edges
      Vamana::<P>::indexing(&mut ann, &mut rng);

      println!(
          "\ntotal indexing time: {:?}",
          Instant::now().duration_since(start_time)
      );

      ann
  }

  pub fn random_graph_init(points: Vec<P>, builder: Builder, _rng: &mut SmallRng) -> Self {
      if points.is_empty() {
          return Self {
              nodes: Vec::new(),
              centroid: u32::MAX,
              builder,
          };
      }

      assert!(points.len() < u32::MAX as usize);
      let points_len = points.len();

      /* Find Centroid */
      let mut sum = points[0].clone();
      for p in &points[1..] {
          sum = sum.add(p);
      }

      let average_point = sum.div(&points_len);
      let mut min_dist = f32::MAX;
      let mut centroid = u32::MAX;
      for (i, p) in points.iter().enumerate() {
          let dist = p.distance(&average_point);
          if dist < min_dist {
              min_dist = dist;
              centroid = i as u32;
          }
      }

      /* Get random connected graph */

      // edge (in, out)
      let edges: Vec<(RwLock<Vec<u32>>, RwLock<Vec<u32>>)> = (0..points_len)
          .map(|_| {
              (
                  RwLock::new(Vec::with_capacity(builder.l)),
                  RwLock::new(Vec::with_capacity(builder.l)),
              )
          })
          .collect();

      (0..points_len).into_par_iter().for_each(|node_i| {
          let mut rng = SmallRng::seed_from_u64(builder.seed + node_i as u64);

          let mut new_ids = Vec::with_capacity(builder.l);
          while new_ids.len() < builder.r {
              let candidate_i = rng.gen_range(0..points_len as u32);
              if node_i as u32 == candidate_i
                  || new_ids.contains(&candidate_i)
                  || edges[candidate_i as usize].0.read().len() >= builder.r + builder.r / 2
              {
                  continue;
              } else {
                  edges[candidate_i as usize].0.write().push(node_i as u32);
                  new_ids.push(candidate_i);
              }
          }

          let mut n_out = edges[node_i].1.write();
          *n_out = new_ids;
      });

      println!("make nodes");

      let nodes: Vec<Node<P>> = edges
          .into_iter()
          .zip(points)
          .map(|((_n_in, n_out), p)| Node { n_out, p })
          .collect();

      Self {
          nodes,
          centroid,
          builder,
      }
  }

  pub fn indexing(ann: &mut Vamana<P>, rng: &mut SmallRng) {
      #[cfg(feature = "progress-bar")]
      let progress = &ann.builder.progress;
      #[cfg(feature = "progress-bar")]
      let progress_done = AtomicUsize::new(0);
      #[cfg(feature = "progress-bar")]
      if let Some(bar) = &progress {
          bar.set_length((ann.nodes.len() * 2) as u64);
          bar.set_message("Build index (preparation)");
      }

      let node_len = ann.nodes.len();
      let mut shuffled: Vec<usize> = (0..node_len).collect();
      shuffled.shuffle(rng);

      // for 1 ≤ i ≤ n do
      shuffled
          .into_par_iter()
          .enumerate()
          .for_each(|(_count, i)| {
              // if count % 10000 == 0 {
              //     println!("id : {}\t/{}", count, ann.nodes.len());
              // }

              // let [L; V] ← GreedySearch(s, xσ(i), 1, L)
              let (_, visited) = ann.greedy_search(&ann.nodes[i].p, 1, ann.builder.l);

              // V ← (V ∪ Nout(p)) \ {p}
              let prev_n_out = ann.nodes[i].n_out.read().clone();
              let mut candidates = visited;
              for out_i in &prev_n_out {
                  if !is_contained_in(out_i, &candidates) {
                      // let dist = self.node_distance(xp, out_i);
                      let dist = ann.nodes[i].p.distance(&ann.nodes[*out_i as usize].p);
                      insert_dist((dist, *out_i), &mut candidates)
                  }
              }

              // run RobustPrune(σ(i), V, α, R) to update out-neighbors of σ(i)
              let mut new_n_out = ann.prune(&mut candidates);
              let new_added_ids = diff_ids(&ann.nodes[i as usize].n_out.read(), &prev_n_out);
              for out_i in new_added_ids {
                  insert_id(out_i, &mut new_n_out);
              }

              {
                  let mut current_n_out = ann.nodes[i as usize].n_out.write();
                  current_n_out.clone_from(&new_n_out);
              } // unlock the write lock

              // for all points j in Nout(σ(i)) do
              for j in new_n_out {
                  if ann.nodes[j as usize].n_out.read().contains(&(i as u32)) {
                      continue;
                  } else {
                      // Todo : refactor, self.make_edge or union. above ann.nodes[j].n_out.contains(&i) not necessary if use union
                      insert_id(i as u32, &mut ann.nodes[j as usize].n_out.write());
                      // insert_id(j, &mut ann.nodes[i].n_in);
                  }
              }

              #[cfg(feature = "progress-bar")]
              if let Some(bar) = &progress {
                  let value = progress_done.fetch_add(1, atomic::Ordering::Relaxed);
                  if value % 1000 == 0 {
                      bar.set_position(value as u64);
                  }
              }
          });

      (0..node_len).into_par_iter().for_each(|node_i| {
          let node_p = &ann.nodes[node_i].p;
          let mut n_out_dist = ann.nodes[node_i]
              .n_out
              .write()
              .clone()
              .into_iter()
              .map(|out_i| (node_p.distance(&ann.nodes[out_i as usize].p), out_i))
              .collect();

          *ann.nodes[node_i].n_out.write() = ann.prune(&mut n_out_dist);

          #[cfg(feature = "progress-bar")]
          if let Some(bar) = &progress {
              let value = progress_done.fetch_add(1, atomic::Ordering::Relaxed);
              if value % 1000 == 0 {
                  bar.set_position(value as u64);
              }
          }
      });

      #[cfg(feature = "progress-bar")]
      if let Some(bar) = &progress {
          bar.finish();
      }
  }

  pub fn prune(&self, candidates: &mut Vec<(f32, u32)>) -> Vec<u32> {
      let mut new_n_out = vec![];

      while let Some((first, rest)) = candidates.split_first() {
          let (_, pa) = *first; // pa is p asterisk (p*), which is nearest point to p in this loop
          new_n_out.push(pa);

          if new_n_out.len() == self.builder.r {
              break;
          }
          *candidates = rest.to_vec();

          // if α · d(p*, p') <= d(p, p') then remove p' from v
          candidates.retain(|&(dist_xp_pd, pd)| {
              let pa_point = &self.nodes[pa as usize].p;
              let pd_point = &self.nodes[pd as usize].p;
              let dist_pa_pd = pa_point.distance(pd_point);

              self.builder.a * dist_pa_pd > dist_xp_pd
          })
      }

      new_n_out
  }

  pub fn greedy_search(
      &self,
      query_point: &P,
      k: usize,
      l: usize,
  ) -> (Vec<(f32, u32)>, Vec<(f32, u32)>) {
      // k-anns, visited
      assert!(l >= k);
      let s = self.centroid;
      let mut visited: Vec<(f32, u32)> = Vec::with_capacity(self.builder.l * 2);
      let mut touched = FxHashSet::default();
      touched.reserve(self.builder.l * 100);

      let mut list: Vec<(f32, u32, bool)> = Vec::with_capacity(self.builder.l);
      list.push((query_point.distance(&self.nodes[s as usize].p), s, true));
      let mut working = Some(list[0]);
      visited.push((list[0].0, list[0].1));
      touched.insert(list[0].1);

      while let Some((_, nearest_i, _)) = working {
          let nearest_n_out = self.nodes[nearest_i as usize].n_out.read().clone();
          let mut nouts: Vec<(f32, u32, bool)> = Vec::with_capacity(nearest_n_out.len());
          for out_i in nearest_n_out {
              if !touched.contains(&out_i) {
                  touched.insert(out_i);
                  nouts.push((query_point.distance(&self.nodes[out_i as usize].p), out_i, false))
              }
          }

          sort_list_by_dist(&mut nouts);

          let mut new_list = Vec::with_capacity(self.builder.l);
          let mut new_list_idx = 0;

          let mut l_idx = 0; // Index for list
          let mut n_idx = 0; // Index for dists

          working = None;

          while new_list_idx < self.builder.l {
              let mut new_min = if l_idx >= list.len() && n_idx >= nouts.len() {
                  break;
              } else if l_idx >= list.len() {
                  let new_min = nouts[n_idx];
                  n_idx += 1;
                  new_min
              } else if n_idx >= nouts.len() {
                  let new_min = list[l_idx];
                  l_idx += 1;
                  new_min
              } else {
                  let l_min = list[l_idx];
                  let n_min = nouts[n_idx];

                  if l_min.0 <= n_min.0 {
                      l_idx += 1;
                      l_min
                  } else {
                      n_idx += 1;
                      n_min
                  }
              };

              let is_not_visited = !new_min.2;

              if working.is_none() && is_not_visited {
                  new_min.2 = true; // Mark as visited
                  working = Some(new_min);
                  visited.push((new_min.0, new_min.1));
              }

              new_list.push(new_min);
              new_list_idx += 1;
          }

          list = new_list;
      }

      let mut k_anns = list
          .into_iter()
          .map(|(dist, id, _)| (dist, id))
          .collect::<Vec<(f32, u32)>>();
      k_anns.truncate(k);

      sort_list_by_dist_v1(&mut visited);

      (k_anns, visited)
  }
}