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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
//! Database built on memory.

use core::borrow::Borrow;
use core::hash::Hash;
use core::iter::{IntoIterator, Iterator};
use core::num::NonZeroUsize;
use std::collections::HashMap;
use std::collections::hash_map::{Entry as HashMapEntry};
use uuid::Uuid;

use crate::error::Error;
use crate::kmeans::{ClusterEvent, Codebook, Scalar, cluster_with_events};
use crate::linalg::{dot, subtract_in};
use crate::partitions::{Partitioning, Partitions};
use crate::slice::AsSlice;
use crate::vector::{BlockVectorSet, VectorSet, divide_vector_set};

use super::{Attributes, AttributeValue};

pub mod proto;

/// Vector database builder.
pub struct DatabaseBuilder<T, VS>
where
    VS: VectorSet<T>,
{
    _t: core::marker::PhantomData<T>,
    // Input vector set.
    vs: VS,
    // Number of partitions.
    num_partitions: usize,
    // Number of subvector divisions.
    num_divisions: usize,
    // Number of clusters for product quantization (PQ).
    num_clusters: usize,
}

impl<T, VS> DatabaseBuilder<T, VS>
where
    T: Scalar,
    VS: VectorSet<T> + Partitioning<T, VS>,
{
    /// Initializes a builder for a given vector set.
    pub fn new(vs: VS) -> Self {
        Self {
            _t: core::marker::PhantomData,
            vs,
            num_partitions: 10,
            num_divisions: 8,
            num_clusters: 16,
        }
    }

    /// Sets the number of partitions.
    pub fn with_partitions(mut self, num_partitions: NonZeroUsize) -> Self {
        self.num_partitions = num_partitions.get();
        self
    }

    /// Sets the number of subvector divisions.
    pub fn with_divisions(mut self, num_divisions: NonZeroUsize) -> Self {
        self.num_divisions = num_divisions.get();
        self
    }

    /// Sets the number of clusters for product quantization (PQ).
    pub fn with_clusters(mut self, num_clusters: NonZeroUsize) -> Self {
        self.num_clusters = num_clusters.get();
        self
    }

    /// Builds the vector database.
    pub fn build(self) -> Result<Database<T, VS>, Error> {
        self.build_with_events(|_| {})
    }

    /// Builds the vector database with an event handler.
    pub fn build_with_events<EventHandler>(
        self,
        mut event: EventHandler,
    ) -> Result<Database<T, VS>, Error>
    where
        EventHandler: FnMut(BuildEvent<'_, T>) -> (),
    {
        // assigns IDs to vectors
        event(BuildEvent::StartingIdAssignment);
        let mut vector_ids: Vec<Uuid> = Vec::with_capacity(self.vs.len());
        for _ in 0..self.vs.len() {
            vector_ids.push(Uuid::new_v4());
        }
        event(BuildEvent::FinishedIdAssignment);
        // partitions all the data
        event(BuildEvent::StartingPartitioning);
        let partitions = self.vs.partition_with_events(
            self.num_partitions.try_into().unwrap(),
            |e| event(BuildEvent::ClusterEvent(e)),
        )?;
        event(BuildEvent::FinishedPartitioning);
        // divides residual vectors
        event(BuildEvent::StartingSubvectorDivision);
        let divided = divide_vector_set(
            &partitions.residues,
            self.num_divisions.try_into().unwrap(),
        )?;
        event(BuildEvent::FinishedSubvectorDivision);
        // builds codebooks for residues
        let mut codebooks: Vec<Codebook<T>> = Vec::with_capacity(
            self.num_divisions.try_into().unwrap(),
        );
        for (i, subvs) in divided.iter().enumerate() {
            event(BuildEvent::StartingQuantization(i));
            codebooks.push(cluster_with_events(
                subvs,
                self.num_clusters.try_into().unwrap(),
                |e| event(BuildEvent::ClusterEvent(e)),
            )?);
            event(BuildEvent::FinishedQuantization(i));
        }
        Ok(Database {
            vector_size: partitions.residues.vector_size(),
            num_partitions: self.num_partitions,
            num_divisions: self.num_divisions,
            num_clusters: self.num_clusters,
            vector_ids,
            partitions,
            codebooks,
            attribute_table: HashMap::new(),
        })
    }
}

/// Events from [`DatabaseBuilder::build_with_events`].
#[derive(Debug)]
pub enum BuildEvent<'a, T> {
    /// Starting to assign unique IDs to individual vectors.
    StartingIdAssignment,
    /// Finished assigning unique IDs to individual vectors.
    FinishedIdAssignment,
    /// Starting to partition vectors.
    StartingPartitioning,
    /// Finished partitioning vectors.
    FinishedPartitioning,
    /// Starting to divide vectors into subvectors.
    StartingSubvectorDivision,
    /// Finished dividing vectors into subvectors.
    FinishedSubvectorDivision,
    /// Starting to quantize subvectors in a specific division.
    StartingQuantization(usize),
    /// Finished to quantize subvectors in a specific division.
    FinishedQuantization(usize),
    /// Event from clustering.
    ClusterEvent(ClusterEvent<'a, T>),
}

/// Database.
pub struct Database<T, VS>
where
    VS: VectorSet<T>,
{
    // Vector size.
    vector_size: usize,
    // Number of partitions.
    num_partitions: usize,
    // Number of subvector divisions.
    num_divisions: usize,
    // Number of clusters.
    num_clusters: usize,
    // Vector IDs.
    vector_ids: Vec<Uuid>,
    // Partitions.
    partitions: Partitions<T, VS>,
    // Codebooks for PQ.
    codebooks: Vec<Codebook<T>>,
    // Attributes associated with vectors.
    attribute_table: HashMap<Uuid, Attributes>,
}

impl<T, VS> Database<T, VS>
where
    VS: VectorSet<T>,
{
    /// Returns the number of vectors in the database.
    pub fn num_vectors(&self) -> usize {
        self.vector_ids.len()
    }

    /// Returns the vector size.
    pub const fn vector_size(&self) -> usize {
        self.vector_size
    }

    /// Returns the number of partitions.
    pub const fn num_partitions(&self) -> usize {
        self.num_partitions
    }

    /// Returns the number of subvector divisions.
    pub const fn num_divisions(&self) -> usize {
        self.num_divisions
    }

    /// Returns the size of a subvector.
    pub fn subvector_size(&self) -> usize {
        self.vector_size / self.num_divisions
    }

    /// Returns the number of clusters.
    pub const fn num_clusters(&self) -> usize {
        self.num_clusters
    }

    /// Returns an iterator of vector IDs.
    pub fn vector_ids(&self) -> impl Iterator<Item = &Uuid> {
        self.vector_ids.iter()
    }

    /// Returns an iterator of partitions.
    pub fn partitions(&self) -> PartitionIter<'_, T, VS> {
        PartitionIter {
            database: self,
            next_index: 0,
        }
    }

    /// Returns the attribute value of a given vector.
    ///
    /// Fails if no vector is associated with `id`.
    pub fn get_attribute<K>(
        &self,
        id: &Uuid,
        key: &K,
    ) -> Result<Option<&AttributeValue>, Error>
    where
        String: Borrow<K>,
        K: Hash + Eq + ?Sized,
    {
        Ok(
            self.attribute_table
                .get(id)
                .ok_or(Error::InvalidArgs(
                    format!("no such vector ID: {}", id),
                ))?
                .get(key),
            )
    }

    /// Sets an attribute value for the i-th vector.
    ///
    /// Replaces with the new value if the vector already has the attribute.
    ///
    /// Fails if `i` is out of bounds.
    pub fn set_attribute_at<KV, KEY, VAL>(
        &mut self,
        i: usize,
        attribute: KV,
    ) -> Result<(), Error>
    where
        KV: Into<(KEY, VAL)>,
        KEY: Into<String>,
        VAL: Into<AttributeValue>,
    {
        let id = self.vector_ids.get(i)
            .ok_or(Error::InvalidArgs(
                format!("vector index out of bounds: {}", i),
            ))?;
        let (key, value) = attribute.into();
        let key = key.into();
        let value = value.into();
        if let Some(attributes) = self.attribute_table.get_mut(id) {
            match attributes.entry(key.into()) {
                HashMapEntry::Occupied(entry) => {
                    *entry.into_mut() = value.into();
                },
                HashMapEntry::Vacant(entry) => {
                    entry.insert(value.into());
                },
            };
        } else {
            self.attribute_table.insert(
                id.clone(),
                Attributes::from([(key, value)]),
            );
        }
        Ok(())
    }
}

