summaryrefslogtreecommitdiffstats
path: root/src/postings/recorder.rs
blob: c340d13fdd2bdef1586c18ad801e598b956b2d61 (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
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
use DocId;
use std::io;
use postings::PostingsSerializer;
use datastruct::stacker::{ExpUnrolledLinkedList, Heap, HeapAllocable};

const EMPTY_ARRAY: [u32; 0] = [0u32; 0];
const POSITION_END: u32 = 4294967295;

/// Recorder is in charge of recording relevant information about
/// the presence of a term in a document.
///
/// Depending on the `TextIndexingOptions` associated to the
/// field, the recorder may records
///   * the document frequency
///   * the document id
///   * the term frequency
///   * the term positions
pub trait Recorder: HeapAllocable {
    /// Returns the current document
    fn current_doc(&self) -> u32;
    /// Starts recording information about a new document
    /// This method shall only be called if the term is within the document.
    fn new_doc(&mut self, doc: DocId, heap: &Heap);
    /// Record the position of a term. For each document,
    /// this method will be called `term_freq` times.
    fn record_position(&mut self, position: u32, heap: &Heap);
    /// Close the document. It will help record the term frequency.
    fn close_doc(&mut self, heap: &Heap);
    /// Pushes the postings information to the serializer.
    fn serialize(&self,
                 self_addr: u32,
                 serializer: &mut PostingsSerializer,
                 heap: &Heap)
                 -> io::Result<()>;
}

/// Only records the doc ids
#[repr(C, packed)]
pub struct NothingRecorder {
    stack: ExpUnrolledLinkedList,
    current_doc: DocId,
}

impl HeapAllocable for NothingRecorder {
    fn with_addr(addr: u32) -> NothingRecorder {
        NothingRecorder {
            stack: ExpUnrolledLinkedList::with_addr(addr),
            current_doc: u32::max_value(),
        }
    }
}

impl Recorder for NothingRecorder {
    fn current_doc(&self) -> DocId {
        self.current_doc
    }

    fn new_doc(&mut self, doc: DocId, heap: &Heap) {
        self.current_doc = doc;
        self.stack.push(doc, heap);
    }

    fn record_position(&mut self, _position: u32, _heap: &Heap) {}

    fn close_doc(&mut self, _heap: &Heap) {}

    fn serialize(&self,
                 self_addr: u32,
                 serializer: &mut PostingsSerializer,
                 heap: &Heap)
                 -> io::Result<()> {
        for doc in self.stack.iter(self_addr, heap) {
            try!(serializer.write_doc(doc, 0u32, &EMPTY_ARRAY));
        }
        Ok(())
    }
}


/// Recorder encoding document ids, and term frequencies
#[repr(C, packed)]
pub struct TermFrequencyRecorder {
    stack: ExpUnrolledLinkedList,
    current_doc: DocId,
    current_tf: u32,
}

impl HeapAllocable for TermFrequencyRecorder {
    fn with_addr(addr: u32) -> TermFrequencyRecorder {
        TermFrequencyRecorder {
            stack: ExpUnrolledLinkedList::with_addr(addr),
            current_doc: u32::max_value(),
            current_tf: 0u32,
        }
    }
}

impl Recorder for TermFrequencyRecorder {
    fn current_doc(&self) -> DocId {
        self.current_doc
    }

    fn new_doc(&mut self, doc: DocId, heap: &Heap) {
        self.current_doc = doc;
        self.stack.push(doc, heap);
    }

    fn record_position(&mut self, _position: u32, _heap: &Heap) {
        self.current_tf += 1;
    }

    fn close_doc(&mut self, heap: &Heap) {
        debug_assert!(self.current_tf > 0);
        self.stack.push(self.current_tf, heap);
        self.current_tf = 0;
    }


    fn serialize(&self,
                 self_addr: u32,
                 serializer: &mut PostingsSerializer,
                 heap: &Heap)
                 -> io::Result<()> {
        // the last document has not been closed...
        // its term freq is self.current_tf.
        let mut doc_iter = self.stack
            .iter(self_addr, heap)
            .chain(Some(self.current_tf).into_iter());

        while let Some(doc) = doc_iter.next() {
            let term_freq = doc_iter
                .next()
                .expect("The IndexWriter recorded a doc without a term freq.");
            serializer.write_doc(doc, term_freq, &EMPTY_ARRAY)?;
        }
        Ok(())
    }
}

/// Recorder encoding term frequencies as well as positions.
#[repr(C, packed)]
pub struct TFAndPositionRecorder {
    stack: ExpUnrolledLinkedList,
    current_doc: DocId,
}

impl HeapAllocable for TFAndPositionRecorder {
    fn with_addr(addr: u32) -> TFAndPositionRecorder {
        TFAndPositionRecorder {
            stack: ExpUnrolledLinkedList::with_addr(addr),
            current_doc: u32::max_value(),
        }
    }
}

impl Recorder for TFAndPositionRecorder {
    fn current_doc(&self) -> DocId {
        self.current_doc
    }

    fn new_doc(&mut self, doc: DocId, heap: &Heap) {
        self.current_doc = doc;
        self.stack.push(doc, heap);
    }

    fn record_position(&mut self, position: u32, heap: &Heap) {
        self.stack.push(position, heap);
    }

    fn close_doc(&mut self, heap: &Heap) {
        self.stack.push(POSITION_END, heap);
    }

    fn serialize(&self,
                 self_addr: u32,
                 serializer: &mut PostingsSerializer,
                 heap: &Heap)
                 -> io::Result<()> {
        let mut doc_positions = Vec::with_capacity(100);
        let mut positions_iter = self.stack.iter(self_addr, heap);
        while let Some(doc) = positions_iter.next() {
            let mut prev_position = 0;
            doc_positions.clear();
            for position in &mut positions_iter {
                if position == POSITION_END {
                    break;
                } else {
                    doc_positions.push(position - prev_position);
                    prev_position = position;
                }
            }
            try!(serializer.write_doc(doc, doc_positions.len() as u32, &doc_positions));
        }
        Ok(())
    }
}