Merge branch 'db' of github.com:ethcore/parity into db

This commit is contained in:
arkpar 2016-02-22 13:48:43 +01:00
commit 764f6e9347

View File

@ -31,12 +31,12 @@ impl DBTransaction {
} }
/// Insert a key-value pair in the transaction. Any existing value value will be overwritten upon write. /// Insert a key-value pair in the transaction. Any existing value value will be overwritten upon write.
pub fn put(&self, key: &[u8], value: &[u8]) -> Result<(), String> { pub fn put(&self, key: &[u8], value: &[u8]) -> Result<(), String> {
self.batch.put(key, value) self.batch.put(key, value)
} }
/// Delete value by key. /// Delete value by key.
pub fn delete(&self, key: &[u8]) -> Result<(), String> { pub fn delete(&self, key: &[u8]) -> Result<(), String> {
self.batch.delete(key) self.batch.delete(key)
} }
} }
@ -49,11 +49,11 @@ pub struct DatabaseConfig {
/// Database iterator /// Database iterator
pub struct DatabaseIterator<'a> { pub struct DatabaseIterator<'a> {
iter: DBIterator<'a>, iter: DBIterator<'a>,
} }
impl<'a> Iterator for DatabaseIterator<'a> { impl<'a> Iterator for DatabaseIterator<'a> {
type Item = (Box<[u8]>, Box<[u8]>); type Item = (Box<[u8]>, Box<[u8]>);
#[cfg_attr(feature="dev", allow(type_complexity))] #[cfg_attr(feature="dev", allow(type_complexity))]
fn next(&mut self) -> Option<(Box<[u8]>, Box<[u8]>)> { fn next(&mut self) -> Option<(Box<[u8]>, Box<[u8]>)> {
@ -69,7 +69,7 @@ pub struct Database {
impl Database { impl Database {
/// Open database with default settings. /// Open database with default settings.
pub fn open_default(path: &str) -> Result<Database, String> { pub fn open_default(path: &str) -> Result<Database, String> {
Database::open(&DatabaseConfig { prefix_size: None }, path) Database::open(&DatabaseConfig { prefix_size: None }, path)
} }
/// Open database file. Creates if it does not exist. /// Open database file. Creates if it does not exist.
@ -94,7 +94,8 @@ impl Database {
opts.set_max_background_compactions(4); opts.set_max_background_compactions(4);
opts.set_max_background_flushes(4); opts.set_max_background_flushes(4);
opts.set_filter_deletes(false); opts.set_filter_deletes(false);
opts.set_disable_auto_compactions(false);*/ opts.set_disable_auto_compactions(false);
*/
if let Some(size) = config.prefix_size { if let Some(size) = config.prefix_size {
let mut block_opts = BlockBasedOptions::new(); let mut block_opts = BlockBasedOptions::new();
@ -107,27 +108,27 @@ impl Database {
} }
/// Insert a key-value pair in the transaction. Any existing value value will be overwritten. /// Insert a key-value pair in the transaction. Any existing value value will be overwritten.
pub fn put(&self, key: &[u8], value: &[u8]) -> Result<(), String> { pub fn put(&self, key: &[u8], value: &[u8]) -> Result<(), String> {
self.db.put(key, value) self.db.put(key, value)
} }
/// Delete value by key. /// Delete value by key.
pub fn delete(&self, key: &[u8]) -> Result<(), String> { pub fn delete(&self, key: &[u8]) -> Result<(), String> {
self.db.delete(key) self.db.delete(key)
} }
/// Commit transaction to database. /// Commit transaction to database.
pub fn write(&self, tr: DBTransaction) -> Result<(), String> { pub fn write(&self, tr: DBTransaction) -> Result<(), String> {
self.db.write(tr.batch) self.db.write(tr.batch)
} }
/// Get value by key. /// Get value by key.
pub fn get(&self, key: &[u8]) -> Result<Option<DBVector>, String> { pub fn get(&self, key: &[u8]) -> Result<Option<DBVector>, String> {
self.db.get(key) self.db.get(key)
} }
/// Get value by partial key. Prefix size should match configured prefix size. /// Get value by partial key. Prefix size should match configured prefix size.
pub fn get_by_prefix(&self, prefix: &[u8]) -> Option<Box<[u8]>> { pub fn get_by_prefix(&self, prefix: &[u8]) -> Option<Box<[u8]>> {
let mut iter = self.db.iterator(IteratorMode::From(prefix, Direction::forward)); let mut iter = self.db.iterator(IteratorMode::From(prefix, Direction::forward));
match iter.next() { match iter.next() {
// TODO: use prefix_same_as_start read option (not availabele in C API currently) // TODO: use prefix_same_as_start read option (not availabele in C API currently)
@ -147,12 +148,11 @@ impl Database {
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use hash::*; use hash::*;
use super::*; use super::*;
use tests::helpers::RandomTempPath; use devtools::*;
use std::str::FromStr; use std::str::FromStr;
use std::ops::Deref; use std::ops::Deref;
@ -185,7 +185,7 @@ mod tests {
db.write(transaction).unwrap(); db.write(transaction).unwrap();
assert!(db.get(&key1).unwrap().is_none()); assert!(db.get(&key1).unwrap().is_none());
assert_eq!(db.get(&key3).unwrap().unwrap().deref(), b"elephant"); assert_eq!(db.get(&key3).unwrap().unwrap().deref(), b"elephant");
if config.prefix_size.is_some() { if config.prefix_size.is_some() {
assert_eq!(db.get_by_prefix(&key3).unwrap().deref(), b"elephant"); assert_eq!(db.get_by_prefix(&key3).unwrap().deref(), b"elephant");
assert_eq!(db.get_by_prefix(&key2).unwrap().deref(), b"dog"); assert_eq!(db.get_by_prefix(&key2).unwrap().deref(), b"dog");