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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//! A file's pages, with the key that decodes them.
use std::fmt;
use std::fs;
use std::path::Path;
use std::sync::{Arc, OnceLock};
use crate::cipher::StoreKey;
use crate::error::{Error, PageProblem, Result};
use crate::identity::Identity;
use crate::open_options::OpenOptions;
use crate::page_size::PAGE_SIZE;
use crate::store::key_choice;
use crate::store::{
Page, PageCache, PageKind, PageState, RowPage, StoredPages, Superblock, TextEncoding,
};
/// The pages of a database file, decoded on request.
///
/// The file's bytes are held in memory and never change.
pub struct PageStore {
stored: StoredPages,
superblock: Superblock,
key: StoreKey,
encoding: TextEncoding,
longest_value: u32,
identity: OnceLock<Identity>,
cache: PageCache,
}
impl PageStore {
/// Opens the file at `path`, reads it into memory and finds its key.
///
/// # Errors
///
/// Returns [`Error::Read`] when the file cannot be read, and the errors
/// of [`Self::from_bytes`].
pub fn open(path: impl AsRef<Path>) -> Result<Self> {
Self::open_with(path, &OpenOptions::new())
}
/// Opens the file at `path`, as `options` say.
///
/// # Errors
///
/// Returns [`Error::Read`] when the file cannot be read, and the errors
/// of [`Self::from_bytes_with`].
pub fn open_with(path: impl AsRef<Path>, options: &OpenOptions) -> Result<Self> {
Self::from_bytes_with(fs::read(path)?, options)
}
/// Takes the bytes of a file and finds its key.
///
/// # Errors
///
/// Returns [`Error::Format`] for bytes that are not a SQL Anywhere 17
/// database, [`Error::Page`] for a damaged first page, [`Error::NoKey`] or
/// [`Error::AmbiguousKey`] when the pages do not pin one key, and
/// [`Error::UnsupportedEncoding`] for text in an encoding this reader does
/// not decode.
pub fn from_bytes(bytes: Vec<u8>) -> Result<Self> {
Self::from_bytes_with(bytes, &OpenOptions::new())
}
/// Takes the bytes of a file, as `options` say.
///
/// # Errors
///
/// Returns the errors of [`Self::from_bytes`], and [`Error::WrongKey`] when
/// the key of `options` does not fit the pages.
pub fn from_bytes_with(bytes: Vec<u8>, options: &OpenOptions) -> Result<Self> {
let (stored, superblock) = StoredPages::new(bytes)?;
let key = key_choice::choose(&stored, &superblock, options.key)?;
let first = stored.bytes(0).map_err(page_error(0))?;
let encoding = TextEncoding::detect(first)?;
Ok(Self {
stored,
superblock,
key,
encoding,
longest_value: options.longest_value,
identity: OnceLock::new(),
cache: PageCache::default(),
})
}
/// The size of the file in bytes.
#[must_use]
pub fn size(&self) -> u64 {
self.stored.size()
}
/// The bytes in a page of the file.
#[must_use]
pub fn page_size(&self) -> usize {
PAGE_SIZE
}
/// The number of pages in the file. The ones from
/// [`Superblock::database_page_count`] on are not the database's.
#[must_use]
pub fn page_count(&self) -> u32 {
self.stored.count()
}
/// What the first page says about the file.
#[must_use]
pub fn superblock(&self) -> &Superblock {
&self.superblock
}
/// The key that decodes the file's pages.
#[must_use]
pub fn key(&self) -> StoreKey {
self.key
}
/// What tells the file, and the state it is in, from others. It is worked
/// out the first time.
#[must_use]
pub fn identity(&self) -> Identity {
*self.identity.get_or_init(|| {
let first = self.stored.bytes(0).map_or(&[][..], |first| &first[..]);
let checksums = self.stored.checksums();
Identity::new(self.page_count(), self.superblock.lsn(), first, checksums)
})
}
/// The encoding of the file's character columns.
#[must_use]
pub fn text_encoding(&self) -> TextEncoding {
self.encoding
}
/// The bytes of the page at position `number` as the file stores them.
///
/// # Errors
///
/// Returns [`Error::Page`] when the file has no such page.
pub fn stored(&self, number: u32) -> Result<&[u8]> {
let stored = self.stored.bytes(number).map_err(page_error(number))?;
Ok(&stored[..])
}
/// Checks the page at position `number` against its checksum.
///
/// # Errors
///
/// Returns [`Error::Page`] when they differ, and when the file has no such
/// page.
pub fn check(&self, number: u32) -> Result<PageState> {
self.stored.check(number).map_err(page_error(number))
}
/// Decodes the page at position `number`, after its checksum passes.
///
/// The first page is not scrambled, and a page that was never written
/// holds nothing, so both come back as they are stored.
///
/// # Errors
///
/// Returns [`Error::Page`] when the checksum does not pass, and when the
/// file has no such page.
pub fn page(&self, number: u32) -> Result<Page> {
self.decoded(number).map_err(page_error(number))
}
/// The same page, for a reader that says itself what the page was for.
pub(crate) fn decoded(&self, number: u32) -> Result<Page, PageProblem> {
let state = self.stored.check(number)?;
let stored = self.stored.bytes(number)?;
let decoded = if number == 0 || state == PageState::Unwritten {
Box::new(*stored)
} else {
self.key.page_key(number).restore(stored)
};
Ok(Page::new(number, decoded))
}
/// The page of rows at position `number`, which must be of `kind` and
/// belong to the table with identifier `table_id`.
pub(crate) fn rows(
&self,
number: u32,
kind: PageKind,
table_id: u32,
) -> Result<RowPage, PageProblem> {
RowPage::new(self.decoded(number)?, kind, table_id)
}
/// The same page, kept for the next one who asks for it.
pub(crate) fn shared_rows(
&self,
number: u32,
kind: PageKind,
table_id: u32,
) -> Result<Arc<RowPage>, PageProblem> {
if let Some(kept) = self.cache.get(number) {
kept.page().expect(kind, table_id)?;
return Ok(kept);
}
let page = Arc::new(self.rows(number, kind, table_id)?);
self.cache.put(&page);
Ok(page)
}
/// The most bytes of a value that was stored compressed.
pub(crate) fn longest_value(&self) -> u32 {
self.longest_value
}
}
/// The error of a problem with the page at position `page`.
fn page_error(page: u32) -> impl Fn(PageProblem) -> Error {
move |problem| Error::Page { page, problem }
}
impl fmt::Debug for PageStore {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("PageStore")
.field("size", &self.size())
.field("superblock", &self.superblock)
.field("encoding", &self.encoding)
.finish_non_exhaustive()
}
}
#[cfg(test)]
mod tests {
use sa17_synthetic::{Cell, SyntheticColumn, SyntheticDatabase};
use sa17_synthetic::{SyntheticRow, SyntheticTable};
use super::*;
// A page that was read for one table is not another's because it is at hand.
#[test]
fn a_kept_page_is_checked_as_a_new_one_is() {
let table = SyntheticTable::new("seeds")
.column(SyntheticColumn::new("id", 2))
.row(SyntheticRow::new([Cell::integer(1)]));
let file = SyntheticDatabase::new().table(table).build();
let store = PageStore::from_bytes(file).unwrap();
let read = |kind, table_id| store.shared_rows(11, kind, table_id).err();
for _ in 0..2 {
assert_eq!(
read(PageKind::Table, 1001),
Some(PageProblem::WrongTable { table_id: 1000 })
);
assert!(matches!(
read(PageKind::Extension, 1000),
Some(PageProblem::WrongKind { .. })
));
assert_eq!(read(PageKind::Table, 1000), None);
}
}
}