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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Lance Authors

//! Generic Graph implementation.
//!

use std::cmp::Reverse;
use std::collections::{BinaryHeap, HashSet};
use std::sync::Arc;

use arrow_schema::{DataType, Field};
use lance_core::{Error, Result};
use snafu::{location, Location};

pub(crate) mod builder;
pub mod memory;
pub(super) mod storage;

/// Vector storage to back a graph.
pub use storage::VectorStorage;

use self::storage::DistCalculator;

pub(crate) const NEIGHBORS_COL: &str = "__neighbors";

lazy_static::lazy_static! {
    /// NEIGHBORS field.
    pub static ref NEIGHBORS_FIELD: Field =
        Field::new(NEIGHBORS_COL, DataType::List(Field::new_list_field(DataType::UInt32, true).into()), true);
}

pub struct GraphNode<I = u32> {
    pub id: I,
    pub neighbors: Vec<I>,
}

impl<I> GraphNode<I> {
    pub fn new(id: I, neighbors: Vec<I>) -> Self {
        Self { id, neighbors }
    }
}

impl<I> From<I> for GraphNode<I> {
    fn from(id: I) -> Self {
        Self {
            id,
            neighbors: vec![],
        }
    }
}

/// A wrapper for f32 to make it ordered, so that we can put it into
/// a BTree or Heap
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct OrderedFloat(pub f32);

impl PartialOrd for OrderedFloat {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Eq for OrderedFloat {}

impl Ord for OrderedFloat {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.0.partial_cmp(&other.0).unwrap()
    }
}

impl From<f32> for OrderedFloat {
    fn from(f: f32) -> Self {
        Self(f)
    }
}

impl From<OrderedFloat> for f32 {
    fn from(f: OrderedFloat) -> Self {
        f.0
    }
}

#[derive(Debug, Eq, PartialEq, Clone)]
pub struct OrderedNode {
    pub id: u32,
    pub dist: OrderedFloat,
}

impl OrderedNode {
    pub fn new(id: u32, dist: OrderedFloat) -> Self {
        Self { id, dist }
    }
}

impl PartialOrd for OrderedNode {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.dist.cmp(&other.dist))
    }
}

impl Ord for OrderedNode {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.dist.cmp(&other.dist)
    }
}

impl From<(OrderedFloat, u32)> for OrderedNode {
    fn from((dist, id): (OrderedFloat, u32)) -> Self {
        Self { id, dist }
    }
}

impl From<OrderedNode> for (OrderedFloat, u32) {
    fn from(node: OrderedNode) -> Self {
        (node.dist, node.id)
    }
}

/// Distance calculator.
///
/// This trait is used to calculate a query vector to a stream of vector IDs.
///
pub trait DistanceCalculator {
    /// Compute distances between one query vector to all the vectors in the
    /// list of IDs.
    fn compute_distances(&self, ids: &[u32]) -> Box<dyn Iterator<Item = f32>>;
}

/// Graph trait.
///
/// Type parameters
/// ---------------
/// K: Vertex Index type
/// T: the data type of vector, i.e., ``f32`` or ``f16``.
pub trait Graph {
    /// Get the number of nodes in the graph.
    fn len(&self) -> usize;

    /// Returns true if the graph is empty.
    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Get the neighbors of a graph node, identifyied by the index.
    fn neighbors(&self, key: u32) -> Option<Box<dyn Iterator<Item = u32> + '_>>;

    /// Access to underline storage
    fn storage(&self) -> Arc<dyn VectorStorage>;
}

/// Beam search over a graph
///
/// This is the same as ``search-layer`` in HNSW.
///
/// Parameters
/// ----------
/// graph : Graph
///  The graph to search.
/// start : &[OrderedNode]
///  The starting point.
/// query : &[f32]
///  The query vector.
/// k : usize
///  The number of results to return.
/// bitset : Option<&RoaringBitmap>
///  The bitset of node IDs to filter the results, bit 1 for the node to keep, and bit 0 for the node to discard.
///
/// Returns
/// -------
/// A descending sorted list of ``(dist, node_id)`` pairs.
///
pub(super) fn beam_search(
    graph: &dyn Graph,
    start: &[OrderedNode],
    query: &[f32],
    k: usize,
    dist_calc: Option<Arc<dyn DistCalculator>>,
    bitset: Option<&roaring::bitmap::RoaringBitmap>,
) -> Result<Vec<OrderedNode>> {
    let mut visited: HashSet<_> = start.iter().map(|node| node.id).collect();
    let dist_calc = dist_calc.unwrap_or_else(|| graph.storage().dist_calculator(query).into());
    let mut candidates = start
        .iter()
        .cloned()
        .map(Reverse)
        .collect::<BinaryHeap<_>>();
    let mut results = candidates
        .clone()
        .into_iter()
        .filter(|node| {
            bitset
                .map(|bitset| bitset.contains(node.0.id))
                .unwrap_or(true)
        })
        .map(|v| v.0)
        .collect::<BinaryHeap<_>>();

    while !candidates.is_empty() {
        let current = candidates.pop().expect("candidates is empty").0;
        let furthest = results
            .peek()
            .map(|node| node.dist)
            .unwrap_or(OrderedFloat(f32::INFINITY));
        if current.dist > furthest {
            break;
        }
        let neighbors = graph.neighbors(current.id).ok_or_else(|| Error::Index {
            message: format!("Node {} does not exist in the graph", current.id),
            location: location!(),
        })?;

        for neighbor in neighbors {
            if visited.contains(&neighbor) {
                continue;
            }
            visited.insert(neighbor);
            let furthest = results
                .peek()
                .map(|node| node.dist)
                .unwrap_or(OrderedFloat(f32::INFINITY));
            let dist = dist_calc.distance(&[neighbor])[0].into();
            if dist <= furthest || results.len() < k {
                if bitset
                    .map(|bitset| bitset.contains(neighbor))
                    .unwrap_or(true)
                {
                    results.push((dist, neighbor).into());
                    if results.len() > k {
                        results.pop();
                    }
                }
                candidates.push(Reverse((dist, neighbor).into()));
            }
        }
    }

    Ok(results.into_sorted_vec())
}

/// Greedy search over a graph
///
/// This searches for only one result, only used for finding the entry point
///
/// Parameters
/// ----------
/// graph : Graph
///    The graph to search.
/// start : u32
///   The index starting point.
/// query : &[f32]
///   The query vector.
///
/// Returns
/// -------
/// A ``(dist, node_id)`` pair.
///
pub(super) fn greedy_search(
    graph: &dyn Graph,
    start: OrderedNode,
    query: &[f32],
    dist_calc: Option<Arc<dyn DistCalculator>>,
) -> Result<OrderedNode> {
    let mut current = start.id;
    let mut closest_dist = start.dist.0;
    let dist_calc = dist_calc.unwrap_or_else(|| graph.storage().dist_calculator(query).into());
    loop {
        let neighbors: Vec<_> = graph
            .neighbors(current)
            .ok_or_else(|| Error::Index {
                message: format!("Node {} does not exist in the graph", current),
                location: location!(),
            })?
            .collect();
        let distances = dist_calc.distance(&neighbors);

        let mut next = None;
        for (neighbor, dist) in neighbors.into_iter().zip(distances) {
            if dist < closest_dist {
                closest_dist = dist;
                next = Some(neighbor);
            }
        }

        if let Some(next) = next {
            current = next;
        } else {
            break;
        }
    }

    Ok(OrderedNode::new(current, closest_dist.into()))
}

#[cfg(test)]
mod tests {}