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-16 18:32:12 +00:00
|
|
|
use std::sync::{RwLock, Mutex};
|
|
|
|
|
|
|
|
use fs2::FileExt;
|
2016-01-12 17:52:03 +00:00
|
|
|
|
2016-01-16 19:22:18 +00:00
|
|
|
use entry::Entry;
|
|
|
|
use error::StoreError;
|
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-16 19:53:38 +00:00
|
|
|
/// The Index into the Store
|
2016-01-16 18:52:06 +00:00
|
|
|
pub type StoreId = PathBuf;
|
|
|
|
|
2016-01-16 19:53:38 +00:00
|
|
|
/// This Trait allows you to convert various representations to a single one
|
|
|
|
/// suitable for usage in the Store
|
2016-01-16 18:52:06 +00:00
|
|
|
trait IntoStoreId {
|
|
|
|
fn into_storeid(self) -> StoreId;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> IntoStoreId for &'a str {
|
|
|
|
fn into_storeid(self) -> StoreId {
|
|
|
|
PathBuf::from(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> IntoStoreId for &'a String{
|
|
|
|
fn into_storeid(self) -> StoreId {
|
|
|
|
PathBuf::from(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoStoreId for String{
|
|
|
|
fn into_storeid(self) -> StoreId {
|
|
|
|
PathBuf::from(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoStoreId for PathBuf {
|
|
|
|
fn into_storeid(self) -> StoreId {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> IntoStoreId for &'a PathBuf {
|
|
|
|
fn into_storeid(self) -> StoreId {
|
|
|
|
self.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<ISI: IntoStoreId> IntoStoreId for (ISI, ISI) {
|
|
|
|
fn into_storeid(self) -> StoreId {
|
|
|
|
let (first, second) = self;
|
|
|
|
let mut res : StoreId = first.into_storeid();
|
|
|
|
res.push(second.into_storeid());
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 14:09:10 +00:00
|
|
|
#[derive(PartialEq)]
|
|
|
|
enum StoreEntryPresence {
|
|
|
|
Present,
|
|
|
|
Borrowed
|
|
|
|
}
|
|
|
|
|
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-16 19:30:16 +00:00
|
|
|
struct StoreEntry {
|
|
|
|
file: File,
|
2016-01-17 14:09:10 +00:00
|
|
|
entry: StoreEntryPresence
|
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-17 14:09:10 +00:00
|
|
|
self.entry == StoreEntryPresence::Borrowed
|
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
|
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
|
|
|
|
*/
|
2016-01-16 19:30:16 +00:00
|
|
|
entries: Arc<RwLock<HashMap<StoreId, StoreEntry>>>,
|
2016-01-16 18:04:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Store {
|
2016-01-16 19:53:38 +00:00
|
|
|
/// Creates the Entry at the given location (inside the entry)
|
2016-01-16 19:50:36 +00:00
|
|
|
pub fn create(&self, entry: Entry) -> Result<()> {
|
2016-01-16 18:04:15 +00:00
|
|
|
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
|
2016-01-16 19:50:36 +00:00
|
|
|
pub fn retrieve<'a>(&'a self, id: StoreId) -> Result<FileLockEntry<'a>> {
|
2016-01-16 18:04:15 +00:00
|
|
|
unimplemented!();
|
|
|
|
}
|
2016-01-16 19:18:43 +00:00
|
|
|
|
2016-01-16 19:53:38 +00:00
|
|
|
/// Return the `FileLockEntry` and write to disk
|
2016-01-16 19:50:36 +00:00
|
|
|
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<()> {
|
2016-01-16 18:04:15 +00:00
|
|
|
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
|
2016-01-16 19:50:36 +00:00
|
|
|
pub fn retrieve_copy(&self, id: StoreId) -> Result<Entry> {
|
2016-01-16 18:04:15 +00:00
|
|
|
unimplemented!();
|
|
|
|
}
|
2016-01-16 19:18:43 +00:00
|
|
|
|
2016-01-16 19:53:38 +00:00
|
|
|
/// Delete an entry
|
2016-01-16 19:50:36 +00:00
|
|
|
pub fn delete(&self, id: StoreId) -> Result<()> {
|
2016-01-16 18:04:15 +00:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Store {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unlock all files on drop
|
|
|
|
*
|
|
|
|
* TODO: Error message when file cannot be unlocked?
|
|
|
|
*/
|
|
|
|
fn drop(&mut self) {
|
2016-01-16 18:32:12 +00:00
|
|
|
self.entries.write().unwrap()
|
2016-01-16 19:30:16 +00:00
|
|
|
.iter().map(|f| f.1.file.unlock());
|
2016-01-16 18:04:15 +00:00
|
|
|
}
|
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
|
2016-01-16 18:04:15 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-01-16 18:04:15 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2016-01-16 18:52:06 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use store::{StoreId, IntoStoreId};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn into_storeid_trait() {
|
|
|
|
let buf = PathBuf::from("abc/def");
|
|
|
|
|
|
|
|
let test = ("abc", "def");
|
|
|
|
assert_eq!(buf, test.into_storeid());
|
|
|
|
|
|
|
|
let test = "abc/def";
|
|
|
|
assert_eq!(buf, test.into_storeid());
|
|
|
|
|
|
|
|
let test = String::from("abc/def");
|
|
|
|
assert_eq!(buf, test.into_storeid());
|
|
|
|
|
|
|
|
let test = PathBuf::from("abc/def");
|
|
|
|
assert_eq!(buf, test.into_storeid());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|