Insert pre/post hook execution functions

This commit is contained in:
Matthias Beyer 2016-02-16 20:42:51 +01:00
parent 3cf2588104
commit ac280ba616
1 changed files with 78 additions and 5 deletions

View File

@ -21,9 +21,11 @@ use error::{StoreError, StoreErrorKind};
use storeid::{StoreId, StoreIdIterator};
use lazyfile::LazyFile;
use hook::HookResult;
use hook::read::{PreReadHook, PostReadHook};
use hook::create::{PreCreateHook, PostCreateHook};
use hook::retrieve::{PreRetrieveHook, PostRetrieveHook};
use hook::update::{PreUpdateHook, PostUpdateHook};
use hook::delete::{PreDeleteHook, PostDeleteHook};
/// The Result Type returned by any interaction with the store that could fail
@ -173,6 +175,10 @@ impl Store {
/// Creates the Entry at the given location (inside the entry)
pub fn create<'a>(&'a self, id: StoreId) -> Result<FileLockEntry<'a>> {
let id = self.storify_id(id);
if let Err(e) = self.execute_pre_create_hooks(&id) {
return Err(e);
}
let hsmap = self.entries.write();
if hsmap.is_err() {
return Err(StoreError::new(StoreErrorKind::LockPoisoned, None))
@ -186,13 +192,18 @@ impl Store {
se.status = StoreEntryStatus::Borrowed;
se
});
Ok(FileLockEntry::new(self, Entry::new(id.clone()), id))
self.execute_post_create_hooks(FileLockEntry::new(self, Entry::new(id.clone()), id))
}
/// 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>> {
let id = self.storify_id(id);
if let Err(e) = self.execute_pre_retrieve_hooks(&id) {
return Err(e);
}
self.entries
.write()
.map_err(|_| StoreError::new(StoreErrorKind::LockPoisoned, None))
@ -202,7 +213,7 @@ impl Store {
se.status = StoreEntryStatus::Borrowed;
entry
})
.map(|e| FileLockEntry::new(self, e, id))
.and_then(|e| self.execute_post_retrieve_hooks(FileLockEntry::new(self, e, id)))
}
/// Iterate over all StoreIds for one module name
@ -212,7 +223,15 @@ impl Store {
/// Return the `FileLockEntry` and write to disk
pub fn update<'a>(&'a self, entry: FileLockEntry<'a>) -> Result<()> {
self._update(&entry)
if let Err(e) = self.execute_pre_update_hooks(&entry) {
return Err(e);
}
if let Err(e) = self._update(&entry) {
return Err(e);
}
self.execute_post_update_hooks(&entry)
}
/// Internal method to write to the filesystem store.
@ -263,7 +282,11 @@ impl Store {
/// Delete an entry
pub fn delete(&self, id: StoreId) -> Result<()> {
let id = self.storify_id(id);
let mut entries_lock = self.entries.write();
if let Err(e) = self.execute_pre_delete_hooks(&id) {
return Err(e);
}
let entries_lock = self.entries.write();
if entries_lock.is_err() {
return Err(StoreError::new(StoreErrorKind::LockPoisoned, None))
}
@ -277,7 +300,11 @@ impl Store {
// remove the entry first, then the file
entries.remove(&id);
remove_file(&id).map_err(|e| StoreError::new(StoreErrorKind::FileError, Some(Box::new(e))))
if let Err(e) = remove_file(&id) {
return Err(StoreError::new(StoreErrorKind::FileError, Some(Box::new(e))));
}
self.execute_post_delete_hooks(&id)
}
/// Gets the path where this store is on the disk
@ -376,6 +403,52 @@ impl Store {
.map(|mut guard| guard.deref_mut().push(h))
}
pub fn execute_pre_read_hooks(&self, id: &StoreId) -> Result<()> {
unimplemented!()
}
pub fn execute_post_read_hooks<'a>(&'a self, fle: FileLockEntry<'a>)
-> Result<FileLockEntry<'a>>
{
unimplemented!()
}
pub fn execute_pre_create_hooks(&self, id: &StoreId) -> Result<()> {
unimplemented!()
}
pub fn execute_post_create_hooks<'a>(&'a self, fle: FileLockEntry<'a>)
-> Result<FileLockEntry<'a>>
{
unimplemented!()
}
pub fn execute_pre_retrieve_hooks(&self, id: &StoreId) -> Result<()> {
unimplemented!()
}
pub fn execute_post_retrieve_hooks<'a>(&'a self, fle: FileLockEntry<'a>)
-> Result<FileLockEntry<'a>>
{
unimplemented!()
}
pub fn execute_pre_update_hooks(&self, id: &FileLockEntry) -> Result<()> {
unimplemented!()
}
pub fn execute_post_update_hooks(&self, id: &FileLockEntry) -> Result<()> {
unimplemented!()
}
pub fn execute_pre_delete_hooks(&self, id: &StoreId) -> Result<()> {
unimplemented!()
}
pub fn execute_post_delete_hooks(&self, id: &StoreId) -> Result<()> {
unimplemented!()
}
}
impl Drop for Store {