changeset 147:3cc327257435

Convert rest of code to new Status/StatusCode types
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 29 Jan 2017 13:54:14 +0100
parents 4a41aab21046
children 08f5579b8598
files src/disk_env.rs src/env.rs src/memtable.rs src/table_reader.rs src/version_edit.rs
diffstat 5 files changed, 55 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/src/disk_env.rs	Sun Jan 29 13:53:45 2017 +0100
+++ b/src/disk_env.rs	Sun Jan 29 13:54:14 2017 +0100
@@ -1,9 +1,9 @@
 use env::Env;
 use env::Logger;
+use error::{from_io_result, Status, StatusCode, Result};
 
 use std::collections::HashSet;
 use std::fs;
-use std::io::{Result, Error, ErrorKind};
 use std::iter::FromIterator;
 use std::mem;
 use std::os::unix::io::{FromRawFd, IntoRawFd};
@@ -40,16 +40,16 @@
     type FileLock = DiskFileLock;
 
     fn open_sequential_file(&self, p: &Path) -> Result<Self::SequentialReader> {
-        fs::OpenOptions::new().read(true).open(p)
+        from_io_result(fs::OpenOptions::new().read(true).open(p))
     }
     fn open_random_access_file(&self, p: &Path) -> Result<Self::RandomReader> {
-        fs::OpenOptions::new().read(true).open(p)
+        from_io_result(fs::OpenOptions::new().read(true).open(p))
     }
     fn open_writable_file(&self, p: &Path) -> Result<Self::Writer> {
-        fs::OpenOptions::new().create(true).write(true).append(false).open(p)
+        from_io_result(fs::OpenOptions::new().create(true).write(true).append(false).open(p))
     }
     fn open_appendable_file(&self, p: &Path) -> Result<Self::Writer> {
-        fs::OpenOptions::new().create(true).write(true).append(true).open(p)
+        from_io_result(fs::OpenOptions::new().create(true).write(true).append(true).open(p))
     }
 
     fn exists(&self, p: &Path) -> Result<bool> {
@@ -59,10 +59,11 @@
         let dir_reader = try!(fs::read_dir(p));
         let filenames = dir_reader.map(|r| {
                 if !r.is_ok() {
-                    return "".to_string();
+                    "".to_string()
+                } else {
+                    let direntry = r.unwrap();
+                    direntry.file_name().into_string().unwrap_or("".to_string())
                 }
-                let direntry = r.unwrap();
-                direntry.file_name().into_string().unwrap_or("".to_string())
             })
             .filter(|s| !s.is_empty());
         Ok(Vec::from_iter(filenames))
@@ -73,23 +74,23 @@
     }
 
     fn delete(&self, p: &Path) -> Result<()> {
-        fs::remove_file(p)
+        from_io_result(fs::remove_file(p))
     }
     fn mkdir(&self, p: &Path) -> Result<()> {
-        fs::create_dir(p)
+        from_io_result(fs::create_dir(p))
     }
     fn rmdir(&self, p: &Path) -> Result<()> {
-        fs::remove_dir_all(p)
+        from_io_result(fs::remove_dir_all(p))
     }
     fn rename(&self, old: &Path, new: &Path) -> Result<()> {
-        fs::rename(old, new)
+        from_io_result(fs::rename(old, new))
     }
 
     fn lock(&self, p: &Path) -> Result<DiskFileLock> {
         let mut locks = self.locks.lock().unwrap();
 
         if locks.contains(&p.to_str().unwrap().to_string()) {
-            Err(Error::new(ErrorKind::AlreadyExists, "Lock is held"))
+            Err(Status::new(StatusCode::AlreadyExists, "Lock is held"))
         } else {
             let f = try!(fs::OpenOptions::new().write(true).open(p));
 
@@ -108,7 +109,7 @@
             };
 
             if result < 0 {
-                return Err(Error::new(ErrorKind::PermissionDenied, "Lock is held (fcntl)"));
+                return Err(Status::new(StatusCode::AlreadyExists, "Lock is held (fcntl)"));
             }
 
             locks.insert(p.to_str().unwrap().to_string());
--- a/src/env.rs	Sun Jan 29 13:53:45 2017 +0100
+++ b/src/env.rs	Sun Jan 29 13:54:14 2017 +0100
@@ -1,7 +1,9 @@
 //! An `env` is an abstraction layer that allows the database to run both on different platforms as
 //! well as persisting data on disk or in memory.
 
-use std::io::{Read, Write, Seek, Result};
+use error::Result;
+
+use std::io::{Read, Write, Seek};
 use std::path::Path;
 
 pub trait Env {
--- a/src/memtable.rs	Sun Jan 29 13:53:45 2017 +0100
+++ b/src/memtable.rs	Sun Jan 29 13:54:14 2017 +0100
@@ -1,7 +1,8 @@
 use key_types::{LookupKey, UserKey, InternalKey, MemtableKey};
 use cmp::MemtableKeyCmp;
+use error::{Status, StatusCode, Result};
 use key_types::{parse_memtable_key, build_memtable_key};
-use types::{ValueType, SequenceNumber, Status, LdbIterator};
+use types::{ValueType, SequenceNumber, LdbIterator};
 use skipmap::{SkipMap, SkipMapIter};
 use options::Options;
 
@@ -40,7 +41,7 @@
     }
 
     #[allow(unused_variables)]
-    pub fn get(&self, key: &LookupKey) -> Result<Vec<u8>, Status> {
+    pub fn get(&self, key: &LookupKey) -> Result<Vec<u8>> {
         let mut iter = self.map.iter();
         iter.seek(key.memtable_key());
 
@@ -53,13 +54,13 @@
             // We only care about user key equality here
             if key.user_key() == &foundkey[fkeyoff..fkeyoff + fkeylen] {
                 if tag & 0xff == ValueType::TypeValue as u64 {
-                    return Result::Ok(foundkey[valoff..valoff + vallen].to_vec());
+                    return Ok(foundkey[valoff..valoff + vallen].to_vec());
                 } else {
-                    return Result::Err(Status::NotFound(String::new()));
+                    return Err(Status::new(StatusCode::NotFound, ""));
                 }
             }
         }
-        Result::Err(Status::NotFound("not found".to_string()))
+        Err(Status::new(StatusCode::NotFound, ""))
     }
 
     pub fn iter<'a>(&'a self) -> MemtableIterator<'a> {
