forked from nervosnetwork/ckb
/
diskdb.rs
213 lines (188 loc) · 7.09 KB
/
diskdb.rs
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
use crate::batch::{Batch, Col, Operation};
use crate::config::DBConfig;
use crate::kvdb::{ErrorKind, KeyValueDB, Result};
use log::warn;
use rocksdb::{ColumnFamily, Options, WriteBatch, DB};
use std::ops::Range;
struct Inner {
db: DB,
cfnames: Vec<String>,
}
pub struct RocksDB {
inner: Inner,
}
impl RocksDB {
pub fn open(config: &DBConfig, columns: u32) -> Self {
let mut opts = Options::default();
opts.create_if_missing(true);
opts.create_missing_column_families(true);
let cfnames: Vec<_> = (0..columns).map(|c| format!("c{}", c)).collect();
let cf_options: Vec<&str> = cfnames.iter().map(|n| n as &str).collect();
let db = DB::open_cf(&opts, &config.path, &cf_options).unwrap_or_else(|err| {
if err.as_ref().starts_with("Corruption:") {
warn!("Try repairing the rocksdb since {} ...", err);
let mut repair_opts = Options::default();
repair_opts.create_if_missing(false);
repair_opts.create_missing_column_families(false);
DB::repair(repair_opts, &config.path)
.unwrap_or_else(|err| panic!("Failed to repair the rocksdb: {}", err));
warn!("Try opening the repaired rocksdb ...");
DB::open_cf(&opts, &config.path, &cf_options)
.unwrap_or_else(|err| panic!("Failed to open the repaired rocksdb: {}", err))
} else {
panic!("Failed to open rocksdb: {}", err);
}
});
if let Some(db_opt) = config.options.as_ref() {
let rocksdb_options: Vec<(&str, &str)> = db_opt
.iter()
.map(|(k, v)| (k.as_str(), v.as_str()))
.collect();
db.set_options(&rocksdb_options)
.expect("Failed to set rocksdb option");
}
let inner = Inner {
db,
cfnames: cfnames.clone(),
};
RocksDB { inner }
}
fn cf_handle(&self, col: Option<u32>) -> Result<Option<ColumnFamily>> {
if let Some(col) = col {
self.inner
.cfnames
.get(col as usize)
.ok_or_else(|| ErrorKind::DBError(format!("column {:?} not found ", col)))
.map(|cfname| self.inner.db.cf_handle(&cfname))
} else {
Ok(None)
}
}
}
impl KeyValueDB for RocksDB {
fn write(&self, batch: Batch) -> Result<()> {
let mut wb = WriteBatch::default();
for op in batch.operations {
match op {
Operation::Insert { col, key, value } => match self.cf_handle(col)? {
Some(cf) => wb.put_cf(cf, &key, &value),
None => wb.put(&key, &value),
},
Operation::Delete { col, key } => match self.cf_handle(col)? {
None => wb.delete(&key),
Some(cf) => wb.delete_cf(cf, &key),
},
}?;
}
self.inner.db.write(wb)?;
Ok(())
}
fn read(&self, col: Col, key: &[u8]) -> Result<Option<Vec<u8>>> {
match self.cf_handle(col)? {
Some(cf) => self.inner.db.get_cf(cf, &key),
None => self.inner.db.get(&key),
}
.map(|v| v.map(|vi| vi.to_vec()))
.map_err(Into::into)
}
fn partial_read(&self, col: Col, key: &[u8], range: &Range<usize>) -> Result<Option<Vec<u8>>> {
match self.cf_handle(col)? {
Some(cf) => self.inner.db.get_pinned_cf(cf, &key),
None => self.inner.db.get_pinned(&key),
}
.map(|v| v.and_then(|vi| vi.get(range.start..range.end).map(|slice| slice.to_vec())))
.map_err(Into::into)
}
fn iter(&self, col: Col, key: &[u8]) -> Option<DBIterator> {
self.cf_handle(col).expect("invalid col").map(|cf| {
self.inner
.db
.iterator_cf(cf, IteratorMode::From(key, Direction::Forward))
.expect("invalid iterator")
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
use tempfile;
fn setup_db(prefix: &str, columns: u32) -> RocksDB {
let tmp_dir = tempfile::Builder::new().prefix(prefix).tempdir().unwrap();
let config = DBConfig {
path: tmp_dir.as_ref().to_path_buf(),
..Default::default()
};
RocksDB::open(&config, columns)
}
#[test]
fn test_set_rocksdb_options() {
let tmp_dir = tempfile::Builder::new()
.prefix("test_set_rocksdb_options")
.tempdir()
.unwrap();
let config = DBConfig {
path: tmp_dir.as_ref().to_path_buf(),
options: Some({
let mut opts = HashMap::new();
opts.insert("disable_auto_compactions".to_owned(), "true".to_owned());
opts
}),
};
RocksDB::open(&config, 2); // no panic
}
#[test]
#[should_panic]
fn test_panic_on_invalid_rocksdb_options() {
let tmp_dir = tempfile::Builder::new()
.prefix("test_panic_on_invalid_rocksdb_options")
.tempdir()
.unwrap();
let config = DBConfig {
path: tmp_dir.as_ref().to_path_buf(),
options: Some({
let mut opts = HashMap::new();
opts.insert("letsrock".to_owned(), "true".to_owned());
opts
}),
};
RocksDB::open(&config, 2); // panic
}
#[test]
fn write_and_read() {
let db = setup_db("write_and_read", 2);
let mut batch = Batch::default();
batch.insert(None, vec![0, 0], vec![0, 0, 0]);
batch.insert(Some(1), vec![1, 1], vec![1, 1, 1]);
db.write(batch).unwrap();
assert_eq!(Some(vec![0, 0, 0]), db.read(None, &[0, 0]).unwrap());
assert_eq!(None, db.read(None, &[1, 1]).unwrap());
assert_eq!(None, db.read(Some(1), &[0, 0]).unwrap());
assert_eq!(Some(vec![1, 1, 1]), db.read(Some(1), &[1, 1]).unwrap());
// return err when col doesn't exist
assert!(db.read(Some(2), &[0, 0]).is_err());
}
#[test]
fn write_and_partial_read() {
let db = setup_db("write_and_partial_read", 2);
let mut batch = Batch::default();
batch.insert(None, vec![0, 0], vec![5, 4, 3, 2]);
batch.insert(Some(1), vec![1, 1], vec![1, 2, 3, 4, 5]);
db.write(batch).unwrap();
assert_eq!(
Some(vec![2, 3, 4]),
db.partial_read(Some(1), &[1, 1], &(1..4)).unwrap()
);
assert_eq!(None, db.partial_read(Some(1), &[0, 0], &(1..4)).unwrap());
// return None when invalid range is passed
assert_eq!(None, db.partial_read(Some(1), &[1, 1], &(2..8)).unwrap());
// range must be increasing
assert_eq!(None, db.partial_read(Some(1), &[1, 1], &(3..0)).unwrap());
// return err when col doesn't exist
assert!(db.partial_read(Some(2), &[0, 0], &(0..1)).is_err());
assert_eq!(
Some(vec![4, 3, 2]),
db.partial_read(None, &[0, 0], &(1..4)).unwrap()
);
}
}