milli_core/update/index_documents/helpers/
mod.rs

1mod clonable_mmap;
2mod grenad_helpers;
3mod merge_functions;
4
5use std::collections::HashSet;
6use std::convert::{TryFrom, TryInto};
7
8pub use clonable_mmap::{ClonableMmap, CursorClonableMmap};
9use fst::{IntoStreamer, Streamer};
10pub use grenad_helpers::*;
11pub use merge_functions::*;
12
13use crate::MAX_LMDB_KEY_LENGTH;
14
15pub fn valid_lmdb_key(key: impl AsRef<[u8]>) -> bool {
16    key.as_ref().len() <= MAX_LMDB_KEY_LENGTH - 3 && !key.as_ref().is_empty()
17}
18
19pub fn valid_facet_value(facet_value: impl AsRef<[u8]>) -> bool {
20    facet_value.as_ref().len() <= MAX_LMDB_KEY_LENGTH - 3 && !facet_value.as_ref().is_empty()
21}
22
23/// Divides one slice into two at an index, returns `None` if mid is out of bounds.
24pub fn try_split_at<T>(slice: &[T], mid: usize) -> Option<(&[T], &[T])> {
25    if mid <= slice.len() {
26        Some(slice.split_at(mid))
27    } else {
28        None
29    }
30}
31
32/// Divides one slice into an array and the tail at an index,
33/// returns `None` if `N` is out of bounds.
34pub fn try_split_array_at<T, const N: usize>(slice: &[T]) -> Option<([T; N], &[T])>
35where
36    [T; N]: for<'a> TryFrom<&'a [T]>,
37{
38    let (head, tail) = try_split_at(slice, N)?;
39    let head = head.try_into().ok()?;
40    Some((head, tail))
41}
42
43/// Converts an fst Stream into an HashSet of Strings.
44pub fn fst_stream_into_hashset<'f, I, S>(stream: I) -> HashSet<Vec<u8>>
45where
46    I: for<'a> IntoStreamer<'a, Into = S, Item = &'a [u8]>,
47    S: 'f + for<'a> Streamer<'a, Item = &'a [u8]>,
48{
49    let mut hashset = HashSet::new();
50    let mut stream = stream.into_stream();
51    while let Some(value) = stream.next() {
52        hashset.insert(value.to_owned());
53    }
54    hashset
55}
56
57// Converts an fst Stream into a Vec of Strings.
58pub fn fst_stream_into_vec<'f, I, S>(stream: I) -> Vec<String>
59where
60    I: for<'a> IntoStreamer<'a, Into = S, Item = &'a [u8]>,
61    S: 'f + for<'a> Streamer<'a, Item = &'a [u8]>,
62{
63    let mut strings = Vec::new();
64    let mut stream = stream.into_stream();
65    while let Some(word) = stream.next() {
66        let s = std::str::from_utf8(word).unwrap();
67        strings.push(s.to_owned());
68    }
69    strings
70}