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
use std::io::Write;
use crate::error::{Error, Result};
use crate::column::Salt;
use rand::Rng;
const CURRENT_VERSION: u32 = 3;
const LAST_SUPPORTED_VERSION: u32 = 2;
#[derive(Clone, Debug)]
pub struct Options {
pub path: std::path::PathBuf,
pub columns: Vec<ColumnOptions>,
pub sync: bool,
pub stats: bool,
}
#[derive(Clone, Debug)]
pub struct ColumnOptions {
pub preimage: bool,
pub uniform: bool,
pub sizes: [u16; 15],
pub ref_counted: bool,
}
impl ColumnOptions {
fn as_string(&self) -> String {
format!("preimage: {}, uniform: {}, refc: {}, sizes: [{}]",
self.preimage,
self.uniform,
self.ref_counted,
self.sizes.iter().fold(String::new(), |mut r, s| {
if !r.is_empty() {
r.push_str(", ");
}
r.push_str(&s.to_string()); r
})
)
}
}
impl Default for ColumnOptions {
fn default() -> ColumnOptions {
ColumnOptions {
preimage: false,
uniform: false,
ref_counted: false,
sizes: [96, 128, 192, 256, 320, 512, 768, 1024, 1536, 2048, 3072, 4096, 8192, 16384, 32768],
}
}
}
impl Options {
pub fn with_columns(path: &std::path::Path, num_columns: u8) -> Options {
Options {
path: path.into(),
sync: false,
stats: true,
columns: (0..num_columns).map(|_| Default::default()).collect(),
}
}
pub fn write_metadata(&self, path: &std::path::Path, salt: &Salt) -> Result<()> {
let mut file = std::fs::File::create(path)?;
writeln!(file, "version={}", CURRENT_VERSION)?;
writeln!(file, "salt={}", hex::encode(salt))?;
for i in 0..self.columns.len() {
writeln!(file, "col{}={}", i, self.columns[i].as_string())?;
}
Ok(())
}
pub fn load_and_validate_metadata(&self) -> Result<Option<Salt>> {
use std::io::BufRead;
use std::str::FromStr;
let mut path = self.path.clone();
path.push("metadata");
if !path.exists() {
let salt: Salt = rand::thread_rng().gen();
self.write_metadata(&path, &salt)?;
return Ok(Some(salt))
}
let file = std::io::BufReader::new(std::fs::File::open(path)?);
let mut salt = None;
for l in file.lines() {
let l = l?;
let mut vals = l.split("=");
let k = vals.next().ok_or(Error::Corruption("Bad metadata".into()))?;
let v = vals.next().ok_or(Error::Corruption("Bad metadata".into()))?;
if k == "version" {
let version = u32::from_str(v).map_err(|_| Error::Corruption("Bad version string".into()))?;
if version < LAST_SUPPORTED_VERSION {
return Err(Error::InvalidConfiguration(format!(
"Unsupported database version {}. Expected {}", version, CURRENT_VERSION)));
}
} else if k == "salt" {
let salt_slice = hex::decode(v).map_err(|_| Error::Corruption("Bad salt string".into()))?;
let mut s = Salt::default();
s.copy_from_slice(&salt_slice);
salt = Some(s);
} else if k.starts_with("col") {
let col_index = u8::from_str(&k[3..]).map_err(|_| Error::Corruption("Bad metadata column index".into()))?;
if col_index as usize > self.columns.len() {
return Err(Error::InvalidConfiguration(format!("Column config mismatch. Bad metadata column index: {}", col_index)));
}
let column_meta = self.columns[col_index as usize].as_string();
if column_meta != v {
return Err(Error::InvalidConfiguration(format!(
"Column config mismatch for column {}. Expected \"{}\", got \"{}\"",
col_index, v, column_meta)));
}
}
}
Ok(salt)
}
}