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
//! Key-Value semantics.
use super::Database;
use super::slice::Slice;
use super::bytes::Bytes;
use super::error::Error;
use crate::binding::*;
use crate::options::{ReadOptions, WriteOptions, c_readoptions, c_writeoptions};
use libc::{c_char, size_t};
use std::ptr;
/// Key-Value-Access to the LevelDB instance, providing
/// a basic interface.
pub trait KV {
/// get a value from the LevelDB instance.
///
/// The passed key will be compared using the comparator.
fn get<'a>(
&self,
options: ReadOptions<'a>,
key: Slice,
) -> Result<Option<Slice<'static>>, Error>;
/// put a binary value into the LevelDB instance.
///
/// If the key is already present in the LevelDB instance, it will be overwritten.
///
/// The passed key will be compared using the comparator.
///
/// The LevelDB instance will be synced to disk if `options.sync == true`. This is
/// NOT the default.
fn put(&self, options: WriteOptions, key: Slice, value: Slice) -> Result<(), Error>;
/// delete a value from the LevelDB instance.
///
/// The passed key will be compared using the comparator.
///
/// The LevelDB instance will be synced to disk if `options.sync == true`. This is
/// NOT the default.
fn delete(&self, options: WriteOptions, key: Slice) -> Result<(), Error>;
}
impl KV for Database {
/// put a binary value into the LevelDB instance.
///
/// If the key is already present in the LevelDB instance, it will be overwritten.
///
/// The passed key will be compared using the comparator.
///
/// The LevelDB instance will be synced to disk if `options.sync == true`. This is
/// NOT the default.
fn put(&self, options: WriteOptions, key: Slice, value: Slice) -> Result<(), Error> {
unsafe {
let k = key.as_bytes();
let v = value.as_bytes();
let mut error = ptr::null_mut();
let c_writeoptions = c_writeoptions(options);
leveldb_put(
self.database.ptr,
c_writeoptions,
k.as_ptr() as *mut c_char,
k.len() as size_t,
v.as_ptr() as *mut c_char,
v.len() as size_t,
&mut error,
);
leveldb_writeoptions_destroy(c_writeoptions);
if error.is_null() {
Ok(())
} else {
Err(Error::new_from_char(error))
}
}
}
/// delete a value from the LevelDB instance.
///
/// The passed key will be compared using the comparator.
///
/// The LevelDB instance will be synced to disk if `options.sync == true`. This is
/// NOT the default.
fn delete(&self, options: WriteOptions, key: Slice) -> Result<(), Error> {
unsafe {
let k = key.as_bytes();
let mut error = ptr::null_mut();
let c_writeoptions = c_writeoptions(options);
leveldb_delete(
self.database.ptr,
c_writeoptions,
k.as_ptr() as *mut c_char,
k.len() as size_t,
&mut error,
);
leveldb_writeoptions_destroy(c_writeoptions);
if error.is_null() {
Ok(())
} else {
Err(Error::new_from_char(error))
}
}
}
fn get<'a>(
&self,
options: ReadOptions<'a>,
key: Slice,
) -> Result<Option<Slice<'static>>, Error> {
unsafe {
let k = key.as_bytes();
let mut error = ptr::null_mut();
let mut length: size_t = 0;
let c_readoptions = c_readoptions(&options);
let result = leveldb_get(
self.database.ptr,
c_readoptions,
k.as_ptr() as *mut c_char,
k.len() as size_t,
&mut length,
&mut error,
);
leveldb_readoptions_destroy(c_readoptions);
if error.is_null() {
if result.is_null() {
Ok(None)
} else if let Some(bytes) = Bytes::from_raw(result as *mut u8, length) {
Ok(Some(bytes.into()))
} else {
Ok(None)
}
} else {
Err(Error::new_from_char(error))
}
}
}
}