--- a/src/table_reader.rs	Sun Jan 29 13:53:45 2017 +0100
+++ b/src/table_reader.rs	Sun Jan 29 13:54:14 2017 +0100
@@ -1,16 +1,17 @@
 use block::{Block, BlockIter};
 use blockhandle::BlockHandle;
 use cache::CacheID;
+use cmp::InternalKeyCmp;
+use error::{Status, StatusCode, Result};
 use filter::{BoxedFilterPolicy, InternalFilterPolicy};
 use filter_block::FilterBlockReader;
 use key_types::InternalKey;
-use cmp::InternalKeyCmp;
 use options::{self, CompressionType, Options};
 use table_builder::{self, Footer};
 use types::LdbIterator;
 
 use std::cmp::Ordering;
-use std::io::{self, Read, Seek, SeekFrom, Result};
+use std::io::{Read, Seek, SeekFrom};
 use std::sync::Arc;
 
 use integer_encoding::FixedInt;
@@ -97,8 +98,8 @@
             try!(TableBlock::read_block(opt.clone(), &mut file, &footer.meta_index));
 
         if !indexblock.verify() || !metaindexblock.verify() {
-            return Err(io::Error::new(io::ErrorKind::InvalidData,
-                                      "Indexblock/Metaindexblock failed verification"));
+            return Err(Status::new(StatusCode::InvalidData,
+                                   "Indexblock/Metaindexblock failed verification"));
         }
 
         // Open filter block for reading
@@ -146,7 +147,7 @@
         let b = try!(TableBlock::read_block(self.opt.clone(), &mut self.file, location));
 
         if !b.verify() {
-            Err(io::Error::new(io::ErrorKind::InvalidData, "Data block failed verification"))
+            Err(Status::new(StatusCode::InvalidData, "Data block failed verification"))
         } else {
             Ok(b)
         }
--- a/src/version_edit.rs	Sun Jan 29 13:53:45 2017 +0100
+++ b/src/version_edit.rs	Sun Jan 29 13:54:14 2017 +0100
@@ -1,5 +1,6 @@
-use types::{FileMetaData, SequenceNumber, Status};
+use error::{Status, StatusCode};
 use key_types::InternalKey;
