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
use std::fmt;
use std::path::Path;
use std::ptr;
use std::slice;
use std::str;
use rocks_sys as ll;
use crate::env::{Env, Logger};
use crate::to_raw::ToRaw;
use crate::{Error, Result};
pub struct PersistentCache {
raw: *mut ll::rocks_persistent_cache_t,
}
impl ToRaw<ll::rocks_persistent_cache_t> for PersistentCache {
fn raw(&self) -> *mut ll::rocks_persistent_cache_t {
self.raw
}
}
impl Drop for PersistentCache {
fn drop(&mut self) {
unsafe { ll::rocks_persistent_cache_destroy(self.raw) }
}
}
impl Clone for PersistentCache {
fn clone(&self) -> Self {
PersistentCache {
raw: unsafe { ll::rocks_persistent_cache_clone(self.raw) },
}
}
}
impl fmt::Debug for PersistentCache {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("PersistentCache")
.field("options", &self.get_printable_options())
.finish()
}
}
impl PersistentCache {
pub fn new<P: AsRef<Path>>(
env: &Env,
path: P,
size: u64,
log: Option<&Logger>,
optimized_for_nvm: bool,
) -> Result<PersistentCache> {
let path_str = path.as_ref().to_str().expect("valid path");
let mut status = ptr::null_mut::<ll::rocks_status_t>();
unsafe {
let raw = ll::rocks_new_persistent_cache(
env.raw(),
path_str.as_ptr() as *const _,
path_str.len(),
size,
log.map(|logger| logger.raw()).unwrap_or_else(ptr::null_mut),
optimized_for_nvm as u8,
&mut status,
);
Error::from_ll(status).map(|()| PersistentCache { raw: raw })
}
}
pub fn get_printable_options(&self) -> String {
unsafe {
let cxx_string = ll::rocks_persistent_cache_get_printable_options(self.raw);
let ptr = ll::cxx_string_data(cxx_string) as *const u8;
let len = ll::cxx_string_size(cxx_string);
let ret = str::from_utf8_unchecked(slice::from_raw_parts(ptr, len)).into();
ll::cxx_string_destroy(cxx_string);
ret
}
}
}
#[test]
fn test_persistent_cache() {
let tmp_dir = ::tempdir::TempDir::new_in("", "rocks").unwrap();
let pcache = PersistentCache::new(Env::default_instance(), tmp_dir.path(), 1 << 30, None, true).unwrap();
assert!(format!("{:?}", pcache).contains("is_compressed: 1"));
}