Mercurial > lbo > hg > leveldb-rs
changeset 166:ef5426303667
Simplify in-memory implementation of RandomAccess file.
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sun, 16 Jul 2017 10:56:40 +0200 |
parents | bb7c31aa2825 |
children | 9e7e1a943718 |
files | src/env.rs src/table_reader.rs |
diffstat | 2 files changed, 8 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/src/env.rs Mon Jul 10 20:01:47 2017 +0200 +++ b/src/env.rs Sun Jul 16 10:56:40 2017 +0200 @@ -3,7 +3,7 @@ use error::{self, Result}; -use std::io::{Cursor, Read, Seek, SeekFrom, Write}; +use std::io::prelude::*; use std::fs::File; use std::os::unix::fs::FileExt; use std::path::Path; @@ -22,18 +22,18 @@ /// BufferBackedFile implements RandomAccess on a cursor. It wraps the cursor in a mutex /// to enable using an immutable receiver, like the File implementation. -pub type BufferBackedFile = Mutex<Cursor<Vec<u8>>>; +pub type BufferBackedFile = Mutex<Vec<u8>>; impl RandomAccess for BufferBackedFile { fn read_at(&self, off: usize, len: usize) -> Result<Vec<u8>> { - let mut c = self.lock().unwrap(); - error::from_io_result(c.seek(SeekFrom::Start(off as u64)))?; - let mut buf = vec![0 as u8; len]; - error::from_io_result(c.read_exact(&mut buf)).map(|_| buf) + let c = self.lock().unwrap(); + if off + len > c.len() { + return Err(error::Status::new(error::StatusCode::InvalidArgument, "off-limits read")); + } + Ok(c[off..off + len].to_vec()) } } - pub struct FileLock { pub id: String, }
--- a/src/table_reader.rs Mon Jul 10 20:01:47 2017 +0200 +++ b/src/table_reader.rs Sun Jul 16 10:56:40 2017 +0200 @@ -390,7 +390,6 @@ use types::LdbIterator; use key_types::LookupKey; - use std::io::Cursor; use std::sync::Mutex; use super::*; @@ -468,8 +467,7 @@ } fn wrap_buffer(src: Vec<u8>) -> Arc<Box<RandomAccess>> { - // sigh... - let file = Mutex::new(Cursor::new(src)); + let file = Mutex::new(src); Arc::new(Box::new(file)) }