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
//! Going through the records of a table, page by page.
use crate::error::{Error, Result};
use crate::record::{Met, RecordReader};
use crate::schema::TableDefinition;
use crate::store::{RowPage, Slot};
/// Owned scan cursor state, independent of borrows into the database.
pub(crate) struct ScannerState {
/// Why there is nothing to scan, until it has been reported once.
refusal: Option<Error>,
/// Index into the table's page list for the next page to open.
next_page: usize,
/// Currently open page and the number of its slots visited so far.
open: Option<OpenPage>,
/// Extension-page addresses already visited during this scan.
met: Met,
/// Set once a refusal stops the scan from reading any pages.
stopped: bool,
}
/// A page with the number of its slots that were gone through.
struct OpenPage {
page: RowPage,
done: usize,
}
impl ScannerState {
/// Creates a scan cursor for `definition`, or one that yields only `refusal`.
pub(crate) fn new(definition: Option<&TableDefinition>, refusal: Option<Error>) -> Self {
let refusal = refusal.or_else(|| definition.and_then(TableDefinition::unlisted));
let stopped = refusal.is_some() || definition.is_none();
Self {
refusal,
next_page: 0,
open: None,
met: Met::default(),
stopped,
}
}
/// Returns any pending refusal without opening pages.
pub(crate) fn refusal_only<T>(&mut self) -> Option<Result<T>> {
self.refusal.take().map(Err)
}
/// Advances to the next slot for which `read` produces a value.
pub(crate) fn next<'db, T>(
&mut self,
reader: &RecordReader<'db>,
read: impl Fn(&RecordReader<'db>, &RowPage, &Slot, &mut Met) -> Result<Option<T>>,
) -> Option<Result<T>> {
if let Some(error) = self.refusal.take() {
return Some(Err(error));
}
if self.stopped {
return None;
}
let pages = reader.definition().pages();
loop {
if let Some(OpenPage { page, done }) = &mut self.open {
while let Some(slot) = page.slots().get(*done) {
*done += 1;
let found = read(reader, page, slot, &mut self.met);
if let Some(found) = found.transpose() {
return Some(found);
}
}
self.open = None;
}
let &number = pages.get(self.next_page)?;
self.next_page += 1;
match reader.page(number) {
Ok(page) => self.open = Some(OpenPage { page, done: 0 }),
Err(problem) => {
return Some(Err(Error::TablePage {
table: reader.definition().name.clone(),
page: number,
problem,
}));
}
}
}
}
}
/// The occupied slots of a table's pages, in page and slot order.
pub(crate) struct Scanner<'db> {
reader: RecordReader<'db>,
state: ScannerState,
}
impl<'db> Scanner<'db> {
/// The slots of the table that `reader` reads, or only `refusal`.
pub(crate) fn new(reader: RecordReader<'db>, refusal: Option<Error>) -> Self {
let state = ScannerState::new(Some(reader.definition()), refusal);
Self { reader, state }
}
/// The next thing that `read` makes of a slot. A slot it makes nothing of
/// is passed over. After an error the scan goes on with the next slot, or
/// with the next page when a page cannot be opened.
pub(crate) fn next<T>(
&mut self,
read: impl Fn(&RecordReader<'db>, &RowPage, &Slot, &mut Met) -> Result<Option<T>>,
) -> Option<Result<T>> {
self.state.next(&self.reader, read)
}
}