+use types::{FileMetaData, SequenceNumber};
 
 use integer_encoding::VarIntWriter;
 use integer_encoding::VarIntReader;
@@ -48,14 +49,14 @@
 
         if let Ok(l) = reader.read(&mut keybuf) {
             if l != klen {
-                return Err(Status::IOError("Couldn't read full key".to_string()));
+                return Err(Status::new(StatusCode::IOError, "Couldn't read full key"));
             }
             return Ok(keybuf);
         } else {
-            return Err(Status::IOError("Couldn't read key".to_string()));
+            return Err(Status::new(StatusCode::IOError, "Couldn't read key"));
         }
     } else {
-        return Err(Status::IOError("Couldn't read key length".to_string()));
+        return Err(Status::new(StatusCode::IOError, "Couldn't read key length"));
     }
 }
 
@@ -193,7 +194,8 @@
                         if let Ok(c) = String::from_utf8(buf) {
                             ve.comparator = Some(c);
                         } else {
-                            return Err(Status::Corruption("Bad comparator encoding".to_string()));
+                            return Err(Status::new(StatusCode::Corruption,
+                                                   "Bad comparator encoding"));
                         }
                     }
 
@@ -201,7 +203,7 @@
                         if let Ok(ln) = reader.read_varint() {
                             ve.log_number = Some(ln);
                         } else {
-                            return Err(Status::IOError("Couldn't read lognumber".to_string()));
+                            return Err(Status::new(StatusCode::IOError, "Couldn't read lognumber"));
                         }
                     }
 
@@ -209,8 +211,8 @@
                         if let Ok(nfn) = reader.read_varint() {
                             ve.next_file_number = Some(nfn);
                         } else {
-                            return Err(Status::IOError("Couldn't read next_file_number"
-                                .to_string()));
+                            return Err(Status::new(StatusCode::IOError,
+                                                   "Couldn't read next_file_number"));
                         }
                     }
 
@@ -218,7 +220,8 @@
                         if let Ok(ls) = reader.read_varint() {
                             ve.last_seq = Some(ls);
                         } else {
-                            return Err(Status::IOError("Couldn't read last_sequence".to_string()));
+                            return Err(Status::new(StatusCode::IOError,
+                                                   "Couldn't read last_sequence"));
                         }
                     }
 
@@ -232,7 +235,7 @@
                                 key: key,
                             });
                         } else {
-                            return Err(Status::IOError("Couldn't read level".to_string()));
+                            return Err(Status::new(StatusCode::IOError, "Couldn't read level"));
                         }
                     }
 
@@ -241,10 +244,11 @@
                             if let Ok(num) = reader.read_varint() {
                                 ve.deleted.insert((lvl, num));
                             } else {
-                                return Err(Status::IOError("Couldn't read file num".to_string()));
+                                return Err(Status::new(StatusCode::IOError,
+                                                       "Couldn't read file num"));
                             }
                         } else {
-                            return Err(Status::IOError("Couldn't read level".to_string()));
+                            return Err(Status::new(StatusCode::IOError, "Couldn't read level"));
                         }
                     }
 
@@ -263,14 +267,16 @@
                                         allowed_seeks: 0,
                                     }))
                                 } else {
-                                    return Err(Status::IOError("Couldn't read file size"
-                                        .to_string()));
+                                    return Err(Status::new(StatusCode::IOError,
+                                                           "Couldn't read file size"));
                                 }
                             } else {
-                                return Err(Status::IOError("Couldn't read file num".to_string()));
+                                return Err(Status::new(StatusCode::IOError,
+                                                       "Couldn't read file num"));
                             }
                         } else {
-                            return Err(Status::IOError("Couldn't read file level".to_string()));
+                            return Err(Status::new(StatusCode::IOError,
+                                                   "Couldn't read file level"));
                         }
                     }
 
@@ -278,13 +284,13 @@
                         if let Ok(pln) = reader.read_varint() {
                             ve.prev_log_number = Some(pln);
                         } else {
-                            return Err(Status::IOError("Couldn't read prev_log_number"
-                                .to_string()));
+                            return Err(Status::new(StatusCode::IOError,
+                                                   "Couldn't read prev_log_number"));
                         }
                     }
                 }
             } else {
-                return Err(Status::Corruption("Invalid tag number".to_string()));
+                return Err(Status::new(StatusCode::Corruption, "Invalid tag number"));
             }
         }