issue/77 Added managed directory

This commit is contained in:
Paul Masurel
2017-03-03 22:41:30 +09:00
parent 590a8582c9
commit 4b7afa2ae7
13 changed files with 168 additions and 124 deletions

View File

@@ -78,9 +78,6 @@ pub trait Directory: fmt::Debug + Send + Sync + 'static {
/// Clones the directory and boxes the clone
fn box_clone(&self) -> Box<Directory>;
/// Returns the list of files starting by a given
/// prefix.
fn ls_starting_with(&self, prefix: &str) -> io::Result<Vec<PathBuf>>;
}

View File

@@ -0,0 +1,81 @@
use Result;
use std::path::{Path, PathBuf};
use directory::error::{FileError, OpenWriteError};
use directory::{ReadOnlySource, WritePtr};
use std::result;
use std::io;
use Directory;
use std::sync::{Arc, RwLock};
use std::collections::HashSet;
use std::io::Write;
use rustc_serialize::json;
use core::MANAGED_FILEPATH;
#[derive(Debug)]
pub struct ManagedDirectory {
directory: Box<Directory>,
managed_paths: Arc<RwLock<HashSet<PathBuf>>>,
}
impl ManagedDirectory {
pub fn new<Dir: Directory>(directory: Dir) -> ManagedDirectory {
ManagedDirectory {
directory: box directory,
managed_paths: Arc::default(),
}
}
fn register_file_as_managed(&mut self, filepath: PathBuf) -> Result<()> {
let mut managed_files_lock = self.managed_paths.write()?;
if managed_files_lock.insert(filepath) {
let mut w = vec!();
try!(write!(&mut w, "{}\n", json::as_pretty_json(&*managed_files_lock)));
self.directory.atomic_write(&MANAGED_FILEPATH, &w[..])?;
}
Ok(())
}
}
impl Directory for ManagedDirectory {
fn open_read(&self, path: &Path) -> result::Result<ReadOnlySource, FileError> {
self.directory.open_read(path)
}
fn open_write(&mut self, path: &Path) -> result::Result<WritePtr, OpenWriteError> {
self.directory.open_write(path)
}
fn atomic_write(&mut self, path: &Path, data: &[u8]) -> io::Result<()> {
self.directory.atomic_write(path, data)
}
fn atomic_read(&self, path: &Path) -> result::Result<Vec<u8>, FileError> {
self.directory.atomic_read(path)
}
fn delete(&self, path: &Path) -> result::Result<(), FileError> {
self.directory.delete(path)
}
fn exists(&self, path: &Path) -> bool {
self.directory.exists(path)
}
fn box_clone(&self) -> Box<Directory> {
box self.clone()
}
}
impl Clone for ManagedDirectory {
fn clone(&self) -> ManagedDirectory {
ManagedDirectory {
directory: self.directory.box_clone(),
managed_paths: self.managed_paths.clone(),
}
}
}

View File

@@ -352,26 +352,6 @@ impl Directory for MmapDirectory {
fn box_clone(&self,) -> Box<Directory> {
Box::new(self.clone())
}
fn ls_starting_with(&self, prefix: &str) -> io::Result<Vec<PathBuf>> {
fs::read_dir(&self.root_path)
.map(|paths: ReadDir| {
paths
.filter_map(|dir_entry_res|
dir_entry_res
.ok()
.map(|dir_entry| dir_entry.path())
)
.filter(|path|
path.to_str()
.map(|filepath| filepath.starts_with(prefix))
.unwrap_or(false)
)
.map(PathBuf::from)
.collect()
})
}
}

View File

@@ -3,6 +3,7 @@ mod ram_directory;
mod directory;
mod read_only_source;
mod shared_vec_slice;
mod managed_directory;
/// Errors specific to the directory module.
pub mod error;
@@ -14,6 +15,7 @@ pub use self::read_only_source::ReadOnlySource;
pub use self::directory::Directory;
pub use self::ram_directory::RAMDirectory;
pub use self::mmap_directory::MmapDirectory;
pub use self::managed_directory::ManagedDirectory;
/// Synonym of Seek + Write
pub trait SeekableWrite: Seek + Write {}

View File

@@ -130,20 +130,6 @@ impl InnerDirectory {
.contains_key(path)
}
fn ls_starting_with(&self, prefix: &str) -> Vec<PathBuf> {
self.0
.read()
.expect("Failed to get read lock directory.")
.keys()
.filter(|path: &&PathBuf|
path.to_str()
.map(|p: &str| p.starts_with(prefix))
.unwrap_or(false)
)
.cloned()
.collect()
}
}
impl fmt::Debug for RAMDirectory {
@@ -218,9 +204,4 @@ impl Directory for RAMDirectory {
Box::new(self.clone())
}
fn ls_starting_with(&self, prefix: &str) -> io::Result<Vec<PathBuf>> {
Ok(self.fs.ls_starting_with(prefix))
}
}