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
use leveldb_sys::*;
use self::options::{Options, c_options};
use self::error::Error;
use std::ffi::CString;
use std::path::Path;
use std::ptr;
use comparator::{Comparator, create_comparator};
pub mod options;
pub mod error;
pub mod iterator;
pub mod comparator;
pub mod snapshots;
pub mod cache;
pub mod kv;
pub mod batch;
pub mod management;
pub mod compaction;
#[allow(missing_docs)]
struct RawDB {
ptr: *mut leveldb_t,
}
#[allow(missing_docs)]
impl Drop for RawDB {
fn drop(&mut self) {
unsafe {
leveldb_close(self.ptr);
}
}
}
#[allow(missing_docs)]
struct RawComparator {
ptr: *mut leveldb_comparator_t,
}
impl Drop for RawComparator {
fn drop(&mut self) {
unsafe {
leveldb_comparator_destroy(self.ptr);
}
}
}
pub struct Database {
database: RawDB,
#[allow(dead_code)]
comparator: Option<RawComparator>,
#[allow(dead_code)]
options: Options,
}
unsafe impl Sync for Database {}
unsafe impl Send for Database {}
impl Database {
fn new(database: *mut leveldb_t,
options: Options,
comparator: Option<*mut leveldb_comparator_t>)
-> Database {
let raw_comp = match comparator {
Some(p) => Some(RawComparator { ptr: p }),
None => None,
};
Database {
database: RawDB { ptr: database },
comparator: raw_comp,
options: options,
}
}
pub fn open(name: &Path, options: Options) -> Result<Database, Error> {
let mut error = ptr::null_mut();
let res = unsafe {
let c_string = CString::new(name.to_str().unwrap()).unwrap();
let c_options = c_options(&options, None);
let db = leveldb_open(c_options as *const leveldb_options_t,
c_string.as_bytes_with_nul().as_ptr() as *const i8,
&mut error);
leveldb_options_destroy(c_options);
db
};
if error == ptr::null_mut() {
Ok(Database::new(res, options, None))
} else {
Err(Error::new_from_i8(error))
}
}
pub fn open_with_comparator<C: Comparator>(name: &Path,
options: Options,
comparator: C)
-> Result<Database, Error> {
let mut error = ptr::null_mut();
let comp_ptr = create_comparator(Box::new(comparator));
let res = unsafe {
let c_string = CString::new(name.to_str().unwrap()).unwrap();
let c_options = c_options(&options, Some(comp_ptr));
let db = leveldb_open(c_options as *const leveldb_options_t,
c_string.as_bytes_with_nul().as_ptr() as *const i8,
&mut error);
leveldb_options_destroy(c_options);
db
};
if error == ptr::null_mut() {
Ok(Database::new(res, options, Some(comp_ptr)))
} else {
Err(Error::new_from_i8(error))
}
}
}