Merge pull request #46 from siddontang/master

Format  codes.
master
Tyler Neely 9 years ago
commit 5616f81d03
  1. 1
      src/comparator.rs
  2. 23
      src/ffi.rs
  3. 4
      src/lib.rs
  4. 31
      src/merge_operator.rs
  5. 80
      src/rocksdb.rs
  6. 16
      src/rocksdb_options.rs

@ -1,4 +1,3 @@
//
// Copyright 2014 Tyler Neely // Copyright 2014 Tyler Neely
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");

@ -1,4 +1,3 @@
//
// Copyright 2014 Tyler Neely // Copyright 2014 Tyler Neely
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
@ -101,7 +100,7 @@ pub fn error_message(ptr: *const i8) -> String {
// TODO audit the use of boolean arguments, b/c I think they need to be u8 // TODO audit the use of boolean arguments, b/c I think they need to be u8
// instead... // instead...
#[link(name = "rocksdb")] #[link(name = "rocksdb")]
extern { extern "C" {
pub fn rocksdb_options_create() -> DBOptions; pub fn rocksdb_options_create() -> DBOptions;
pub fn rocksdb_options_destroy(opts: DBOptions); pub fn rocksdb_options_destroy(opts: DBOptions);
pub fn rocksdb_cache_create_lru(capacity: size_t) -> DBCache; pub fn rocksdb_cache_create_lru(capacity: size_t) -> DBCache;
@ -376,15 +375,17 @@ extern {
// Comparator // Comparator
pub fn rocksdb_options_set_comparator(options: DBOptions, pub fn rocksdb_options_set_comparator(options: DBOptions,
cb: DBComparator); cb: DBComparator);
pub fn rocksdb_comparator_create( pub fn rocksdb_comparator_create(state: *mut c_void,
state: *mut c_void, destroy: extern "C" fn(*mut c_void) -> (),
destroy: extern fn(*mut c_void) -> (), compare: extern "C" fn(arg: *mut c_void,
compare: extern fn (arg: *mut c_void, a: *const c_char,
a: *const c_char, alen: size_t, alen: size_t,
b: *const c_char, blen: size_t b: *const c_char,
) -> c_int, blen: size_t)
name_fn: extern fn(*mut c_void) -> *const c_char -> c_int,
) -> DBComparator; name_fn: extern "C" fn(*mut c_void)
-> *const c_char)
-> DBComparator;
pub fn rocksdb_comparator_destroy(cmp: DBComparator); pub fn rocksdb_comparator_destroy(cmp: DBComparator);
// Column Family // Column Family

@ -1,4 +1,3 @@
//
// Copyright 2014 Tyler Neely // Copyright 2014 Tyler Neely
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
@ -15,7 +14,8 @@
// //
pub use ffi as rocksdb_ffi; pub use ffi as rocksdb_ffi;
pub use ffi::{DBCompactionStyle, DBComparator, new_bloom_filter}; pub use ffi::{DBCompactionStyle, DBComparator, new_bloom_filter};
pub use rocksdb::{DB, DBIterator, DBVector, Direction, Writable, WriteBatch, IteratorMode}; pub use rocksdb::{DB, DBIterator, DBVector, Direction, IteratorMode, Writable,
WriteBatch};
pub use rocksdb_options::{BlockBasedOptions, Options}; pub use rocksdb_options::{BlockBasedOptions, Options};
pub use merge_operator::MergeOperands; pub use merge_operator::MergeOperands;
pub mod rocksdb; pub mod rocksdb;

@ -1,4 +1,3 @@
//
// Copyright 2014 Tyler Neely // Copyright 2014 Tyler Neely
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
@ -133,21 +132,19 @@ impl<'a> Iterator for &'a mut MergeOperands {
fn next(&mut self) -> Option<&'a [u8]> { fn next(&mut self) -> Option<&'a [u8]> {
match self.cursor == self.num_operands { match self.cursor == self.num_operands {
true => None, true => None,
false => { false => unsafe {
unsafe { let base = self.operands_list as usize;
let base = self.operands_list as usize; let base_len = self.operands_list_len as usize;
let base_len = self.operands_list_len as usize; let spacing = mem::size_of::<*const *const u8>();
let spacing = mem::size_of::<*const *const u8>(); let spacing_len = mem::size_of::<*const size_t>();
let spacing_len = mem::size_of::<*const size_t>(); let len_ptr =
let len_ptr = (base_len + (spacing_len * self.cursor)) (base_len + (spacing_len * self.cursor)) as *const size_t;
as *const size_t; let len = *len_ptr as usize;
let len = *len_ptr as usize; let ptr = base + (spacing * self.cursor);
let ptr = base + (spacing * self.cursor); self.cursor += 1;
self.cursor += 1; Some(mem::transmute(slice::from_raw_parts(*(ptr as *const *const u8)
Some(mem::transmute(slice::from_raw_parts(*(ptr as *const *const u8)
as *const u8, len))) as *const u8, len)))
} },
}
} }
} }
@ -203,9 +200,7 @@ fn mergetest() {
None => println!("did not read valid utf-8 out of the db"), None => println!("did not read valid utf-8 out of the db"),
} }
} }
Err(e) => { Err(e) => println!("error reading value"),
println!("error reading value")
}
_ => panic!("value not present"), _ => panic!("value not present"),
} }

