Commit 443cb419 authored by Jay's avatar Jay

*: reorganize code (#6)

* seperate ffi crate
* fix clippy warning
parent cf4c00b2
...@@ -26,3 +26,6 @@ path = "test/test.rs" ...@@ -26,3 +26,6 @@ path = "test/test.rs"
[dependencies] [dependencies]
libc = "0.1.8" libc = "0.1.8"
tempdir = "0.3.4" tempdir = "0.3.4"
[dependencies.librocksdb_sys]
path = "librocksdb_sys"
[package]
name = "librocksdb_sys"
version = "0.1.0"
authors = ["Jay Lee <busyjaylee@gmail.com>"]
[dependencies]
libc = "0.1.8"
...@@ -12,7 +12,10 @@ ...@@ -12,7 +12,10 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// //
use libc::{self, c_char, c_int, c_void, size_t, uint64_t};
extern crate libc;
use libc::{c_char, c_int, c_void, size_t, uint64_t};
use std::ffi::CStr; use std::ffi::CStr;
use std::str::from_utf8; use std::str::from_utf8;
......
...@@ -18,14 +18,14 @@ extern crate libc; ...@@ -18,14 +18,14 @@ extern crate libc;
#[cfg(test)] #[cfg(test)]
extern crate tempdir; extern crate tempdir;
pub extern crate librocksdb_sys;
pub mod rocksdb; pub mod rocksdb;
pub mod ffi;
pub mod rocksdb_options; pub mod rocksdb_options;
pub mod merge_operator; pub mod merge_operator;
pub mod comparator; pub mod comparator;
pub use ffi::{DBCompactionStyle, DBComparator, new_bloom_filter, pub use librocksdb_sys::{DBCompactionStyle, DBComparator, new_bloom_filter,
self as rocksdb_ffi}; self as rocksdb_ffi};
pub use rocksdb::{DB, DBIterator, DBVector, Direction, IteratorMode, Writable, pub use rocksdb::{DB, DBIterator, DBVector, Direction, IteratorMode, Writable,
WriteBatch}; WriteBatch};
......
...@@ -19,9 +19,11 @@ use std::ptr; ...@@ -19,9 +19,11 @@ use std::ptr;
use std::slice; use std::slice;
pub type MergeFn = fn(&[u8], Option<&[u8]>, &mut MergeOperands) -> Vec<u8>;
pub struct MergeOperatorCallback { pub struct MergeOperatorCallback {
pub name: CString, pub name: CString,
pub merge_fn: fn(&[u8], Option<&[u8]>, &mut MergeOperands) -> Vec<u8>, pub merge_fn: MergeFn,
} }
pub extern "C" fn destructor_callback(raw_cb: *mut c_void) { pub extern "C" fn destructor_callback(raw_cb: *mut c_void) {
...@@ -39,6 +41,7 @@ pub extern "C" fn name_callback(raw_cb: *mut c_void) -> *const c_char { ...@@ -39,6 +41,7 @@ pub extern "C" fn name_callback(raw_cb: *mut c_void) -> *const c_char {
} }
} }
#[allow(too_many_arguments)]
pub extern "C" fn full_merge_callback(raw_cb: *mut c_void, pub extern "C" fn full_merge_callback(raw_cb: *mut c_void,
raw_key: *const c_char, raw_key: *const c_char,
key_len: size_t, key_len: size_t,
...@@ -72,6 +75,7 @@ pub extern "C" fn full_merge_callback(raw_cb: *mut c_void, ...@@ -72,6 +75,7 @@ pub extern "C" fn full_merge_callback(raw_cb: *mut c_void,
} }
} }
#[allow(too_many_arguments)]
pub extern "C" fn partial_merge_callback(raw_cb: *mut c_void, pub extern "C" fn partial_merge_callback(raw_cb: *mut c_void,
raw_key: *const c_char, raw_key: *const c_char,
key_len: size_t, key_len: size_t,
......
...@@ -59,12 +59,17 @@ pub enum Direction { ...@@ -59,12 +59,17 @@ pub enum Direction {
Reverse, Reverse,
} }
// TODO: should we use Vec<u8> instead?
pub type Kv = (Box<[u8]>, Box<[u8]>);
impl Iterator for DBIterator { impl Iterator for DBIterator {
type Item = (Box<[u8]>, Box<[u8]>); type Item = Kv;
fn next(&mut self) -> Option<(Box<[u8]>, Box<[u8]>)> { fn next(&mut self) -> Option<Kv> {
let native_iter = self.inner; let native_iter = self.inner;
if !self.just_seeked { if self.just_seeked {
self.just_seeked = false;
} else {
match self.direction { match self.direction {
Direction::Forward => unsafe { Direction::Forward => unsafe {
rocksdb_ffi::rocksdb_iter_next(native_iter) rocksdb_ffi::rocksdb_iter_next(native_iter)
...@@ -73,8 +78,6 @@ impl Iterator for DBIterator { ...@@ -73,8 +78,6 @@ impl Iterator for DBIterator {
rocksdb_ffi::rocksdb_iter_prev(native_iter) rocksdb_ffi::rocksdb_iter_prev(native_iter)
}, },
} }
} else {
self.just_seeked = false;
} }
if unsafe { rocksdb_ffi::rocksdb_iter_valid(native_iter) } { if unsafe { rocksdb_ffi::rocksdb_iter_valid(native_iter) } {
let mut key_len: size_t = 0; let mut key_len: size_t = 0;
...@@ -240,9 +243,9 @@ pub trait Writable { ...@@ -240,9 +243,9 @@ pub trait Writable {
fn delete_cf(&self, cf: DBCFHandle, key: &[u8]) -> Result<(), String>; fn delete_cf(&self, cf: DBCFHandle, key: &[u8]) -> Result<(), String>;
} }
/// A range of keys, start_key is included, but not end_key. /// A range of keys, `start_key` is included, but not `end_key`.
/// ///
/// You should make sure end_key is not less than start_key. /// You should make sure `end_key` is not less than `start_key`.
pub struct Range<'a> { pub struct Range<'a> {
start_key: &'a [u8], start_key: &'a [u8],
end_key: &'a [u8], end_key: &'a [u8],
...@@ -336,14 +339,14 @@ impl DB { ...@@ -336,14 +339,14 @@ impl DB {
.collect(); .collect();
// Prepare to ship to C. // Prepare to ship to C.
let copts: *const rocksdb_ffi::DBOptions = cfopts.as_ptr(); let cfopts_ptr: *const rocksdb_ffi::DBOptions = cfopts.as_ptr();
let handles: *const rocksdb_ffi::DBCFHandle = cfhandles.as_ptr(); let handles: *const rocksdb_ffi::DBCFHandle = cfhandles.as_ptr();
let nfam = cfs_v.len(); let nfam = cfs_v.len();
unsafe { unsafe {
db = rocksdb_ffi::rocksdb_open_column_families(opts.inner, cpath_ptr as *const _, db = rocksdb_ffi::rocksdb_open_column_families(opts.inner, cpath_ptr as *const _,
nfam as c_int, nfam as c_int,
cfnames.as_ptr() as *const _, cfnames.as_ptr() as *const _,
copts, handles, err_ptr); cfopts_ptr, handles, err_ptr);
} }
for handle in &cfhandles { for handle in &cfhandles {
...@@ -763,8 +766,12 @@ impl DB { ...@@ -763,8 +766,12 @@ impl DB {
.map(|x| x.end_key.len() as u64) .map(|x| x.end_key.len() as u64)
.collect(); .collect();
let mut sizes: Vec<u64> = vec![0; ranges.len()]; let mut sizes: Vec<u64> = vec![0; ranges.len()];
let (n, sk_ptr, skl_ptr, ek_ptr, ekl_ptr, s_ptr) = let (n,
(ranges.len() as i32, start_key_ptr,
start_key_len_ptr,
end_key_ptr,
end_key_len_ptr,
size_ptr) = (ranges.len() as i32,
start_keys.as_ptr(), start_keys.as_ptr(),
start_key_lens.as_ptr(), start_key_lens.as_ptr(),
end_keys.as_ptr(), end_keys.as_ptr(),
...@@ -774,21 +781,21 @@ impl DB { ...@@ -774,21 +781,21 @@ impl DB {
None => unsafe { None => unsafe {
rocksdb_ffi::rocksdb_approximate_sizes(self.inner, rocksdb_ffi::rocksdb_approximate_sizes(self.inner,
n, n,
sk_ptr, start_key_ptr,
skl_ptr, start_key_len_ptr,
ek_ptr, end_key_ptr,
ekl_ptr, end_key_len_ptr,
s_ptr) size_ptr)
}, },
Some(cf) => unsafe { Some(cf) => unsafe {
rocksdb_ffi::rocksdb_approximate_sizes_cf(self.inner, rocksdb_ffi::rocksdb_approximate_sizes_cf(self.inner,
cf, cf,
n, n,
sk_ptr, start_key_ptr,
skl_ptr, start_key_len_ptr,
ek_ptr, end_key_ptr,
ekl_ptr, end_key_len_ptr,
s_ptr) size_ptr)
}, },
} }
sizes sizes
...@@ -829,14 +836,20 @@ impl Writable for DB { ...@@ -829,14 +836,20 @@ impl Writable for DB {
} }
} }
impl WriteBatch { impl Default for WriteBatch {
pub fn new() -> WriteBatch { fn default() -> WriteBatch {
WriteBatch { WriteBatch {
inner: unsafe { rocksdb_ffi::rocksdb_writebatch_create() }, inner: unsafe { rocksdb_ffi::rocksdb_writebatch_create() },
} }
} }
} }
impl WriteBatch {
pub fn new() -> WriteBatch {
WriteBatch::default()
}
}
impl Drop for WriteBatch { impl Drop for WriteBatch {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { rocksdb_ffi::rocksdb_writebatch_destroy(self.inner) } unsafe { rocksdb_ffi::rocksdb_writebatch_destroy(self.inner) }
...@@ -935,12 +948,18 @@ impl Drop for ReadOptions { ...@@ -935,12 +948,18 @@ impl Drop for ReadOptions {
} }
} }
impl ReadOptions { impl Default for ReadOptions {
fn new() -> ReadOptions { fn default() -> ReadOptions {
unsafe { unsafe {
ReadOptions { inner: rocksdb_ffi::rocksdb_readoptions_create() } ReadOptions { inner: rocksdb_ffi::rocksdb_readoptions_create() }
} }
} }
}
impl ReadOptions {
fn new() -> ReadOptions {
ReadOptions::default()
}
// TODO add snapshot setting here // TODO add snapshot setting here
// TODO add snapshot wrapper structs with proper destructors; // TODO add snapshot wrapper structs with proper destructors;
// that struct needs an "iterator" impl too. // that struct needs an "iterator" impl too.
......
...@@ -17,9 +17,10 @@ use std::ffi::CString; ...@@ -17,9 +17,10 @@ use std::ffi::CString;
use std::mem; use std::mem;
use rocksdb_ffi; use rocksdb_ffi;
use merge_operator::{self, MergeOperands, MergeOperatorCallback, use merge_operator::{self, MergeOperatorCallback, full_merge_callback,
full_merge_callback, partial_merge_callback}; partial_merge_callback};
use comparator::{self, ComparatorCallback, compare_callback}; use comparator::{self, ComparatorCallback, compare_callback};
use merge_operator::MergeFn;
pub struct BlockBasedOptions { pub struct BlockBasedOptions {
inner: rocksdb_ffi::DBBlockBasedTableOptions, inner: rocksdb_ffi::DBBlockBasedTableOptions,
...@@ -57,8 +58,8 @@ impl Drop for WriteOptions { ...@@ -57,8 +58,8 @@ impl Drop for WriteOptions {
} }
} }
impl BlockBasedOptions { impl Default for BlockBasedOptions {
pub fn new() -> BlockBasedOptions { fn default() -> BlockBasedOptions {
let block_opts = unsafe { let block_opts = unsafe {
rocksdb_ffi::rocksdb_block_based_options_create() rocksdb_ffi::rocksdb_block_based_options_create()
}; };
...@@ -68,6 +69,12 @@ impl BlockBasedOptions { ...@@ -68,6 +69,12 @@ impl BlockBasedOptions {
} }
BlockBasedOptions { inner: block_opts } BlockBasedOptions { inner: block_opts }
} }
}
impl BlockBasedOptions {
pub fn new() -> BlockBasedOptions {
BlockBasedOptions::default()
}
pub fn set_block_size(&mut self, size: u64) { pub fn set_block_size(&mut self, size: u64) {
unsafe { unsafe {
...@@ -85,24 +92,24 @@ impl BlockBasedOptions { ...@@ -85,24 +92,24 @@ impl BlockBasedOptions {
// / } // / }
// /} // /}
/// /pub fn set_cache(&mut self, cache: rocksdb_ffi::DBCache) { // /pub fn set_cache(&mut self, cache: rocksdb_ffi::DBCache) {
/// / unsafe { // / unsafe {
/// / rocksdb_ffi::rocksdb_block_based_options_set_block_cache( // / rocksdb_ffi::rocksdb_block_based_options_set_block_cache(
/// / self.inner, cache); // / self.inner, cache);
/// / } // / }
/// /} // /}
/// /pub fn set_cache_compressed(&mut self, cache: rocksdb_ffi::DBCache) { // /pub fn set_cache_compressed(&mut self, cache: rocksdb_ffi::DBCache) {
/// / unsafe { // / unsafe {
/// / rocksdb_ffi:: // / rocksdb_ffi::
/// rocksdb_block_based_options_set_block_cache_compressed( // rocksdb_block_based_options_set_block_cache_compressed(
/// / self.inner, cache); // / self.inner, cache);
/// / } // / }
/// /} // /}
impl Options { impl Default for Options {
pub fn new() -> Options { fn default() -> Options {
unsafe { unsafe {
let opts = rocksdb_ffi::rocksdb_options_create(); let opts = rocksdb_ffi::rocksdb_options_create();
let rocksdb_ffi::DBOptions(opt_ptr) = opts; let rocksdb_ffi::DBOptions(opt_ptr) = opts;
...@@ -112,6 +119,12 @@ impl Options { ...@@ -112,6 +119,12 @@ impl Options {
Options { inner: opts } Options { inner: opts }
} }
} }
}
impl Options {
pub fn new() -> Options {
Options::default()
}
pub fn increase_parallelism(&mut self, parallelism: i32) { pub fn increase_parallelism(&mut self, parallelism: i32) {
unsafe { unsafe {
...@@ -135,12 +148,7 @@ impl Options { ...@@ -135,12 +148,7 @@ impl Options {
} }
} }
pub fn add_merge_operator(&mut self, pub fn add_merge_operator(&mut self, name: &str, merge_fn: MergeFn) {
name: &str,
merge_fn: fn(&[u8],
Option<&[u8]>,
&mut MergeOperands)
-> Vec<u8>) {
let cb = Box::new(MergeOperatorCallback { let cb = Box::new(MergeOperatorCallback {
name: CString::new(name.as_bytes()).unwrap(), name: CString::new(name.as_bytes()).unwrap(),
merge_fn: merge_fn, merge_fn: merge_fn,
...@@ -304,13 +312,10 @@ impl Options { ...@@ -304,13 +312,10 @@ impl Options {
pub fn set_disable_auto_compactions(&mut self, disable: bool) { pub fn set_disable_auto_compactions(&mut self, disable: bool) {
unsafe { unsafe {
match disable { if disable {
true => rocksdb_ffi::rocksdb_options_set_disable_auto_compactions(self.inner, 1)
rocksdb_ffi::rocksdb_options_set_disable_auto_compactions( } else {
self.inner, 1), rocksdb_ffi::rocksdb_options_set_disable_auto_compactions(self.inner, 0)
false =>
rocksdb_ffi::rocksdb_options_set_disable_auto_compactions(
self.inner, 0),
} }
} }
} }
...@@ -323,8 +328,8 @@ impl Options { ...@@ -323,8 +328,8 @@ impl Options {
} }
} }
impl WriteOptions { impl Default for WriteOptions {
pub fn new() -> WriteOptions { fn default() -> WriteOptions {
let write_opts = unsafe { rocksdb_ffi::rocksdb_writeoptions_create() }; let write_opts = unsafe { rocksdb_ffi::rocksdb_writeoptions_create() };
let rocksdb_ffi::DBWriteOptions(opt_ptr) = write_opts; let rocksdb_ffi::DBWriteOptions(opt_ptr) = write_opts;
if opt_ptr.is_null() { if opt_ptr.is_null() {
...@@ -332,6 +337,12 @@ impl WriteOptions { ...@@ -332,6 +337,12 @@ impl WriteOptions {
} }
WriteOptions { inner: write_opts } WriteOptions { inner: write_opts }
} }
}
impl WriteOptions {
pub fn new() -> WriteOptions {
WriteOptions::default()
}
pub fn set_sync(&mut self, sync: bool) { pub fn set_sync(&mut self, sync: bool) {
unsafe { unsafe {
rocksdb_ffi::rocksdb_writeoptions_set_sync(self.inner, sync); rocksdb_ffi::rocksdb_writeoptions_set_sync(self.inner, sync);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment