summaryrefslogtreecommitdiffstats
path: root/src/postings/postings.rs
blob: 52f16198aca0419d1607e3cf95392926dbbf7f2c (plain)
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
use std::borrow::Borrow;
use postings::docset::DocSet;

/// Postings (also called inverted list)
///
/// For a given term, it is the list of doc ids of the doc
/// containing the term. Optionally, for each document,
/// it may also give access to the term frequency
/// as well as the list of term positions.
///
/// Its main implementation is `SegmentPostings`,
/// but other implementations mocking `SegmentPostings` exist,
/// for merging segments or for testing.
pub trait Postings: DocSet {
    /// Returns the term frequency
    fn term_freq(&self) -> u32;
    /// Returns the list of positions of the term, expressed as a list of
    /// token ordinals.
    fn positions(&self) -> &[u32];
}

impl<TPostings: Postings> Postings for Box<TPostings> {
    fn term_freq(&self) -> u32 {
        let unboxed: &TPostings = self.borrow();
        unboxed.term_freq()
    }

    fn positions(&self) -> &[u32] {
        let unboxed: &TPostings = self.borrow();
        unboxed.positions()
    }
}

impl<'a, TPostings: Postings> Postings for &'a mut TPostings {
    fn term_freq(&self) -> u32 {
        let unref: &TPostings = *self;
        unref.term_freq()
    }

    fn positions(&self) -> &[u32] {
        let unref: &TPostings = *self;
        unref.positions()
    }
}