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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
use crate::{
column::{ColId, Salt},
compress::CompressionType,
error::{Error, Result},
};
use rand::Rng;
use std::{collections::HashMap, io::Write, path::Path};
pub const CURRENT_VERSION: u32 = 7;
const LAST_SUPPORTED_VERSION: u32 = 4;
#[derive(Clone, Debug)]
pub struct Options {
pub path: std::path::PathBuf,
pub columns: Vec<ColumnOptions>,
pub sync_wal: bool,
pub sync_data: bool,
pub stats: bool,
pub salt: Option<Salt>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ColumnOptions {
pub preimage: bool,
pub uniform: bool,
pub ref_counted: bool,
pub compression: CompressionType,
pub compression_threshold: u32,
pub btree_index: bool,
}
#[derive(Clone, Debug)]
pub struct Metadata {
pub salt: Salt,
pub version: u32,
pub columns: Vec<ColumnOptions>,
}
impl ColumnOptions {
fn as_string(&self) -> String {
format!(
"preimage: {}, uniform: {}, refc: {}, compression: {}, ordered: {}",
self.preimage, self.uniform, self.ref_counted, self.compression as u8, self.btree_index,
)
}
pub fn is_valid(&self) -> bool {
if self.btree_index && self.preimage {
log::error!(target: "parity-db", "Using `preimage` option on an ordered column is not supported");
return false
}
true
}
fn from_string(s: &str) -> Option<Self> {
let mut split = s.split("sizes: ");
let vals = split.next()?;
let vals: HashMap<&str, &str> = vals
.split(", ")
.filter_map(|s| {
let mut pair = s.split(": ");
Some((pair.next()?, pair.next()?))
})
.collect();
let preimage = vals.get("preimage")?.parse().ok()?;
let uniform = vals.get("uniform")?.parse().ok()?;
let ref_counted = vals.get("refc")?.parse().ok()?;
let compression: u8 = vals.get("compression").and_then(|c| c.parse().ok()).unwrap_or(0);
let btree_index = vals.get("ordered").and_then(|c| c.parse().ok()).unwrap_or(false);
Some(ColumnOptions {
preimage,
uniform,
ref_counted,
compression: compression.into(),
compression_threshold: ColumnOptions::default().compression_threshold,
btree_index,
})
}
}
impl Default for ColumnOptions {
fn default() -> ColumnOptions {
ColumnOptions {
preimage: false,
uniform: false,
ref_counted: false,
compression: CompressionType::NoCompression,
compression_threshold: 4096,
btree_index: false,
}
}
}
impl Options {
pub fn with_columns(path: &std::path::Path, num_columns: u8) -> Options {
Options {
path: path.into(),
sync_wal: true,
sync_data: true,
stats: true,
salt: None,
columns: (0..num_columns).map(|_| Default::default()).collect(),
}
}
pub fn write_metadata(&self, path: &std::path::Path, salt: &Salt) -> Result<()> {
let mut path = path.to_path_buf();
path.push("metadata");
self.write_metadata_file(&path, salt)
}
pub fn write_metadata_file(&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 write_metadata_with_version(
&self,
path: &std::path::Path,
salt: &Salt,
version: Option<u32>,
) -> Result<()> {
let mut path = path.to_path_buf();
path.push("metadata");
self.write_metadata_file_with_version(&path, salt, version)
}
pub fn write_metadata_file_with_version(
&self,
path: &std::path::Path,
salt: &Salt,
version: Option<u32>,
) -> Result<()> {
let version = version.unwrap_or(CURRENT_VERSION);
let mut file = std::fs::File::create(path)?;
writeln!(file, "version={}", 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, create: bool) -> Result<Metadata> {
let meta = Self::load_metadata(&self.path)?;
if let Some(meta) = meta {
if meta.columns.len() != self.columns.len() {
return Err(Error::InvalidConfiguration("Column config mismatch".into()))
}
for c in 0..meta.columns.len() {
if meta.columns[c] != self.columns[c] {
return Err(Error::IncompatibleColumnConfig {
id: c as ColId,
reason: format!(
"Column config mismatch. Expected \"{}\", got \"{}\"",
self.columns[c].as_string(),
meta.columns[c].as_string(),
),
})
}
}
Ok(meta)
} else if create {
let s: Salt = self.salt.unwrap_or_else(|| rand::thread_rng().gen());
self.write_metadata(&self.path, &s)?;
Ok(Metadata { version: CURRENT_VERSION, columns: self.columns.clone(), salt: s })
} else {
Err(Error::DatabaseNotFound)
}
}
pub fn load_metadata(path: &Path) -> Result<Option<Metadata>> {
let mut path = path.to_path_buf();
path.push("metadata");
Self::load_metadata_file(&path)
}
pub fn load_metadata_file(path: &Path) -> Result<Option<Metadata>> {
use std::{io::BufRead, str::FromStr};
if !path.exists() {
return Ok(None)
}
let file = std::io::BufReader::new(std::fs::File::open(path)?);
let mut salt = None;
let mut columns = Vec::new();
let mut version = 0;
for l in file.lines() {
let l = l?;
let mut vals = l.split('=');
let k = vals.next().ok_or_else(|| Error::Corruption("Bad metadata".into()))?;
let v = vals.next().ok_or_else(|| Error::Corruption("Bad metadata".into()))?;
if k == "version" {
version =
u32::from_str(v).map_err(|_| Error::Corruption("Bad version string".into()))?;
} 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 = ColumnOptions::from_string(v)
.ok_or_else(|| Error::Corruption("Bad column metadata".into()))?;
columns.push(col);
}
}
if version < LAST_SUPPORTED_VERSION {
return Err(Error::InvalidConfiguration(format!(
"Unsupported database version {}. Expected {}",
version, CURRENT_VERSION
)))
}
let salt = salt.ok_or_else(|| Error::InvalidConfiguration("Missing salt value".into()))?;
Ok(Some(Metadata { version, columns, salt }))
}
pub fn is_valid(&self) -> bool {
for option in self.columns.iter() {
if !option.is_valid() {
return false
}
}
true
}
}
impl Metadata {
pub fn columns_to_migrate(&self) -> std::collections::BTreeSet<u8> {
std::collections::BTreeSet::new()
}
}