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
use std::io::{self, Read, Seek, Write};
use crate::{BlockCompression, MetadataEntry, Page};
mod v1;
pub(crate) mod datablock;
#[derive(thiserror::Error, Debug)]
#[non_exhaustive]
pub enum PersistenceError {
#[error("I/O error: {0}.")]
Io(#[from] io::Error),
#[error("Integer number: {0}.")]
InvalidNumber(#[from] std::num::TryFromIntError),
#[error("Invalid magic number.")]
InvalidMagic,
#[error("The book contains too many pages.")]
TooManyPages,
#[error("Unable to load page index.")]
PageError(#[from] crate::page::PageError),
}
const MAGIC_SIZE: usize = 8;
pub(crate) fn load<I>(mut input: I) -> Result<crate::Book<I>, PersistenceError>
where
I: Read + Seek,
{
let mut magic = [0; MAGIC_SIZE];
input
.read_exact(&mut magic)
.map_err(|_| PersistenceError::InvalidMagic)?;
match &magic {
v1::MAGIC => v1::load(input),
_ => Err(PersistenceError::InvalidMagic),
}
}
pub(crate) fn dump<O>(
output: O,
pages: &[Page],
metadata: &[MetadataEntry],
compression: BlockCompression,
) -> Result<(), PersistenceError>
where
O: Write + Seek,
{
v1::dump(output, pages, metadata, compression)
}