-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathlib.rs
More file actions
247 lines (205 loc) · 6.33 KB
/
Copy pathlib.rs
File metadata and controls
247 lines (205 loc) · 6.33 KB
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
use std::{
fmt::Debug,
iter::Iterator,
marker::PhantomData,
path::{Path, PathBuf},
sync::Arc,
};
use anyhow::format_err;
use bincode;
use rocksdb::{
BlockBasedOptions, Cache, ColumnFamily, ColumnFamilyDescriptor, DBIterator, IteratorMode,
Options, DB,
};
use serde::{de::DeserializeOwned, Serialize};
pub type Result<T> = result::Result<T, DBError>;
fn get_db_options() -> Options {
let mut options = Options::default();
options.create_if_missing(true);
options.create_missing_column_families(true);
let n = num_cpus::get();
options.increase_parallelism(n as i32);
let mut env = rocksdb::Env::new().unwrap();
// While a compaction is ongoing, all the background threads
// could be used by the compaction. This can stall writes which
// need to flush the memtable. Add some high-priority background threads
// which can service these writes.
env.set_high_priority_background_threads(4);
options.set_env(&env);
// Set max total wal size to 4G.
options.set_max_total_wal_size(4 * 1024 * 1024 * 1024);
let cache = Cache::new_lru_cache(512 * 1024 * 1024);
let mut block_opts = BlockBasedOptions::default();
block_opts.set_block_cache(&cache);
block_opts.set_bloom_filter(10.0, false);
options.set_block_based_table_factory(&block_opts);
options
}
#[derive(Debug)]
pub struct Rocks(DB);
impl Rocks {
pub fn open(path: &PathBuf, cf_names: &[&'static str]) -> Result<Self> {
let cf_descriptors = cf_names
.into_iter()
.map(|cf_name| ColumnFamilyDescriptor::new(*cf_name, Options::default()));
let db_opts = get_db_options();
let db = DB::open_cf_descriptors(&db_opts, path, cf_descriptors)?;
Ok(Self(db))
}
#[allow(dead_code)]
fn destroy(path: &Path) -> Result<()> {
DB::destroy(&Options::default(), path)?;
Ok(())
}
pub fn cf_handle(&self, cf: &str) -> Result<&ColumnFamily> {
self.0
.cf_handle(cf)
.ok_or_else(|| DBError::Custom(format_err!("Unknown column: {}", cf)))
}
fn get_cf(&self, cf: &ColumnFamily, key: &[u8]) -> Result<Option<Vec<u8>>> {
let opt = self.0.get_cf(cf, key)?;
Ok(opt)
}
fn put_cf(&self, cf: &ColumnFamily, key: &[u8], value: &[u8]) -> Result<()> {
self.0.put_cf(cf, key, value)?;
Ok(())
}
fn delete_cf(&self, cf: &ColumnFamily, key: &[u8]) -> Result<()> {
self.0.delete_cf(cf, key)?;
Ok(())
}
pub fn iterator_cf<C>(&self, cf: &ColumnFamily, iterator_mode: IteratorMode) -> DBIterator
where
C: Column,
{
self.0.iterator_cf(cf, iterator_mode)
}
pub fn flush(&self) -> Result<()> {
self.0.flush()?;
Ok(())
}
}
//
// ColumnName trait. Define associated column family NAME
//
pub trait ColumnName {
const NAME: &'static str;
}
//
// Column trait. Define associated index type
//
pub trait Column {
type Index: Debug + Serialize + DeserializeOwned;
fn key(index: &Self::Index) -> Result<Vec<u8>> {
bincode::serialize(index).map_err(DBError::Bincode)
}
fn get_key(raw_key: Box<[u8]>) -> Result<Self::Index> {
bincode::deserialize(&raw_key).map_err(DBError::Bincode)
}
}
//
// TypedColumn trait. Define associated value type
//
pub trait TypedColumn: Column {
type Type: Serialize + DeserializeOwned + Debug;
}
#[derive(Debug, Clone)]
pub struct LedgerColumn<C>
where
C: Column + ColumnName,
{
pub backend: Arc<Rocks>,
pub column: PhantomData<C>,
}
impl<C> LedgerColumn<C>
where
C: Column + ColumnName,
{
pub fn get_bytes(&self, key: &C::Index) -> Result<Option<Vec<u8>>> {
self.backend.get_cf(self.handle()?, &C::key(key)?)
}
pub fn put_bytes(&self, key: &C::Index, value: &[u8]) -> Result<()> {
self.backend.put_cf(self.handle()?, &C::key(key)?, value)
}
pub fn handle(&self) -> Result<&ColumnFamily> {
self.backend.cf_handle(C::NAME)
}
}
impl<C> LedgerColumn<C>
where
C: TypedColumn + ColumnName,
{
pub fn get(&self, key: &C::Index) -> Result<Option<C::Type>> {
if let Some(serialized_value) = self.get_bytes(key)? {
let value = bincode::deserialize(&serialized_value)?;
Ok(Some(value))
} else {
Ok(None)
}
}
pub fn put(&self, key: &C::Index, value: &C::Type) -> Result<()> {
let serialized_value = bincode::serialize(value)?;
self.put_bytes(key, &serialized_value)
}
pub fn delete(&self, key: &C::Index) -> Result<()> {
self.backend.delete_cf(self.handle()?, &C::key(key)?)
}
pub fn iter(
&self,
from: Option<C::Index>,
) -> Result<impl Iterator<Item = Result<(C::Index, C::Type)>> + '_> {
let index = from
.as_ref()
.map(|i| C::key(i))
.transpose()?
.unwrap_or_default();
let iterator_mode = from.map_or(IteratorMode::End, |_| {
IteratorMode::From(&index, rocksdb::Direction::Reverse)
});
Ok(self
.backend
.iterator_cf::<C>(self.handle()?, iterator_mode)
.map(|k| {
let (key, value) = k?;
let value = bincode::deserialize(&value)?;
let key = C::get_key(key)?;
Ok((key, value))
}))
}
}
use std::{error::Error, fmt, result};
use bincode::Error as BincodeError;
use rocksdb::Error as RocksDBError;
#[derive(Debug)]
pub enum DBError {
RocksDBError(RocksDBError),
Bincode(BincodeError),
ParseKey,
Custom(anyhow::Error),
}
impl fmt::Display for DBError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DBError::RocksDBError(e) => write!(f, "RocksDB Error: {e}"),
DBError::Bincode(e) => write!(f, "Bincode Error: {e}"),
DBError::ParseKey => write!(f, "Error parsing key"),
DBError::Custom(e) => write!(f, "Custom Error: {e}"),
}
}
}
impl Error for DBError {}
impl From<RocksDBError> for DBError {
fn from(e: RocksDBError) -> Self {
DBError::RocksDBError(e)
}
}
impl From<BincodeError> for DBError {
fn from(e: BincodeError) -> Self {
DBError::Bincode(e)
}
}
impl From<anyhow::Error> for DBError {
fn from(e: anyhow::Error) -> Self {
DBError::Custom(e)
}
}