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
use crate::{internal_error, Result};
use ckb_db_schema::Col;
use ckb_logger::info;
use rocksdb::ops::{GetColumnFamilys, GetPinned, GetPinnedCF, OpenCF};
use rocksdb::{DBPinnableSlice, Options, ReadOnlyDB as RawReadOnlyDB};
use std::path::Path;
use std::sync::Arc;
pub struct ReadOnlyDB {
pub(crate) inner: Arc<RawReadOnlyDB>,
}
impl ReadOnlyDB {
pub fn open_cf<P, I, N>(path: P, cf_names: I) -> Result<Option<Self>>
where
P: AsRef<Path>,
I: IntoIterator<Item = N>,
N: AsRef<str>,
{
let opts = Options::default();
RawReadOnlyDB::open_cf(&opts, path, cf_names).map_or_else(
|err| {
let err_str = err.as_ref();
if err_str.starts_with("IO error: No such file or directory")
{
Ok(None)
} else if err_str.starts_with("Corruption:") {
info!(
"DB corrupted: {}.\n\
Try ckb db-repair command to repair DB.\n\
Note: Currently there is a limitation that un-flushed column families will be lost after repair.\
This would happen even if the DB is in healthy state.\n\
See https://github.com/facebook/rocksdb/wiki/RocksDB-Repairer for detail",
err_str
);
Err(internal_error("DB corrupted"))
} else {
Err(internal_error(format!(
"failed to open the database: {}",
err
)))
}
},
|db| {
Ok(Some(ReadOnlyDB {
inner: Arc::new(db),
}))
},
)
}
pub fn get_pinned_default(&self, key: &[u8]) -> Result<Option<DBPinnableSlice>> {
self.inner.get_pinned(&key).map_err(internal_error)
}
pub fn get_pinned(&self, col: Col, key: &[u8]) -> Result<Option<DBPinnableSlice>> {
let cf = self
.inner
.cf_handle(col)
.ok_or_else(|| internal_error(format!("column {} not found", col)))?;
self.inner.get_pinned_cf(cf, &key).map_err(internal_error)
}
}