rlucene 0.1.0-alpha.3

A Rust port of Apache Lucene, a full-text search engine library
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
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
use std::borrow::Borrow;
use std::sync::Arc;

use crate::core::search::doc_id_set_iterator::NO_MORE_DOCS;
use crate::core::util::SliceCopyOps;
use crate::core::util::error::lucene_error::{LuceneError, Result};
use crate::core::util::hnsw::neighbor_array::NeighborArray;
/// Hierarchical Navigable Small World (HNSW) graph.
///
/// Provides efficient approximate nearest neighbor search for high-dimensional
/// vectors. See the paper:
/// ["Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs (2018)"](https://arxiv.org/abs/1603.09320)
/// for algorithmic details.
///
/// The nomenclature here differs slightly from the paper:
///
/// ## Hyperparameters
///
/// - `beam_width` in
///   [`HnswGraphBuilder`](crate::core::util::hnsw::hnsw_graph_builder::HnswGraphBuilder)
///   corresponds to `efConst` in the
///   paper: it is the number of nearest neighbor candidates tracked while
///   searching the graph for each newly inserted node.
///
/// - `max_conn` corresponds to `M` in the paper: it controls how many of the
///   `efConst` neighbors are connected to the new node.
///
/// Note: The graph may be searched by multiple threads concurrently, but
/// **updates are not thread-safe**. The search method optionally takes a set of
/// *accepted nodes*, which can be used to exclude deleted documents.
pub trait HnswGraph {
  /// Whether traversal can exclusively borrow its current neighbor array.
  const HAS_BORROWED_NEIGHBORS: bool = false;

  /// Move the pointer to exactly the given `level`'s `target`.
  ///
  /// After this method returns, call [`next_neighbor()`](Self::next_neighbor)
  /// to return successive (ordered) connected node ordinals.
  ///
  /// - `level`: the level of the graph.
  /// - `target`: the ordinal of a node in the graph; must be `>= 0` and `<
  ///   [FloatVectorValues::size()]`.
  fn seek(&mut self, level: usize, target: usize) -> Result<()>;
  /// Returns the number of nodes in the graph
  fn size(&self) -> usize;
  /// Returns max node id, inclusive. Normally this value will be size - 1.
  fn max_node_id(&self) -> Option<usize> {
    Some(self.size() - 1)
  }
  /// Iterates over the neighbor list.
  ///
  /// It is illegal to call this method after it returns [`NO_MORE_DOCS`]
  /// without calling [`seek(level, target)`](Self::seek), which resets the
  /// iterator.
  ///
  /// # Returns
  ///
  /// A node ordinal in the graph, or [`NO_MORE_DOCS`] if the iteration is
  /// complete.
  fn next_neighbor(&mut self) -> Result<usize>;
  /// Borrows the current array and the graph's original next-neighbor index.
  ///
  /// Only implementations with [`Self::HAS_BORROWED_NEIGHBORS`] may use this
  /// entry. Success leaves the index unchanged. Failure, including a panic,
  /// must leave it as a failed first [`Self::next_neighbor`] call would.
  /// The array cannot change while borrowed; shared traversal must keep its
  /// original per-neighbor access and lock boundaries instead.
  fn borrow_neighbors(&mut self) -> Result<(&NeighborArray<true>, &mut usize)> {
    Err(LuceneError::unsupported_operation(
      "exclusive neighbor borrowing is unavailable",
    ))
  }
  /// Returns the number of levels of the graph
  fn num_levels(&self) -> Result<usize>;
  /// Returns graph's entry point on the top level *
  fn entry_node(&self) -> Result<Option<usize>>;
  type NodeIterator: NodesIterator;
  /// Get all nodes on a given level as node 0th ordinals.
  ///
  /// The nodes are **NOT** guaranteed to be presented in any particular
  /// order.
  ///
  /// # Arguments
  ///
  /// * `level` - Level for which to get all nodes.
  ///
  /// # Returns
  ///
  /// An iterator over nodes where `next_int()` returns the next node on the
  /// level.
  fn get_nodes_on_level(&mut self, level: usize) -> Result<Self::NodeIterator>;
  /// Executes an action with the [`NeighborArray`] connected to the given
  /// node.
  ///
  /// # Arguments
  ///
  /// * `level` - The level of the graph.
  /// * `node` - The node whose neighbors are returned, represented as an
  ///   ordinal on level 0.
  fn with_neighbors<T, F>(&self, _level: usize, _node: usize, _action: F) -> Result<T>
  where
    F: FnOnce(&NeighborArray<true>) -> Result<T>,
  {
    Err(LuceneError::unsupported_operation(""))
  }
}
impl<T> HnswGraph for Box<T>
where
  T: HnswGraph + ?Sized,
{
  const HAS_BORROWED_NEIGHBORS: bool = T::HAS_BORROWED_NEIGHBORS;

  fn seek(&mut self, level: usize, target: usize) -> Result<()> {
    (**self).seek(level, target)
  }

  fn size(&self) -> usize {
    (**self).size()
  }

  fn max_node_id(&self) -> Option<usize> {
    (**self).max_node_id()
  }

  fn next_neighbor(&mut self) -> Result<usize> {
    (**self).next_neighbor()
  }

  #[inline]
  fn borrow_neighbors(&mut self) -> Result<(&NeighborArray<true>, &mut usize)> {
    (**self).borrow_neighbors()
  }

  fn num_levels(&self) -> Result<usize> {
    (**self).num_levels()
  }

  fn entry_node(&self) -> Result<Option<usize>> {
    (**self).entry_node()
  }

  type NodeIterator = T::NodeIterator;

  fn get_nodes_on_level(&mut self, level: usize) -> Result<Self::NodeIterator> {
    (**self).get_nodes_on_level(level)
  }

  fn with_neighbors<R, F>(&self, level: usize, node: usize, action: F) -> Result<R>
  where
    F: FnOnce(&NeighborArray<true>) -> Result<R>,
  {
    (**self).with_neighbors(level, node, action)
  }
}
// Preserve the graph's mutable cursor when a search owns only its access handle.
// Shared on-heap graphs provide their separate implementation for `&OnHeapHnswGraph`.
impl<T> HnswGraph for &mut T
where
  T: HnswGraph + ?Sized,
{
  const HAS_BORROWED_NEIGHBORS: bool = T::HAS_BORROWED_NEIGHBORS;

  fn seek(&mut self, level: usize, target: usize) -> Result<()> {
    (**self).seek(level, target)
  }

  fn size(&self) -> usize {
    (**self).size()
  }

  fn max_node_id(&self) -> Option<usize> {
    (**self).max_node_id()
  }

  #[inline]
  fn next_neighbor(&mut self) -> Result<usize> {
    (**self).next_neighbor()
  }

  #[inline]
  fn borrow_neighbors(&mut self) -> Result<(&NeighborArray<true>, &mut usize)> {
    (**self).borrow_neighbors()
  }

  fn num_levels(&self) -> Result<usize> {
    (**self).num_levels()
  }

  fn entry_node(&self) -> Result<Option<usize>> {
    (**self).entry_node()
  }

  type NodeIterator = T::NodeIterator;

  fn get_nodes_on_level(&mut self, level: usize) -> Result<Self::NodeIterator> {
    (**self).get_nodes_on_level(level)
  }

  fn with_neighbors<R, F>(&self, level: usize, node: usize, action: F) -> Result<R>
  where
    F: FnOnce(&NeighborArray<true>) -> Result<R>,
  {
    (**self).with_neighbors(level, node, action)
  }
}

