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
use crate::cf_options::RocksColumnFamilyOptions;
use crate::db_options::RocksDBOptions;
use crate::engine::RocksEngine;
use crate::raw_util::new_engine as new_engine_raw;
use crate::raw_util::new_engine_opt as new_engine_opt_raw;
use crate::raw_util::CFOptions;
use crate::rocks_metrics_defs::*;
use engine_traits::Engines;
use engine_traits::Range;
use engine_traits::CF_DEFAULT;
use engine_traits::{Error, Result};
use rocksdb::Range as RocksRange;
use rocksdb::{CFHandle, SliceTransform, DB};
use std::str::FromStr;
use std::sync::Arc;
use tikv_util::box_err;
pub fn new_temp_engine(path: &tempfile::TempDir) -> Engines<RocksEngine, RocksEngine> {
let raft_path = path.path().join(std::path::Path::new("raft"));
Engines::new(
new_engine(
path.path().to_str().unwrap(),
None,
engine_traits::ALL_CFS,
None,
)
.unwrap(),
new_engine(
raft_path.to_str().unwrap(),
None,
&[engine_traits::CF_DEFAULT],
None,
)
.unwrap(),
)
}
pub fn new_default_engine(path: &str) -> Result<RocksEngine> {
let engine =
new_engine_raw(path, None, &[CF_DEFAULT], None).map_err(|e| Error::Other(box_err!(e)))?;
let engine = Arc::new(engine);
let engine = RocksEngine::from_db(engine);
Ok(engine)
}
pub struct RocksCFOptions<'a> {
cf: &'a str,
options: RocksColumnFamilyOptions,
}
impl<'a> RocksCFOptions<'a> {
pub fn new(cf: &'a str, options: RocksColumnFamilyOptions) -> RocksCFOptions<'a> {
RocksCFOptions { cf, options }
}
pub fn into_raw(self) -> CFOptions<'a> {
CFOptions::new(self.cf, self.options.into_raw())
}
}
pub fn new_engine(
path: &str,
db_opts: Option<RocksDBOptions>,
cfs: &[&str],
opts: Option<Vec<RocksCFOptions<'_>>>,
) -> Result<RocksEngine> {
let db_opts = db_opts.map(RocksDBOptions::into_raw);
let opts = opts.map(|o| o.into_iter().map(RocksCFOptions::into_raw).collect());
let engine = new_engine_raw(path, db_opts, cfs, opts).map_err(|e| Error::Other(box_err!(e)))?;
let engine = Arc::new(engine);
let engine = RocksEngine::from_db(engine);
Ok(engine)
}
pub fn new_engine_opt(
path: &str,
db_opt: RocksDBOptions,
cfs_opts: Vec<RocksCFOptions<'_>>,
) -> Result<RocksEngine> {
let db_opt = db_opt.into_raw();
let cfs_opts = cfs_opts.into_iter().map(RocksCFOptions::into_raw).collect();
let engine =
new_engine_opt_raw(path, db_opt, cfs_opts).map_err(|e| Error::Other(box_err!(e)))?;
let engine = Arc::new(engine);
let engine = RocksEngine::from_db(engine);
Ok(engine)
}
pub fn get_cf_handle<'a>(db: &'a DB, cf: &str) -> Result<&'a CFHandle> {
let handle = db
.cf_handle(cf)
.ok_or_else(|| Error::Engine(format!("cf {} not found", cf)))?;
Ok(handle)
}
pub fn range_to_rocks_range<'a>(range: &Range<'a>) -> RocksRange<'a> {
RocksRange::new(range.start_key, range.end_key)
}
pub fn get_engine_cf_used_size(engine: &DB, handle: &CFHandle) -> u64 {
let mut cf_used_size = engine
.get_property_int_cf(handle, ROCKSDB_TOTAL_SST_FILES_SIZE)
.expect("rocksdb is too old, missing total-sst-files-size property");
if let Some(mem_table) = engine.get_property_int_cf(handle, ROCKSDB_CUR_SIZE_ALL_MEM_TABLES) {
cf_used_size += mem_table;
}
if let Some(live_blob) = engine.get_property_int_cf(handle, ROCKSDB_TITANDB_LIVE_BLOB_FILE_SIZE)
{
cf_used_size += live_blob;
}
if let Some(obsolete_blob) =
engine.get_property_int_cf(handle, ROCKSDB_TITANDB_OBSOLETE_BLOB_FILE_SIZE)
{
cf_used_size += obsolete_blob;
}
cf_used_size
}
pub fn get_engine_compression_ratio_at_level(
engine: &DB,
handle: &CFHandle,
level: usize,
) -> Option<f64> {
let prop = format!("{}{}", ROCKSDB_COMPRESSION_RATIO_AT_LEVEL, level);
if let Some(v) = engine.get_property_value_cf(handle, &prop) {
if let Ok(f) = f64::from_str(&v) {
if f >= 0.0 {
return Some(f);
}
}
}
None
}
pub fn get_cf_num_files_at_level(engine: &DB, handle: &CFHandle, level: usize) -> Option<u64> {
let prop = format!("{}{}", ROCKSDB_NUM_FILES_AT_LEVEL, level);
engine.get_property_int_cf(handle, &prop)
}
pub fn get_cf_num_blob_files_at_level(engine: &DB, handle: &CFHandle, level: usize) -> Option<u64> {
let prop = format!("{}{}", ROCKSDB_TITANDB_NUM_BLOB_FILES_AT_LEVEL, level);
engine.get_property_int_cf(handle, &prop)
}
pub fn get_cf_num_immutable_mem_table(engine: &DB, handle: &CFHandle) -> Option<u64> {
engine.get_property_int_cf(handle, ROCKSDB_NUM_IMMUTABLE_MEM_TABLE)
}
pub fn get_cf_compaction_pending_bytes(engine: &DB, handle: &CFHandle) -> Option<u64> {
engine.get_property_int_cf(handle, ROCKSDB_PENDING_COMPACTION_BYTES)
}
pub struct FixedSuffixSliceTransform {
pub suffix_len: usize,
}
impl FixedSuffixSliceTransform {
pub fn new(suffix_len: usize) -> FixedSuffixSliceTransform {
FixedSuffixSliceTransform { suffix_len }
}
}
impl SliceTransform for FixedSuffixSliceTransform {
fn transform<'a>(&mut self, key: &'a [u8]) -> &'a [u8] {
let mid = key.len() - self.suffix_len;
let (left, _) = key.split_at(mid);
left
}
fn in_domain(&mut self, key: &[u8]) -> bool {
key.len() >= self.suffix_len
}
fn in_range(&mut self, _: &[u8]) -> bool {
true
}
}
pub struct FixedPrefixSliceTransform {
pub prefix_len: usize,
}
impl FixedPrefixSliceTransform {
pub fn new(prefix_len: usize) -> FixedPrefixSliceTransform {
FixedPrefixSliceTransform { prefix_len }
}
}
impl SliceTransform for FixedPrefixSliceTransform {
fn transform<'a>(&mut self, key: &'a [u8]) -> &'a [u8] {
&key[..self.prefix_len]
}
fn in_domain(&mut self, key: &[u8]) -> bool {
key.len() >= self.prefix_len
}
fn in_range(&mut self, _: &[u8]) -> bool {
true
}
}
pub struct NoopSliceTransform;
impl SliceTransform for NoopSliceTransform {
fn transform<'a>(&mut self, key: &'a [u8]) -> &'a [u8] {
key
}
fn in_domain(&mut self, _: &[u8]) -> bool {
true
}
fn in_range(&mut self, _: &[u8]) -> bool {
true
}
}