imag/libimagstore/src/store.rs

201 lines
5.5 KiB
Rust
Raw Normal View History

2016-01-13 20:47:23 +00:00
use std::collections::HashMap;
2016-01-17 15:22:50 +00:00
use std::fs::{File, remove_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;
use std::sync::Arc;
2016-01-17 15:23:35 +00:00
use std::sync::RwLock;
2016-01-16 18:32:12 +00:00
use fs2::FileExt;
2016-01-12 17:52:03 +00:00
2016-01-16 19:22:18 +00:00
use entry::Entry;
2016-01-17 15:22:50 +00:00
use error::{StoreError, StoreErrorKind};
use storeid::StoreId;
2016-01-23 15:15:34 +00:00
use lazyfile::LazyFile;
2016-01-12 17:52:03 +00:00
2016-01-16 19:53:38 +00:00
/// The Result Type returned by any interaction with the store that could fail
2016-01-12 17:52:03 +00:00
pub type Result<T> = RResult<T, StoreError>;
2016-01-17 14:09:10 +00:00
2016-01-16 19:53:38 +00:00
/// A store entry, depending on the option type it is either borrowed currently
/// or not.
2016-01-23 15:15:34 +00:00
enum StoreEntry {
Present(StoreId, LazyFile),
Borrowed
}
impl PartialEq for StoreEntry {
fn eq(&self, other: &StoreEntry) -> bool {
use store::StoreEntry::*;
match (*self, *other) {
(Borrowed, Borrowed) => true,
(Borrowed, Present(_,_)) => false,
(Present(_,_), Borrowed) => false,
(Present(ref a,_), Present(ref b, _)) => a == b
}
}
2016-01-16 19:30:16 +00:00
}
2016-01-16 19:53:38 +00:00
2016-01-16 19:30:16 +00:00
impl StoreEntry {
2016-01-16 19:53:38 +00:00
/// The entry is currently borrowed, meaning that some thread is currently
/// mutating it
2016-01-16 19:30:16 +00:00
fn is_borrowed(&self) -> bool {
2016-01-23 15:15:34 +00:00
*self == StoreEntry::Borrowed
}
fn get_entry(&self) -> Result<Entry> {
if let &StoreEntry::Present(ref id, ref file) = self {
let file = file.get_file();
if let Err(StoreError{err_type: StoreErrorKind::FileNotFound, ..}) = file {
Ok(Entry::new(id.clone()))
} else {
unimplemented!()
}
} else {
return Err(StoreError::new(StoreErrorKind::EntryAlreadyBorrowed, None))
}
2016-01-16 19:30:16 +00:00
}
}
2016-01-16 18:52:06 +00:00
2016-01-16 19:53:38 +00:00
/// The Store itself, through this object one can interact with IMAG's entries
pub struct Store {
location: PathBuf,
2016-01-13 20:47:23 +00:00
/**
* Internal Path->File cache map
*
* Caches the files, so they remain flock()ed
*
* Could be optimized for a threadsafe HashMap
*/
2016-01-16 19:30:16 +00:00
entries: Arc<RwLock<HashMap<StoreId, StoreEntry>>>,
}
impl Store {
2016-01-17 15:04:31 +00:00
/// Create a new Store object
pub fn new(location: PathBuf) -> Result<Store> {
use std::fs::create_dir_all;
if !location.exists() {
2016-01-18 22:00:51 +00:00
let c = create_dir_all(location.clone());
if c.is_err() {
return Err(StoreError::new(StoreErrorKind::StorePathCreate,
Some(Box::new(c.err().unwrap()))));
}
} else {
if location.is_file() {
return Err(StoreError::new(StoreErrorKind::StorePathExists, None));
}
}
Ok(Store {
2016-01-17 15:04:31 +00:00
location: location,
entries: Arc::new(RwLock::new(HashMap::new())),
})
2016-01-17 15:04:31 +00:00
}
2016-01-16 19:53:38 +00:00
/// Creates the Entry at the given location (inside the entry)
pub fn create(&self, entry: Entry) -> Result<()> {
unimplemented!();
}
2016-01-16 19:18:43 +00:00
2016-01-16 19:53:38 +00:00
/// Borrow a given Entry. When the `FileLockEntry` is either `update`d or
/// dropped, the new Entry is written to disk
pub fn retrieve<'a>(&'a self, id: StoreId) -> Result<FileLockEntry<'a>> {
2016-01-17 15:58:58 +00:00
let hsmap = self.entries.write();
if hsmap.is_err() {
return Err(StoreError::new(StoreErrorKind::LockPoisoned, None))
}
hsmap.unwrap().get_mut(&id)
.ok_or(StoreError::new(StoreErrorKind::IdNotFound, None))
.and_then(|store_entry| store_entry.get_entry())
.and_then(|entry| Ok(FileLockEntry::new(self, entry, id)))
}
2016-01-16 19:18:43 +00:00
2016-01-16 19:53:38 +00:00
/// Return the `FileLockEntry` and write to disk
pub fn update<'a>(&'a self, entry: FileLockEntry<'a>) -> Result<()> {
2016-01-17 14:09:10 +00:00
self._update(&entry)
}
/// Internal method to write to the filesystem store.
///
/// # Assumptions
/// This method assumes that entry is dropped _right after_ the call, hence
/// it is not public.
fn _update<'a>(&'a self, entry: &FileLockEntry<'a>) -> Result<()> {
unimplemented!();
}
2016-01-16 19:18:43 +00:00
2016-01-16 19:53:38 +00:00
/// Retrieve a copy of a given entry, this cannot be used to mutate
/// the one on disk
pub fn retrieve_copy(&self, id: StoreId) -> Result<Entry> {
unimplemented!();
}
2016-01-16 19:18:43 +00:00
2016-01-16 19:53:38 +00:00
/// Delete an entry
pub fn delete(&self, id: StoreId) -> Result<()> {
2016-01-17 15:22:50 +00:00
let mut entries_lock = self.entries.write();
let mut entries = entries_lock.unwrap();
// if the entry is currently modified by the user, we cannot drop it
if entries.get(&id).map(|e| e.is_borrowed()).unwrap_or(false) {
2016-01-17 15:22:50 +00:00
return Err(StoreError::new(StoreErrorKind::IdLocked, None));
}
// remove the entry first, then the file
entries.remove(&id);
remove_file(&id).map_err(|e| StoreError::new(StoreErrorKind::FileError, Some(Box::new(e))))
}
}
impl Drop for Store {
/**
* Unlock all files on drop
*
2016-01-23 15:15:34 +00:00
* TODO: Unlock them
*/
fn drop(&mut self) {
}
2016-01-13 20:51:40 +00:00
}
2016-01-16 19:53:38 +00:00
/// A struct that allows you to borrow an Entry
pub struct FileLockEntry<'a> {
store: &'a Store,
2016-01-16 18:32:12 +00:00
entry: Entry,
2016-01-16 18:52:06 +00:00
key: StoreId,
2016-01-16 17:25:48 +00:00
}
impl<'a> FileLockEntry<'a, > {
2016-01-16 18:52:06 +00:00
fn new(store: &'a Store, entry: Entry, key: StoreId) -> FileLockEntry<'a> {
2016-01-16 17:25:48 +00:00
FileLockEntry {
store: store,
entry: entry,
2016-01-16 18:32:12 +00:00
key: key,
2016-01-16 17:25:48 +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
}
}
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
}
}
2016-01-16 18:32:12 +00:00
impl<'a> Drop for FileLockEntry<'a> {
fn drop(&mut self) {
2016-01-17 14:09:10 +00:00
self.store._update(self).unwrap()
2016-01-16 18:32:12 +00:00
}
}