pub struct EmptyHnswGraph;
impl HnswGraph for EmptyHnswGraph {
  fn seek(&mut self, _level: usize, _target: usize) -> Result<()> {
    Ok(())
  }

  fn size(&self) -> usize {
    0
  }

  fn next_neighbor(&mut self) -> Result<usize> {
    Ok(NO_MORE_DOCS as usize)
  }

  fn num_levels(&self) -> Result<usize> {
    Ok(0)
  }

  fn entry_node(&self) -> Result<Option<usize>> {
    Ok(Some(0))
  }

  type NodeIterator = ArrayNodesIterator;

  fn get_nodes_on_level(&mut self, _level: usize) -> Result<Self::NodeIterator> {
    Ok(ArrayNodesIterator::empty())
  }
}
/// Iterator over the graph nodes on a certain level. Iterator also provides the
/// size – the total number of nodes to be iterated over. The nodes are NOT
/// guaranteed to be presented in any  particular order.
pub trait NodesIterator: Iterator<Item = usize> {
  ///  The number of elements in this iterator
  fn size(&self) -> usize;
  /// Consume integers from the iterator and place them into the `dest` array.
  ///
  /// # Arguments
  ///
  /// * `dest` - Where to put the integers.
  ///
  /// # Returns
  ///
  /// The number of integers written to `dest`.
  fn consume(&mut self, dest: &mut [usize]) -> Result<usize>;

