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
//! How to open a file.
use crate::cipher::StoreKey;
/// The most bytes of a value that was stored compressed, unless a program says
/// otherwise: 16 MiB.
const LONGEST_VALUE: u32 = 16 << 20;
/// How to open a file, for a program that wants it otherwise than
/// [`Database::open`](crate::Database::open) does it.
///
/// ```no_run
/// use sa17::{Database, OpenOptions};
///
/// let options = OpenOptions::new().longest_value(256 << 20);
/// let database = Database::open_with("garden.db", &options)?;
/// # Ok::<(), sa17::Error>(())
/// ```
#[derive(Clone, Debug)]
pub struct OpenOptions {
pub(crate) key: Option<StoreKey>,
pub(crate) longest_value: u32,
}
impl OpenOptions {
/// Finds the key, and takes values of up to 16 MiB out of compression.
#[must_use]
pub fn new() -> Self {
Self {
key: None,
longest_value: LONGEST_VALUE,
}
}
/// Opens the file with `key`, and does not look for it.
#[must_use]
pub fn key(mut self, key: StoreKey) -> Self {
self.key = Some(key);
self
}
/// Takes values of up to `bytes` out of compression. A row with a longer
/// one cannot be read with it.
///
/// A few bytes can decompress to a thousand times as many, so this is what
/// bounds the memory that a hostile file can use.
#[must_use]
pub fn longest_value(mut self, bytes: u32) -> Self {
self.longest_value = bytes;
self
}
}
impl Default for OpenOptions {
fn default() -> Self {
Self::new()
}
}