Mercurial > lbo > hg > leveldb-rs
changeset 226:199a993cfe94
error/all: Introduce err() as replacement for Err(Status::new(...))
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sun, 10 Sep 2017 11:40:13 +0200 |
parents | 4b044ef081e0 |
children | 0144298e0a7c |
files | src/error.rs src/log.rs src/memtable.rs src/table_reader.rs src/version_edit.rs |
diffstat | 5 files changed, 38 insertions(+), 41 deletions(-) [+] |
line wrap: on
line diff
--- a/src/error.rs Sat Sep 09 19:27:25 2017 +0200 +++ b/src/error.rs Sun Sep 10 11:40:13 2017 +0200 @@ -67,6 +67,11 @@ /// LevelDB's result type pub type Result<T> = result::Result<T, Status>; +/// err returns a new Status wrapped in a Result. +pub fn err<T>(code: StatusCode, msg: &str) -> Result<T> { + Err(Status::new(code, msg)) +} + impl From<io::Error> for Status { fn from(e: io::Error) -> Status { let c = match e.kind() {
--- a/src/log.rs Sat Sep 09 19:27:25 2017 +0200 +++ b/src/log.rs Sun Sep 10 11:40:13 2017 +0200 @@ -3,7 +3,9 @@ //! A record is a bytestring: [checksum: uint32, length: uint16, type: uint8, data: [u8]] //! checksum is the crc32 sum of type and data; type is one of RecordType::{Full/First/Middle/Last} -use std::io::{Error, ErrorKind, Read, Result, Write}; +use error::{err, StatusCode, Result}; + +use std::io::{Error, ErrorKind, Read, Write}; use crc::crc32; use crc::Hasher32; @@ -38,7 +40,7 @@ } } - pub fn add_record(&mut self, r: &Vec<u8>) -> Result<usize> { + pub fn add_record(&mut self, r: &[u8]) -> Result<usize> { let mut record = &r[..]; let mut first_frag = true; let mut result = Ok(0); @@ -158,7 +160,7 @@ if self.checksums && !self.check_integrity(typ, &dst[dst_offset..dst_offset + bytes_read], checksum) { - return Err(Error::new(ErrorKind::InvalidData, "Invalid Checksum".to_string())); + return err(StatusCode::InvalidData, "Invalid Checksum"); } dst_offset += length as usize;
--- a/src/memtable.rs Sat Sep 09 19:27:25 2017 +0200 +++ b/src/memtable.rs Sun Sep 10 11:40:13 2017 +0200 @@ -1,6 +1,6 @@ use key_types::{LookupKey, UserKey}; use cmp::MemtableKeyCmp; -use error::{Status, StatusCode, Result}; +use error::{err, StatusCode, Result}; use key_types::{parse_memtable_key, build_memtable_key}; use types::{current_key_val, LdbIterator, SequenceNumber, ValueType}; use skipmap::{SkipMap, SkipMapIter}; @@ -57,11 +57,11 @@ if tag & 0xff == ValueType::TypeValue as u64 { return Ok(foundkey[valoff..valoff + vallen].to_vec()); } else { - return Err(Status::new(StatusCode::NotFound, "")); + return err(StatusCode::NotFound, ""); } } } - Err(Status::new(StatusCode::NotFound, "")) + err(StatusCode::NotFound, "") } pub fn iter(&self) -> MemtableIterator {
--- a/src/table_reader.rs Sat Sep 09 19:27:25 2017 +0200 +++ b/src/table_reader.rs Sun Sep 10 11:40:13 2017 +0200 @@ -3,7 +3,7 @@ use cache; use cmp::InternalKeyCmp; use env::RandomAccess; -use error::{Status, StatusCode, Result}; +use error::{err, StatusCode, Result}; use filter; use filter_block::FilterBlockReader; use key_types::InternalKey; @@ -90,8 +90,8 @@ try!(TableBlock::read_block(opt.clone(), file.as_ref().as_ref(), &footer.meta_index)); if !indexblock.verify() || !metaindexblock.verify() { - return Err(Status::new(StatusCode::InvalidData, - "Indexblock/Metaindexblock failed verification")); + return err(StatusCode::InvalidData, + "Indexblock/Metaindexblock failed verification"); } // Open filter block for reading @@ -157,7 +157,7 @@ try!(TableBlock::read_block(self.opt.clone(), self.file.as_ref().as_ref(), location)); if !b.verify() { - return Err(Status::new(StatusCode::InvalidData, "Data block failed verification")); + return err(StatusCode::InvalidData, "Data block failed verification"); } if let Ok(ref mut block_cache) = self.opt.block_cache.lock() { // insert a cheap copy (Rc). @@ -491,7 +491,7 @@ let table = Table::new_raw(opt, wrap_buffer(src), size).unwrap(); let mut iter = table.iter(); - let expected_offsets = vec![0,0,0,42,42,42,86]; + let expected_offsets = vec![0, 0, 0, 42, 42, 42, 86]; let mut i = 0; for (k, _) in LdbIteratorIter::wrap(&mut iter) { assert_eq!(expected_offsets[i], table.approx_offset_of(&k));
--- a/src/version_edit.rs Sat Sep 09 19:27:25 2017 +0200 +++ b/src/version_edit.rs Sun Sep 10 11:40:13 2017 +0200 @@ -1,13 +1,11 @@ -use error::{Status, StatusCode}; +use error::{err, Result, StatusCode}; use key_types::InternalKey; use types::{FileMetaData, FileNum, SequenceNumber}; -use integer_encoding::VarIntWriter; -use integer_encoding::VarIntReader; +use integer_encoding::{VarIntReader, VarIntWriter}; use std::collections::HashSet; -use std::io::Write; -use std::io::Read; +use std::io::{Read, Write}; #[derive(PartialEq, Debug, Clone)] struct CompactionPointer { @@ -42,21 +40,21 @@ } } -fn read_length_prefixed<R: Read>(reader: &mut R) -> Result<Vec<u8>, Status> { +fn read_length_prefixed<R: Read>(reader: &mut R) -> Result<Vec<u8>> { if let Ok(klen) = reader.read_varint() { let mut keybuf = Vec::new(); keybuf.resize(klen, 0); if let Ok(l) = reader.read(&mut keybuf) { if l != klen { - return Err(Status::new(StatusCode::IOError, "Couldn't read full key")); + return err(StatusCode::IOError, "Couldn't read full key"); } return Ok(keybuf); } else { - return Err(Status::new(StatusCode::IOError, "Couldn't read key")); + return err(StatusCode::IOError, "Couldn't read key"); } } else { - return Err(Status::new(StatusCode::IOError, "Couldn't read key length")); + return err(StatusCode::IOError, "Couldn't read key length"); } } @@ -182,7 +180,7 @@ buf } - pub fn decode_from(src: &[u8]) -> Result<VersionEdit, Status> { + pub fn decode_from(src: &[u8]) -> Result<VersionEdit> { let mut reader = src; let mut ve = VersionEdit::new(); @@ -194,8 +192,7 @@ if let Ok(c) = String::from_utf8(buf) { ve.comparator = Some(c); } else { - return Err(Status::new(StatusCode::Corruption, - "Bad comparator encoding")); + return err(StatusCode::Corruption, "Bad comparator encoding"); } } @@ -203,7 +200,7 @@ if let Ok(ln) = reader.read_varint() { ve.log_number = Some(ln); } else { - return Err(Status::new(StatusCode::IOError, "Couldn't read lognumber")); + return err(StatusCode::IOError, "Couldn't read lognumber"); } } @@ -211,8 +208,7 @@ if let Ok(nfn) = reader.read_varint() { ve.next_file_number = Some(nfn); } else { - return Err(Status::new(StatusCode::IOError, - "Couldn't read next_file_number")); + return err(StatusCode::IOError, "Couldn't read next_file_number"); } } @@ -220,8 +216,7 @@ if let Ok(ls) = reader.read_varint() { ve.last_seq = Some(ls); } else { - return Err(Status::new(StatusCode::IOError, - "Couldn't read last_sequence")); + return err(StatusCode::IOError, "Couldn't read last_sequence"); } } @@ -235,7 +230,7 @@ key: key, }); } else { - return Err(Status::new(StatusCode::IOError, "Couldn't read level")); + return err(StatusCode::IOError, "Couldn't read level"); } } @@ -244,11 +239,10 @@ if let Ok(num) = reader.read_varint() { ve.deleted.insert((lvl, num)); } else { - return Err(Status::new(StatusCode::IOError, - "Couldn't read file num")); + return err(StatusCode::IOError, "Couldn't read file num"); } } else { - return Err(Status::new(StatusCode::IOError, "Couldn't read level")); + return err(StatusCode::IOError, "Couldn't read level"); } } @@ -267,16 +261,13 @@ allowed_seeks: 0, })) } else { - return Err(Status::new(StatusCode::IOError, - "Couldn't read file size")); + return err(StatusCode::IOError, "Couldn't read file size"); } } else { - return Err(Status::new(StatusCode::IOError, - "Couldn't read file num")); + return err(StatusCode::IOError, "Couldn't read file num"); } } else { - return Err(Status::new(StatusCode::IOError, - "Couldn't read file level")); + return err(StatusCode::IOError, "Couldn't read file level"); } } @@ -284,13 +275,12 @@ if let Ok(pln) = reader.read_varint() { ve.prev_log_number = Some(pln); } else { - return Err(Status::new(StatusCode::IOError, - "Couldn't read prev_log_number")); + return err(StatusCode::IOError, "Couldn't read prev_log_number"); } } } } else { - return Err(Status::new(StatusCode::Corruption, "Invalid tag number")); + return err(StatusCode::Corruption, "Invalid tag number"); } }