  fn has_next(&self) -> bool;
}
macro_rules! define_nodes_iterator_enum {
    (
        $enum_name:ident,
        [$($V:ident),+ $(,)?]
    ) => {
        pub enum $enum_name<$($V),+> {
            $($V($V)),+
        }

        impl<$($V),+> Iterator for $enum_name<$($V),+>
        where
            $($V: Iterator<Item = usize>,)+
        {
            type Item = usize;

            fn next(&mut self) -> Option<Self::Item> {
                match self {
                    $(Self::$V(iter) => iter.next(),)+
                }
            }
        }

        impl<$($V),+> NodesIterator for $enum_name<$($V),+>
        where
            $($V: NodesIterator,)+
        {
            fn size(&self) -> usize {
                match self {
                    $(Self::$V(iter) => iter.size(),)+
                }
            }

            fn consume(&mut self, dest: &mut [usize]) -> Result<usize> {
                match self {
                    $(Self::$V(iter) => iter.consume(dest),)+
                }
            }

            fn has_next(&self) -> bool {
                match self {
                    $(Self::$V(iter) => iter.has_next(),)+
                }
            }
        }
    };
}
define_nodes_iterator_enum!(NodesIteratorEnum2, [A, B]);
pub fn get_sorted_nodes<I>(nodes: &mut I) -> Vec<usize>
where
  I: NodesIterator,
{
  let mut sorted = Vec::with_capacity(nodes.size());
  for v in nodes.by_ref() {
    sorted.push(v);
  }
  sorted.sort_unstable();
  sorted
}
/// NodesIterator that accepts nodes as an integer array.
pub struct ArrayNodesIterator<N = Arc<Vec<usize>>> {
  nodes: Option<N>,
  cur: usize,
  size: usize,
}

impl<N: Borrow<Vec<usize>>> ArrayNodesIterator<N> {
  /// Creates explicit node list (with partial length).
  pub fn from_nodes(nodes: Option<N>, size: usize) -> Self {
    debug_assert!(
      nodes
        .as_ref()
        .is_some_and(|nodes| size <= <N as Borrow<Vec<usize>>>::borrow(nodes).len())
    );
    Self {
      nodes,
      cur: 0,
      size,
    }
  }

  /// Creates implicit index-based iteration (0..size).
  pub fn from_size(size: usize) -> Self {
    Self {
      nodes: None,
      cur: 0,
      size,
    }
  }

  /// Shared empty singleton.
  pub fn empty() -> Self {
    Self::from_size(0)
  }
}

impl<N: Borrow<Vec<usize>>> Iterator for ArrayNodesIterator<N> {
  type Item = usize;

  fn next(&mut self) -> Option<Self::Item> {
    if !self.has_next() {
      None
    } else {
      let value = match &self.nodes {
        Some(vec) => <N as Borrow<Vec<usize>>>::borrow(vec)[self.cur],
        None => self.cur,
      };
      self.cur += 1;
      Some(value)
    }
  }
}

impl<N: Borrow<Vec<usize>>> NodesIterator for ArrayNodesIterator<N> {
  fn size(&self) -> usize {
    self.size
  }

  fn consume(&mut self, dest: &mut [usize]) -> Result<usize> {
    if !self.has_next() {
      return Err(LuceneError::no_such_element(""));
    }
    let num_to_copy = std::cmp::min(dest.len(), self.size - self.cur);
    match &self.nodes {
      Some(vec) => {
        let vec: &Vec<usize> = vec.borrow();
        dest.copy_from(&vec[self.cur..self.cur + num_to_copy], 0);
        self.cur += num_to_copy;
        Ok(num_to_copy)
      },
      None => {
        for (i, slot) in dest.iter_mut().enumerate().take(num_to_copy) {
          *slot = self.cur + i;
        }
        Ok(num_to_copy)
      },
    }
  }

  fn has_next(&self) -> bool {
    self.cur < self.size
  }
}

/// Nodes iterator based on set representation of nodes.
pub struct CollectionNodesIterator {
  nodes: Arc<Option<Vec<usize>>>,
  index: usize,
  size: usize,
}

impl CollectionNodesIterator {
  /// Constructs a new iterator from a collection of nodes.
  pub fn new(nodes: Arc<Option<Vec<usize>>>) -> Self {
    let size = nodes.as_ref().as_ref().map_or(0, |v| v.len());
    Self {
      nodes,
      index: 0,
      size,
    }
  }
}

impl Iterator for CollectionNodesIterator {
  type Item = usize;

  fn next(&mut self) -> Option<Self::Item> {
    if self.size == 0 {
      return None;
    }
    let vec = self.nodes.as_ref().as_ref()?;
    if self.index < vec.len() {
      let val = vec[self.index];
      self.index += 1;
      Some(val)
    } else {
      None
    }
  }
}

impl NodesIterator for CollectionNodesIterator {
  fn size(&self) -> usize {
    self.size
  }

  fn consume(&mut self, dest: &mut [usize]) -> Result<usize> {
    if !self.has_next() {
      return Err(LuceneError::no_such_element(""));
    }
    let mut dest_index = 0;
    while self.has_next() && dest_index < dest.len() {
      dest[dest_index] = self
        .next()
        .ok_or_else(|| LuceneError::no_such_element(""))?;
      dest_index += 1;
    }
    Ok(dest_index)
  }

  fn has_next(&self) -> bool {
    self.index < self.size
  }
}