From c1c067f1f47f6adcb23e1c437470e9f7a023cd7a Mon Sep 17 00:00:00 2001 From: Ceri Storey Date: Wed, 24 Feb 2016 11:06:39 +0000 Subject: [PATCH 1/9] Start adding WriteOptions. --- src/ffi.rs | 2 ++ src/rocksdb_options.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/ffi.rs b/src/ffi.rs index 6318dde..8b0f86d 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -190,6 +190,8 @@ extern { -> DBInstance; pub fn rocksdb_writeoptions_create() -> DBWriteOptions; pub fn rocksdb_writeoptions_destroy(writeopts: DBWriteOptions); + pub fn rocksdb_writeoptions_set_sync(writeopts: DBWriteOptions, v: bool); + pub fn rocksdb_writeoptions_disable_WAL(writeopts: DBWriteOptions, v: c_int); pub fn rocksdb_put(db: DBInstance, writeopts: DBWriteOptions, k: *const u8, diff --git a/src/rocksdb_options.rs b/src/rocksdb_options.rs index e53bd27..7210061 100644 --- a/src/rocksdb_options.rs +++ b/src/rocksdb_options.rs @@ -31,6 +31,10 @@ pub struct Options { pub inner: rocksdb_ffi::DBOptions, } +pub struct WriteOptions { + pub inner: rocksdb_ffi::DBWriteOptions, +} + impl Drop for Options { fn drop(&mut self) { unsafe { @@ -47,6 +51,14 @@ impl Drop for BlockBasedOptions { } } +impl Drop for WriteOptions { + fn drop(&mut self) { + unsafe { + rocksdb_ffi::rocksdb_writeoptions_destroy(self.inner); + } + } +} + impl BlockBasedOptions { pub fn new() -> BlockBasedOptions { let block_opts = unsafe { @@ -311,3 +323,19 @@ impl Options { } } } + +impl WriteOptions { + pub fn new() -> WriteOptions { + let write_opts = unsafe { rocksdb_ffi::rocksdb_writeoptions_create() }; + let rocksdb_ffi::DBWriteOptions(opt_ptr) = write_opts; + if opt_ptr.is_null() { + panic!("Could not create rocksdb write options".to_string()); + } + WriteOptions { inner: write_opts } + } + pub fn set_sync(&mut self, sync: bool) { + unsafe { + rocksdb_ffi::rocksdb_writeoptions_set_sync(self.inner, sync); + } + } +} From df6d49506bd412c72f0d04bb4187118453472b2c Mon Sep 17 00:00:00 2001 From: Ceri Storey Date: Wed, 24 Feb 2016 11:18:27 +0000 Subject: [PATCH 2/9] Add a DB#put_opt method for use with writeOptions. --- src/rocksdb.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/rocksdb.rs b/src/rocksdb.rs index 3ecbb12..92b0ff3 100644 --- a/src/rocksdb.rs +++ b/src/rocksdb.rs @@ -28,7 +28,7 @@ use std::str::from_utf8; use self::libc::{c_void, size_t}; use rocksdb_ffi::{self, DBCFHandle, error_message}; -use rocksdb_options::Options; +use rocksdb_options::{Options,WriteOptions}; pub struct DB { inner: rocksdb_ffi::DBInstance, @@ -522,28 +522,30 @@ impl DB { pub fn snapshot(&self) -> Snapshot { Snapshot::new(self) } -} -impl Writable for DB { - fn put(&self, key: &[u8], value: &[u8]) -> Result<(), String> { + fn put_opt(&self, key: &[u8], value: &[u8], writeopts: &WriteOptions) -> Result<(), String> { unsafe { - let writeopts = rocksdb_ffi::rocksdb_writeoptions_create(); let mut err: *const i8 = 0 as *const i8; let err_ptr: *mut *const i8 = &mut err; rocksdb_ffi::rocksdb_put(self.inner, - writeopts.clone(), + writeopts.inner, key.as_ptr(), key.len() as size_t, value.as_ptr(), value.len() as size_t, err_ptr); - rocksdb_ffi::rocksdb_writeoptions_destroy(writeopts); if !err.is_null() { return Err(error_message(err)); } Ok(()) } } +} + +impl Writable for DB { + fn put(&self, key: &[u8], value: &[u8]) -> Result<(), String> { + self.put_opt(key, value, &WriteOptions::new()) + } fn put_cf(&self, cf: DBCFHandle, From a1dd3f8f229552b2d7eced53a32e88879fbac2c8 Mon Sep 17 00:00:00 2001 From: Ceri Storey Date: Wed, 24 Feb 2016 11:22:30 +0000 Subject: [PATCH 3/9] Add DB#put_cf_opt for WriteOptions. --- src/rocksdb.rs | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/rocksdb.rs b/src/rocksdb.rs index 92b0ff3..bf03331 100644 --- a/src/rocksdb.rs +++ b/src/rocksdb.rs @@ -540,37 +540,44 @@ impl DB { Ok(()) } } -} - -impl Writable for DB { - fn put(&self, key: &[u8], value: &[u8]) -> Result<(), String> { - self.put_opt(key, value, &WriteOptions::new()) - } - fn put_cf(&self, + fn put_cf_opt(&self, cf: DBCFHandle, key: &[u8], - value: &[u8]) + value: &[u8], + writeopts: &WriteOptions) -> Result<(), String> { unsafe { - let writeopts = rocksdb_ffi::rocksdb_writeoptions_create(); let mut err: *const i8 = 0 as *const i8; let err_ptr: *mut *const i8 = &mut err; rocksdb_ffi::rocksdb_put_cf(self.inner, - writeopts.clone(), + writeopts.inner, cf, key.as_ptr(), key.len() as size_t, value.as_ptr(), value.len() as size_t, err_ptr); - rocksdb_ffi::rocksdb_writeoptions_destroy(writeopts); if !err.is_null() { return Err(error_message(err)); } Ok(()) } } +} + +impl Writable for DB { + fn put(&self, key: &[u8], value: &[u8]) -> Result<(), String> { + self.put_opt(key, value, &WriteOptions::new()) + } + + fn put_cf(&self, + cf: DBCFHandle, + key: &[u8], + value: &[u8]) + -> Result<(), String> { + self.put_cf_opt(cf, key, value, &WriteOptions::new()) + } fn merge(&self, key: &[u8], value: &[u8]) -> Result<(), String> { unsafe { From 4d4a140f9eb7d1ff564d40889813a5cc0350a2cd Mon Sep 17 00:00:00 2001 From: Ceri Storey Date: Wed, 24 Feb 2016 11:25:05 +0000 Subject: [PATCH 4/9] Export WriteOptions. --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index b2233b3..0e013aa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,7 @@ pub use ffi as rocksdb_ffi; pub use ffi::{DBCompactionStyle, DBComparator, new_bloom_filter}; pub use rocksdb::{DB, DBIterator, DBVector, Direction, Writable, WriteBatch, IteratorMode}; -pub use rocksdb_options::{BlockBasedOptions, Options}; +pub use rocksdb_options::{BlockBasedOptions, Options, WriteOptions}; pub use merge_operator::MergeOperands; pub mod rocksdb; pub mod ffi; From 5744f128fe85821e0592a8dff3de65d02abe21e5 Mon Sep 17 00:00:00 2001 From: Ceri Storey Date: Wed, 24 Feb 2016 11:26:28 +0000 Subject: [PATCH 5/9] Publicise. --- src/rocksdb.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rocksdb.rs b/src/rocksdb.rs index bf03331..6d56f9b 100644 --- a/src/rocksdb.rs +++ b/src/rocksdb.rs @@ -523,7 +523,7 @@ impl DB { Snapshot::new(self) } - fn put_opt(&self, key: &[u8], value: &[u8], writeopts: &WriteOptions) -> Result<(), String> { + pub fn put_opt(&self, key: &[u8], value: &[u8], writeopts: &WriteOptions) -> Result<(), String> { unsafe { let mut err: *const i8 = 0 as *const i8; let err_ptr: *mut *const i8 = &mut err; @@ -541,7 +541,7 @@ impl DB { } } - fn put_cf_opt(&self, + pub fn put_cf_opt(&self, cf: DBCFHandle, key: &[u8], value: &[u8], From 14d02a3631ecd826cd9ebaa6cfcc4eef738ba8fe Mon Sep 17 00:00:00 2001 From: Ceri Storey Date: Wed, 24 Feb 2016 11:46:36 +0000 Subject: [PATCH 6/9] Add variants for other DB/Writable methods that take WriteOptions. --- src/rocksdb.rs | 79 ++++++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 35 deletions(-) diff --git a/src/rocksdb.rs b/src/rocksdb.rs index 6d56f9b..fd3c47f 100644 --- a/src/rocksdb.rs +++ b/src/rocksdb.rs @@ -564,96 +564,71 @@ impl DB { Ok(()) } } -} - -impl Writable for DB { - fn put(&self, key: &[u8], value: &[u8]) -> Result<(), String> { - self.put_opt(key, value, &WriteOptions::new()) - } - - fn put_cf(&self, - cf: DBCFHandle, - key: &[u8], - value: &[u8]) - -> Result<(), String> { - self.put_cf_opt(cf, key, value, &WriteOptions::new()) - } - - fn merge(&self, key: &[u8], value: &[u8]) -> Result<(), String> { + pub fn merge_opt(&self, key: &[u8], value: &[u8], writeopts: &WriteOptions) -> Result<(), String> { unsafe { - let writeopts = rocksdb_ffi::rocksdb_writeoptions_create(); let mut err: *const i8 = 0 as *const i8; let err_ptr: *mut *const i8 = &mut err; rocksdb_ffi::rocksdb_merge(self.inner, - writeopts.clone(), + writeopts.inner, key.as_ptr(), key.len() as size_t, value.as_ptr(), value.len() as size_t, err_ptr); - rocksdb_ffi::rocksdb_writeoptions_destroy(writeopts); if !err.is_null() { return Err(error_message(err)); } Ok(()) } } - - fn merge_cf(&self, + fn merge_cf_opt(&self, cf: DBCFHandle, key: &[u8], - value: &[u8]) + value: &[u8], + writeopts: &WriteOptions) -> Result<(), String> { unsafe { - let writeopts = rocksdb_ffi::rocksdb_writeoptions_create(); let mut err: *const i8 = 0 as *const i8; let err_ptr: *mut *const i8 = &mut err; rocksdb_ffi::rocksdb_merge_cf(self.inner, - writeopts.clone(), + writeopts.inner, cf, key.as_ptr(), key.len() as size_t, value.as_ptr(), value.len() as size_t, err_ptr); - rocksdb_ffi::rocksdb_writeoptions_destroy(writeopts); if !err.is_null() { return Err(error_message(err)); } Ok(()) } } - - fn delete(&self, key: &[u8]) -> Result<(), String> { + fn delete_opt(&self, key: &[u8], writeopts: &WriteOptions) -> Result<(), String> { unsafe { - let writeopts = rocksdb_ffi::rocksdb_writeoptions_create(); let mut err: *const i8 = 0 as *const i8; let err_ptr: *mut *const i8 = &mut err; rocksdb_ffi::rocksdb_delete(self.inner, - writeopts.clone(), + writeopts.inner, key.as_ptr(), key.len() as size_t, err_ptr); - rocksdb_ffi::rocksdb_writeoptions_destroy(writeopts); if !err.is_null() { return Err(error_message(err)); } Ok(()) } } - - fn delete_cf(&self, cf: DBCFHandle, key: &[u8]) -> Result<(), String> { + fn delete_cf_opt(&self, cf: DBCFHandle, key: &[u8], writeopts: &WriteOptions) -> Result<(), String> { unsafe { - let writeopts = rocksdb_ffi::rocksdb_writeoptions_create(); let mut err: *const i8 = 0 as *const i8; let err_ptr: *mut *const i8 = &mut err; rocksdb_ffi::rocksdb_delete_cf(self.inner, - writeopts.clone(), + writeopts.inner, cf, key.as_ptr(), key.len() as size_t, err_ptr); - rocksdb_ffi::rocksdb_writeoptions_destroy(writeopts); if !err.is_null() { return Err(error_message(err)); } @@ -662,6 +637,40 @@ impl Writable for DB { } } +impl Writable for DB { + fn put(&self, key: &[u8], value: &[u8]) -> Result<(), String> { + self.put_opt(key, value, &WriteOptions::new()) + } + + fn put_cf(&self, + cf: DBCFHandle, + key: &[u8], + value: &[u8]) + -> Result<(), String> { + self.put_cf_opt(cf, key, value, &WriteOptions::new()) + } + + fn merge(&self, key: &[u8], value: &[u8]) -> Result<(), String> { + self.merge_opt(key, value, &WriteOptions::new()) + } + + fn merge_cf(&self, + cf: DBCFHandle, + key: &[u8], + value: &[u8]) + -> Result<(), String> { + self.merge_cf_opt(cf, key, value, &WriteOptions::new()) + } + + fn delete(&self, key: &[u8]) -> Result<(), String> { + self.delete_opt(key, &WriteOptions::new()) + } + + fn delete_cf(&self, cf: DBCFHandle, key: &[u8]) -> Result<(), String> { + self.delete_cf_opt(cf, key, &WriteOptions::new()) + } +} + impl WriteBatch { pub fn new() -> WriteBatch { WriteBatch { From e36d9892e734700bef1dcd198d7082920f6f2b10 Mon Sep 17 00:00:00 2001 From: Ceri Storey Date: Wed, 24 Feb 2016 11:48:13 +0000 Subject: [PATCH 7/9] Add DB#write variant with WriteOptions. --- src/rocksdb.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/rocksdb.rs b/src/rocksdb.rs index fd3c47f..5c9b771 100644 --- a/src/rocksdb.rs +++ b/src/rocksdb.rs @@ -366,16 +366,14 @@ impl DB { Ok(()) } - pub fn write(&self, batch: WriteBatch) -> Result<(), String> { - let writeopts = unsafe { rocksdb_ffi::rocksdb_writeoptions_create() }; + pub fn write_opt(&self, batch: WriteBatch, writeopts: &WriteOptions) -> Result<(), String> { let mut err: *const i8 = 0 as *const i8; let err_ptr: *mut *const i8 = &mut err; unsafe { rocksdb_ffi::rocksdb_write(self.inner, - writeopts.clone(), + writeopts.inner, batch.inner, err_ptr); - rocksdb_ffi::rocksdb_writeoptions_destroy(writeopts); } if !err.is_null() { return Err(error_message(err)); @@ -383,6 +381,10 @@ impl DB { return Ok(()); } + pub fn write(&self, batch: WriteBatch) -> Result<(), String> { + self.write_opt(batch, &WriteOptions::new()) + } + pub fn get(&self, key: &[u8]) -> Result, String> { unsafe { let readopts = rocksdb_ffi::rocksdb_readoptions_create(); From 45d6436f0ea43ee2d4b4ee050364693c15f7d4dc Mon Sep 17 00:00:00 2001 From: Ceri Storey Date: Mon, 29 Feb 2016 10:13:54 +0000 Subject: [PATCH 8/9] Format with rustfmt. --- src/comparator.rs | 1 - src/ffi.rs | 26 +++++---- src/lib.rs | 4 +- src/merge_operator.rs | 31 +++++----- src/rocksdb.rs | 130 ++++++++++++++++++++++++++--------------- src/rocksdb_options.rs | 16 +++-- 6 files changed, 122 insertions(+), 86 deletions(-) diff --git a/src/comparator.rs b/src/comparator.rs index 27affa6..1e59ec4 100644 --- a/src/comparator.rs +++ b/src/comparator.rs @@ -1,4 +1,3 @@ -// // Copyright 2014 Tyler Neely // // Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/ffi.rs b/src/ffi.rs index 8b0f86d..f2728f2 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -1,4 +1,3 @@ -// // Copyright 2014 Tyler Neely // // 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 // instead... #[link(name = "rocksdb")] -extern { +extern "C" { pub fn rocksdb_options_create() -> DBOptions; pub fn rocksdb_options_destroy(opts: DBOptions); pub fn rocksdb_cache_create_lru(capacity: size_t) -> DBCache; @@ -191,7 +190,8 @@ extern { pub fn rocksdb_writeoptions_create() -> DBWriteOptions; pub fn rocksdb_writeoptions_destroy(writeopts: DBWriteOptions); pub fn rocksdb_writeoptions_set_sync(writeopts: DBWriteOptions, v: bool); - pub fn rocksdb_writeoptions_disable_WAL(writeopts: DBWriteOptions, v: c_int); + pub fn rocksdb_writeoptions_disable_WAL(writeopts: DBWriteOptions, + v: c_int); pub fn rocksdb_put(db: DBInstance, writeopts: DBWriteOptions, k: *const u8, @@ -378,15 +378,17 @@ extern { // Comparator pub fn rocksdb_options_set_comparator(options: DBOptions, cb: DBComparator); - pub fn rocksdb_comparator_create( - state: *mut c_void, - destroy: extern fn(*mut c_void) -> (), - compare: extern fn (arg: *mut c_void, - a: *const c_char, alen: size_t, - b: *const c_char, blen: size_t - ) -> c_int, - name_fn: extern fn(*mut c_void) -> *const c_char - ) -> DBComparator; + pub fn rocksdb_comparator_create(state: *mut c_void, + destroy: extern "C" fn(*mut c_void) -> (), + compare: extern "C" fn(arg: *mut c_void, + a: *const c_char, + alen: size_t, + b: *const c_char, + blen: size_t) + -> c_int, + name_fn: extern "C" fn(*mut c_void) + -> *const c_char) + -> DBComparator; pub fn rocksdb_comparator_destroy(cmp: DBComparator); // Column Family diff --git a/src/lib.rs b/src/lib.rs index 0e013aa..8b774f4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,3 @@ -// // Copyright 2014 Tyler Neely // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,7 +14,8 @@ // pub use ffi as rocksdb_ffi; 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, WriteOptions}; pub use merge_operator::MergeOperands; pub mod rocksdb; diff --git a/src/merge_operator.rs b/src/merge_operator.rs index 972bc9a..1beed45 100644 --- a/src/merge_operator.rs +++ b/src/merge_operator.rs @@ -1,4 +1,3 @@ -// // Copyright 2014 Tyler Neely // // 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]> { match self.cursor == self.num_operands { true => None, - false => { - unsafe { - let base = self.operands_list as usize; - let base_len = self.operands_list_len as usize; - let spacing = mem::size_of::<*const *const u8>(); - let spacing_len = mem::size_of::<*const size_t>(); - let len_ptr = (base_len + (spacing_len * self.cursor)) - as *const size_t; - let len = *len_ptr as usize; - let ptr = base + (spacing * self.cursor); - self.cursor += 1; - Some(mem::transmute(slice::from_raw_parts(*(ptr as *const *const u8) + false => unsafe { + let base = self.operands_list as usize; + let base_len = self.operands_list_len as usize; + let spacing = mem::size_of::<*const *const u8>(); + let spacing_len = mem::size_of::<*const size_t>(); + let len_ptr = + (base_len + (spacing_len * self.cursor)) as *const size_t; + let len = *len_ptr as usize; + let ptr = base + (spacing * self.cursor); + self.cursor += 1; + Some(mem::transmute(slice::from_raw_parts(*(ptr as *const *const u8) as *const u8, len))) - } - } + }, } } @@ -203,9 +200,7 @@ fn mergetest() { 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"), } diff --git a/src/rocksdb.rs b/src/rocksdb.rs index 5c9b771..82932fc 100644 --- a/src/rocksdb.rs +++ b/src/rocksdb.rs @@ -1,4 +1,3 @@ -// // Copyright 2014 Tyler Neely // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -28,7 +27,7 @@ use std::str::from_utf8; use self::libc::{c_void, size_t}; use rocksdb_ffi::{self, DBCFHandle, error_message}; -use rocksdb_options::{Options,WriteOptions}; +use rocksdb_options::{Options, WriteOptions}; pub struct DB { inner: rocksdb_ffi::DBInstance, @@ -70,8 +69,12 @@ impl<'a> Iterator for DBIterator<'a> { let native_iter = self.inner; if !self.just_seeked { match self.direction { - Direction::forward => unsafe { rocksdb_ffi::rocksdb_iter_next(native_iter) }, - Direction::reverse => unsafe { rocksdb_ffi::rocksdb_iter_prev(native_iter) }, + Direction::forward => unsafe { + rocksdb_ffi::rocksdb_iter_next(native_iter) + }, + Direction::reverse => unsafe { + rocksdb_ffi::rocksdb_iter_prev(native_iter) + }, } } else { self.just_seeked = false; @@ -110,7 +113,10 @@ pub enum IteratorMode<'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 { let iterator = rocksdb_ffi::rocksdb_create_iterator(db.inner, readopts.inner); @@ -134,11 +140,11 @@ impl<'a> DBIterator<'a> { IteratorMode::Start => { rocksdb_ffi::rocksdb_iter_seek_to_first(self.inner); self.direction = Direction::forward; - }, + } IteratorMode::End => { rocksdb_ffi::rocksdb_iter_seek_to_last(self.inner); self.direction = Direction::reverse; - }, + } IteratorMode::From(key, dir) => { rocksdb_ffi::rocksdb_iter_seek(self.inner, 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 { let snapshot = unsafe { 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) { unsafe { rocksdb_ffi::rocksdb_release_snapshot(self.db.inner, self.inner); @@ -244,16 +250,19 @@ impl DB { -> Result { let cpath = match CString::new(path.as_bytes()) { Ok(c) => c, - Err(_) => return Err("Failed to convert path to CString when \ - opening rocksdb" - .to_string()), + Err(_) => { + return Err("Failed to convert path to CString when opening \ + rocksdb" + .to_string()) + } }; let cpath_ptr = cpath.as_ptr(); let ospath = Path::new(path); match fs::create_dir_all(&ospath) { - Err(e) => - return Err("Failed to create rocksdb directory.".to_string()), + Err(e) => { + return Err("Failed to create rocksdb directory.".to_string()) + } Ok(_) => (), } @@ -264,7 +273,9 @@ impl DB { if cfs.len() == 0 { 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 { let mut cfs_v = cfs.to_vec(); @@ -283,8 +294,8 @@ impl DB { .collect(); let cfnames: Vec<*const _> = c_cfs.iter() - .map(|cf| cf.as_ptr()) - .collect(); + .map(|cf| cf.as_ptr()) + .collect(); // These handles will be populated by DB. let mut cfhandles: Vec = @@ -342,7 +353,9 @@ impl DB { let mut err: *const i8 = 0 as *const i8; let err_ptr: *mut *const i8 = &mut err; 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() { return Err(error_message(err)); @@ -358,7 +371,9 @@ impl DB { let mut err: *const i8 = 0 as *const i8; let err_ptr: *mut *const i8 = &mut err; 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() { return Err(error_message(err)); @@ -366,7 +381,10 @@ impl DB { Ok(()) } - pub fn write_opt(&self, batch: WriteBatch, writeopts: &WriteOptions) -> Result<(), String> { + pub fn write_opt(&self, + batch: WriteBatch, + writeopts: &WriteOptions) + -> Result<(), String> { let mut err: *const i8 = 0 as *const i8; let err_ptr: *mut *const i8 = &mut err; unsafe { @@ -413,9 +431,7 @@ impl DB { } match val.is_null() { true => Ok(None), - false => { - Ok(Some(DBVector::from_c(val, val_len))) - } + false => Ok(Some(DBVector::from_c(val, val_len))), } } } @@ -452,9 +468,7 @@ impl DB { } match val.is_null() { true => Ok(None), - false => { - Ok(Some(DBVector::from_c(val, val_len))) - } + false => Ok(Some(DBVector::from_c(val, val_len))), } } } @@ -465,9 +479,11 @@ impl DB { -> Result { let cname = match CString::new(name.as_bytes()) { Ok(c) => c, - Err(_) => return Err("Failed to convert path to CString when \ - opening rocksdb" - .to_string()), + Err(_) => { + return Err("Failed to convert path to CString when opening \ + rocksdb" + .to_string()) + } }; let cname_ptr = cname.as_ptr(); let mut err: *const i8 = 0 as *const i8; @@ -516,7 +532,10 @@ impl DB { DBIterator::new(&self, &opts, mode) } - pub fn iterator_cf(&self, cf_handle: DBCFHandle, mode: IteratorMode) -> Result { + pub fn iterator_cf(&self, + cf_handle: DBCFHandle, + mode: IteratorMode) + -> Result { let opts = ReadOptions::new(); DBIterator::new_cf(&self, cf_handle, &opts, mode) } @@ -525,7 +544,11 @@ impl DB { Snapshot::new(self) } - pub fn put_opt(&self, key: &[u8], value: &[u8], writeopts: &WriteOptions) -> Result<(), String> { + pub fn put_opt(&self, + key: &[u8], + value: &[u8], + writeopts: &WriteOptions) + -> Result<(), String> { unsafe { let mut err: *const i8 = 0 as *const i8; let err_ptr: *mut *const i8 = &mut err; @@ -544,11 +567,11 @@ impl DB { } pub fn put_cf_opt(&self, - cf: DBCFHandle, - key: &[u8], - value: &[u8], - writeopts: &WriteOptions) - -> Result<(), String> { + cf: DBCFHandle, + key: &[u8], + value: &[u8], + writeopts: &WriteOptions) + -> Result<(), String> { unsafe { let mut err: *const i8 = 0 as *const i8; let err_ptr: *mut *const i8 = &mut err; @@ -566,7 +589,11 @@ impl DB { Ok(()) } } - pub fn merge_opt(&self, key: &[u8], value: &[u8], writeopts: &WriteOptions) -> Result<(), String> { + pub fn merge_opt(&self, + key: &[u8], + value: &[u8], + writeopts: &WriteOptions) + -> Result<(), String> { unsafe { let mut err: *const i8 = 0 as *const i8; let err_ptr: *mut *const i8 = &mut err; @@ -584,11 +611,11 @@ impl DB { } } fn merge_cf_opt(&self, - cf: DBCFHandle, - key: &[u8], - value: &[u8], - writeopts: &WriteOptions) - -> Result<(), String> { + cf: DBCFHandle, + key: &[u8], + value: &[u8], + writeopts: &WriteOptions) + -> Result<(), String> { unsafe { let mut err: *const i8 = 0 as *const i8; let err_ptr: *mut *const i8 = &mut err; @@ -606,7 +633,10 @@ impl DB { Ok(()) } } - fn delete_opt(&self, key: &[u8], writeopts: &WriteOptions) -> Result<(), String> { + fn delete_opt(&self, + key: &[u8], + writeopts: &WriteOptions) + -> Result<(), String> { unsafe { let mut err: *const i8 = 0 as *const i8; let err_ptr: *mut *const i8 = &mut err; @@ -621,7 +651,11 @@ impl DB { Ok(()) } } - fn delete_cf_opt(&self, cf: DBCFHandle, key: &[u8], writeopts: &WriteOptions) -> Result<(), String> { + fn delete_cf_opt(&self, + cf: DBCFHandle, + key: &[u8], + writeopts: &WriteOptions) + -> Result<(), String> { unsafe { let mut err: *const i8 = 0 as *const i8; let err_ptr: *mut *const i8 = &mut err; @@ -861,9 +895,11 @@ fn errors_do_stuff() { let opts = Options::new(); // The DB will still be open when we try to destroy and the lock should fail match DB::destroy(&opts, path) { - Err(ref s) => assert!(s == - "IO error: lock _rust_rocksdb_error/LOCK: No \ - locks available"), + Err(ref s) => { + assert!(s == + "IO error: lock _rust_rocksdb_error/LOCK: No locks \ + available") + } Ok(_) => panic!("should fail"), } } diff --git a/src/rocksdb_options.rs b/src/rocksdb_options.rs index 7210061..35d9f6d 100644 --- a/src/rocksdb_options.rs +++ b/src/rocksdb_options.rs @@ -1,4 +1,3 @@ -// // Copyright 2014 Tyler Neely // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -139,7 +138,10 @@ impl Options { pub fn add_merge_operator<'a>(&mut self, name: &str, - merge_fn: fn(&[u8], Option<&[u8]>, &mut MergeOperands) -> Vec) { + merge_fn: fn(&[u8], + Option<&[u8]>, + &mut MergeOperands) + -> Vec) { let cb = Box::new(MergeOperatorCallback { name: CString::new(name.as_bytes()).unwrap(), merge_fn: merge_fn, @@ -192,10 +194,12 @@ impl Options { pub fn set_use_fsync(&mut self, useit: bool) { unsafe { match useit { - true => - rocksdb_ffi::rocksdb_options_set_use_fsync(self.inner, 1), - false => - rocksdb_ffi::rocksdb_options_set_use_fsync(self.inner, 0), + true => { + rocksdb_ffi::rocksdb_options_set_use_fsync(self.inner, 1) + } + false => { + rocksdb_ffi::rocksdb_options_set_use_fsync(self.inner, 0) + } } } } From a7f36e53b8067c093cd7c1d2874e428c66b93978 Mon Sep 17 00:00:00 2001 From: Ceri Storey Date: Mon, 29 Feb 2016 11:50:24 +0000 Subject: [PATCH 9/9] Export WriteOptions. --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 9d27965..8b774f4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,7 @@ pub use ffi as rocksdb_ffi; pub use ffi::{DBCompactionStyle, DBComparator, new_bloom_filter}; pub use rocksdb::{DB, DBIterator, DBVector, Direction, IteratorMode, Writable, WriteBatch}; -pub use rocksdb_options::{BlockBasedOptions, Options}; +pub use rocksdb_options::{BlockBasedOptions, Options, WriteOptions}; pub use merge_operator::MergeOperands; pub mod rocksdb; pub mod ffi;