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
use crate::{error::GenericError, persy::PersyImpl, IndexOpsError, IndexType, Persy, PersyId, PE};
use std::sync::Arc;

mod inspect_tree;
#[cfg(test)]
mod tests;

pub type TreeInspectorResult<T> = std::result::Result<T, GenericError>;

pub(crate) use inspect_tree::inspect_tree;
pub use inspect_tree::PrintTreeInspector;

pub trait TreeInspector<K, V> {
    fn start_node(&mut self, _node_id: PersyId, _prev: Option<K>, _next: Option<K>) -> TreeInspectorResult<()> {
        Ok(())
    }
    fn failed_load(&mut self, _node_id: PersyId) -> TreeInspectorResult<()> {
        Ok(())
    }
    fn end_node(&mut self, _node_id: PersyId) -> TreeInspectorResult<()> {
        Ok(())
    }
    fn start_leaf(&mut self, _node_id: PersyId, _prev: Option<K>, _next: Option<K>) -> TreeInspectorResult<()> {
        Ok(())
    }
    fn end_leaf(&mut self, _node_id: PersyId) -> TreeInspectorResult<()> {
        Ok(())
    }
    fn start_key(&mut self, _node_pos: u32, _k: K) -> TreeInspectorResult<()> {
        Ok(())
    }
    fn end_key(&mut self, _node_pos: u32, _k: K) -> TreeInspectorResult<()> {
        Ok(())
    }
    fn value(&mut self, _node_pos: u32, _v: V) -> TreeInspectorResult<()> {
        Ok(())
    }
    fn empty(&mut self) -> TreeInspectorResult<()> {
        Ok(())
    }
}

pub trait PersyInspect {
    fn inspect_tree<K, V, I>(&self, index_name: &str, inspector: &mut I) -> Result<(), PE<IndexOpsError>>
    where
        K: IndexType,
        V: IndexType,
        I: TreeInspector<K, V>;

    fn page_state_scan(&self) -> Result<PageStateIter, PE<GenericError>>;
}

pub struct PageState {
    position: u64,
    exp: u8,
    free: bool,
}

impl PageState {
    pub(crate) fn new(position: u64, exp: u8, free: bool) -> Self {
        Self { position, exp, free }
    }
    pub fn position(&self) -> u64 {
        self.position
    }
    pub fn size(&self) -> u64 {
        1 << self.exp
    }
    pub fn size_exp(&self) -> u8 {
        self.exp
    }
    pub fn is_free(&self) -> bool {
        self.free
    }
}

pub struct PageStateIter {
    persy_impl: Arc<PersyImpl>,
    next: u64,
}
impl PageStateIter {
    fn new(persy_impl: Arc<PersyImpl>) -> Self {
        Self { persy_impl, next: 0 }
    }
}
impl Iterator for PageStateIter {
    type Item = PageState;
    fn next(&mut self) -> Option<Self::Item> {
        let next = if self.next == 0 {
            Some(PageState::new(self.next, crate::persy::DEFAULT_PAGE_EXP, false))
        } else {
            self.persy_impl.page_state(self.next)
        };
        if let Some(n) = &next {
            self.next = n.position() + n.size();
        }
        next
    }
}

impl PersyInspect for Persy {
    fn inspect_tree<K, V, I>(&self, index_name: &str, inspector: &mut I) -> Result<(), PE<IndexOpsError>>
    where
        K: IndexType,
        V: IndexType,
        I: TreeInspector<K, V>,
    {
        let index_id = self
            .solve_index_id(index_name)
            .map_err(|e| PE::PE(IndexOpsError::from(e.error())))?;
        self.persy_impl
            .inspect_tree::<K::Wrapper, V::Wrapper, I>(index_id, inspector)?;
        Ok(())
    }

    fn page_state_scan(&self) -> Result<PageStateIter, PE<GenericError>> {
        Ok(PageStateIter::new(self.persy_impl.clone()))
    }
}