Commit edf9421f authored by Tyler Neely's avatar Tyler Neely

rustfmt

parent ad201263
...@@ -3,3 +3,7 @@ ...@@ -3,3 +3,7 @@
target target
Cargo.lock Cargo.lock
*.orig *.orig
*.bk
_rust_rocksdb*
*rlib
tags
reorder_imports = true
max_width = 80
ideal_width = 80
/* //
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");
you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
You may obtain a copy of the License at // You may obtain a copy of the License at
//
http://www.apache.org/licenses/LICENSE-2.0 // http://www.apache.org/licenses/LICENSE-2.0
//
Unless required by applicable law or agreed to in writing, software // Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, // distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
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.
*/ //
extern crate libc; extern crate libc;
use self::libc::{c_char, c_int, c_void, size_t}; use self::libc::{c_char, c_int, c_void, size_t};
use std::ffi::CString; use std::ffi::CString;
...@@ -30,7 +30,7 @@ pub struct ComparatorCallback { ...@@ -30,7 +30,7 @@ pub struct ComparatorCallback {
pub extern "C" fn destructor_callback(raw_cb: *mut c_void) { pub extern "C" fn destructor_callback(raw_cb: *mut c_void) {
// turn this back into a local variable so rust will reclaim it // turn this back into a local variable so rust will reclaim it
let _: Box<ComparatorCallback> = unsafe {mem::transmute(raw_cb)}; let _: Box<ComparatorCallback> = unsafe { mem::transmute(raw_cb) };
} }
pub extern "C" fn name_callback(raw_cb: *mut c_void) -> *const c_char { pub extern "C" fn name_callback(raw_cb: *mut c_void) -> *const c_char {
...@@ -51,8 +51,10 @@ pub extern "C" fn compare_callback(raw_cb: *mut c_void, ...@@ -51,8 +51,10 @@ pub extern "C" fn compare_callback(raw_cb: *mut c_void,
unsafe { unsafe {
let cb: &mut ComparatorCallback = let cb: &mut ComparatorCallback =
&mut *(raw_cb as *mut ComparatorCallback); &mut *(raw_cb as *mut ComparatorCallback);
let a: &[u8] = slice::from_raw_parts(a_raw as *const u8, a_len as usize); let a: &[u8] = slice::from_raw_parts(a_raw as *const u8,
let b: &[u8] = slice::from_raw_parts(b_raw as *const u8, b_len as usize); a_len as usize);
let b: &[u8] = slice::from_raw_parts(b_raw as *const u8,
b_len as usize);
(cb.f)(a, b) (cb.f)(a, b)
} }
} }
...@@ -67,9 +69,9 @@ fn test_reverse_compare(a: &[u8], b: &[u8]) -> c_int { ...@@ -67,9 +69,9 @@ fn test_reverse_compare(a: &[u8], b: &[u8]) -> c_int {
} }
} }
//#[allow(dead_code)] // #[allow(dead_code)]
//#[test] // #[test]
//fn compare_works() { // fn compare_works() {
// let path = "_rust_rocksdb_comparetest"; // let path = "_rust_rocksdb_comparetest";
// let mut opts = Options::new(); // let mut opts = Options::new();
// opts.create_if_missing(true); // opts.create_if_missing(true);
...@@ -79,4 +81,4 @@ fn test_reverse_compare(a: &[u8], b: &[u8]) -> c_int { ...@@ -79,4 +81,4 @@ fn test_reverse_compare(a: &[u8], b: &[u8]) -> c_int {
// // TODO add interesting test // // TODO add interesting test
// } // }
// assert!(DB::destroy(&opts, path).is_ok()); // assert!(DB::destroy(&opts, path).is_ok());
//} // }
This diff is collapsed.
/* //
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");
you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
You may obtain a copy of the License at // You may obtain a copy of the License at
//
http://www.apache.org/licenses/LICENSE-2.0 // http://www.apache.org/licenses/LICENSE-2.0
//
Unless required by applicable law or agreed to in writing, software // Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, // distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
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.
*/ //
pub use ffi as rocksdb_ffi; pub use ffi as rocksdb_ffi;
pub use ffi::{new_bloom_filter, DBCompactionStyle, DBComparator}; pub use ffi::{DBCompactionStyle, DBComparator, new_bloom_filter};
pub use rocksdb::{DB, DBVector, WriteBatch, Writable, Direction}; pub use rocksdb::{DB, DBVector, Direction, Writable, WriteBatch};
pub use rocksdb_options::{Options, BlockBasedOptions}; pub use rocksdb_options::{BlockBasedOptions, Options};
pub use merge_operator::MergeOperands; pub use merge_operator::MergeOperands;
pub mod rocksdb; pub mod rocksdb;
pub mod ffi; pub mod ffi;
......
/* //
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");
you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
You may obtain a copy of the License at // You may obtain a copy of the License at
//
http://www.apache.org/licenses/LICENSE-2.0 // http://www.apache.org/licenses/LICENSE-2.0
//
Unless required by applicable law or agreed to in writing, software // Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, // distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
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.
*/ //
extern crate rocksdb; extern crate rocksdb;
use rocksdb::{Options, DB, MergeOperands, Writable, }; use rocksdb::{DB, MergeOperands, Options, Writable};
//fn snapshot_test() { // fn snapshot_test() {
// let path = "_rust_rocksdb_iteratortest"; // let path = "_rust_rocksdb_iteratortest";
// { // {
// let mut db = DB::open_default(path).unwrap(); // let mut db = DB::open_default(path).unwrap();
...@@ -30,18 +30,21 @@ use rocksdb::{Options, DB, MergeOperands, Writable, }; ...@@ -30,18 +30,21 @@ use rocksdb::{Options, DB, MergeOperands, Writable, };
// let mut view1 = snap.iterator(); // let mut view1 = snap.iterator();
// println!("See the output of the first iter"); // println!("See the output of the first iter");
// for (k,v) in view1.from_start() { // for (k,v) in view1.from_start() {
// println!("Hello {}: {}", std::str::from_utf8(k).unwrap(), std::str::from_utf8(v).unwrap()); // println!("Hello {}: {}", std::str::from_utf8(k).unwrap(),
// std::str::from_utf8(v).unwrap());
// }; // };
// for (k,v) in view1.from_start() { // for (k,v) in view1.from_start() {
// println!("Hello {}: {}", std::str::from_utf8(k).unwrap(), std::str::from_utf8(v).unwrap()); // println!("Hello {}: {}", std::str::from_utf8(k).unwrap(),
// std::str::from_utf8(v).unwrap());
// }; // };
// for (k,v) in view1.from_end() { // for (k,v) in view1.from_end() {
// println!("Hello {}: {}", std::str::from_utf8(k).unwrap(), std::str::from_utf8(v).unwrap()); // println!("Hello {}: {}", std::str::from_utf8(k).unwrap(),
// std::str::from_utf8(v).unwrap());
// }; // };
// } // }
// let opts = Options::new(); // let opts = Options::new();
// assert!(DB::destroy(&opts, path).is_ok()); // assert!(DB::destroy(&opts, path).is_ok());
//} // }
#[cfg(not(feature = "valgrind"))] #[cfg(not(feature = "valgrind"))]
fn main() { fn main() {
...@@ -51,12 +54,10 @@ fn main() { ...@@ -51,12 +54,10 @@ fn main() {
match db.get(b"my key") { match db.get(b"my key") {
Ok(Some(value)) => { Ok(Some(value)) => {
match value.to_utf8() { match value.to_utf8() {
Some(v) => Some(v) => println!("retrieved utf8 value: {}", v),
println!("retrieved utf8 value: {}", v), 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) => println!("error retrieving value: {}", e), Err(e) => println!("error retrieving value: {}", e),
_ => panic!("value not present!"), _ => panic!("value not present!"),
} }
...@@ -66,8 +67,10 @@ fn main() { ...@@ -66,8 +67,10 @@ fn main() {
custom_merge(); custom_merge();
} }
fn concat_merge(_: &[u8], existing_val: Option<&[u8]>, fn concat_merge(_: &[u8],
operands: &mut MergeOperands) -> Vec<u8> { existing_val: Option<&[u8]>,
operands: &mut MergeOperands)
-> Vec<u8> {
let mut result: Vec<u8> = Vec::with_capacity(operands.size_hint().0); let mut result: Vec<u8> = Vec::with_capacity(operands.size_hint().0);
match existing_val { match existing_val {
Some(v) => for e in v { Some(v) => for e in v {
...@@ -99,10 +102,8 @@ fn custom_merge() { ...@@ -99,10 +102,8 @@ fn custom_merge() {
match db.get(b"k1") { match db.get(b"k1") {
Ok(Some(value)) => { Ok(Some(value)) => {
match value.to_utf8() { match value.to_utf8() {
Some(v) => Some(v) => println!("retrieved utf8 value: {}", v),
println!("retrieved utf8 value: {}", v), 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) => println!("error retrieving value: {}", e), Err(e) => println!("error retrieving value: {}", e),
...@@ -126,13 +127,14 @@ fn main() { ...@@ -126,13 +127,14 @@ fn main() {
db.merge(b"k1", b"d"); db.merge(b"k1", b"d");
db.merge(b"k1", b"efg"); db.merge(b"k1", b"efg");
db.merge(b"k1", b"h"); db.merge(b"k1", b"h");
db.get(b"k1").map( |value| { db.get(b"k1")
.map(|value| {
match value.to_utf8() { match value.to_utf8() {
Some(v) => (), Some(v) => (),
None => panic!("value corrupted"), None => panic!("value corrupted"),
} }
}) })
.or_else( |e| { panic!("error retrieving value: {}", e) }); .or_else(|e| panic!("error retrieving value: {}", e));
db.delete(b"k1"); db.delete(b"k1");
} }
} }
...@@ -142,10 +144,14 @@ fn main() { ...@@ -142,10 +144,14 @@ fn main() {
mod tests { mod tests {
use std::thread::sleep_ms; use std::thread::sleep_ms;
use rocksdb::{BlockBasedOptions, Options, DB, MergeOperands, new_bloom_filter, Writable }; use rocksdb::{BlockBasedOptions, DB, MergeOperands, Options, Writable,
new_bloom_filter};
use rocksdb::DBCompactionStyle::DBUniversalCompaction; use rocksdb::DBCompactionStyle::DBUniversalCompaction;
fn tuned_for_somebody_elses_disk(path: &str, opts: & mut Options, blockopts: &mut BlockBasedOptions) -> DB { fn tuned_for_somebody_elses_disk(path: &str,
opts: &mut Options,
blockopts: &mut BlockBasedOptions)
-> DB {
opts.create_if_missing(true); opts.create_if_missing(true);
opts.set_max_open_files(10000); opts.set_max_open_files(10000);
opts.set_use_fsync(false); opts.set_use_fsync(false);
...@@ -168,44 +174,44 @@ mod tests { ...@@ -168,44 +174,44 @@ mod tests {
opts.set_disable_auto_compactions(true); opts.set_disable_auto_compactions(true);
let filter = new_bloom_filter(10); let filter = new_bloom_filter(10);
//opts.set_filter(filter); // opts.set_filter(filter);
DB::open(&opts, path).unwrap() DB::open(&opts, path).unwrap()
} }
/* TODO(tyler) unstable // TODO(tyler) unstable
#[bench] // #[bench]
fn a_writes(b: &mut Bencher) { // fn a_writes(b: &mut Bencher) {
// dirty hack due to parallel tests causing contention. // dirty hack due to parallel tests causing contention.
sleep_ms(1000); // sleep_ms(1000);
let path = "_rust_rocksdb_optimizetest"; // let path = "_rust_rocksdb_optimizetest";
let mut opts = Options::new(); // let mut opts = Options::new();
let mut blockopts = BlockBasedOptions::new(); // let mut blockopts = BlockBasedOptions::new();
let mut db = tuned_for_somebody_elses_disk(path, &mut opts, &mut blockopts); // let mut db = tuned_for_somebody_elses_disk(path, &mut opts, &mut blockopts);
let mut i = 0 as u64; // let mut i = 0 as u64;
b.iter(|| { // b.iter(|| {
db.put(i.to_string().as_bytes(), b"v1111"); // db.put(i.to_string().as_bytes(), b"v1111");
i += 1; // i += 1;
}); // });
} // }
//
#[bench] // #[bench]
fn b_reads(b: &mut Bencher) { // fn b_reads(b: &mut Bencher) {
let path = "_rust_rocksdb_optimizetest"; // let path = "_rust_rocksdb_optimizetest";
let mut opts = Options::new(); // let mut opts = Options::new();
let mut blockopts = BlockBasedOptions::new(); // let mut blockopts = BlockBasedOptions::new();
{ // {
let db = tuned_for_somebody_elses_disk(path, &mut opts, &mut blockopts); // let db = tuned_for_somebody_elses_disk(path, &mut opts, &mut blockopts);
let mut i = 0 as u64; // let mut i = 0 as u64;
b.iter(|| { // b.iter(|| {
db.get(i.to_string().as_bytes()).on_error( |e| { // db.get(i.to_string().as_bytes()).on_error( |e| {
println!("error: {}", e); // println!("error: {}", e);
e // e
}); // });
i += 1; // i += 1;
}); // });
} // }
DB::destroy(&opts, path).is_ok(); // DB::destroy(&opts, path).is_ok();
} // }
*/ //
} }
/* //
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");
you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
You may obtain a copy of the License at // You may obtain a copy of the License at
//
http://www.apache.org/licenses/LICENSE-2.0 // http://www.apache.org/licenses/LICENSE-2.0
//
Unless required by applicable law or agreed to in writing, software // Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, // distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
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.
*/ //
extern crate libc; extern crate libc;
use self::libc::{c_char, c_int, c_void, size_t}; use self::libc::{c_char, c_int, c_void, size_t};
use std::ffi::CString; use std::ffi::CString;
...@@ -30,7 +30,7 @@ pub struct MergeOperatorCallback { ...@@ -30,7 +30,7 @@ pub struct MergeOperatorCallback {
pub extern "C" fn destructor_callback(raw_cb: *mut c_void) { pub extern "C" fn destructor_callback(raw_cb: *mut c_void) {
// turn this back into a local variable so rust will reclaim it // turn this back into a local variable so rust will reclaim it
let _: Box<MergeOperatorCallback> = unsafe {mem::transmute(raw_cb)}; let _: Box<MergeOperatorCallback> = unsafe { mem::transmute(raw_cb) };
} }
...@@ -57,17 +57,16 @@ pub extern "C" fn full_merge_callback(raw_cb: *mut c_void, ...@@ -57,17 +57,16 @@ pub extern "C" fn full_merge_callback(raw_cb: *mut c_void,
unsafe { unsafe {
let cb: &mut MergeOperatorCallback = let cb: &mut MergeOperatorCallback =
&mut *(raw_cb as *mut MergeOperatorCallback); &mut *(raw_cb as *mut MergeOperatorCallback);
let operands = let operands = &mut MergeOperands::new(operands_list,
&mut MergeOperands::new(operands_list,
operands_list_len, operands_list_len,
num_operands); num_operands);
let key: &[u8] = slice::from_raw_parts(raw_key as *const u8, key_len as usize); let key: &[u8] = slice::from_raw_parts(raw_key as *const u8,
key_len as usize);
let oldval: &[u8] = slice::from_raw_parts(existing_value as *const u8, let oldval: &[u8] = slice::from_raw_parts(existing_value as *const u8,
existing_value_len as usize); existing_value_len as usize);
let mut result = let mut result = (cb.merge_fn)(key, Some(oldval), operands);
(cb.merge_fn)(key, Some(oldval), operands);
result.shrink_to_fit(); result.shrink_to_fit();
//TODO(tan) investigate zero-copy techniques to improve performance // TODO(tan) investigate zero-copy techniques to improve performance
let buf = libc::malloc(result.len() as size_t); let buf = libc::malloc(result.len() as size_t);
assert!(!buf.is_null()); assert!(!buf.is_null());
*new_value_length = result.len() as size_t; *new_value_length = result.len() as size_t;
...@@ -92,10 +91,11 @@ pub extern "C" fn partial_merge_callback(raw_cb: *mut c_void, ...@@ -92,10 +91,11 @@ pub extern "C" fn partial_merge_callback(raw_cb: *mut c_void,
let operands = &mut MergeOperands::new(operands_list, let operands = &mut MergeOperands::new(operands_list,
operands_list_len, operands_list_len,
num_operands); num_operands);
let key: &[u8] = slice::from_raw_parts(raw_key as *const u8, key_len as usize); let key: &[u8] = slice::from_raw_parts(raw_key as *const u8,
key_len as usize);
let mut result = (cb.merge_fn)(key, None, operands); let mut result = (cb.merge_fn)(key, None, operands);
result.shrink_to_fit(); result.shrink_to_fit();
//TODO(tan) investigate zero-copy techniques to improve performance // TODO(tan) investigate zero-copy techniques to improve performance
let buf = libc::malloc(result.len() as size_t); let buf = libc::malloc(result.len() as size_t);
assert!(!buf.is_null()); assert!(!buf.is_null());
*new_value_length = 1 as size_t; *new_value_length = 1 as size_t;
...@@ -168,7 +168,7 @@ fn test_provided_merge(new_key: &[u8], ...@@ -168,7 +168,7 @@ fn test_provided_merge(new_key: &[u8],
for e in v { for e in v {
result.push(*e); result.push(*e);
} }
}, }
None => (), None => (),
} }
for op in operands { for op in operands {
...@@ -199,13 +199,13 @@ fn mergetest() { ...@@ -199,13 +199,13 @@ fn mergetest() {
match db.get(b"k1") { match db.get(b"k1") {
Ok(Some(value)) => { Ok(Some(value)) => {
match value.to_utf8() { match value.to_utf8() {
Some(v) => Some(v) => println!("retrieved utf8 value: {}", v),
println!("retrieved utf8 value: {}", v), 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) => {
println!("error reading value")
} }
},
Err(e) => { println!("error reading value")},
_ => panic!("value not present"), _ => panic!("value not present"),
} }
......
This diff is collapsed.
/* //
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");
you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
You may obtain a copy of the License at // You may obtain a copy of the License at
//
http://www.apache.org/licenses/LICENSE-2.0 // http://www.apache.org/licenses/LICENSE-2.0
//
Unless required by applicable law or agreed to in writing, software // Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, // distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
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.
*/ //
extern crate libc; extern crate libc;
use self::libc::{c_int, size_t}; use self::libc::{c_int, size_t};
use std::ffi::CString; use std::ffi::CString;
use std::mem; use std::mem;
use rocksdb_ffi; use rocksdb_ffi;
use merge_operator::{self, MergeOperatorCallback, MergeOperands, use merge_operator::{self, MergeOperands, MergeOperatorCallback,
full_merge_callback, partial_merge_callback}; full_merge_callback, partial_merge_callback};
use comparator::{self, ComparatorCallback, compare_callback}; use comparator::{self, ComparatorCallback, compare_callback};
...@@ -49,7 +49,9 @@ impl Drop for BlockBasedOptions { ...@@ -49,7 +49,9 @@ impl Drop for BlockBasedOptions {
impl BlockBasedOptions { impl BlockBasedOptions {
pub fn new() -> BlockBasedOptions { pub fn new() -> BlockBasedOptions {
let block_opts = unsafe {rocksdb_ffi::rocksdb_block_based_options_create() }; let block_opts = unsafe {
rocksdb_ffi::rocksdb_block_based_options_create()
};
let rocksdb_ffi::DBBlockBasedTableOptions(opt_ptr) = block_opts; let rocksdb_ffi::DBBlockBasedTableOptions(opt_ptr) = block_opts;
if opt_ptr.is_null() { if opt_ptr.is_null() {
panic!("Could not create rocksdb block based options".to_string()); panic!("Could not create rocksdb block based options".to_string());
...@@ -59,34 +61,35 @@ impl BlockBasedOptions { ...@@ -59,34 +61,35 @@ impl BlockBasedOptions {
pub fn set_block_size(&mut self, size: u64) { pub fn set_block_size(&mut self, size: u64) {
unsafe { unsafe {
rocksdb_ffi::rocksdb_block_based_options_set_block_size( rocksdb_ffi::rocksdb_block_based_options_set_block_size(self.inner,
self.inner, size); size);
} }
} }
}
//TODO figure out how to create these in a Rusty way // TODO figure out how to create these in a Rusty way
////pub fn set_filter(&mut self, filter: rocksdb_ffi::DBFilterPolicy) { // /pub fn set_filter(&mut self, filter: rocksdb_ffi::DBFilterPolicy) {
//// unsafe { // / unsafe {
//// rocksdb_ffi::rocksdb_block_based_options_set_filter_policy( // / rocksdb_ffi::rocksdb_block_based_options_set_filter_policy(
//// self.inner, filter); // / self.inner, filter);
//// } // / }
////} // /}
////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_block_based_options_set_block_cache_compressed( /// / rocksdb_ffi::
//// self.inner, cache); /// rocksdb_block_based_options_set_block_cache_compressed(
//// } /// / self.inner, cache);
////} /// / }
/// /}
}
impl Options { impl Options {
pub fn new() -> Options { pub fn new() -> Options {
...@@ -96,18 +99,19 @@ impl Options { ...@@ -96,18 +99,19 @@ impl Options {
if opt_ptr.is_null() { if opt_ptr.is_null() {
panic!("Could not create rocksdb options".to_string()); panic!("Could not create rocksdb options".to_string());
} }
Options{ inner: opts, } Options { inner: opts }
} }
} }
pub fn increase_parallelism(&mut self, parallelism: i32) { pub fn increase_parallelism(&mut self, parallelism: i32) {
unsafe { unsafe {
rocksdb_ffi::rocksdb_options_increase_parallelism( rocksdb_ffi::rocksdb_options_increase_parallelism(self.inner,
self.inner, parallelism); parallelism);
} }
} }
pub fn optimize_level_style_compaction(&mut self, memtable_memory_budget: i32) { pub fn optimize_level_style_compaction(&mut self,
memtable_memory_budget: i32) {
unsafe { unsafe {
rocksdb_ffi::rocksdb_options_optimize_level_style_compaction( rocksdb_ffi::rocksdb_options_optimize_level_style_compaction(
self.inner, memtable_memory_budget); self.inner, memtable_memory_budget);
...@@ -141,7 +145,9 @@ impl Options { ...@@ -141,7 +145,9 @@ impl Options {
} }
} }
pub fn add_comparator<'a>(&mut self, name: &str, compare_fn: fn(&[u8], &[u8]) -> i32) { pub fn add_comparator<'a>(&mut self,
name: &str,
compare_fn: fn(&[u8], &[u8]) -> i32) {
let cb = Box::new(ComparatorCallback { let cb = Box::new(ComparatorCallback {
name: CString::new(name.as_bytes()).unwrap(), name: CString::new(name.as_bytes()).unwrap(),
f: compare_fn, f: compare_fn,
...@@ -160,8 +166,8 @@ impl Options { ...@@ -160,8 +166,8 @@ impl Options {
pub fn set_block_cache_size_mb(&mut self, cache_size: u64) { pub fn set_block_cache_size_mb(&mut self, cache_size: u64) {
unsafe { unsafe {
rocksdb_ffi::rocksdb_options_optimize_for_point_lookup( rocksdb_ffi::rocksdb_options_optimize_for_point_lookup(self.inner,
self.inner, cache_size); cache_size);
} }
} }
...@@ -184,8 +190,7 @@ impl Options { ...@@ -184,8 +190,7 @@ impl Options {
pub fn set_bytes_per_sync(&mut self, nbytes: u64) { pub fn set_bytes_per_sync(&mut self, nbytes: u64) {
unsafe { unsafe {
rocksdb_ffi::rocksdb_options_set_bytes_per_sync( rocksdb_ffi::rocksdb_options_set_bytes_per_sync(self.inner, nbytes);
self.inner, nbytes);
} }
} }
...@@ -204,8 +209,8 @@ impl Options { ...@@ -204,8 +209,8 @@ impl Options {
pub fn set_table_cache_num_shard_bits(&mut self, nbits: c_int) { pub fn set_table_cache_num_shard_bits(&mut self, nbits: c_int) {
unsafe { unsafe {
rocksdb_ffi::rocksdb_options_set_table_cache_numshardbits( rocksdb_ffi::rocksdb_options_set_table_cache_numshardbits(self.inner,
self.inner, nbits); nbits);
} }
} }
...@@ -218,22 +223,22 @@ impl Options { ...@@ -218,22 +223,22 @@ impl Options {
pub fn set_max_write_buffer_number(&mut self, nbuf: c_int) { pub fn set_max_write_buffer_number(&mut self, nbuf: c_int) {
unsafe { unsafe {
rocksdb_ffi::rocksdb_options_set_max_write_buffer_number( rocksdb_ffi::rocksdb_options_set_max_write_buffer_number(self.inner,
self.inner, nbuf); nbuf);
} }
} }
pub fn set_write_buffer_size(&mut self, size: size_t) { pub fn set_write_buffer_size(&mut self, size: size_t) {
unsafe { unsafe {
rocksdb_ffi::rocksdb_options_set_write_buffer_size( rocksdb_ffi::rocksdb_options_set_write_buffer_size(self.inner,
self.inner, size); size);
} }
} }
pub fn set_target_file_size_base(&mut self, size: u64) { pub fn set_target_file_size_base(&mut self, size: u64) {
unsafe { unsafe {
rocksdb_ffi::rocksdb_options_set_target_file_size_base( rocksdb_ffi::rocksdb_options_set_target_file_size_base(self.inner,
self.inner, size); size);
} }
} }
...@@ -258,10 +263,11 @@ impl Options { ...@@ -258,10 +263,11 @@ impl Options {
} }
} }
pub fn set_compaction_style(&mut self, style: rocksdb_ffi::DBCompactionStyle) { pub fn set_compaction_style(&mut self,
style: rocksdb_ffi::DBCompactionStyle) {
unsafe { unsafe {
rocksdb_ffi::rocksdb_options_set_compaction_style( rocksdb_ffi::rocksdb_options_set_compaction_style(self.inner,
self.inner, style); style);
} }
} }
...@@ -274,15 +280,14 @@ impl Options { ...@@ -274,15 +280,14 @@ impl Options {
pub fn set_max_background_flushes(&mut self, n: c_int) { pub fn set_max_background_flushes(&mut self, n: c_int) {
unsafe { unsafe {
rocksdb_ffi::rocksdb_options_set_max_background_flushes( rocksdb_ffi::rocksdb_options_set_max_background_flushes(self.inner,
self.inner, n); n);
} }
} }
pub fn set_filter_deletes(&mut self, filter: bool) { pub fn set_filter_deletes(&mut self, filter: bool) {
unsafe { unsafe {
rocksdb_ffi::rocksdb_options_set_filter_deletes( rocksdb_ffi::rocksdb_options_set_filter_deletes(self.inner, filter);
self.inner, filter);
} }
} }
...@@ -299,7 +304,8 @@ impl Options { ...@@ -299,7 +304,8 @@ impl Options {
} }
} }
pub fn set_block_based_table_factory(&mut self, factory: &BlockBasedOptions) { pub fn set_block_based_table_factory(&mut self,
factory: &BlockBasedOptions) {
unsafe { unsafe {
rocksdb_ffi::rocksdb_options_set_block_based_table_factory(self.inner, factory.inner); rocksdb_ffi::rocksdb_options_set_block_based_table_factory(self.inner, factory.inner);
} }
......
/* //
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");
you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
You may obtain a copy of the License at // You may obtain a copy of the License at
//
http://www.apache.org/licenses/LICENSE-2.0 // http://www.apache.org/licenses/LICENSE-2.0
//
Unless required by applicable law or agreed to in writing, software // Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, // distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
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 rocksdb::{Options, DB, Writable, MergeOperands}; use rocksdb::{DB, MergeOperands, Options, Writable};
#[test] #[test]
pub fn test_column_family() { pub fn test_column_family() {
...@@ -30,7 +30,7 @@ pub fn test_column_family() { ...@@ -30,7 +30,7 @@ pub fn test_column_family() {
Ok(_) => println!("cf1 created successfully"), Ok(_) => println!("cf1 created successfully"),
Err(e) => { Err(e) => {
panic!("could not create column family: {}", e); panic!("could not create column family: {}", e);
}, }
} }
} }
...@@ -39,9 +39,11 @@ pub fn test_column_family() { ...@@ -39,9 +39,11 @@ pub fn test_column_family() {
let mut opts = Options::new(); let mut opts = Options::new();
opts.add_merge_operator("test operator", test_provided_merge); opts.add_merge_operator("test operator", test_provided_merge);
match DB::open(&opts, path) { match DB::open(&opts, path) {
Ok(_) => panic!("should not have opened DB successfully without specifying column Ok(_) => panic!("should not have opened DB successfully without \
specifying column
families"), families"),
Err(e) => assert!(e.starts_with("Invalid argument: You have to open all column families.")), Err(e) => assert!(e.starts_with("Invalid argument: You have to \
open all column families.")),
} }
} }
...@@ -62,12 +64,13 @@ pub fn test_column_family() { ...@@ -62,12 +64,13 @@ pub fn test_column_family() {
Ok(db) => { Ok(db) => {
println!("successfully opened db with column family"); println!("successfully opened db with column family");
db db
}, }
Err(e) => panic!("failed to open db with column family: {}", e), Err(e) => panic!("failed to open db with column family: {}", e),
}; };
let cf1 = *db.cf_handle("cf1").unwrap(); let cf1 = *db.cf_handle("cf1").unwrap();
assert!(db.put_cf(cf1, b"k1", b"v1").is_ok()); assert!(db.put_cf(cf1, b"k1", b"v1").is_ok());
assert!(db.get_cf(cf1, b"k1").unwrap().unwrap().to_utf8().unwrap() == "v1"); assert!(db.get_cf(cf1, b"k1").unwrap().unwrap().to_utf8().unwrap() ==
"v1");
let p = db.put_cf(cf1, b"k1", b"a"); let p = db.put_cf(cf1, b"k1", b"a");
assert!(p.is_ok()); assert!(p.is_ok());
db.merge_cf(cf1, b"k1", b"b").unwrap(); db.merge_cf(cf1, b"k1", b"b").unwrap();
...@@ -80,12 +83,10 @@ pub fn test_column_family() { ...@@ -80,12 +83,10 @@ pub fn test_column_family() {
match db.get(b"k1") { match db.get(b"k1") {
Ok(Some(value)) => { Ok(Some(value)) => {
match value.to_utf8() { match value.to_utf8() {
Some(v) => Some(v) => println!("retrieved utf8 value: {}", v),
println!("retrieved utf8 value: {}", v), 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) => println!("error reading value"), Err(e) => println!("error reading value"),
_ => panic!("value not present!"), _ => panic!("value not present!"),
} }
...@@ -124,7 +125,7 @@ fn test_provided_merge(_: &[u8], ...@@ -124,7 +125,7 @@ fn test_provided_merge(_: &[u8],
for e in v { for e in v {
result.push(*e); result.push(*e);
} }
}, }
None => (), None => (),
} }
for op in operands { for op in operands {
......
use rocksdb::{Options, DB, Writable, Direction}; use rocksdb::{DB, Direction, Options, Writable};
fn cba(input: &Box<[u8]>) -> Box<[u8]> { fn cba(input: &Box<[u8]>) -> Box<[u8]> {
input.iter().cloned().collect::<Vec<_>>().into_boxed_slice() input.iter().cloned().collect::<Vec<_>>().into_boxed_slice()
...@@ -8,14 +8,14 @@ fn cba(input: &Box<[u8]>) -> Box<[u8]> { ...@@ -8,14 +8,14 @@ fn cba(input: &Box<[u8]>) -> Box<[u8]> {
pub fn test_iterator() { pub fn test_iterator() {
let path = "_rust_rocksdb_iteratortest"; let path = "_rust_rocksdb_iteratortest";
{ {
let k1:Box<[u8]> = b"k1".to_vec().into_boxed_slice(); let k1: Box<[u8]> = b"k1".to_vec().into_boxed_slice();
let k2:Box<[u8]> = b"k2".to_vec().into_boxed_slice(); let k2: Box<[u8]> = b"k2".to_vec().into_boxed_slice();
let k3:Box<[u8]> = b"k3".to_vec().into_boxed_slice(); let k3: Box<[u8]> = b"k3".to_vec().into_boxed_slice();
let k4:Box<[u8]> = b"k4".to_vec().into_boxed_slice(); let k4: Box<[u8]> = b"k4".to_vec().into_boxed_slice();
let v1:Box<[u8]> = b"v1111".to_vec().into_boxed_slice(); let v1: Box<[u8]> = b"v1111".to_vec().into_boxed_slice();
let v2:Box<[u8]> = b"v2222".to_vec().into_boxed_slice(); let v2: Box<[u8]> = b"v2222".to_vec().into_boxed_slice();
let v3:Box<[u8]> = b"v3333".to_vec().into_boxed_slice(); let v3: Box<[u8]> = b"v3333".to_vec().into_boxed_slice();
let v4:Box<[u8]> = b"v4444".to_vec().into_boxed_slice(); let v4: Box<[u8]> = b"v4444".to_vec().into_boxed_slice();
let db = DB::open_default(path).unwrap(); let db = DB::open_default(path).unwrap();
let p = db.put(&*k1, &*v1); let p = db.put(&*k1, &*v1);
assert!(p.is_ok()); assert!(p.is_ok());
...@@ -24,7 +24,9 @@ pub fn test_iterator() { ...@@ -24,7 +24,9 @@ pub fn test_iterator() {
let p = db.put(&*k3, &*v3); let p = db.put(&*k3, &*v3);
assert!(p.is_ok()); assert!(p.is_ok());
let mut view1 = db.iterator(); let mut view1 = db.iterator();
let expected = vec![(cba(&k1), cba(&v1)), (cba(&k2), cba(&v2)), (cba(&k3), cba(&v3))]; let expected = vec![(cba(&k1), cba(&v1)),
(cba(&k2), cba(&v2)),
(cba(&k3), cba(&v3))];
{ {
let iterator1 = view1.from_start(); let iterator1 = view1.from_start();
assert_eq!(iterator1.collect::<Vec<_>>(), expected); assert_eq!(iterator1.collect::<Vec<_>>(), expected);
...@@ -86,7 +88,10 @@ pub fn test_iterator() { ...@@ -86,7 +88,10 @@ pub fn test_iterator() {
let p = db.put(&*k4, &*v4); let p = db.put(&*k4, &*v4);
assert!(p.is_ok()); assert!(p.is_ok());
let mut view3 = db.iterator(); let mut view3 = db.iterator();
let expected2 = vec![(cba(&k1), cba(&v1)), (cba(&k2), cba(&v2)), (cba(&k3), cba(&v3)), (cba(&k4), cba(&v4))]; let expected2 = vec![(cba(&k1), cba(&v1)),
(cba(&k2), cba(&v2)),
(cba(&k3), cba(&v3)),
(cba(&k4), cba(&v4))];
{ {
let iterator1 = view1.from_start(); let iterator1 = view1.from_start();
assert_eq!(iterator1.collect::<Vec<_>>(), expected); assert_eq!(iterator1.collect::<Vec<_>>(), expected);
...@@ -97,7 +102,9 @@ pub fn test_iterator() { ...@@ -97,7 +102,9 @@ pub fn test_iterator() {
} }
{ {
let iterator1 = view3.from(b"k2", Direction::forward); let iterator1 = view3.from(b"k2", Direction::forward);
let expected = vec![(cba(&k2), cba(&v2)), (cba(&k3), cba(&v3)), (cba(&k4), cba(&v4))]; let expected = vec![(cba(&k2), cba(&v2)),
(cba(&k3), cba(&v3)),
(cba(&k4), cba(&v4))];
assert_eq!(iterator1.collect::<Vec<_>>(), expected); assert_eq!(iterator1.collect::<Vec<_>>(), expected);
} }
{ {
...@@ -109,4 +116,3 @@ pub fn test_iterator() { ...@@ -109,4 +116,3 @@ pub fn test_iterator() {
let opts = Options::new(); let opts = Options::new();
assert!(DB::destroy(&opts, path).is_ok()); assert!(DB::destroy(&opts, path).is_ok());
} }
use rocksdb::{Options, DB, Writable}; use rocksdb::{DB, Options, Writable};
use std::thread; use std::thread;
use std::sync::Arc; use std::sync::Arc;
...@@ -14,21 +14,21 @@ pub fn test_multithreaded() { ...@@ -14,21 +14,21 @@ pub fn test_multithreaded() {
db.put(b"key", b"value1").unwrap(); db.put(b"key", b"value1").unwrap();
let db1 = db.clone(); let db1 = db.clone();
let j1 = thread::spawn(move|| { let j1 = thread::spawn(move || {
for _ in 1..N { for _ in 1..N {
db1.put(b"key", b"value1").unwrap(); db1.put(b"key", b"value1").unwrap();
} }
}); });
let db2 = db.clone(); let db2 = db.clone();
let j2 = thread::spawn(move|| { let j2 = thread::spawn(move || {
for _ in 1..N { for _ in 1..N {
db2.put(b"key", b"value2").unwrap(); db2.put(b"key", b"value2").unwrap();
} }
}); });
let db3 = db.clone(); let db3 = db.clone();
let j3 = thread::spawn(move|| { let j3 = thread::spawn(move || {
for _ in 1..N { for _ in 1..N {
match db3.get(b"key") { match db3.get(b"key") {
Ok(Some(v)) => { Ok(Some(v)) => {
......
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