Skip to content

web search done right

3.0 KB
Raw Blame
use fst::map::{OpBuilder, Union};
use fst::raw::IndexedValue;
use fst::Streamer;

use super::termdict::TermDictionary;
use crate::postings::TermInfo;
use crate::termdict::{TermOrdinal, TermStreamer};

/// Given a list of sorted term streams,
/// returns an iterator over sorted unique terms.
///
/// The item yielded is actually a pair with
/// - the term
/// - a slice with the ordinal of the segments containing
///     the term.
pub struct TermMerger<'a> {
    dictionaries: Vec<&'a TermDictionary>,
    union: Union<'a>,
    current_key: Vec<u8>,
    current_segment_and_term_ordinals: Vec<IndexedValue>,
}

impl<'a> TermMerger<'a> {
    /// Stream of merged term dictionary
    pub fn new(streams: Vec<TermStreamer<'a>>) -> TermMerger<'a> {
        let mut op_builder = OpBuilder::new();
        let mut dictionaries = Vec::with_capacity(streams.len());
        for streamer in streams {
            op_builder.push(streamer.stream);
            dictionaries.push(streamer.fst_map);
        }
        TermMerger {
            dictionaries,
            union: op_builder.union(),
            current_key: vec![],
            current_segment_and_term_ordinals: vec![],
        }
    }

    /// Iterator over `(segment ordinal, TermOrdinal)` pairs sorted by segment ordinal
    ///
    /// This method may be called
    /// if [`Self::advance`] has been called before
    /// and `true` was returned.
    pub fn matching_segments<'b: 'a>(&'b self) -> impl 'b + Iterator<Item = (usize, TermOrdinal)> {
        self.current_segment_and_term_ordinals
            .iter()
            .map(|iv| (iv.index, iv.value))
    }

    /// Advance the term iterator to the next term.
    /// Returns `true` if there is indeed another term
    /// `false` if there is none.
    pub fn advance(&mut self) -> bool {
        let (key, values) = if let Some((key, values)) = self.union.next() {
            (key, values)
        } else {
            return false;
        };
        self.current_key.clear();
        self.current_key.extend_from_slice(key);
        self.current_segment_and_term_ordinals.clear();
        self.current_segment_and_term_ordinals
            .extend_from_slice(values);
        self.current_segment_and_term_ordinals
            .sort_by_key(|iv| iv.index);
        true
    }

    /// Returns the current term.
    ///
    /// This method may be called if [`Self::advance`] has been called before
    /// and `true` was returned.
    pub fn key(&self) -> &[u8] {
        &self.current_key
    }

    /// Iterator over `(segment ordinal, TermInfo)` pairs sorted by the ordinal.
    ///
    /// This method may be called if [`Self::advance`] has been called before
    /// and `true` was returned.
    pub fn current_segment_ords_and_term_infos<'b: 'a>(
        &'b self,
    ) -> impl 'b + Iterator<Item = (usize, TermInfo)> {
        self.current_segment_and_term_ordinals
            .iter()
            .map(move |iv| {
                (
                    iv.index,
                    self.dictionaries[iv.index].term_info_from_ord(iv.value),
                )
            })
    }
}