impl<T, VS> Database<T, VS>
where
    T: Scalar,
    VS: VectorSet<T>,
{
    /// Queries k-nearest neighbors (k-NN) of a given vector.
    pub fn query<V>(
        &self,
        v: &V,
        k: NonZeroUsize,
        nprobe: NonZeroUsize,
    ) -> Result<Vec<QueryResult<T>>, Error>
    where
        V: AsSlice<T> + ?Sized,
    {
        self.query_with_events(v, k, nprobe, |_| {})
    }

    /// Queries k-nearest neighbors (k-NN) of a given vector.
    pub fn query_with_events<V, EventHandler>(
        &self,
        v: &V,
        k: NonZeroUsize,
        nprobe: NonZeroUsize,
        mut event: EventHandler,
    ) -> Result<Vec<QueryResult<T>>, Error>
    where
        V: AsSlice<T> + ?Sized,
        EventHandler: FnMut(QueryEvent) -> (),
    {
        event(QueryEvent::StartingPartitionSelection);
        let v = v.as_slice();
        let queries = self.query_partitions(v, nprobe)?;
        event(QueryEvent::FinishedPartitionSelection);
        let mut all_results: Vec<QueryResult<T>> = Vec::new();
        for query in &queries {
            event(QueryEvent::StartingPartitionQuery(
                query.partition_index,
            ));
            let results = query.execute()?;
            all_results.extend(results);
            event(QueryEvent::FinishedPartitionQuery(
                query.partition_index,
            ));
        }
        event(QueryEvent::StartingResultSelection);
        all_results.sort_by(|lhs, rhs| {
            lhs.squared_distance.partial_cmp(&rhs.squared_distance).unwrap()
        });
        all_results.truncate(k.get());
        event(QueryEvent::FinishedResultSelection);
        Ok(all_results)
    }

    // Queries partitions.
    //
    // Fails if `nprobe` exceeds the number of partitions.
    fn query_partitions<'a>(
        &'a self,
        v: &[T],
        nprobe: NonZeroUsize,
    ) -> Result<Vec<PartitionQuery<'a, T, VS>>, Error> {
        let nprobe = nprobe.get();
        if nprobe > self.num_partitions {
            return Err(Error::InvalidArgs(format!(
                "nprobe {} exceeds the number of partitions {}",
                nprobe,
                self.num_partitions,
            )));
        }
        // localizes vectors and calculates distances
        let mut local_vectors: Vec<(usize, Vec<T>, T)> =
            Vec::with_capacity(self.num_partitions);
        for pi in 0..self.num_partitions {
            let mut localized: Vec<T> = Vec::new();
            localized.extend_from_slice(v);
            let centroid = self.partitions.codebook.centroids.get(pi);
            subtract_in(&mut localized[..], centroid.as_slice());
            let distance = dot(&localized[..], &localized[..]);
            local_vectors.push((pi, localized, distance));
        }
        // chooses `nprobe` shortest distances
        local_vectors.sort_by(|lhs, rhs| lhs.2.partial_cmp(&rhs.2).unwrap());
        local_vectors.truncate(nprobe);
        // queries
        let queries = local_vectors
            .into_iter()
            .map(|(partition_index, localized, _)| PartitionQuery {
                db: self,
                partition_index,
                localized,
            })
            .collect();
        Ok(queries)
    }
}

