structdb/
iterator_single.rs

1use crate::{
2    record::SeqRecord,
3    table::Table,
4    topic::{TopicImpl, TOPIC_KEY_PREFIX},
5};
6
7pub struct IteratorSingle<'a, T>
8where
9    T: Table + 'a,
10{
11    pub topic: Box<&'a TopicImpl<T>>,
12    _state: rocksdb::DBRawIterator<'a>,
13}
14
15impl<'a, T> IteratorSingle<'a, T>
16where
17    T: Table,
18{
19    pub fn new(topic: Box<&'a TopicImpl<T>>) -> Self {
20        let _state = topic.table.prefix_iterator(TOPIC_KEY_PREFIX);
21
22        Self {
23            topic: topic,
24            _state: _state,
25        }
26    }
27}
28
29impl<'a, T> Iterator for IteratorSingle<'a, T>
30where
31    T: Table,
32{
33    type Item = SeqRecord;
34
35    fn next(&mut self) -> Option<Self::Item> {
36        let item = self._state.item();
37        if item.is_none() {
38            return None;
39        }
40
41        let record = SeqRecord::from(item);
42        if !record.is_valid() {
43            return None;
44        }
45
46        self._state.next();
47        Some(record)
48    }
49}