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"
[dependencies]
libc = "0.1.8"
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 @@
// See the License for the specific language governing permissions and
// 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::str::from_utf8;
......
......@@ -18,15 +18,15 @@ extern crate libc;
#[cfg(test)]
extern crate tempdir;
pub extern crate librocksdb_sys;
pub mod rocksdb;
pub mod ffi;
pub mod rocksdb_options;
pub mod merge_operator;
pub mod comparator;
pub use ffi::{DBCompactionStyle, DBComparator, new_bloom_filter,
self as rocksdb_ffi};
pub use librocksdb_sys::{DBCompactionStyle, DBComparator, new_bloom_filter,
self as rocksdb_ffi};
pub use rocksdb::{DB, DBIterator, DBVector, Direction, IteratorMode, Writable,
WriteBatch};
pub use rocksdb_options::{BlockBasedOptions, Options, WriteOptions};
......
......@@ -19,9 +19,11 @@ use std::ptr;
use std::slice;
pub type MergeFn = fn(&[u8], Option<&[u8]>, &mut MergeOperands) -> Vec<u8>;
pub struct MergeOperatorCallback {
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) {
......@@ -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,
raw_key: *const c_char,
key_len: size_t,
......@@ -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,
raw_key: *const c_char,
key_len: size_t,
......
......@@ -59,12 +59,17 @@ pub enum Direction {
Reverse,
}
// TODO: should we use Vec<u8> instead?
pub type Kv = (Box<[u8]>, Box<[u8]>);
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;
if !self.just_seeked {
if self.just_seeked {
self.just_seeked = false;
} else {
match self.direction {
Direction::Forward => unsafe {
rocksdb_ffi::rocksdb_iter_next(native_iter)
......@@ -73,8 +78,6 @@ impl Iterator for DBIterator {
rocksdb_ffi::rocksdb_iter_prev(native_iter)
},
}
} else {
self.just_seeked = false;
}
if unsafe { rocksdb_ffi::rocksdb_iter_valid(native_iter) } {
let mut key_len: size_t = 0;
......@@ -240,9 +243,9 @@ pub trait Writable {
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> {
start_key: &'a [u8],
end_key: &'a [u8],
......@@ -336,14 +339,14 @@ impl DB {
.collect();
// 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 nfam = cfs_v.len();
unsafe {
db = rocksdb_ffi::rocksdb_open_column_families(opts.inner, cpath_ptr as *const _,
nfam as c_int,
cfnames.as_ptr() as *const _,
copts, handles, err_ptr);
cfopts_ptr, handles, err_ptr);
}
for handle in &cfhandles {
......@@ -763,32 +766,36 @@ impl DB {
.map(|x| x.end_key.len() as u64)
.collect();
let mut sizes: Vec<u64> = vec![0; ranges.len()];
let (n, sk_ptr, skl_ptr, ek_ptr, ekl_ptr, s_ptr) =
(ranges.len() as i32,
start_keys.as_ptr(),
start_key_lens.as_ptr(),
end_keys.as_ptr(),
end_key_lens.as_ptr(),
sizes.as_mut_ptr());
let (n,
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_key_lens.as_ptr(),
end_keys.as_ptr(),
end_key_lens.as_ptr(),
sizes.as_mut_ptr());
match cf {
None => unsafe {
rocksdb_ffi::rocksdb_approximate_sizes(self.inner,
n,
sk_ptr,
skl_ptr,
ek_ptr,
ekl_ptr,
s_ptr)
start_key_ptr,
start_key_len_ptr,
end_key_ptr,
end_key_len_ptr,
size_ptr)
},
Some(cf) => unsafe {
rocksdb_ffi::rocksdb_approximate_sizes_cf(self.inner,
cf,
n,
sk_ptr,
skl_ptr,
ek_ptr,
ekl_ptr,
s_ptr)
start_key_ptr,
start_key_len_ptr,
end_key_ptr,
end_key_len_ptr,
size_ptr)
},
}
sizes
......@@ -829,14 +836,20 @@ impl Writable for DB {
}
}
impl WriteBatch {
pub fn new() -> WriteBatch {
impl Default for WriteBatch {
fn default() -> WriteBatch {
WriteBatch {
inner: unsafe { rocksdb_ffi::rocksdb_writebatch_create() },
}
}
}
impl WriteBatch {
pub fn new() -> WriteBatch {
WriteBatch::default()
}
}
impl Drop for WriteBatch {
fn drop(&mut self) {
unsafe { rocksdb_ffi::rocksdb_writebatch_destroy(self.inner) }
......@@ -935,12 +948,18 @@ impl Drop for ReadOptions {
}
}
impl ReadOptions {
fn new() -> ReadOptions {
impl Default for ReadOptions {
fn default() -> ReadOptions {
unsafe {
ReadOptions { inner: rocksdb_ffi::rocksdb_readoptions_create() }
}
}
}
impl ReadOptions {
fn new() -> ReadOptions {
ReadOptions::default()
}
// TODO add snapshot setting here
// TODO add snapshot wrapper structs with proper destructors;
// that struct needs an "iterator" impl too.
......
......@@ -17,9 +17,10 @@ use std::ffi::CString;
use std::mem;
use rocksdb_ffi;
use merge_operator::{self, MergeOperands, MergeOperatorCallback,
full_merge_callback, partial_merge_callback};
use merge_operator::{self, MergeOperatorCallback, full_merge_callback,
partial_merge_callback};
use comparator::{self, ComparatorCallback, compare_callback};
use merge_operator::MergeFn;
pub struct BlockBasedOptions {
inner: rocksdb_ffi::DBBlockBasedTableOptions,
......@@ -57,8 +58,8 @@ impl Drop for WriteOptions {
}
}
impl BlockBasedOptions {
pub fn new() -> BlockBasedOptions {
impl Default for BlockBasedOptions {
fn default() -> BlockBasedOptions {
let block_opts = unsafe {
rocksdb_ffi::rocksdb_block_based_options_create()
};
......@@ -68,6 +69,12 @@ impl BlockBasedOptions {
}
BlockBasedOptions { inner: block_opts }
}
}
impl BlockBasedOptions {
pub fn new() -> BlockBasedOptions {
BlockBasedOptions::default()
}
pub fn set_block_size(&mut self, size: u64) {
unsafe {
......@@ -85,24 +92,24 @@ impl BlockBasedOptions {
// / }
// /}
/// /pub fn set_cache(&mut self, cache: rocksdb_ffi::DBCache) {
/// / unsafe {
/// / rocksdb_ffi::rocksdb_block_based_options_set_block_cache(
/// / self.inner, cache);
/// / }
/// /}
// /pub fn set_cache(&mut self, cache: rocksdb_ffi::DBCache) {
// / unsafe {
// / rocksdb_ffi::rocksdb_block_based_options_set_block_cache(
// / self.inner, cache);
// / }
// /}
/// /pub fn set_cache_compressed(&mut self, cache: rocksdb_ffi::DBCache) {
/// / unsafe {
/// / rocksdb_ffi::
/// rocksdb_block_based_options_set_block_cache_compressed(
/// / self.inner, cache);
/// / }
/// /}
// /pub fn set_cache_compressed(&mut self, cache: rocksdb_ffi::DBCache) {
// / unsafe {
// / rocksdb_ffi::
// rocksdb_block_based_options_set_block_cache_compressed(
// / self.inner, cache);
// / }
// /}
impl Options {
pub fn new() -> Options {
impl Default for Options {
fn default() -> Options {
unsafe {
let opts = rocksdb_ffi::rocksdb_options_create();
let rocksdb_ffi::DBOptions(opt_ptr) = opts;
......@@ -112,6 +119,12 @@ impl Options {
Options { inner: opts }
}
}
}
impl Options {
pub fn new() -> Options {
Options::default()
}
pub fn increase_parallelism(&mut self, parallelism: i32) {
unsafe {
......@@ -135,12 +148,7 @@ impl Options {
}
}
pub fn add_merge_operator(&mut self,
name: &str,
merge_fn: fn(&[u8],
Option<&[u8]>,
&mut MergeOperands)
-> Vec<u8>) {
pub fn add_merge_operator(&mut self, name: &str, merge_fn: MergeFn) {
let cb = Box::new(MergeOperatorCallback {
name: CString::new(name.as_bytes()).unwrap(),
merge_fn: merge_fn,
......@@ -304,13 +312,10 @@ impl Options {
pub fn set_disable_auto_compactions(&mut self, disable: bool) {
unsafe {
match disable {
true =>
rocksdb_ffi::rocksdb_options_set_disable_auto_compactions(
self.inner, 1),
false =>
rocksdb_ffi::rocksdb_options_set_disable_auto_compactions(
self.inner, 0),
if disable {
rocksdb_ffi::rocksdb_options_set_disable_auto_compactions(self.inner, 1)
} else {
rocksdb_ffi::rocksdb_options_set_disable_auto_compactions(self.inner, 0)
}
}
}
......@@ -323,8 +328,8 @@ impl Options {
}
}
impl WriteOptions {
pub fn new() -> WriteOptions {
impl Default for WriteOptions {
fn default() -> WriteOptions {
let write_opts = unsafe { rocksdb_ffi::rocksdb_writeoptions_create() };
let rocksdb_ffi::DBWriteOptions(opt_ptr) = write_opts;
if opt_ptr.is_null() {
......@@ -332,6 +337,12 @@ impl WriteOptions {
}
WriteOptions { inner: write_opts }
}
}
impl WriteOptions {
pub fn new() -> WriteOptions {
WriteOptions::default()
}
pub fn set_sync(&mut self, sync: bool) {
unsafe {
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