/// Iterator of partitions in a database.
pub struct PartitionIter<'a, T, VS>
where
    VS: VectorSet<T>,
{
    // Database.
    database: &'a Database<T, VS>,
    // Next partition index.
    next_index: usize,
}

impl<'a, T, VS> Iterator for PartitionIter<'a, T, VS>
where
    T: Clone,
    VS: VectorSet<T>,
{
    type Item = Partition<T>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.next_index < self.database.num_partitions {
            let partition = Partition::new(self.database, self.next_index);
            self.next_index += 1;
            Some(partition)
        } else {
            None
        }
    }
}

/// Partition in a database.
pub struct Partition<T> {
    // Centroid of the partition.
    centroid: Vec<T>,
    // Encoded vectors.
    encoded_vectors: BlockVectorSet<u32>,
    // Vector IDs.
    vector_ids: Vec<Uuid>,
}

impl<T> Partition<T> {
    /// Returns the vector size.
    pub fn vector_size(&self) -> usize {
        self.centroid.len()
    }

    /// Returns the number of subvector divisions.
    pub fn num_divisions(&self) -> usize {
        self.encoded_vectors.vector_size()
    }

    /// Returns the number of vectors.
    pub fn num_vectors(&self) -> usize {
        self.encoded_vectors.len()
    }
}

