diff --git a/src/database/batch.rs b/src/database/batch.rs index 7a7fe4f..e0059d6 100644 --- a/src/database/batch.rs +++ b/src/database/batch.rs @@ -1,15 +1,15 @@ //! Module providing write batches +use super::error::Error; +use super::Database; +use database::key::from_u8; +use database::key::Key; use leveldb_sys::*; -use libc::{c_char, size_t, c_void}; +use libc::{c_char, c_void, size_t}; +use options::{c_writeoptions, WriteOptions}; use std::marker::PhantomData; -use database::key::Key; -use database::key::from_u8; -use std::slice; -use options::{WriteOptions, c_writeoptions}; -use super::error::Error; use std::ptr; -use super::Database; +use std::slice; #[allow(missing_docs)] struct RawWritebatch { @@ -43,10 +43,12 @@ impl Batch for Database { let mut error = ptr::null_mut(); let c_writeoptions = c_writeoptions(options); - leveldb_write(self.database.ptr, - c_writeoptions, - batch.writebatch.ptr, - &mut error); + leveldb_write( + self.database.ptr, + c_writeoptions, + batch.writebatch.ptr, + &mut error, + ); leveldb_writeoptions_destroy(c_writeoptions); if error == ptr::null_mut() { @@ -78,11 +80,13 @@ impl Writebatch { pub fn put(&mut self, key: K, value: &[u8]) { unsafe { key.as_slice(|k| { - leveldb_writebatch_put(self.writebatch.ptr, - k.as_ptr() as *mut c_char, - k.len() as size_t, - value.as_ptr() as *mut c_char, - value.len() as size_t); + leveldb_writebatch_put( + self.writebatch.ptr, + k.as_ptr() as *mut c_char, + k.len() as size_t, + value.as_ptr() as *mut c_char, + value.len() as size_t, + ); }) } } @@ -91,9 +95,11 @@ impl Writebatch { pub fn delete(&mut self, key: K) { unsafe { key.as_slice(|k| { - leveldb_writebatch_delete(self.writebatch.ptr, - k.as_ptr() as *mut c_char, - k.len() as size_t); + leveldb_writebatch_delete( + self.writebatch.ptr, + k.as_ptr() as *mut c_char, + k.len() as size_t, + ); }) } } @@ -102,10 +108,12 @@ impl Writebatch { pub fn iterate>(&mut self, iterator: Box) -> Box { unsafe { let iter = Box::into_raw(iterator); - leveldb_writebatch_iterate(self.writebatch.ptr, - iter as *mut c_void, - put_callback::, - deleted_callback::); + leveldb_writebatch_iterate( + self.writebatch.ptr, + iter as *mut c_void, + put_callback::, + deleted_callback::, + ); Box::from_raw(iter) } } @@ -123,11 +131,13 @@ pub trait WritebatchIterator { fn deleted(&mut self, key: Self::K); } -extern "C" fn put_callback>(state: *mut c_void, - key: *const c_char, - keylen: size_t, - val: *const c_char, - vallen: size_t) { +extern "C" fn put_callback>( + state: *mut c_void, + key: *const c_char, + keylen: size_t, + val: *const c_char, + vallen: size_t, +) { unsafe { let iter: &mut T = &mut *(state as *mut T); let key_slice = slice::from_raw_parts::(key as *const u8, keylen as usize); @@ -137,9 +147,11 @@ extern "C" fn put_callback>(state: *mut c_v } } -extern "C" fn deleted_callback>(state: *mut c_void, - key: *const c_char, - keylen: size_t) { +extern "C" fn deleted_callback>( + state: *mut c_void, + key: *const c_char, + keylen: size_t, +) { unsafe { let iter: &mut T = &mut *(state as *mut T); let key_slice = slice::from_raw_parts::(key as *const u8, keylen as usize); diff --git a/src/database/bytes.rs b/src/database/bytes.rs index 3894348..7973fb2 100644 --- a/src/database/bytes.rs +++ b/src/database/bytes.rs @@ -1,4 +1,4 @@ -use ::std::slice; +use std::slice; /// Bytes allocated by leveldb /// @@ -53,17 +53,13 @@ impl ::std::ops::Deref for Bytes { type Target = [u8]; fn deref(&self) -> &Self::Target { - unsafe { - slice::from_raw_parts(self.bytes, self.size) - } + unsafe { slice::from_raw_parts(self.bytes, self.size) } } } impl ::std::ops::DerefMut for Bytes { fn deref_mut(&mut self) -> &mut Self::Target { - unsafe { - slice::from_raw_parts_mut(self.bytes as *mut u8, self.size) - } + unsafe { slice::from_raw_parts_mut(self.bytes as *mut u8, self.size) } } } diff --git a/src/database/cache.rs b/src/database/cache.rs index e1eb17c..97ddc99 100644 --- a/src/database/cache.rs +++ b/src/database/cache.rs @@ -1,5 +1,5 @@ //! Structs and traits to work with the leveldb cache. -use leveldb_sys::{leveldb_cache_t, leveldb_cache_create_lru, leveldb_cache_destroy}; +use leveldb_sys::{leveldb_cache_create_lru, leveldb_cache_destroy, leveldb_cache_t}; use libc::size_t; #[allow(missing_docs)] @@ -24,7 +24,9 @@ impl Cache { /// Create a leveldb LRU cache of a given size pub fn new(size: size_t) -> Cache { let cache = unsafe { leveldb_cache_create_lru(size) }; - Cache { raw: RawCache { ptr: cache } } + Cache { + raw: RawCache { ptr: cache }, + } } #[allow(missing_docs)] diff --git a/src/database/compaction.rs b/src/database/compaction.rs index 6eddc80..3d7f390 100644 --- a/src/database/compaction.rs +++ b/src/database/compaction.rs @@ -1,6 +1,6 @@ //! Compaction -use super::Database; use super::key::Key; +use super::Database; use leveldb_sys::leveldb_compact_range; use libc::{c_char, size_t}; @@ -13,11 +13,13 @@ impl<'a, K: Key + 'a> Compaction<'a, K> for Database { unsafe { start.as_slice(|s| { limit.as_slice(|l| { - leveldb_compact_range(self.database.ptr, - s.as_ptr() as *mut c_char, - s.len() as size_t, - l.as_ptr() as *mut c_char, - l.len() as size_t); + leveldb_compact_range( + self.database.ptr, + s.as_ptr() as *mut c_char, + s.len() as size_t, + l.as_ptr() as *mut c_char, + l.len() as size_t, + ); }); }); } diff --git a/src/database/comparator.rs b/src/database/comparator.rs index 64e278d..c3c05b7 100644 --- a/src/database/comparator.rs +++ b/src/database/comparator.rs @@ -4,13 +4,13 @@ //! Comparators allow to override this comparison. //! The ordering of keys introduced by the compartor influences iteration order. //! Databases written with one Comparator cannot be opened with another. +use database::key::from_u8; +use database::key::Key; use leveldb_sys::*; -use libc::{size_t, c_void, c_char}; -use std::slice; +use libc::{c_char, c_void, size_t}; use std::cmp::Ordering; -use database::key::Key; -use database::key::from_u8; use std::marker::PhantomData; +use std::slice; /// A comparator has two important functions: /// @@ -48,22 +48,25 @@ impl OrdComparator { } /// DefaultComparator is the a stand in for "no comparator set" -#[derive(Copy,Clone)] +#[derive(Copy, Clone)] pub struct DefaultComparator; -unsafe trait InternalComparator : Comparator where Self: Sized { - +unsafe trait InternalComparator: Comparator +where + Self: Sized, +{ extern "C" fn name(state: *mut c_void) -> *const c_char { let x = unsafe { &*(state as *mut Self) }; x.name() } - extern "C" fn compare(state: *mut c_void, - a: *const c_char, - a_len: size_t, - b: *const c_char, - b_len: size_t) - -> i32 { + extern "C" fn compare( + state: *mut c_void, + a: *const c_char, + a_len: size_t, + b: *const c_char, + b_len: size_t, + ) -> i32 { unsafe { let a_slice = slice::from_raw_parts::(a as *const u8, a_len as usize); let b_slice = slice::from_raw_parts::(b as *const u8, b_len as usize); @@ -80,7 +83,7 @@ unsafe trait InternalComparator : Comparator where Self: Sized { extern "C" fn destructor(state: *mut c_void) { let _x: Box = unsafe { Box::from_raw(state as *mut Self) }; - // let the Box fall out of scope and run the T's destructor + // let the Box fall out of scope and run the T's destructor } } @@ -89,15 +92,17 @@ unsafe impl InternalComparator for C {} #[allow(missing_docs)] pub fn create_comparator(x: Box) -> *mut leveldb_comparator_t { unsafe { - leveldb_comparator_create(Box::into_raw(x) as *mut c_void, - ::destructor, - ::compare, - ::name) + leveldb_comparator_create( + Box::into_raw(x) as *mut c_void, + ::destructor, + ::compare, + ::name, + ) } } impl Comparator for OrdComparator { - type K = K; + type K = K; fn name(&self) -> *const c_char { let slice: &str = self.name.as_ref(); @@ -110,7 +115,7 @@ impl Comparator for OrdComparator { } impl Comparator for DefaultComparator { - type K = i32; + type K = i32; fn name(&self) -> *const c_char { "default_comparator".as_ptr() as *const c_char diff --git a/src/database/error.rs b/src/database/error.rs index 0915b95..b303a14 100644 --- a/src/database/error.rs +++ b/src/database/error.rs @@ -1,9 +1,9 @@ //! The module defining custom leveldb error type. -use libc::c_void; use leveldb_sys::leveldb_free; -use std; use libc::c_char; +use libc::c_void; +use std; /// A leveldb error, just containing the error string /// provided by leveldb. @@ -23,10 +23,12 @@ impl Error { /// This method is `unsafe` because the pointer must be valid and point to heap. /// The pointer will be passed to `free`! pub unsafe fn new_from_char(message: *const c_char) -> Error { - use std::str::from_utf8; use std::ffi::CStr; + use std::str::from_utf8; - let err_string = from_utf8(CStr::from_ptr(message).to_bytes()).unwrap().to_string(); + let err_string = from_utf8(CStr::from_ptr(message).to_bytes()) + .unwrap() + .to_string(); leveldb_free(message as *mut c_void); Error::new(err_string) } diff --git a/src/database/iterator.rs b/src/database/iterator.rs index bd57f3b..65f0061 100644 --- a/src/database/iterator.rs +++ b/src/database/iterator.rs @@ -2,17 +2,17 @@ //! //! Iteration is one of the most important parts of leveldb. This module provides //! Iterators to iterate over key, values and pairs of both. -use leveldb_sys::{leveldb_iterator_t, leveldb_iter_seek_to_first, leveldb_iter_destroy, - leveldb_iter_seek_to_last, leveldb_create_iterator, leveldb_iter_valid, - leveldb_iter_next, leveldb_iter_key, leveldb_iter_value, - leveldb_readoptions_destroy, leveldb_iter_seek}; -use libc::{size_t, c_char}; -use std::iter; +use super::key::{from_u8, Key}; +use super::options::{c_readoptions, ReadOptions}; use super::Database; -use super::options::{ReadOptions, c_readoptions}; -use super::key::{Key, from_u8}; -use std::slice::from_raw_parts; +use leveldb_sys::{ + leveldb_create_iterator, leveldb_iter_destroy, leveldb_iter_key, leveldb_iter_next, leveldb_iter_seek, leveldb_iter_seek_to_first, leveldb_iter_seek_to_last, + leveldb_iter_valid, leveldb_iter_value, leveldb_iterator_t, leveldb_readoptions_destroy, +}; +use libc::{c_char, size_t}; +use std::iter; use std::marker::PhantomData; +use std::slice::from_raw_parts; #[allow(missing_docs)] struct RawIterator { @@ -54,7 +54,6 @@ pub struct ValueIterator<'a, K: Key + 'a> { inner: Iterator<'a, K>, } - /// A trait to allow access to the three main iteration styles of leveldb. pub trait Iterable<'a, K: Key + 'a> { /// Return an Iterator iterating over (Key,Value) pairs @@ -103,7 +102,6 @@ pub trait LevelDBIterator<'a, K: Key> { fn advance(&mut self) -> bool { unsafe { if !self.start() { - leveldb_iter_next(self.raw_iterator()); } else { if let Some(k) = self.from_key() { @@ -148,15 +146,16 @@ pub trait LevelDBIterator<'a, K: Key> { fn seek(&self, key: &K) { unsafe { key.as_slice(|k| { - leveldb_iter_seek(self.raw_iterator(), - k.as_ptr() as *mut c_char, - k.len() as size_t); + leveldb_iter_seek( + self.raw_iterator(), + k.as_ptr() as *mut c_char, + k.len() as size_t, + ); }) } } } - impl<'a, K: Key> Iterator<'a, K> { fn new(database: &'a Database, options: ReadOptions<'a, K>) -> Iterator<'a, K> { unsafe { @@ -181,7 +180,7 @@ impl<'a, K: Key> Iterator<'a, K> { } } -impl<'a, K: Key> LevelDBIterator<'a, K> for Iterator<'a,K> { +impl<'a, K: Key> LevelDBIterator<'a, K> for Iterator<'a, K> { #[inline] fn raw_iterator(&self) -> *mut leveldb_iterator_t { self.iter.ptr @@ -216,9 +215,11 @@ impl<'a, K: Key> LevelDBIterator<'a, K> for Iterator<'a,K> { } } -impl<'a,K: Key> KeyIterator<'a,K> { +impl<'a, K: Key> KeyIterator<'a, K> { fn new(database: &'a Database, options: ReadOptions<'a, K>) -> KeyIterator<'a, K> { - KeyIterator { inner: Iterator::new(database, options) } + KeyIterator { + inner: Iterator::new(database, options), + } } /// return the last element of the iterator @@ -228,7 +229,7 @@ impl<'a,K: Key> KeyIterator<'a,K> { } } -impl<'a,K: Key> LevelDBIterator<'a, K> for KeyIterator<'a,K> { +impl<'a, K: Key> LevelDBIterator<'a, K> for KeyIterator<'a, K> { #[inline] fn raw_iterator(&self) -> *mut leveldb_iterator_t { self.inner.iter.ptr @@ -263,9 +264,11 @@ impl<'a,K: Key> LevelDBIterator<'a, K> for KeyIterator<'a,K> { } } -impl<'a,K: Key> ValueIterator<'a,K> { +impl<'a, K: Key> ValueIterator<'a, K> { fn new(database: &'a Database, options: ReadOptions<'a, K>) -> ValueIterator<'a, K> { - ValueIterator { inner: Iterator::new(database, options) } + ValueIterator { + inner: Iterator::new(database, options), + } } /// return the last element of the iterator @@ -275,7 +278,7 @@ impl<'a,K: Key> ValueIterator<'a,K> { } } -impl<'a,K: Key> LevelDBIterator<'a, K> for ValueIterator<'a,K> { +impl<'a, K: Key> LevelDBIterator<'a, K> for ValueIterator<'a, K> { #[inline] fn raw_iterator(&self) -> *mut leveldb_iterator_t { self.inner.iter.ptr @@ -310,8 +313,8 @@ impl<'a,K: Key> LevelDBIterator<'a, K> for ValueIterator<'a,K> { } } -impl<'a,K: Key> iter::Iterator for Iterator<'a,K> { - type Item = (K,Vec); +impl<'a, K: Key> iter::Iterator for Iterator<'a, K> { + type Item = (K, Vec); fn next(&mut self) -> Option<(K, Vec)> { if self.advance() { diff --git a/src/database/kv.rs b/src/database/kv.rs index 250a51d..5d3c3ef 100644 --- a/src/database/kv.rs +++ b/src/database/kv.rs @@ -2,14 +2,14 @@ use super::Database; -use options::{WriteOptions, ReadOptions, c_writeoptions, c_readoptions}; +use super::bytes::Bytes; use super::error::Error; use database::key::Key; -use std::ptr; -use std::borrow::Borrow; -use libc::{c_char, size_t}; use leveldb_sys::*; -use super::bytes::Bytes; +use libc::{c_char, size_t}; +use options::{c_readoptions, c_writeoptions, ReadOptions, WriteOptions}; +use std::borrow::Borrow; +use std::ptr; /// Key-Value-Access to the leveldb database, providing /// a basic interface. @@ -17,7 +17,11 @@ pub trait KV { /// get a value from the database. /// /// The passed key will be compared using the comparator. - fn get<'a, BK: Borrow>(&self, options: ReadOptions<'a, K>, key: BK) -> Result>, Error>; + fn get<'a, BK: Borrow>( + &self, + options: ReadOptions<'a, K>, + key: BK, + ) -> Result>, Error>; /// get a value from the database. /// @@ -25,7 +29,11 @@ pub trait KV { /// /// This version returns bytes allocated by leveldb without converting to `Vec`, which may /// lead to better performance. - fn get_bytes<'a, BK: Borrow>(&self, options: ReadOptions<'a, K>, key: BK) -> Result, Error>; + fn get_bytes<'a, BK: Borrow>( + &self, + options: ReadOptions<'a, K>, + key: BK, + ) -> Result, Error>; /// put a binary value into the database. /// /// If the key is already present in the database, it will be overwritten. @@ -34,7 +42,8 @@ pub trait KV { /// /// The database will be synced to disc if `options.sync == true`. This is /// NOT the default. - fn put>(&self, options: WriteOptions, key: BK, value: &[u8]) -> Result<(), Error>; + fn put>(&self, options: WriteOptions, key: BK, value: &[u8]) + -> Result<(), Error>; /// delete a value from the database. /// /// The passed key will be compared using the comparator. @@ -53,18 +62,25 @@ impl KV for Database { /// /// The database will be synced to disc if `options.sync == true`. This is /// NOT the default. - fn put>(&self, options: WriteOptions, key: BK, value: &[u8]) -> Result<(), Error> { + fn put>( + &self, + options: WriteOptions, + key: BK, + value: &[u8], + ) -> Result<(), Error> { unsafe { key.borrow().as_slice(|k| { let mut error = ptr::null_mut(); let c_writeoptions = c_writeoptions(options); - leveldb_put(self.database.ptr, - c_writeoptions, - k.as_ptr() as *mut c_char, - k.len() as size_t, - value.as_ptr() as *mut c_char, - value.len() as size_t, - &mut error); + leveldb_put( + self.database.ptr, + c_writeoptions, + k.as_ptr() as *mut c_char, + k.len() as size_t, + value.as_ptr() as *mut c_char, + value.len() as size_t, + &mut error, + ); leveldb_writeoptions_destroy(c_writeoptions); if error == ptr::null_mut() { @@ -87,11 +103,13 @@ impl KV for Database { key.borrow().as_slice(|k| { let mut error = ptr::null_mut(); let c_writeoptions = c_writeoptions(options); - leveldb_delete(self.database.ptr, - c_writeoptions, - k.as_ptr() as *mut c_char, - k.len() as size_t, - &mut error); + leveldb_delete( + self.database.ptr, + c_writeoptions, + k.as_ptr() as *mut c_char, + k.len() as size_t, + &mut error, + ); leveldb_writeoptions_destroy(c_writeoptions); if error == ptr::null_mut() { Ok(()) @@ -102,18 +120,24 @@ impl KV for Database { } } - fn get_bytes<'a, BK: Borrow>(&self, options: ReadOptions<'a, K>, key: BK) -> Result, Error> { + fn get_bytes<'a, BK: Borrow>( + &self, + options: ReadOptions<'a, K>, + key: BK, + ) -> Result, Error> { unsafe { key.borrow().as_slice(|k| { let mut error = ptr::null_mut(); let mut length: size_t = 0; let c_readoptions = c_readoptions(&options); - let result = leveldb_get(self.database.ptr, - c_readoptions, - k.as_ptr() as *mut c_char, - k.len() as size_t, - &mut length, - &mut error); + let result = leveldb_get( + self.database.ptr, + c_readoptions, + k.as_ptr() as *mut c_char, + k.len() as size_t, + &mut length, + &mut error, + ); leveldb_readoptions_destroy(c_readoptions); if error == ptr::null_mut() { @@ -125,7 +149,11 @@ impl KV for Database { } } - fn get<'a, BK: Borrow>(&self, options: ReadOptions<'a, K>, key: BK) -> Result>, Error> { + fn get<'a, BK: Borrow>( + &self, + options: ReadOptions<'a, K>, + key: BK, + ) -> Result>, Error> { self.get_bytes(options, key).map(|val| val.map(Into::into)) } } diff --git a/src/database/management.rs b/src/database/management.rs index b29d58d..542a4df 100644 --- a/src/database/management.rs +++ b/src/database/management.rs @@ -1,10 +1,10 @@ //! Management functions, e.g. for destroying and reparing a database. -use options::{Options, c_options}; use error::Error; +use libc::c_char; +use options::{c_options, Options}; use std::ffi::CString; -use std::ptr; use std::path::Path; -use libc::c_char; +use std::ptr; use leveldb_sys::{leveldb_destroy_db, leveldb_repair_db}; @@ -14,9 +14,11 @@ pub fn destroy(name: &Path, options: Options) -> Result<(), Error> { unsafe { let c_string = CString::new(name.to_str().unwrap()).unwrap(); let c_options = c_options(&options, None); - leveldb_destroy_db(c_options, - c_string.as_bytes_with_nul().as_ptr() as *const c_char, - &mut error); + leveldb_destroy_db( + c_options, + c_string.as_bytes_with_nul().as_ptr() as *const c_char, + &mut error, + ); if error == ptr::null_mut() { Ok(()) @@ -32,9 +34,11 @@ pub fn repair(name: &Path, options: Options) -> Result<(), Error> { unsafe { let c_string = CString::new(name.to_str().unwrap()).unwrap(); let c_options = c_options(&options, None); - leveldb_repair_db(c_options, - c_string.as_bytes_with_nul().as_ptr() as *const c_char, - &mut error); + leveldb_repair_db( + c_options, + c_string.as_bytes_with_nul().as_ptr() as *const c_char, + &mut error, + ); if error == ptr::null_mut() { Ok(()) diff --git a/src/database/mod.rs b/src/database/mod.rs index cd17f00..6644190 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -4,30 +4,30 @@ extern crate db_key as key; use leveldb_sys::*; -use self::options::{Options, c_options}; use self::error::Error; +use self::options::{c_options, Options}; use std::ffi::CString; use std::path::Path; -use std::ptr; -use comparator::{Comparator, create_comparator}; use self::key::Key; +use comparator::{create_comparator, Comparator}; +use std::ptr; -use std::marker::PhantomData; use libc::c_char; +use std::marker::PhantomData; -pub mod options; +pub mod batch; +pub mod bytes; +pub mod cache; +pub mod compaction; +pub mod comparator; pub mod error; pub mod iterator; -pub mod comparator; -pub mod snapshots; -pub mod cache; pub mod kv; -pub mod batch; pub mod management; -pub mod compaction; -pub mod bytes; +pub mod options; +pub mod snapshots; #[allow(missing_docs)] struct RawDB { @@ -85,10 +85,11 @@ unsafe impl Sync for Database {} unsafe impl Send for Database {} impl Database { - fn new(database: *mut leveldb_t, - options: Options, - comparator: Option<*mut leveldb_comparator_t>) - -> Database { + fn new( + database: *mut leveldb_t, + options: Options, + comparator: Option<*mut leveldb_comparator_t>, + ) -> Database { let raw_comp = match comparator { Some(p) => Some(RawComparator { ptr: p }), None => None, @@ -110,9 +111,11 @@ impl Database { unsafe { let c_string = CString::new(name.to_str().unwrap()).unwrap(); let c_options = c_options(&options, None); - let db = leveldb_open(c_options as *const leveldb_options_t, - c_string.as_bytes_with_nul().as_ptr() as *const c_char, - &mut error); + let db = leveldb_open( + c_options as *const leveldb_options_t, + c_string.as_bytes_with_nul().as_ptr() as *const c_char, + &mut error, + ); leveldb_options_destroy(c_options); if error == ptr::null_mut() { @@ -131,18 +134,21 @@ impl Database { /// The comparator must implement a total ordering over the keyspace. /// /// For keys that implement Ord, consider the `OrdComparator`. - pub fn open_with_comparator>(name: &Path, - options: Options, - comparator: C) - -> Result, Error> { + pub fn open_with_comparator>( + name: &Path, + options: Options, + comparator: C, + ) -> Result, Error> { let mut error = ptr::null_mut(); let comp_ptr = create_comparator(Box::new(comparator)); unsafe { let c_string = CString::new(name.to_str().unwrap()).unwrap(); let c_options = c_options(&options, Some(comp_ptr)); - let db = leveldb_open(c_options as *const leveldb_options_t, - c_string.as_bytes_with_nul().as_ptr() as *const c_char, - &mut error); + let db = leveldb_open( + c_options as *const leveldb_options_t, + c_string.as_bytes_with_nul().as_ptr() as *const c_char, + &mut error, + ); leveldb_options_destroy(c_options); if error == ptr::null_mut() { diff --git a/src/database/options.rs b/src/database/options.rs index 97242ee..a4cb5ad 100644 --- a/src/database/options.rs +++ b/src/database/options.rs @@ -6,10 +6,10 @@ //! * `WriteOptions`: used when writng to leveldb use leveldb_sys::*; -use libc::size_t; -use database::snapshots::Snapshot; -use database::key::Key; use database::cache::Cache; +use database::key::Key; +use database::snapshots::Snapshot; +use libc::size_t; /// Options to consider when opening a new or pre-existing database. /// @@ -76,7 +76,7 @@ impl Options { } /// The write options to use for a write operation. -#[derive(Copy,Clone)] +#[derive(Copy, Clone)] pub struct WriteOptions { /// `fsync` before acknowledging a write operation. /// @@ -124,9 +124,10 @@ impl<'a, K: Key + 'a> ReadOptions<'a, K> { } #[allow(missing_docs)] -pub unsafe fn c_options(options: &Options, - comparator: Option<*mut leveldb_comparator_t>) - -> *mut leveldb_options_t { +pub unsafe fn c_options( + options: &Options, + comparator: Option<*mut leveldb_comparator_t>, +) -> *mut leveldb_options_t { let c_options = leveldb_options_create(); leveldb_options_set_create_if_missing(c_options, options.create_if_missing as u8); leveldb_options_set_error_if_exists(c_options, options.error_if_exists as u8); @@ -162,7 +163,8 @@ pub unsafe fn c_writeoptions(options: WriteOptions) -> *mut leveldb_writeoptions #[allow(missing_docs)] pub unsafe fn c_readoptions<'a, K>(options: &ReadOptions<'a, K>) -> *mut leveldb_readoptions_t - where K: Key +where + K: Key, { let c_readoptions = leveldb_readoptions_create(); leveldb_readoptions_set_verify_checksums(c_readoptions, options.verify_checksums as u8); diff --git a/src/database/snapshots.rs b/src/database/snapshots.rs index b131ba1..d62aeb6 100644 --- a/src/database/snapshots.rs +++ b/src/database/snapshots.rs @@ -2,16 +2,16 @@ //! //! Snapshots give you a reference to the database at a certain //! point in time and won't change while you work with them. -use leveldb_sys::{leveldb_t, leveldb_snapshot_t}; -use leveldb_sys::{leveldb_release_snapshot, leveldb_create_snapshot}; +use leveldb_sys::{leveldb_create_snapshot, leveldb_release_snapshot}; +use leveldb_sys::{leveldb_snapshot_t, leveldb_t}; use database::key::Key; -use database::Database; use database::kv::KV; +use database::Database; use database::error::Error; -use database::options::ReadOptions; use database::iterator::{Iterable, Iterator, KeyIterator, ValueIterator}; +use database::options::ReadOptions; use std::borrow::Borrow; @@ -64,10 +64,11 @@ impl<'a, K: Key> Snapshot<'a, K> { /// fetches a key from the database /// /// Inserts this snapshot into ReadOptions before reading - pub fn get>(&'a self, - mut options: ReadOptions<'a, K>, - key: BK) - -> Result>, Error> { + pub fn get>( + &'a self, + mut options: ReadOptions<'a, K>, + key: BK, + ) -> Result>, Error> { options.snapshot = Some(self); self.database.get(options, key) } diff --git a/src/lib.rs b/src/lib.rs index d6672f2..034809c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,19 +43,19 @@ #![crate_name = "leveldb"] #![deny(missing_docs)] -extern crate libc; extern crate leveldb_sys; +extern crate libc; -use leveldb_sys::{leveldb_major_version, leveldb_minor_version}; -pub use database::options; +pub use database::batch; +pub use database::compaction; +pub use database::comparator; pub use database::error; pub use database::iterator; -pub use database::snapshots; -pub use database::comparator; pub use database::kv; -pub use database::batch; pub use database::management; -pub use database::compaction; +pub use database::options; +pub use database::snapshots; +use leveldb_sys::{leveldb_major_version, leveldb_minor_version}; #[allow(missing_docs)] pub mod database; diff --git a/tests/binary.rs b/tests/binary.rs index f25632e..56707b5 100644 --- a/tests/binary.rs +++ b/tests/binary.rs @@ -1,57 +1,53 @@ -use utils::{open_database,tmpdir,db_put_simple}; -use leveldb::options::{ReadOptions,WriteOptions}; -use leveldb::database::kv::{KV}; +use leveldb::database::kv::KV; +use leveldb::options::{ReadOptions, WriteOptions}; +use utils::{db_put_simple, open_database, tmpdir}; #[test] fn test_write_to_database() { - let tmp = tmpdir("write"); - let database = open_database(tmp.path(), true); - let write_opts = WriteOptions::new(); - let result = database.put(write_opts, - 1, - &[1]); - assert!(result.is_ok()); + let tmp = tmpdir("write"); + let database = open_database(tmp.path(), true); + let write_opts = WriteOptions::new(); + let result = database.put(write_opts, 1, &[1]); + assert!(result.is_ok()); } #[test] fn test_delete_from_database() { - let tmp = tmpdir("delete_simple"); - let database = &mut open_database(tmp.path(), true); - db_put_simple(database, 1, &[1]); + let tmp = tmpdir("delete_simple"); + let database = &mut open_database(tmp.path(), true); + db_put_simple(database, 1, &[1]); - let write2 = WriteOptions::new(); - let res2 = database.delete(write2, - 1); - assert!(res2.is_ok()); + let write2 = WriteOptions::new(); + let res2 = database.delete(write2, 1); + assert!(res2.is_ok()); } #[test] fn test_get_from_empty_database() { - let tmp = tmpdir("get_simple"); - let database = &mut open_database(tmp.path(), true); - let read_opts = ReadOptions::new(); - let res = database.get(read_opts, 1); - match res { - Ok(data) => { assert!(data.is_none()) }, - Err(_) => { panic!("failed reading data") } - } + let tmp = tmpdir("get_simple"); + let database = &mut open_database(tmp.path(), true); + let read_opts = ReadOptions::new(); + let res = database.get(read_opts, 1); + match res { + Ok(data) => assert!(data.is_none()), + Err(_) => panic!("failed reading data"), + } } #[test] fn test_get_from_filled_database() { - let tmp = tmpdir("get_filled"); - let database = &mut open_database(tmp.path(), true); - db_put_simple(database, 1, &[1]); + let tmp = tmpdir("get_filled"); + let database = &mut open_database(tmp.path(), true); + db_put_simple(database, 1, &[1]); - let read_opts = ReadOptions::new(); - let res = database.get(read_opts, - 1); - match res { - Ok(data) => { - assert!(data.is_some()); - let data = data.unwrap(); - assert_eq!(data, vec!(1)); - }, - Err(_) => { panic!("failed reading data") } - } + let read_opts = ReadOptions::new(); + let res = database.get(read_opts, 1); + match res { + Ok(data) => { + assert!(data.is_some()); + let data = data.unwrap(); + assert_eq!(data, vec!(1)); + } + Err(_) => panic!("failed reading data"), + } } diff --git a/tests/cache.rs b/tests/cache.rs index 47bc681..aae830c 100644 --- a/tests/cache.rs +++ b/tests/cache.rs @@ -1,14 +1,14 @@ -use utils::{tmpdir}; -use leveldb::database::{Database}; -use leveldb::options::{Options}; -use leveldb::database::cache::{Cache}; +use leveldb::database::cache::Cache; +use leveldb::database::Database; +use leveldb::options::Options; +use utils::tmpdir; #[test] fn test_open_database_with_cache() { - let mut opts = Options::new(); - opts.create_if_missing = true; - opts.cache = Some(Cache::new(20)); - let tmp = tmpdir("create_if_missing"); - let res: Result,_> = Database::open(tmp.path(), opts); - assert!(res.is_ok()); + let mut opts = Options::new(); + opts.create_if_missing = true; + opts.cache = Some(Cache::new(20)); + let tmp = tmpdir("create_if_missing"); + let res: Result, _> = Database::open(tmp.path(), opts); + assert!(res.is_ok()); } diff --git a/tests/compaction.rs b/tests/compaction.rs index cdf9f13..73f1841 100644 --- a/tests/compaction.rs +++ b/tests/compaction.rs @@ -1,7 +1,7 @@ #[cfg(test)] mod compaction { - use utils::{open_database,tmpdir,db_put_simple}; - use leveldb::compaction::Compaction; + use leveldb::compaction::Compaction; + use utils::{db_put_simple, open_database, tmpdir}; #[test] fn test_iterator_from_to() { diff --git a/tests/comparator.rs b/tests/comparator.rs index f2c0236..1eb4862 100644 --- a/tests/comparator.rs +++ b/tests/comparator.rs @@ -1,62 +1,64 @@ #[cfg(test)] mod comparator { - use libc::c_char; - use key::Key; - use utils::{tmpdir, db_put_simple}; - use leveldb::database::{Database}; - use leveldb::iterator::Iterable; - use leveldb::options::{Options,ReadOptions}; - use leveldb::comparator::{Comparator,OrdComparator}; - use std::cmp::Ordering; - use std::marker::PhantomData; - - struct ReverseComparator { - marker: PhantomData - } - - impl Comparator for ReverseComparator { - type K = K; - - fn name(&self) -> *const c_char { - "reverse".as_ptr() as *const c_char + use key::Key; + use leveldb::comparator::{Comparator, OrdComparator}; + use leveldb::database::Database; + use leveldb::iterator::Iterable; + use leveldb::options::{Options, ReadOptions}; + use libc::c_char; + use std::cmp::Ordering; + use std::marker::PhantomData; + use utils::{db_put_simple, tmpdir}; + + struct ReverseComparator { + marker: PhantomData, + } + + impl Comparator for ReverseComparator { + type K = K; + + fn name(&self) -> *const c_char { + "reverse".as_ptr() as *const c_char + } + + fn compare(&self, a: &K, b: &K) -> Ordering { + b.cmp(a) + } + } + + #[test] + fn test_comparator() { + let comparator: ReverseComparator = ReverseComparator { + marker: PhantomData, + }; + let mut opts = Options::new(); + opts.create_if_missing = true; + let tmp = tmpdir("reverse_comparator"); + let database = &mut Database::open_with_comparator(tmp.path(), opts, comparator).unwrap(); + db_put_simple(database, 1, &[1]); + db_put_simple(database, 2, &[2]); + + let read_opts = ReadOptions::new(); + let mut iter = database.iter(read_opts); + + assert_eq!((2, vec![2]), iter.next().unwrap()); + assert_eq!((1, vec![1]), iter.next().unwrap()); } - - fn compare(&self, a: &K, b: &K) -> Ordering { - b.cmp(a) + + #[test] + fn test_ord_comparator() { + let comparator: OrdComparator = OrdComparator::new("foo"); + let mut opts = Options::new(); + opts.create_if_missing = true; + let tmp = tmpdir("ord_comparator"); + let database = &mut Database::open_with_comparator(tmp.path(), opts, comparator).unwrap(); + db_put_simple(database, 1, &[1]); + db_put_simple(database, 2, &[2]); + + let read_opts = ReadOptions::new(); + let mut iter = database.iter(read_opts); + + assert_eq!((1, vec![1]), iter.next().unwrap()); + assert_eq!((2, vec![2]), iter.next().unwrap()); } - } - - #[test] - fn test_comparator() { - let comparator: ReverseComparator = ReverseComparator { marker: PhantomData }; - let mut opts = Options::new(); - opts.create_if_missing = true; - let tmp = tmpdir("reverse_comparator"); - let database = &mut Database::open_with_comparator(tmp.path(), opts, comparator).unwrap(); - db_put_simple(database, 1, &[1]); - db_put_simple(database, 2, &[2]); - - let read_opts = ReadOptions::new(); - let mut iter = database.iter(read_opts); - - assert_eq!((2, vec![2]), iter.next().unwrap()); - assert_eq!((1, vec![1]), iter.next().unwrap()); - } - - #[test] - fn test_ord_comparator() { - let comparator: OrdComparator = OrdComparator::new("foo"); - let mut opts = Options::new(); - opts.create_if_missing = true; - let tmp = tmpdir("ord_comparator"); - let database = &mut Database::open_with_comparator(tmp.path(), opts, comparator).unwrap(); - db_put_simple(database, 1, &[1]); - db_put_simple(database, 2, &[2]); - - let read_opts = ReadOptions::new(); - let mut iter = database.iter(read_opts); - - assert_eq!((1, vec![1]), iter.next().unwrap()); - assert_eq!((2, vec![2]), iter.next().unwrap()); - } } diff --git a/tests/concurrent_access.rs b/tests/concurrent_access.rs index 541163f..d43b4bb 100644 --- a/tests/concurrent_access.rs +++ b/tests/concurrent_access.rs @@ -1,6 +1,6 @@ -use utils::{tmpdir,open_database}; -use leveldb::options::{Options,WriteOptions}; use leveldb::database::kv::KV; +use leveldb::options::{Options, WriteOptions}; +use utils::{open_database, tmpdir}; #[test] fn access_from_threads() { @@ -14,17 +14,18 @@ fn access_from_threads() { let database = open_database(tmp.path(), true); let shared = Arc::new(database); - (0..10).map(|i| { - let local_db = shared.clone(); + (0..10) + .map(|i| { + let local_db = shared.clone(); - thread::spawn(move || { - let write_opts = WriteOptions::new(); - match local_db.put(write_opts, i, &[i as u8]) { - Ok(_) => { }, - Err(e) => { panic!("failed to write to database: {:?}", e) } - } - }) - }) - .map(JoinHandle::join) - .collect::>(); -} \ No newline at end of file + thread::spawn(move || { + let write_opts = WriteOptions::new(); + match local_db.put(write_opts, i, &[i as u8]) { + Ok(_) => {} + Err(e) => panic!("failed to write to database: {:?}", e), + } + }) + }) + .map(JoinHandle::join) + .collect::>(); +} diff --git a/tests/database.rs b/tests/database.rs index a654c11..26df927 100644 --- a/tests/database.rs +++ b/tests/database.rs @@ -1,26 +1,26 @@ -use utils::{tmpdir}; -use leveldb::database::{Database}; -use leveldb::options::{Options}; +use leveldb::database::Database; +use leveldb::options::Options; +use utils::tmpdir; #[test] fn test_create_options() { - Options::new(); + Options::new(); } #[test] fn test_open_database() { - let mut opts = Options::new(); - opts.create_if_missing = true; - let tmp = tmpdir("create_if_missing"); - let res: Result,_> = Database::open(tmp.path(), opts); - assert!(res.is_ok()); + let mut opts = Options::new(); + opts.create_if_missing = true; + let tmp = tmpdir("create_if_missing"); + let res: Result, _> = Database::open(tmp.path(), opts); + assert!(res.is_ok()); } #[test] fn test_open_non_existant_database_without_create() { - let mut opts = Options::new(); - opts.create_if_missing = false; - let tmp = tmpdir("missing"); - let res: Result,_> = Database::open(tmp.path(), opts); - assert!(res.is_err()); + let mut opts = Options::new(); + opts.create_if_missing = false; + let tmp = tmpdir("missing"); + let res: Result, _> = Database::open(tmp.path(), opts); + assert!(res.is_err()); } diff --git a/tests/iterator.rs b/tests/iterator.rs index fd958b5..c60cac5 100644 --- a/tests/iterator.rs +++ b/tests/iterator.rs @@ -1,86 +1,85 @@ -use utils::{open_database,tmpdir,db_put_simple}; use leveldb::iterator::Iterable; use leveldb::iterator::LevelDBIterator; -use leveldb::options::{ReadOptions}; +use leveldb::options::ReadOptions; +use utils::{db_put_simple, open_database, tmpdir}; #[test] fn test_iterator() { - let tmp = tmpdir("iter"); - let database = &mut open_database(tmp.path(), true); - db_put_simple(database, 1, &[1]); - db_put_simple(database, 2, &[2]); - - let read_opts = ReadOptions::new(); - let mut iter = database.iter(read_opts); - - let entry = iter.next(); - assert!(entry.is_some()); - assert_eq!(entry.unwrap(), (1, vec![1])); - let entry2 = iter.next(); - assert!(entry2.is_some()); - assert_eq!(entry2.unwrap(), (2, vec![2])); - assert!(iter.next().is_none()); + let tmp = tmpdir("iter"); + let database = &mut open_database(tmp.path(), true); + db_put_simple(database, 1, &[1]); + db_put_simple(database, 2, &[2]); + + let read_opts = ReadOptions::new(); + let mut iter = database.iter(read_opts); + + let entry = iter.next(); + assert!(entry.is_some()); + assert_eq!(entry.unwrap(), (1, vec![1])); + let entry2 = iter.next(); + assert!(entry2.is_some()); + assert_eq!(entry2.unwrap(), (2, vec![2])); + assert!(iter.next().is_none()); } #[test] fn test_iterator_last() { - let tmp = tmpdir("iter_last"); - let database = &mut open_database(tmp.path(), true); - db_put_simple(database, 1, &[1]); - db_put_simple(database, 2, &[2]); + let tmp = tmpdir("iter_last"); + let database = &mut open_database(tmp.path(), true); + db_put_simple(database, 1, &[1]); + db_put_simple(database, 2, &[2]); - let read_opts = ReadOptions::new(); - let iter = database.iter(read_opts); + let read_opts = ReadOptions::new(); + let iter = database.iter(read_opts); - assert!(iter.last().is_some()); + assert!(iter.last().is_some()); } #[test] fn test_iterator_from_to() { - let tmp = tmpdir("from_to"); - let database = &mut open_database(tmp.path(), true); - db_put_simple(database, 1, &[1]); - db_put_simple(database, 2, &[2]); - db_put_simple(database, 3, &[3]); - db_put_simple(database, 4, &[4]); - db_put_simple(database, 5, &[5]); - - let from = 2; - let to = 4; - let read_opts = ReadOptions::new(); - let mut iter = database.iter(read_opts).from(&from).to(&to); - - assert_eq!(iter.next().unwrap(), (2,vec![2])); - assert_eq!(iter.last().unwrap(), (4,vec![4])); + let tmp = tmpdir("from_to"); + let database = &mut open_database(tmp.path(), true); + db_put_simple(database, 1, &[1]); + db_put_simple(database, 2, &[2]); + db_put_simple(database, 3, &[3]); + db_put_simple(database, 4, &[4]); + db_put_simple(database, 5, &[5]); + + let from = 2; + let to = 4; + let read_opts = ReadOptions::new(); + let mut iter = database.iter(read_opts).from(&from).to(&to); + + assert_eq!(iter.next().unwrap(), (2, vec![2])); + assert_eq!(iter.last().unwrap(), (4, vec![4])); } - #[test] fn test_key_iterator() { - let tmp = tmpdir("key_iter"); - let database = &mut open_database(tmp.path(), true); - db_put_simple(database, 1, &[1]); - db_put_simple(database, 2, &[2]); + let tmp = tmpdir("key_iter"); + let database = &mut open_database(tmp.path(), true); + db_put_simple(database, 1, &[1]); + db_put_simple(database, 2, &[2]); - let iterable: &mut Iterable = database; + let iterable: &mut Iterable = database; - let read_opts = ReadOptions::new(); - let mut iter = iterable.keys_iter(read_opts); - let value = iter.next().unwrap(); - assert_eq!(value, 1); + let read_opts = ReadOptions::new(); + let mut iter = iterable.keys_iter(read_opts); + let value = iter.next().unwrap(); + assert_eq!(value, 1); } #[test] fn test_value_iterator() { - let tmp = tmpdir("value_iter"); - let database = &mut open_database(tmp.path(), true); - db_put_simple(database, 1, &[1]); - db_put_simple(database, 2, &[2]); + let tmp = tmpdir("value_iter"); + let database = &mut open_database(tmp.path(), true); + db_put_simple(database, 1, &[1]); + db_put_simple(database, 2, &[2]); - let iterable: &mut Iterable = database; + let iterable: &mut Iterable = database; - let read_opts = ReadOptions::new(); - let mut iter = iterable.value_iter(read_opts); - let value = iter.next().unwrap(); - assert_eq!(value, vec![1]); + let read_opts = ReadOptions::new(); + let mut iter = iterable.value_iter(read_opts); + let value = iter.next().unwrap(); + assert_eq!(value, vec![1]); } diff --git a/tests/management.rs b/tests/management.rs index 4ce1772..f4f3222 100644 --- a/tests/management.rs +++ b/tests/management.rs @@ -1,6 +1,6 @@ use leveldb::management::*; use leveldb::options::*; -use utils::{open_database,tmpdir}; +use utils::{open_database, tmpdir}; #[test] fn test_destroy_database() { diff --git a/tests/snapshots.rs b/tests/snapshots.rs index 10a079e..a88407e 100644 --- a/tests/snapshots.rs +++ b/tests/snapshots.rs @@ -1,32 +1,32 @@ -use utils::{open_database,tmpdir,db_put_simple}; +use leveldb::iterator::Iterable; +use leveldb::options::ReadOptions; use leveldb::snapshots::Snapshots; -use leveldb::options::{ReadOptions}; -use leveldb::iterator::{Iterable}; +use utils::{db_put_simple, open_database, tmpdir}; #[test] fn test_snapshots() { - let tmp = tmpdir("snapshots"); - let database = &mut open_database(tmp.path(), true); - db_put_simple(database, 1, &[1]); - let snapshot = database.snapshot(); - db_put_simple(database, 2, &[2]); - let read_opts = ReadOptions::new(); - let res = snapshot.get(read_opts, 2); - assert!(res.is_ok()); - assert_eq!(None, res.unwrap()); + let tmp = tmpdir("snapshots"); + let database = &mut open_database(tmp.path(), true); + db_put_simple(database, 1, &[1]); + let snapshot = database.snapshot(); + db_put_simple(database, 2, &[2]); + let read_opts = ReadOptions::new(); + let res = snapshot.get(read_opts, 2); + assert!(res.is_ok()); + assert_eq!(None, res.unwrap()); } #[test] fn test_snapshot_iterator() { - let tmp = tmpdir("snap_iterator"); - let database = &mut open_database(tmp.path(), true); - db_put_simple(database, 1, &[1]); - let snapshot = database.snapshot(); - db_put_simple(database, 2, &[2]); - let read_opts = ReadOptions::new(); - let mut iter = snapshot.keys_iter(read_opts); - let key = iter.next(); - assert_eq!(Some(1), key); - let next = iter.next(); - assert_eq!(None, next); + let tmp = tmpdir("snap_iterator"); + let database = &mut open_database(tmp.path(), true); + db_put_simple(database, 1, &[1]); + let snapshot = database.snapshot(); + db_put_simple(database, 2, &[2]); + let read_opts = ReadOptions::new(); + let mut iter = snapshot.keys_iter(read_opts); + let key = iter.next(); + assert_eq!(Some(1), key); + let next = iter.next(); + assert_eq!(None, next); } diff --git a/tests/tests.rs b/tests/tests.rs index 3d6f386..73bfa49 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,16 +1,16 @@ extern crate db_key as key; extern crate leveldb; -extern crate tempdir; extern crate libc; +extern crate tempdir; -mod utils; -mod database; -mod comparator; mod binary; +mod cache; +mod compaction; +mod comparator; +mod concurrent_access; +mod database; mod iterator; +mod management; mod snapshots; -mod cache; +mod utils; mod writebatch; -mod management; -mod compaction; -mod concurrent_access; \ No newline at end of file diff --git a/tests/utils.rs b/tests/utils.rs index e3f471c..63add45 100644 --- a/tests/utils.rs +++ b/tests/utils.rs @@ -1,29 +1,27 @@ +use key::Key; +use leveldb::database::kv::KV; use leveldb::database::Database; -use leveldb::database::kv::{KV}; -use leveldb::options::{Options,WriteOptions}; +use leveldb::options::{Options, WriteOptions}; use std::path::Path; use tempdir::TempDir; -use key::Key; pub fn open_database(path: &Path, create_if_missing: bool) -> Database { - let mut opts = Options::new(); - opts.create_if_missing = create_if_missing; - match Database::open(path, opts) { - Ok(db) => { db }, - Err(e) => { panic!("failed to open database: {:?}", e) } - } + let mut opts = Options::new(); + opts.create_if_missing = create_if_missing; + match Database::open(path, opts) { + Ok(db) => db, + Err(e) => panic!("failed to open database: {:?}", e), + } } pub fn tmpdir(name: &str) -> TempDir { - TempDir::new(name) - .unwrap() + TempDir::new(name).unwrap() } pub fn db_put_simple(database: &Database, key: K, val: &[u8]) { - let write_opts = WriteOptions::new(); - match database.put(write_opts, key, val) { - Ok(_) => { () }, - Err(e) => { panic!("failed to write to database: {:?}", e) } - } + let write_opts = WriteOptions::new(); + match database.put(write_opts, key, val) { + Ok(_) => (), + Err(e) => panic!("failed to write to database: {:?}", e), + } } - diff --git a/tests/writebatch.rs b/tests/writebatch.rs index 48c1fec..02eb95f 100644 --- a/tests/writebatch.rs +++ b/tests/writebatch.rs @@ -1,8 +1,8 @@ -use utils::{tmpdir}; -use leveldb::database::{Database}; -use leveldb::options::{Options,ReadOptions,WriteOptions}; -use leveldb::database::kv::{KV}; -use leveldb::database::batch::{Batch,Writebatch,WritebatchIterator}; +use leveldb::database::batch::{Batch, Writebatch, WritebatchIterator}; +use leveldb::database::kv::KV; +use leveldb::database::Database; +use leveldb::options::{Options, ReadOptions, WriteOptions}; +use utils::tmpdir; #[test] fn test_writebatch() { @@ -26,15 +26,15 @@ fn test_writebatch() { assert!(data.is_some()); let data = data.unwrap(); assert_eq!(data, vec!(2)); - }, - Err(_) => { panic!("failed reading data") } + } + Err(_) => panic!("failed reading data"), } let read_opts2 = ReadOptions::new(); let res2 = database.get(read_opts2, 1); match res2 { - Ok(data) => { assert!(data.is_none()) }, - Err(_) => { panic!("failed reading data") } + Ok(data) => assert!(data.is_none()), + Err(_) => panic!("failed reading data"), } } @@ -46,14 +46,11 @@ struct Iter { impl WritebatchIterator for Iter { type K = i32; - fn put(&mut self, - _key: i32, - _value: &[u8]) { + fn put(&mut self, _key: i32, _value: &[u8]) { self.put = self.put + 1; } - fn deleted(&mut self, - _key: i32) { + fn deleted(&mut self, _key: i32) { self.deleted = self.deleted + 1; } }