Merge pull request #489 from matthiasbeyer/libimagstore/storify-id

Libimagstore/storify
This commit is contained in:
Matthias Beyer 2016-07-04 19:26:42 +02:00 committed by GitHub
commit d7f22020e6
3 changed files with 26 additions and 16 deletions

View file

@ -34,6 +34,7 @@ generate_custom_error_types!(StoreError, StoreErrorKind, CustomErrorData,
EncodingError => "Encoding error",
StorePathError => "Store Path error",
EntryRenameError => "Entry rename error",
StoreIdHandlingError => "StoreId handling error",
CreateCallError => "Error when calling create()",
RetrieveCallError => "Error when calling retrieve()",

View file

@ -317,17 +317,9 @@ impl Store {
self.configuration.as_ref()
}
fn storify_id(&self, id: StoreId) -> StoreId {
debug!("Create new store id out of: {:?} and {:?}", self.location, id);
let mut new_id = self.location.clone();
new_id.push(id);
debug!("Created: '{:?}'", new_id);
StoreId::from(new_id)
}
/// Creates the Entry at the given location (inside the entry)
pub fn create<'a, S: IntoStoreId>(&'a self, id: S) -> Result<FileLockEntry<'a>> {
let id = self.storify_id(id.into_storeid());
let id = id.into_storeid().storified(self);
if let Err(e) = self.execute_hooks_for_id(self.pre_create_aspects.clone(), &id) {
if e.is_aborting() {
return Err(e)
@ -365,7 +357,7 @@ impl Store {
/// Implicitely creates a entry in the store if there is no entry with the id `id`. For a
/// non-implicitely-create look at `Store::get`.
pub fn retrieve<'a, S: IntoStoreId>(&'a self, id: S) -> Result<FileLockEntry<'a>> {
let id = self.storify_id(id.into_storeid());
let id = id.into_storeid().storified(self);
if let Err(e) = self.execute_hooks_for_id(self.pre_retrieve_aspects.clone(), &id) {
if e.is_aborting() {
return Err(e)
@ -398,7 +390,7 @@ impl Store {
///
/// This executes the {pre,post}_retrieve_aspects hooks.
pub fn get<'a, S: IntoStoreId + Clone>(&'a self, id: S) -> Result<Option<FileLockEntry<'a>>> {
if !self.storify_id(id.clone().into_storeid()).exists() {
if !id.clone().into_storeid().storified(self).exists() {
debug!("Does not exist: {:?}", id.clone().into_storeid());
return Ok(None);
}
@ -520,7 +512,7 @@ impl Store {
/// Retrieve a copy of a given entry, this cannot be used to mutate
/// the one on disk
pub fn retrieve_copy<S: IntoStoreId>(&self, id: S) -> Result<Entry> {
let id = self.storify_id(id.into_storeid());
let id = id.into_storeid().storified(self);
let entries = match self.entries.write() {
Err(_) => {
return Err(SE::new(SEK::LockPoisoned, None))
@ -539,7 +531,7 @@ impl Store {
/// Delete an entry
pub fn delete<S: IntoStoreId>(&self, id: S) -> Result<()> {
let id = self.storify_id(id.into_storeid());
let id = id.into_storeid().storified(self);
if let Err(e) = self.execute_hooks_for_id(self.pre_delete_aspects.clone(), &id) {
if e.is_aborting() {
return Err(e)
@ -593,7 +585,7 @@ impl Store {
use std::fs::copy;
use std::fs::remove_file;
let new_id = self.storify_id(new_id);
let new_id = new_id.storified(self);
let hsmap = self.entries.write();
if hsmap.is_err() {
return Err(SE::new(SEK::LockPoisoned, None)).map_err_into(SEK::MoveCallError)
@ -622,8 +614,8 @@ impl Store {
pub fn move_by_id(&self, old_id: StoreId, new_id: StoreId) -> Result<()> {
use std::fs::rename;
let new_id = self.storify_id(new_id);
let old_id = self.storify_id(old_id);
let new_id = new_id.storified(self);
let old_id = old_id.storified(self);
if let Err(e) = self.execute_hooks_for_id(self.pre_move_aspects.clone(), &old_id) {
if e.is_aborting() {

View file

@ -16,6 +16,23 @@ use store::Store;
#[derive(Debug, Clone, PartialEq, Hash, Eq, PartialOrd, Ord)]
pub struct StoreId(PathBuf);
impl StoreId {
pub fn storified(self, store: &Store) -> StoreId {
if self.starts_with(store.path()) {
debug!("Not storifying {:?}, because it is already.", self);
self
} else {
debug!("Create new store id out of: {:?} and {:?}", store.path(), self);
let mut new_id = store.path().clone();
new_id.push(self);
debug!("Created: '{:?}'", new_id);
StoreId::from(new_id)
}
}
}
impl Into<PathBuf> for StoreId {
fn into(self) -> PathBuf {