@ -1,4 +1,3 @@
//
// Copyright 2014 Tyler Neely // Copyright 2014 Tyler Neely
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
@ -70,8 +69,12 @@ impl<'a> Iterator for DBIterator<'a> {
let native_iter = self.inner; let native_iter = self.inner;
if !self.just_seeked { if !self.just_seeked {
match self.direction { match self.direction {
Direction::forward => unsafe { rocksdb_ffi::rocksdb_iter_next(native_iter) }, Direction::forward => unsafe {
Direction::reverse => unsafe { rocksdb_ffi::rocksdb_iter_prev(native_iter) }, rocksdb_ffi::rocksdb_iter_next(native_iter)
},
Direction::reverse => unsafe {
rocksdb_ffi::rocksdb_iter_prev(native_iter)
},
} }
} else { } else {
self.just_seeked = false; self.just_seeked = false;
@ -110,7 +113,10 @@ pub enum IteratorMode<'a> {
impl<'a> DBIterator<'a> { impl<'a> DBIterator<'a> {
fn new<'b>(db: &'a DB, readopts: &'b ReadOptions, mode: IteratorMode) -> DBIterator<'a> { fn new<'b>(db: &'a DB,
readopts: &'b ReadOptions,
mode: IteratorMode)
-> DBIterator<'a> {
unsafe { unsafe {
let iterator = rocksdb_ffi::rocksdb_create_iterator(db.inner, let iterator = rocksdb_ffi::rocksdb_create_iterator(db.inner,
readopts.inner); readopts.inner);
@ -134,11 +140,11 @@ impl<'a> DBIterator<'a> {
IteratorMode::Start => { IteratorMode::Start => {
rocksdb_ffi::rocksdb_iter_seek_to_first(self.inner); rocksdb_ffi::rocksdb_iter_seek_to_first(self.inner);
self.direction = Direction::forward; self.direction = Direction::forward;
}, }
IteratorMode::End => { IteratorMode::End => {
rocksdb_ffi::rocksdb_iter_seek_to_last(self.inner); rocksdb_ffi::rocksdb_iter_seek_to_last(self.inner);
self.direction = Direction::reverse; self.direction = Direction::reverse;
}, }
IteratorMode::From(key, dir) => { IteratorMode::From(key, dir) => {
rocksdb_ffi::rocksdb_iter_seek(self.inner, rocksdb_ffi::rocksdb_iter_seek(self.inner,
key.as_ptr(), key.as_ptr(),
@ -183,7 +189,7 @@ impl<'a> Drop for DBIterator<'a> {
} }
} }
impl <'a> Snapshot<'a> { impl<'a> Snapshot<'a> {
pub fn new(db: &DB) -> Snapshot { pub fn new(db: &DB) -> Snapshot {
let snapshot = unsafe { let snapshot = unsafe {
rocksdb_ffi::rocksdb_create_snapshot(db.inner) rocksdb_ffi::rocksdb_create_snapshot(db.inner)
@ -201,7 +207,7 @@ impl <'a> Snapshot<'a> {
} }
} }
impl <'a> Drop for Snapshot<'a> { impl<'a> Drop for Snapshot<'a> {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
rocksdb_ffi::rocksdb_release_snapshot(self.db.inner, self.inner); rocksdb_ffi::rocksdb_release_snapshot(self.db.inner, self.inner);
@ -244,16 +250,19 @@ impl DB {
-> Result<DB, String> { -> Result<DB, String> {
let cpath = match CString::new(path.as_bytes()) { let cpath = match CString::new(path.as_bytes()) {
Ok(c) => c, Ok(c) => c,
Err(_) => return Err("Failed to convert path to CString when \ Err(_) => {
opening rocksdb" return Err("Failed to convert path to CString when opening \
.to_string()), rocksdb"
.to_string())
}
}; };
let cpath_ptr = cpath.as_ptr(); let cpath_ptr = cpath.as_ptr();
let ospath = Path::new(path); let ospath = Path::new(path);
match fs::create_dir_all(&ospath) { match fs::create_dir_all(&ospath) {
Err(e) => Err(e) => {
return Err("Failed to create rocksdb directory.".to_string()), return Err("Failed to create rocksdb directory.".to_string())
}
Ok(_) => (), Ok(_) => (),
} }
@ -264,7 +273,9 @@ impl DB {
if cfs.len() == 0 { if cfs.len() == 0 {
unsafe { unsafe {
db = rocksdb_ffi::rocksdb_open(opts.inner, cpath_ptr as *const _, err_ptr); db = rocksdb_ffi::rocksdb_open(opts.inner,
cpath_ptr as *const _,
err_ptr);
} }
} else { } else {
let mut cfs_v = cfs.to_vec(); let mut cfs_v = cfs.to_vec();
@ -283,8 +294,8 @@ impl DB {
.collect(); .collect();
let cfnames: Vec<*const _> = c_cfs.iter() let cfnames: Vec<*const _> = c_cfs.iter()
.map(|cf| cf.as_ptr()) .map(|cf| cf.as_ptr())
.collect(); .collect();
// These handles will be populated by DB. // These handles will be populated by DB.
let mut cfhandles: Vec<rocksdb_ffi::DBCFHandle> = let mut cfhandles: Vec<rocksdb_ffi::DBCFHandle> =
@ -342,7 +353,9 @@ impl DB {
let mut err: *const i8 = 0 as *const i8; let mut err: *const i8 = 0 as *const i8;
let err_ptr: *mut *const i8 = &mut err; let err_ptr: *mut *const i8 = &mut err;
unsafe { unsafe {
rocksdb_ffi::rocksdb_destroy_db(opts.inner, cpath_ptr as *const _, err_ptr); rocksdb_ffi::rocksdb_destroy_db(opts.inner,
cpath_ptr as *const _,
err_ptr);
} }
if !err.is_null() { if !err.is_null() {
return Err(error_message(err)); return Err(error_message(err));
@ -358,7 +371,9 @@ impl DB {
let mut err: *const i8 = 0 as *const i8; let mut err: *const i8 = 0 as *const i8;
let err_ptr: *mut *const i8 = &mut err; let err_ptr: *mut *const i8 = &mut err;
unsafe { unsafe {
rocksdb_ffi::rocksdb_repair_db(opts.inner, cpath_ptr as *const _, err_ptr); rocksdb_ffi::rocksdb_repair_db(opts.inner,
cpath_ptr as *const _,
err_ptr);
} }
if !err.is_null() { if !err.is_null() {
return Err(error_message(err)); return Err(error_message(err));
@ -411,9 +426,7 @@ impl DB {
} }
match val.is_null() { match val.is_null() {
true => Ok(None), true => Ok(None),
false => { false => Ok(Some(DBVector::from_c(val, val_len))),
Ok(Some(DBVector::from_c(val, val_len)))
}
} }
} }
} }
@ -450,9 +463,7 @@ impl DB {
} }
match val.is_null() { match val.is_null() {
true => Ok(None), true => Ok(None),
false => { false => Ok(Some(DBVector::from_c(val, val_len))),
Ok(Some(DBVector::from_c(val, val_len)))
}
} }
} }
} }
@ -463,9 +474,11 @@ impl DB {
-> Result<DBCFHandle, String> { -> Result<DBCFHandle, String> {
let cname = match CString::new(name.as_bytes()) { let cname = match CString::new(name.as_bytes()) {
Ok(c) => c, Ok(c) => c,
Err(_) => return Err("Failed to convert path to CString when \ Err(_) => {
opening rocksdb" return Err("Failed to convert path to CString when opening \
.to_string()), rocksdb"
.to_string())
}
}; };
let cname_ptr = cname.as_ptr(); let cname_ptr = cname.as_ptr();
let mut err: *const i8 = 0 as *const i8; let mut err: *const i8 = 0 as *const i8;
@ -514,7 +527,10 @@ impl DB {
DBIterator::new(&self, &opts, mode) DBIterator::new(&self, &opts, mode)
} }
pub fn iterator_cf(&self, cf_handle: DBCFHandle, mode: IteratorMode) -> Result<DBIterator, String> { pub fn iterator_cf(&self,
cf_handle: DBCFHandle,
mode: IteratorMode)
-> Result<DBIterator, String> {
let opts = ReadOptions::new(); let opts = ReadOptions::new();
DBIterator::new_cf(&self, cf_handle, &opts, mode) DBIterator::new_cf(&self, cf_handle, &opts, mode)
} }
@ -841,9 +857,11 @@ fn errors_do_stuff() {
let opts = Options::new(); let opts = Options::new();
// The DB will still be open when we try to destroy and the lock should fail // The DB will still be open when we try to destroy and the lock should fail
match DB::destroy(&opts, path) { match DB::destroy(&opts, path) {
Err(ref s) => assert!(s == Err(ref s) => {
"IO error: lock _rust_rocksdb_error/LOCK: No \ assert!(s ==
locks available"), "IO error: lock _rust_rocksdb_error/LOCK: No locks \
available")
}
Ok(_) => panic!("should fail"), Ok(_) => panic!("should fail"),
} }
} }

@ -1,4 +1,3 @@
//
// Copyright 2014 Tyler Neely // Copyright 2014 Tyler Neely
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
@ -127,7 +126,10 @@ impl Options {
pub fn add_merge_operator<'a>(&mut self, pub fn add_merge_operator<'a>(&mut self,
name: &str, name: &str,
merge_fn: fn(&[u8], Option<&[u8]>, &mut MergeOperands) -> Vec<u8>) { 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,
@ -180,10 +182,12 @@ impl Options {
pub fn set_use_fsync(&mut self, useit: bool) { pub fn set_use_fsync(&mut self, useit: bool) {
unsafe { unsafe {
match useit { match useit {
true => true => {
rocksdb_ffi::rocksdb_options_set_use_fsync(self.inner, 1), rocksdb_ffi::rocksdb_options_set_use_fsync(self.inner, 1)
false => }
rocksdb_ffi::rocksdb_options_set_use_fsync(self.inner, 0), false => {
rocksdb_ffi::rocksdb_options_set_use_fsync(self.inner, 0)
}
} }
} }
} }

Loading…
Cancel
Save