2016-01-13 20:47:23 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::fs::File;
|
2016-01-13 20:51:40 +00:00
|
|
|
use std::ops::Drop;
|
2016-01-12 17:52:03 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::result::Result as RResult;
|
2016-01-13 10:53:56 +00:00
|
|
|
use std::sync::Arc;
|
2016-01-13 21:03:53 +00:00
|
|
|
use std::sync::RwLock;
|
2016-01-12 17:52:03 +00:00
|
|
|
|
|
|
|
pub use entry::Entry;
|
|
|
|
pub use error::StoreError;
|
|
|
|
|
|
|
|
pub type Result<T> = RResult<T, StoreError>;
|
|
|
|
|
2016-01-16 18:04:15 +00:00
|
|
|
pub struct Store {
|
|
|
|
location: PathBuf,
|
2016-01-13 20:47:23 +00:00
|
|
|
|
2016-01-16 18:04:15 +00:00
|
|
|
/**
|
|
|
|
* Internal Path->File cache map
|
|
|
|
*
|
|
|
|
* Caches the files, so they remain flock()ed
|
|
|
|
*
|
|
|
|
* Could be optimized for a threadsafe HashMap
|
|
|
|
*/
|
|
|
|
cache: Arc<RwLock<HashMap<PathBuf, RwLock<File>>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Store {
|
2016-01-13 20:51:40 +00:00
|
|
|
|
2016-01-16 18:04:15 +00:00
|
|
|
fn create(&self, entry: Entry) -> Result<()> {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
fn retrieve<'a>(&'a self, path: PathBuf) -> Result<FileLockEntry<'a>> {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
fn update<'a>(&'a self, entry: FileLockEntry<'a>) -> Result<()> {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
fn retrieve_copy(&self, id : String) -> Result<Entry> {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
fn delete(&self, path: PathBuf) -> Result<()> {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Store {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unlock all files on drop
|
|
|
|
*
|
|
|
|
* TODO: Error message when file cannot be unlocked?
|
|
|
|
*/
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.cache.iter().map(|f| f.unlock());
|
|
|
|
}
|
2016-01-13 20:51:40 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-16 18:04:15 +00:00
|
|
|
pub struct FileLockEntry<'a> {
|
|
|
|
store: &'a Store,
|
2016-01-16 17:25:48 +00:00
|
|
|
entry: Entry
|
|
|
|
}
|
|
|
|
|
2016-01-16 18:04:15 +00:00
|
|
|
impl<'a> FileLockEntry<'a, > {
|
|
|
|
fn new(store: &'a Store, entry: Entry) -> FileLockEntry<'a> {
|
2016-01-16 17:25:48 +00:00
|
|
|
FileLockEntry {
|
|
|
|
store: store,
|
|
|
|
entry: entry,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-16 18:04:15 +00:00
|
|
|
impl<'a> ::std::ops::Deref for FileLockEntry<'a> {
|
2016-01-16 17:25:48 +00:00
|
|
|
type Target = Entry;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.entry
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-16 18:04:15 +00:00
|
|
|
impl<'a> ::std::ops::DerefMut for FileLockEntry<'a> {
|
2016-01-16 17:25:48 +00:00
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.entry
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|