Move serialize/deserialize calls of Entry to backend
This commit is contained in:
parent
cd99873f17
commit
52011a59b2
4 changed files with 47 additions and 21 deletions
|
@ -25,6 +25,8 @@ use error::{MapErrInto, StoreError as SE, StoreErrorKind as SEK};
|
||||||
|
|
||||||
use super::FileAbstraction;
|
use super::FileAbstraction;
|
||||||
use super::FileAbstractionInstance;
|
use super::FileAbstractionInstance;
|
||||||
|
use store::Entry;
|
||||||
|
use storeid::StoreId;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum FSFileAbstractionInstance {
|
pub enum FSFileAbstractionInstance {
|
||||||
|
@ -37,7 +39,7 @@ impl FileAbstractionInstance for FSFileAbstractionInstance {
|
||||||
/**
|
/**
|
||||||
* Get the content behind this file
|
* Get the content behind this file
|
||||||
*/
|
*/
|
||||||
fn get_file_content(&mut self) -> Result<String, SE> {
|
fn get_file_content(&mut self, id: StoreId) -> Result<Entry, SE> {
|
||||||
debug!("Getting lazy file: {:?}", self);
|
debug!("Getting lazy file: {:?}", self);
|
||||||
let (file, path) = match *self {
|
let (file, path) = match *self {
|
||||||
FSFileAbstractionInstance::File(ref mut f, _) => return {
|
FSFileAbstractionInstance::File(ref mut f, _) => return {
|
||||||
|
@ -50,6 +52,7 @@ impl FileAbstractionInstance for FSFileAbstractionInstance {
|
||||||
f.read_to_string(&mut s)
|
f.read_to_string(&mut s)
|
||||||
.map_err_into(SEK::IoError)
|
.map_err_into(SEK::IoError)
|
||||||
.map(|_| s)
|
.map(|_| s)
|
||||||
|
.and_then(|s| Entry::from_str(id, &s))
|
||||||
},
|
},
|
||||||
FSFileAbstractionInstance::Absent(ref p) =>
|
FSFileAbstractionInstance::Absent(ref p) =>
|
||||||
(try!(open_file(p).map_err_into(SEK::FileNotFound)), p.clone()),
|
(try!(open_file(p).map_err_into(SEK::FileNotFound)), p.clone()),
|
||||||
|
@ -60,6 +63,7 @@ impl FileAbstractionInstance for FSFileAbstractionInstance {
|
||||||
f.read_to_string(&mut s)
|
f.read_to_string(&mut s)
|
||||||
.map_err_into(SEK::IoError)
|
.map_err_into(SEK::IoError)
|
||||||
.map(|_| s)
|
.map(|_| s)
|
||||||
|
.and_then(|s| Entry::from_str(id, &s))
|
||||||
} else {
|
} else {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
}
|
}
|
||||||
|
@ -68,22 +72,25 @@ impl FileAbstractionInstance for FSFileAbstractionInstance {
|
||||||
/**
|
/**
|
||||||
* Write the content of this file
|
* Write the content of this file
|
||||||
*/
|
*/
|
||||||
fn write_file_content(&mut self, buf: &[u8]) -> Result<(), SE> {
|
fn write_file_content(&mut self, buf: &Entry) -> Result<(), SE> {
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
|
let buf = buf.to_str().into_bytes();
|
||||||
|
|
||||||
let (file, path) = match *self {
|
let (file, path) = match *self {
|
||||||
FSFileAbstractionInstance::File(ref mut f, _) => return {
|
FSFileAbstractionInstance::File(ref mut f, _) => return {
|
||||||
// We seek to the beginning of the file since we expect each
|
// We seek to the beginning of the file since we expect each
|
||||||
// access to the file to be in a different context
|
// access to the file to be in a different context
|
||||||
try!(f.seek(SeekFrom::Start(0))
|
try!(f.seek(SeekFrom::Start(0))
|
||||||
.map_err_into(SEK::FileNotCreated));
|
.map_err_into(SEK::FileNotCreated));
|
||||||
f.write_all(buf).map_err_into(SEK::FileNotWritten)
|
f.write_all(&buf).map_err_into(SEK::FileNotWritten)
|
||||||
},
|
},
|
||||||
FSFileAbstractionInstance::Absent(ref p) =>
|
FSFileAbstractionInstance::Absent(ref p) =>
|
||||||
(try!(create_file(p).map_err_into(SEK::FileNotCreated)), p.clone()),
|
(try!(create_file(p).map_err_into(SEK::FileNotCreated)), p.clone()),
|
||||||
};
|
};
|
||||||
*self = FSFileAbstractionInstance::File(file, path);
|
*self = FSFileAbstractionInstance::File(file, path);
|
||||||
if let FSFileAbstractionInstance::File(ref mut f, _) = *self {
|
if let FSFileAbstractionInstance::File(ref mut f, _) = *self {
|
||||||
return f.write_all(buf).map_err_into(SEK::FileNotWritten);
|
return f.write_all(&buf).map_err_into(SEK::FileNotWritten);
|
||||||
}
|
}
|
||||||
unreachable!();
|
unreachable!();
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,6 +32,8 @@ use libimagerror::into::IntoError;
|
||||||
use super::FileAbstraction;
|
use super::FileAbstraction;
|
||||||
use super::FileAbstractionInstance;
|
use super::FileAbstractionInstance;
|
||||||
use error::MapErrInto;
|
use error::MapErrInto;
|
||||||
|
use store::Entry;
|
||||||
|
use storeid::StoreId;
|
||||||
|
|
||||||
type Backend = Arc<Mutex<RefCell<HashMap<PathBuf, Cursor<Vec<u8>>>>>>;
|
type Backend = Arc<Mutex<RefCell<HashMap<PathBuf, Cursor<Vec<u8>>>>>>;
|
||||||
|
|
||||||
|
@ -60,7 +62,7 @@ impl FileAbstractionInstance for InMemoryFileAbstractionInstance {
|
||||||
/**
|
/**
|
||||||
* Get the mutable file behind a InMemoryFileAbstraction object
|
* Get the mutable file behind a InMemoryFileAbstraction object
|
||||||
*/
|
*/
|
||||||
fn get_file_content(&mut self) -> Result<String, SE> {
|
fn get_file_content(&mut self, id: StoreId) -> Result<Entry, SE> {
|
||||||
debug!("Getting lazy file: {:?}", self);
|
debug!("Getting lazy file: {:?}", self);
|
||||||
|
|
||||||
let p = self.absent_path.clone();
|
let p = self.absent_path.clone();
|
||||||
|
@ -74,6 +76,7 @@ impl FileAbstractionInstance for InMemoryFileAbstractionInstance {
|
||||||
t.read_to_string(&mut s)
|
t.read_to_string(&mut s)
|
||||||
.map_err_into(SEK::IoError)
|
.map_err_into(SEK::IoError)
|
||||||
.map(|_| s)
|
.map(|_| s)
|
||||||
|
.and_then(|s| Entry::from_str(id, &s))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,7 +84,8 @@ impl FileAbstractionInstance for InMemoryFileAbstractionInstance {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_file_content(&mut self, buf: &[u8]) -> Result<(), SE> {
|
fn write_file_content(&mut self, buf: &Entry) -> Result<(), SE> {
|
||||||
|
let buf = buf.to_str().into_bytes();
|
||||||
match *self {
|
match *self {
|
||||||
InMemoryFileAbstractionInstance { ref absent_path, .. } => {
|
InMemoryFileAbstractionInstance { ref absent_path, .. } => {
|
||||||
let mut mtx = self.fs_abstraction.lock().expect("Locking Mutex failed");
|
let mut mtx = self.fs_abstraction.lock().expect("Locking Mutex failed");
|
||||||
|
@ -90,7 +94,7 @@ impl FileAbstractionInstance for InMemoryFileAbstractionInstance {
|
||||||
if let Some(ref mut cur) = backend.get_mut(absent_path) {
|
if let Some(ref mut cur) = backend.get_mut(absent_path) {
|
||||||
let mut vec = cur.get_mut();
|
let mut vec = cur.get_mut();
|
||||||
vec.clear();
|
vec.clear();
|
||||||
vec.extend_from_slice(buf);
|
vec.extend_from_slice(&buf);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
let vec = Vec::from(buf);
|
let vec = Vec::from(buf);
|
||||||
|
|
|
@ -21,7 +21,8 @@ use std::path::PathBuf;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
use error::StoreError as SE;
|
use error::StoreError as SE;
|
||||||
|
use store::Entry;
|
||||||
|
use storeid::StoreId;
|
||||||
|
|
||||||
mod fs;
|
mod fs;
|
||||||
mod inmemory;
|
mod inmemory;
|
||||||
|
@ -44,27 +45,43 @@ pub trait FileAbstraction : Debug {
|
||||||
|
|
||||||
/// An abstraction trait over actions on files
|
/// An abstraction trait over actions on files
|
||||||
pub trait FileAbstractionInstance : Debug {
|
pub trait FileAbstractionInstance : Debug {
|
||||||
fn get_file_content(&mut self) -> Result<String, SE>;
|
|
||||||
fn write_file_content(&mut self, buf: &[u8]) -> Result<(), SE>;
|
/// Get the contents of the FileAbstractionInstance, as Entry object.
|
||||||
|
///
|
||||||
|
/// The `StoreId` is passed because the backend does not know where the Entry lives, but the
|
||||||
|
/// Entry type itself must be constructed with the id.
|
||||||
|
fn get_file_content(&mut self, id: StoreId) -> Result<Entry, SE>;
|
||||||
|
fn write_file_content(&mut self, buf: &Entry) -> Result<(), SE>;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use super::FileAbstractionInstance;
|
use super::FileAbstractionInstance;
|
||||||
use super::inmemory::InMemoryFileAbstraction;
|
use super::inmemory::InMemoryFileAbstraction;
|
||||||
use super::inmemory::InMemoryFileAbstractionInstance;
|
use super::inmemory::InMemoryFileAbstractionInstance;
|
||||||
use std::path::PathBuf;
|
use storeid::StoreId;
|
||||||
|
use store::Entry;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn lazy_file() {
|
fn lazy_file() {
|
||||||
let fs = InMemoryFileAbstraction::new();
|
let fs = InMemoryFileAbstraction::new();
|
||||||
|
|
||||||
let mut path = PathBuf::from("/tests");
|
let mut path = PathBuf::from("tests");
|
||||||
path.set_file_name("test1");
|
path.set_file_name("test1");
|
||||||
let mut lf = InMemoryFileAbstractionInstance::new(fs.backend().clone(), path);
|
let mut lf = InMemoryFileAbstractionInstance::new(fs.backend().clone(), path.clone());
|
||||||
lf.write_file_content(b"Hello World").unwrap();
|
|
||||||
let bah = lf.get_file_content().unwrap();
|
let loca = StoreId::new_baseless(path).unwrap();
|
||||||
assert_eq!(bah, "Hello World");
|
let file = Entry::from_str(loca.clone(), r#"---
|
||||||
|
[imag]
|
||||||
|
version = "0.3.0"
|
||||||
|
---
|
||||||
|
Hello World"#).unwrap();
|
||||||
|
|
||||||
|
lf.write_file_content(&file).unwrap();
|
||||||
|
let bah = lf.get_file_content(loca).unwrap();
|
||||||
|
assert_eq!(bah.get_content(), "Hello World");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -158,13 +158,11 @@ impl StoreEntry {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_entry(&mut self) -> Result<Entry> {
|
fn get_entry(&mut self) -> Result<Entry> {
|
||||||
let id = &self.id.clone();
|
|
||||||
if !self.is_borrowed() {
|
if !self.is_borrowed() {
|
||||||
self.file
|
self.file
|
||||||
.get_file_content()
|
.get_file_content(self.id.clone())
|
||||||
.and_then(|content| Entry::from_str(id.clone(), &content))
|
|
||||||
.or_else(|err| if err.err_type() == SEK::FileNotFound {
|
.or_else(|err| if err.err_type() == SEK::FileNotFound {
|
||||||
Ok(Entry::new(id.clone()))
|
Ok(Entry::new(self.id.clone()))
|
||||||
} else {
|
} else {
|
||||||
Err(err)
|
Err(err)
|
||||||
})
|
})
|
||||||
|
@ -176,7 +174,7 @@ impl StoreEntry {
|
||||||
fn write_entry(&mut self, entry: &Entry) -> Result<()> {
|
fn write_entry(&mut self, entry: &Entry) -> Result<()> {
|
||||||
if self.is_borrowed() {
|
if self.is_borrowed() {
|
||||||
assert_eq!(self.id, entry.location);
|
assert_eq!(self.id, entry.location);
|
||||||
self.file.write_file_content(entry.to_str().as_bytes())
|
self.file.write_file_content(entry)
|
||||||
.map_err_into(SEK::FileError)
|
.map_err_into(SEK::FileError)
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue