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
use super::*;

use epoch::pin;

/// An iterator over keys and values in a `Tree`.
pub struct Iter<'a> {
    pub(super) id: PageID,
    pub(super) inner:
        &'a PageCache<BLinkMaterializer, Frag, Vec<(PageID, PageID)>>,
    pub(super) last_key: Bound,
    pub(super) broken: Option<Error<()>>,
    pub(super) done: bool,
    // TODO we have to refactor this in light of pages being deleted
}

impl<'a> Iterator for Iter<'a> {
    type Item = DbResult<(Vec<u8>, Vec<u8>), ()>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.done {
            return None;
        } else if let Some(broken) = self.broken.take() {
            self.done = true;
            return Some(Err(broken));
        };

        let guard = pin();
        loop {
            let res = self.inner.get(self.id, &guard);
            if res.is_err() {
                // TODO this could be None if the node was removed since the last
                // iteration, and we need to just get the inner node again...
                error!("iteration failed: {:?}", res);
                self.done = true;
                return Some(Err(res.unwrap_err().danger_cast()));
            }

            let (frag, _cas_key) = res.unwrap().unwrap();
            let (node, _is_root) = frag.base().unwrap();
            for (ref k, ref v) in node.data.leaf().unwrap() {
                if Bound::Inc(k.clone()) > self.last_key {
                    self.last_key = Bound::Inc(k.to_vec());
                    let ret = Ok((k.clone(), v.clone()));
                    return Some(ret);
                }
            }
            if node.next.is_none() {
                return None;
            }
            self.id = node.next.unwrap();
        }
    }
}