impl<T> Partition<T>
where
    T: Clone,
{
    /// Extracts a partition from a given database.
    fn new<VS>(db: &Database<T, VS>, index: usize) -> Self
    where
        VS: VectorSet<T>,
    {
        let mut centroid: Vec<T> = Vec::with_capacity(db.vector_size());
        centroid.extend_from_slice(
            db.partitions.codebook.centroids.get(index),
        );
        let num_divisions = db.num_divisions();
        let num_vectors = db.partitions.codebook.indices
            .iter()
            .filter(|&&pi| pi == index)
            .count();
        let mut encoded_vectors: Vec<u32> =
            Vec::with_capacity(num_vectors * num_divisions);
        let mut vector_ids: Vec<Uuid> = Vec::with_capacity(num_vectors);
        for (vi, _) in db.partitions.codebook.indices
            .iter()
            .enumerate()
            .filter(|(_, &pi)| pi == index)
        {
            for di in 0..num_divisions {
                encoded_vectors.push(
                    db.codebooks[di].indices[vi].try_into().unwrap(),
                );
            }
            vector_ids.push(db.vector_ids[vi]);
        }
        Partition {
            centroid,
            encoded_vectors: BlockVectorSet::chunk(
                encoded_vectors,
                num_divisions.try_into().unwrap(),
            ).unwrap(),
            vector_ids,
        }
    }
}

/// Database query event.
#[derive(Debug)]
pub enum QueryEvent {
    /// Starting to select partitions.
    StartingPartitionSelection,
    /// Finished selecting partitions.
    FinishedPartitionSelection,
    /// Starting to run a query on a specific partition.
    StartingPartitionQuery(usize),
    /// Finished to run a query on a specific partition.
    FinishedPartitionQuery(usize),
    /// Starting to select k-nearest neighbors.
    StartingResultSelection,
    /// Finished selecting k-nearest neighbors.
    FinishedResultSelection,
}

/// Query in a partition.
pub struct PartitionQuery<'a, T, VS>
where
    VS: VectorSet<T>,
{
    // Database.
    db: &'a Database<T, VS>,
    // Partition index.
    partition_index: usize,
    // Localized query vector.
    localized: Vec<T>,
}

impl<'a, T, VS> PartitionQuery<'a, T, VS>
where
    T: Scalar,
    VS: VectorSet<T>,
{
    /// Executes the query.
    pub fn execute(&self) -> Result<Vec<QueryResult<T>>, Error> {
        let num_divisions = self.db.num_divisions();
        let num_clusters = self.db.num_clusters();
        let md = self.db.subvector_size();
        // calculates the distance table
        let mut distance_table: Vec<T> = Vec::with_capacity(
            num_divisions * num_clusters,
        );
        let mut vector_buf = vec![T::zero(); md];
        for di in 0..num_divisions {
            let from = di * md;
            let to = from + md;
            let subv = &self.localized[from..to];
            for ci in 0..num_clusters {
                let centroid = self.db.codebooks[di].centroids.get(ci);
                let d = &mut vector_buf[..];
                d.copy_from_slice(subv);
                subtract_in(d, centroid.as_slice());
                distance_table.push(dot(d, d));
            }
        }
        // approximates the squared distances to individual vectors
        let mut results: Vec<QueryResult<T>> = Vec::with_capacity(
            self.partition_size(),
        );
        for (pvi, (vi, _)) in self.db.partitions.codebook.indices
            .iter()
            .enumerate()
            .filter(|(_, &pi)| pi == self.partition_index)
            .enumerate()
        {
            let mut distance = T::zero();
            for di in 0..num_divisions {
                let ci = self.db.codebooks[di].indices[vi];
                distance += distance_table[di * num_clusters + ci];
            }
            results.push(QueryResult {
                partition_index: self.partition_index,
                vector_id: self.db.vector_ids[vi].clone(),
                vector_index: pvi,
                squared_distance: distance,
            });
        }
        Ok(results)
    }

    /// Returns the partition size.
    fn partition_size(&self) -> usize {
        self.db.partitions.codebook.indices
            .iter()
            .filter(|pi| **pi == self.partition_index)
            .count()
    }
}

/// Query result.
#[derive(Clone, Debug)]
pub struct QueryResult<T> {
    /// Partition index.
    pub partition_index: usize,
    /// Vector ID. Must be unique across the database.
    pub vector_id: Uuid,
    /// Vector index. Local index in the partition.
    pub vector_index: usize,
    /// Approximate squared distance.
    pub squared_distance: T,
}