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
use std::ffi::{CStr, CString};
use std::ops::Deref;
use crocksdb_ffi::{self, DBCompressionType, DBTitanBlobIndex, DBTitanDBOptions};
use librocksdb_sys::{ctitandb_encode_blob_index, DBTitanDBBlobRunMode};
use rocksdb::Cache;
use rocksdb_options::LRUCacheOptions;
use std::ops::DerefMut;
use std::os::raw::c_double;
use std::os::raw::c_int;
use std::ptr;
use std::slice;
pub struct TitanDBOptions {
pub inner: *mut DBTitanDBOptions,
}
impl TitanDBOptions {
pub fn new() -> Self {
unsafe {
Self {
inner: crocksdb_ffi::ctitandb_options_create(),
}
}
}
pub fn dirname(&self) -> &str {
unsafe {
let name = crocksdb_ffi::ctitandb_options_dirname(self.inner);
CStr::from_ptr(name).to_str().unwrap()
}
}
pub fn set_dirname(&mut self, name: &str) {
let s = CString::new(name).unwrap();
unsafe {
crocksdb_ffi::ctitandb_options_set_dirname(self.inner, s.as_ptr());
}
}
pub fn min_blob_size(&self) -> u64 {
unsafe { crocksdb_ffi::ctitandb_options_min_blob_size(self.inner) }
}
pub fn set_min_blob_size(&mut self, size: u64) {
unsafe {
crocksdb_ffi::ctitandb_options_set_min_blob_size(self.inner, size);
}
}
pub fn blob_file_compression(&self) -> DBCompressionType {
unsafe { crocksdb_ffi::ctitandb_options_blob_file_compression(self.inner) }
}
pub fn set_blob_file_compression(&mut self, t: DBCompressionType) {
unsafe {
crocksdb_ffi::ctitandb_options_set_blob_file_compression(self.inner, t);
}
}
pub fn set_compression_options(
&mut self,
window_bits: i32,
level: i32,
strategy: i32,
max_dict_bytes: i32,
zstd_max_train_bytes: i32,
) {
unsafe {
crocksdb_ffi::ctitandb_options_set_compression_options(
self.inner,
window_bits,
level,
strategy,
max_dict_bytes,
zstd_max_train_bytes,
)
}
}
pub fn set_disable_background_gc(&mut self, disable: bool) {
unsafe {
crocksdb_ffi::ctitandb_options_set_disable_background_gc(self.inner, disable);
}
}
pub fn set_level_merge(&mut self, enable: bool) {
unsafe {
crocksdb_ffi::ctitandb_options_set_level_merge(self.inner, enable);
}
}
pub fn set_range_merge(&mut self, enable: bool) {
unsafe {
crocksdb_ffi::ctitandb_options_set_range_merge(self.inner, enable);
}
}
pub fn set_max_sorted_runs(&mut self, size: i32) {
unsafe {
crocksdb_ffi::ctitandb_options_set_max_sorted_runs(self.inner, size);
}
}
pub fn set_max_background_gc(&mut self, size: i32) {
unsafe {
crocksdb_ffi::ctitandb_options_set_max_background_gc(self.inner, size);
}
}
pub fn set_purge_obsolete_files_period(&mut self, period: usize) {
unsafe {
crocksdb_ffi::ctitandb_options_set_purge_obsolete_files_period_sec(self.inner, period);
}
}
pub fn set_min_gc_batch_size(&mut self, size: u64) {
unsafe {
crocksdb_ffi::ctitandb_options_set_min_gc_batch_size(self.inner, size);
}
}
pub fn set_max_gc_batch_size(&mut self, size: u64) {
unsafe {
crocksdb_ffi::ctitandb_options_set_max_gc_batch_size(self.inner, size);
}
}
pub fn set_blob_cache(
&mut self,
size: usize,
shard_bits: c_int,
capacity_limit: bool,
pri_ratio: c_double,
) {
let mut cache_opt = LRUCacheOptions::new();
cache_opt.set_capacity(size);
cache_opt.set_num_shard_bits(shard_bits);
cache_opt.set_strict_capacity_limit(capacity_limit);
cache_opt.set_high_pri_pool_ratio(pri_ratio);
let cache = Cache::new_lru_cache(cache_opt);
unsafe {
crocksdb_ffi::ctitandb_options_set_blob_cache(self.inner, cache.inner);
}
}
pub fn set_discardable_ratio(&mut self, ratio: f64) {
unsafe {
crocksdb_ffi::ctitandb_options_set_discardable_ratio(self.inner, ratio);
}
}
pub fn set_sample_ratio(&mut self, ratio: f64) {
unsafe {
crocksdb_ffi::ctitandb_options_set_sample_ratio(self.inner, ratio);
}
}
pub fn set_merge_small_file_threshold(&mut self, size: u64) {
unsafe {
crocksdb_ffi::ctitandb_options_set_merge_small_file_threshold(self.inner, size);
}
}
pub fn set_blob_run_mode(&mut self, t: DBTitanDBBlobRunMode) {
unsafe {
crocksdb_ffi::ctitandb_options_set_blob_run_mode(self.inner, t);
}
}
pub fn set_gc_merge_rewrite(&mut self, enable: bool) {
unsafe {
crocksdb_ffi::ctitandb_options_set_gc_merge_rewrite(self.inner, enable);
}
}
}
impl Drop for TitanDBOptions {
fn drop(&mut self) {
unsafe {
crocksdb_ffi::ctitandb_options_destroy(self.inner);
}
}
}
#[derive(Debug, Default)]
pub struct TitanBlobIndex {
inner: DBTitanBlobIndex,
}
impl TitanBlobIndex {
pub fn decode(value: &[u8]) -> Result<Self, String> {
let mut index = Self::default();
unsafe {
ffi_try!(ctitandb_decode_blob_index(
value.as_ptr(),
value.len() as u64,
&mut index.inner as *mut DBTitanBlobIndex
));
}
Ok(index)
}
pub fn encode(&self) -> Vec<u8> {
let mut value = ptr::null_mut();
let mut value_size: u64 = 0;
unsafe {
ctitandb_encode_blob_index(&self.inner, &mut value, &mut value_size);
let slice = slice::from_raw_parts(value, value_size as usize);
let vec = slice.to_vec();
libc::free(value as *mut libc::c_void);
vec
}
}
}
impl Deref for TitanBlobIndex {
type Target = DBTitanBlobIndex;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl DerefMut for TitanBlobIndex {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}