2017-08-27 18:38:26 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2018-02-07 01:48:53 +00:00
|
|
|
// Copyright (C) 2015-2018 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2017-08-27 18:38:26 +00:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; version
|
|
|
|
// 2.1 of the License.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
//
|
|
|
|
|
2018-02-13 20:11:45 +00:00
|
|
|
use std::path::Path;
|
2017-08-27 18:38:26 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use libimagstore::store::FileLockEntry;
|
|
|
|
use libimagstore::store::Store;
|
2018-02-13 20:11:45 +00:00
|
|
|
use libimagstore::storeid::StoreId;
|
2017-08-27 18:38:26 +00:00
|
|
|
|
2018-03-18 16:52:41 +00:00
|
|
|
use reference::Ref;
|
2017-10-14 11:36:04 +00:00
|
|
|
|
2018-10-30 17:40:52 +00:00
|
|
|
use failure::Fallible as Result;
|
|
|
|
use failure::Error;
|
|
|
|
|
2018-02-13 20:11:45 +00:00
|
|
|
/// A UniqueRefPathGenerator generates unique Pathes
|
|
|
|
///
|
|
|
|
/// It is basically a functor which generates a StoreId from a &Path.
|
|
|
|
/// For more information have a look at the documentation of RefStore.
|
|
|
|
pub trait UniqueRefPathGenerator {
|
|
|
|
/// The collection the `StoreId` should be created for
|
|
|
|
fn collection() -> &'static str {
|
|
|
|
"ref"
|
|
|
|
}
|
2017-08-27 18:38:26 +00:00
|
|
|
|
2018-02-13 20:11:45 +00:00
|
|
|
/// A function which should generate a unique string for a Path
|
2018-10-30 17:40:52 +00:00
|
|
|
fn unique_hash<A: AsRef<Path>>(path: A) -> Result<String>;
|
2017-09-23 18:29:32 +00:00
|
|
|
|
2018-02-13 20:11:45 +00:00
|
|
|
/// Postprocess the generated `StoreId` object
|
2018-10-30 17:40:52 +00:00
|
|
|
fn postprocess_storeid(sid: StoreId) -> Result<StoreId> {
|
2018-02-13 20:11:45 +00:00
|
|
|
Ok(sid)
|
|
|
|
}
|
2017-08-27 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2018-02-13 20:11:45 +00:00
|
|
|
/// A extensions for the `Store` to handle `Ref` objects
|
|
|
|
///
|
|
|
|
/// The RefStore handles refs using a `UniqueRefPathGenerator`. The `UniqueRefPathGenerator`, as it
|
|
|
|
/// name suggests, generates unique `StoreId`s for a `&Path`. It is a functor `&Path -> StoreId`.
|
|
|
|
///
|
|
|
|
/// It provides three functions which are called in the following sequence:
|
|
|
|
///
|
|
|
|
/// * The `UniqueRefPathGenerator::collection()` function is used for get the collection a `StoreId`
|
|
|
|
/// should be in (The first element of the `StoreId` path)
|
|
|
|
/// * The `UniqueRefPathGenerator::unique_hash()` gets the `&Path` which it then should generate a
|
|
|
|
/// unique String for. How this is done does not matter. It can hash the Path itself, read the
|
|
|
|
/// file and hash that or something else. It should be reproduceable, though.
|
|
|
|
/// * These two parts are joined and put into a `StoreId` which the
|
|
|
|
/// `UniqueRefPathGenerator::postprocess_storeid()` function is then allowed to postprocess (for
|
|
|
|
/// example add more parts to the StoreId). The default implementation does nothing.
|
|
|
|
///
|
|
|
|
/// The StoreId which is generated is then used to carry out the actual action (reading, creating
|
|
|
|
/// ...).
|
|
|
|
/// If a entry is created, header information is set (that it is a ref, the hash which was just
|
|
|
|
/// generated and the path of the referenced file)
|
|
|
|
///
|
|
|
|
/// # Details
|
|
|
|
///
|
|
|
|
/// The `UniqueRefPathGenerator` is passed as type parameter to enforce some semantics:
|
|
|
|
///
|
|
|
|
/// * The used `UniqueRefPathGenerator` is defined by the implementation rather than by the runtime
|
|
|
|
/// of the program or some environment. Of course this is only a small hurdle to enforce this, but
|
|
|
|
/// a hint.
|
|
|
|
/// * The `UniqueRefPathGenerator` is a functor which does not carry state.
|
|
|
|
///
|
|
|
|
pub trait RefStore<'a> {
|
|
|
|
|
2018-10-30 17:40:52 +00:00
|
|
|
fn get_ref<RPG: UniqueRefPathGenerator, H: AsRef<str>>(&'a self, hash: H) -> Result<Option<FileLockEntry<'a>>>;
|
|
|
|
fn create_ref<RPG: UniqueRefPathGenerator, A: AsRef<Path>>(&'a self, path: A) -> Result<FileLockEntry<'a>>;
|
|
|
|
fn retrieve_ref<RPG: UniqueRefPathGenerator, A: AsRef<Path>>(&'a self, path: A) -> Result<FileLockEntry<'a>>;
|
2017-08-27 18:38:26 +00:00
|
|
|
|
2018-02-13 20:11:45 +00:00
|
|
|
}
|
2017-08-27 18:38:26 +00:00
|
|
|
|
2018-02-13 20:11:45 +00:00
|
|
|
impl<'a> RefStore<'a> for Store {
|
2017-08-27 18:38:26 +00:00
|
|
|
|
2018-02-13 22:39:43 +00:00
|
|
|
fn get_ref<RPG: UniqueRefPathGenerator, H: AsRef<str>>(&'a self, hash: H)
|
2018-10-30 17:40:52 +00:00
|
|
|
-> Result<Option<FileLockEntry<'a>>>
|
2018-02-13 21:07:17 +00:00
|
|
|
{
|
2018-02-13 22:39:43 +00:00
|
|
|
let sid = StoreId::new_baseless(PathBuf::from(format!("{}/{}", RPG::collection(), hash.as_ref())))
|
2018-10-30 17:40:52 +00:00
|
|
|
.map_err(Error::from)?;
|
2017-10-14 11:41:44 +00:00
|
|
|
|
2018-02-13 21:07:17 +00:00
|
|
|
debug!("Getting: {:?}", sid);
|
|
|
|
self.get(sid)
|
2018-10-30 17:40:52 +00:00
|
|
|
.map_err(Error::from)
|
2017-08-27 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2018-02-13 21:07:17 +00:00
|
|
|
fn create_ref<RPG: UniqueRefPathGenerator, A: AsRef<Path>>(&'a self, path: A)
|
2018-10-30 17:40:52 +00:00
|
|
|
-> Result<FileLockEntry<'a>>
|
2018-02-13 21:07:17 +00:00
|
|
|
{
|
2018-04-13 12:03:35 +00:00
|
|
|
let hash = RPG::unique_hash(&path)?;
|
2018-02-13 21:07:17 +00:00
|
|
|
let pathbuf = PathBuf::from(format!("{}/{}", RPG::collection(), hash));
|
2018-10-30 17:40:52 +00:00
|
|
|
let sid = StoreId::new_baseless(pathbuf.clone())?;
|
2018-02-13 21:07:17 +00:00
|
|
|
|
|
|
|
debug!("Creating: {:?}", sid);
|
|
|
|
self.create(sid)
|
2018-10-30 17:40:52 +00:00
|
|
|
.map_err(Error::from)
|
2018-02-13 21:07:17 +00:00
|
|
|
.and_then(|mut fle| {
|
2018-04-13 12:03:35 +00:00
|
|
|
fle.make_ref(hash, path)?;
|
2018-02-13 21:07:17 +00:00
|
|
|
Ok(fle)
|
|
|
|
})
|
2017-08-27 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2018-02-13 21:07:17 +00:00
|
|
|
fn retrieve_ref<RPG: UniqueRefPathGenerator, A: AsRef<Path>>(&'a self, path: A)
|
2018-10-30 17:40:52 +00:00
|
|
|
-> Result<FileLockEntry<'a>>
|
2017-08-27 18:38:26 +00:00
|
|
|
{
|
2018-02-13 22:39:43 +00:00
|
|
|
match self.get_ref::<RPG, String>(RPG::unique_hash(path.as_ref())?)? {
|
2018-02-13 21:07:17 +00:00
|
|
|
Some(r) => Ok(r),
|
|
|
|
None => self.create_ref::<RPG, A>(path),
|
|
|
|
}
|
2017-08-27 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-08-27 19:08:23 +00:00
|
|
|
|