2016-10-01 15:35:06 +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
|
2016-10-01 15:35:06 +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
|
|
|
|
//
|
|
|
|
|
2017-02-22 09:53:27 +00:00
|
|
|
use std::collections::BTreeMap;
|
2017-02-22 10:04:22 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
use std::path::PathBuf;
|
2017-02-22 09:53:27 +00:00
|
|
|
|
2016-02-21 13:48:21 +00:00
|
|
|
use libimagstore::storeid::StoreId;
|
2017-02-22 09:53:27 +00:00
|
|
|
use libimagstore::storeid::IntoStoreId;
|
2016-02-15 12:27:40 +00:00
|
|
|
use libimagstore::store::Entry;
|
2018-02-25 16:04:05 +00:00
|
|
|
use libimagstore::store::Store;
|
2018-10-30 17:40:51 +00:00
|
|
|
use libimagerror::errors::ErrorMsg as EM;
|
2016-02-03 14:47:14 +00:00
|
|
|
|
2017-08-26 15:53:08 +00:00
|
|
|
use toml_query::read::TomlValueReadExt;
|
2017-09-07 20:13:00 +00:00
|
|
|
use toml_query::insert::TomlValueInsertExt;
|
2018-10-30 17:40:51 +00:00
|
|
|
use failure::ResultExt;
|
|
|
|
use failure::Fallible as Result;
|
|
|
|
use failure::Error;
|
|
|
|
use failure::err_msg;
|
2017-08-26 15:53:08 +00:00
|
|
|
|
2016-10-13 16:28:15 +00:00
|
|
|
use self::iter::LinkIter;
|
|
|
|
use self::iter::IntoValues;
|
2016-02-03 14:47:14 +00:00
|
|
|
|
2016-02-07 15:25:17 +00:00
|
|
|
use toml::Value;
|
|
|
|
|
2017-02-22 09:53:27 +00:00
|
|
|
#[derive(Eq, PartialOrd, Ord, Hash, Debug, Clone)]
|
|
|
|
pub enum Link {
|
|
|
|
Id { link: StoreId },
|
|
|
|
Annotated { link: StoreId, annotation: String },
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Link {
|
|
|
|
|
2017-04-22 09:58:14 +00:00
|
|
|
pub fn exists(&self) -> Result<bool> {
|
2017-02-23 13:40:33 +00:00
|
|
|
match *self {
|
|
|
|
Link::Id { ref link } => link.exists(),
|
|
|
|
Link::Annotated { ref link, .. } => link.exists(),
|
|
|
|
}
|
2017-09-23 13:03:29 +00:00
|
|
|
.map_err(From::from)
|
2017-02-23 13:40:33 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 14:25:32 +00:00
|
|
|
pub fn to_str(&self) -> Result<String> {
|
|
|
|
match *self {
|
|
|
|
Link::Id { ref link } => link.to_str(),
|
|
|
|
Link::Annotated { ref link, .. } => link.to_str(),
|
|
|
|
}
|
2017-09-23 13:03:29 +00:00
|
|
|
.map_err(From::from)
|
2017-02-23 14:25:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-22 09:53:27 +00:00
|
|
|
fn eq_store_id(&self, id: &StoreId) -> bool {
|
|
|
|
match self {
|
|
|
|
&Link::Id { link: ref s } => s.eq(id),
|
|
|
|
&Link::Annotated { link: ref s, .. } => s.eq(id),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 15:53:50 +00:00
|
|
|
/// Get the StoreId inside the Link, which is always present
|
|
|
|
pub fn get_store_id(&self) -> &StoreId {
|
|
|
|
match self {
|
|
|
|
&Link::Id { link: ref s } => s,
|
|
|
|
&Link::Annotated { link: ref s, .. } => s,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-22 09:53:27 +00:00
|
|
|
/// Helper wrapper around Link for StoreId
|
|
|
|
fn without_base(self) -> Link {
|
|
|
|
match self {
|
|
|
|
Link::Id { link: s } => Link::Id { link: s.without_base() },
|
|
|
|
Link::Annotated { link: s, annotation: ann } =>
|
|
|
|
Link::Annotated { link: s.without_base(), annotation: ann },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-22 10:04:22 +00:00
|
|
|
/// Helper wrapper around Link for StoreId
|
|
|
|
#[cfg(test)]
|
|
|
|
fn with_base(self, pb: PathBuf) -> Link {
|
|
|
|
match self {
|
|
|
|
Link::Id { link: s } => Link::Id { link: s.with_base(pb) },
|
|
|
|
Link::Annotated { link: s, annotation: ann } =>
|
|
|
|
Link::Annotated { link: s.with_base(pb), annotation: ann },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-22 09:53:27 +00:00
|
|
|
fn to_value(&self) -> Result<Value> {
|
|
|
|
match self {
|
|
|
|
&Link::Id { link: ref s } =>
|
2018-10-30 17:40:51 +00:00
|
|
|
s.to_str()
|
|
|
|
.map(Value::String)
|
|
|
|
.context(EM::ConversionError)
|
|
|
|
.map_err(Error::from),
|
2017-02-22 09:53:27 +00:00
|
|
|
&Link::Annotated { ref link, annotation: ref anno } => {
|
|
|
|
link.to_str()
|
|
|
|
.map(Value::String)
|
2018-10-30 17:40:51 +00:00
|
|
|
.context(EM::ConversionError)
|
|
|
|
.map_err(Error::from)
|
2017-02-22 09:53:27 +00:00
|
|
|
.map(|link| {
|
|
|
|
let mut tab = BTreeMap::new();
|
|
|
|
|
|
|
|
tab.insert("link".to_owned(), link);
|
|
|
|
tab.insert("annotation".to_owned(), Value::String(anno.clone()));
|
|
|
|
Value::Table(tab)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::cmp::PartialEq for Link {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
match (self, other) {
|
|
|
|
(&Link::Id { link: ref a }, &Link::Id { link: ref b }) => a.eq(&b),
|
|
|
|
(&Link::Annotated { link: ref a, annotation: ref ann1 },
|
|
|
|
&Link::Annotated { link: ref b, annotation: ref ann2 }) =>
|
|
|
|
(a, ann1).eq(&(b, ann2)),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<StoreId> for Link {
|
|
|
|
|
|
|
|
fn from(s: StoreId) -> Link {
|
|
|
|
Link::Id { link: s }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<StoreId> for Link {
|
|
|
|
fn into(self) -> StoreId {
|
|
|
|
match self {
|
|
|
|
Link::Id { link } => link,
|
|
|
|
Link::Annotated { link, .. } => link,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoStoreId for Link {
|
2018-10-30 17:40:51 +00:00
|
|
|
fn into_storeid(self) -> Result<StoreId> {
|
2017-02-22 09:53:27 +00:00
|
|
|
match self {
|
|
|
|
Link::Id { link } => Ok(link),
|
|
|
|
Link::Annotated { link, .. } => Ok(link),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<StoreId> for Link {
|
|
|
|
fn as_ref(&self) -> &StoreId {
|
|
|
|
match self {
|
|
|
|
&Link::Id { ref link } => &link,
|
|
|
|
&Link::Annotated { ref link, .. } => &link,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-21 13:48:21 +00:00
|
|
|
|
2016-02-15 12:26:00 +00:00
|
|
|
pub trait InternalLinker {
|
2016-02-03 14:47:14 +00:00
|
|
|
|
2016-02-15 12:26:00 +00:00
|
|
|
/// Get the internal links from the implementor object
|
2016-10-13 16:28:15 +00:00
|
|
|
fn get_internal_links(&self) -> Result<LinkIter>;
|
2016-02-15 12:26:00 +00:00
|
|
|
|
|
|
|
/// Set the internal links for the implementor object
|
2016-10-13 16:28:15 +00:00
|
|
|
fn set_internal_links(&mut self, links: Vec<&mut Entry>) -> Result<LinkIter>;
|
2016-02-15 12:26:00 +00:00
|
|
|
|
|
|
|
/// Add an internal link to the implementor object
|
2016-02-21 13:48:21 +00:00
|
|
|
fn add_internal_link(&mut self, link: &mut Entry) -> Result<()>;
|
2016-02-15 12:26:00 +00:00
|
|
|
|
|
|
|
/// Remove an internal link from the implementor object
|
2016-02-21 13:48:21 +00:00
|
|
|
fn remove_internal_link(&mut self, link: &mut Entry) -> Result<()>;
|
2016-02-03 14:47:14 +00:00
|
|
|
|
2018-02-25 16:04:05 +00:00
|
|
|
/// Remove _all_ internal links
|
|
|
|
fn unlink(&mut self, store: &Store) -> Result<()>;
|
|
|
|
|
2017-02-22 16:40:28 +00:00
|
|
|
/// Add internal annotated link
|
|
|
|
fn add_internal_annotated_link(&mut self, link: &mut Entry, annotation: String) -> Result<()>;
|
2016-02-03 14:47:14 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 16:28:15 +00:00
|
|
|
pub mod iter {
|
|
|
|
use std::vec::IntoIter;
|
|
|
|
use super::Link;
|
|
|
|
|
2018-10-30 17:40:51 +00:00
|
|
|
use failure::Error;
|
|
|
|
use failure::Fallible as Result;
|
|
|
|
use failure::ResultExt;
|
2016-10-13 16:28:15 +00:00
|
|
|
use toml::Value;
|
|
|
|
use itertools::Itertools;
|
|
|
|
|
2016-10-13 16:50:41 +00:00
|
|
|
use libimagstore::store::Store;
|
|
|
|
use libimagstore::store::FileLockEntry;
|
2018-10-30 17:40:51 +00:00
|
|
|
use libimagerror::errors::ErrorMsg as EM;
|
2016-10-13 16:50:41 +00:00
|
|
|
|
2016-10-13 16:28:15 +00:00
|
|
|
pub struct LinkIter(IntoIter<Link>);
|
|
|
|
|
|
|
|
impl LinkIter {
|
|
|
|
|
|
|
|
pub fn new(v: Vec<Link>) -> LinkIter {
|
|
|
|
LinkIter(v.into_iter())
|
|
|
|
}
|
|
|
|
|
2016-10-13 16:50:41 +00:00
|
|
|
pub fn into_getter(self, store: &Store) -> GetIter {
|
|
|
|
GetIter(self.0, store)
|
|
|
|
}
|
|
|
|
|
2016-10-13 16:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for LinkIter {
|
|
|
|
type Item = Link;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
self.0.next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait IntoValues {
|
2017-02-22 09:53:27 +00:00
|
|
|
fn into_values(self) -> Vec<Result<Value>>;
|
2016-10-13 16:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<I: Iterator<Item = Link>> IntoValues for I {
|
2017-02-22 09:53:27 +00:00
|
|
|
fn into_values(self) -> Vec<Result<Value>> {
|
|
|
|
self.map(|s| s.without_base())
|
|
|
|
.unique()
|
|
|
|
.sorted()
|
|
|
|
.into_iter() // Cannot sort toml::Value, hence uglyness here
|
2018-10-30 17:40:51 +00:00
|
|
|
.map(|link| link.to_value().context(EM::ConversionError).map_err(Error::from))
|
2017-02-22 09:53:27 +00:00
|
|
|
.collect()
|
2016-10-13 16:28:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-13 16:50:41 +00:00
|
|
|
/// An Iterator that `Store::get()`s the Entries from the store while consumed
|
|
|
|
pub struct GetIter<'a>(IntoIter<Link>, &'a Store);
|
|
|
|
|
|
|
|
impl<'a> GetIter<'a> {
|
2016-10-20 12:11:22 +00:00
|
|
|
pub fn new(i: IntoIter<Link>, store: &'a Store) -> GetIter<'a> {
|
2016-10-13 16:50:41 +00:00
|
|
|
GetIter(i, store)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Turn this iterator into a LinkGcIter, which `Store::delete()`s entries that are not
|
|
|
|
/// linked to any other entry.
|
2016-10-17 07:23:20 +00:00
|
|
|
pub fn delete_unlinked(self) -> DeleteUnlinkedIter<'a> {
|
|
|
|
DeleteUnlinkedIter(self)
|
2016-10-13 16:50:41 +00:00
|
|
|
}
|
|
|
|
|
2016-10-17 13:45:04 +00:00
|
|
|
/// Turn this iterator into a FilterLinksIter that removes all entries that are not linked
|
|
|
|
/// to any other entry, by filtering them out the iterator.
|
|
|
|
///
|
|
|
|
/// This does _not_ remove the entries from the store.
|
|
|
|
pub fn without_unlinked(self) -> FilterLinksIter<'a> {
|
|
|
|
FilterLinksIter::new(self, Box::new(|links: &[Link]| links.len() > 0))
|
2016-10-17 11:49:56 +00:00
|
|
|
}
|
|
|
|
|
2016-10-17 13:45:04 +00:00
|
|
|
/// Turn this iterator into a FilterLinksIter that removes all entries that have less than
|
|
|
|
/// `n` links to any other entries.
|
|
|
|
///
|
|
|
|
/// This does _not_ remove the entries from the store.
|
|
|
|
pub fn with_less_than_n_links(self, n: usize) -> FilterLinksIter<'a> {
|
|
|
|
FilterLinksIter::new(self, Box::new(move |links: &[Link]| links.len() < n))
|
2016-10-17 07:25:41 +00:00
|
|
|
}
|
|
|
|
|
2016-10-17 13:45:04 +00:00
|
|
|
/// Turn this iterator into a FilterLinksIter that removes all entries that have more than
|
|
|
|
/// `n` links to any other entries.
|
|
|
|
///
|
|
|
|
/// This does _not_ remove the entries from the store.
|
|
|
|
pub fn with_more_than_n_links(self, n: usize) -> FilterLinksIter<'a> {
|
|
|
|
FilterLinksIter::new(self, Box::new(move |links: &[Link]| links.len() > n))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Turn this iterator into a FilterLinksIter that removes all entries where the predicate
|
|
|
|
/// `F` returns false
|
|
|
|
///
|
|
|
|
/// This does _not_ remove the entries from the store.
|
|
|
|
pub fn filtered_for_links(self, f: Box<Fn(&[Link]) -> bool>) -> FilterLinksIter<'a> {
|
|
|
|
FilterLinksIter::new(self, f)
|
2016-10-17 11:51:40 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 16:50:41 +00:00
|
|
|
pub fn store(&self) -> &Store {
|
|
|
|
self.1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for GetIter<'a> {
|
|
|
|
type Item = Result<FileLockEntry<'a>>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2017-09-23 13:03:29 +00:00
|
|
|
self.0.next().and_then(|id| match self.1.get(id) {
|
2016-10-13 16:50:41 +00:00
|
|
|
Ok(None) => None,
|
|
|
|
Ok(Some(x)) => Some(Ok(x)),
|
2017-09-23 13:03:29 +00:00
|
|
|
Err(e) => Some(Err(e).map_err(From::from)),
|
2016-10-13 16:50:41 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-10-17 13:43:33 +00:00
|
|
|
/// An iterator helper that has a function F.
|
|
|
|
///
|
|
|
|
/// If the function F returns `false` for the number of links, the entry is ignored, else it is
|
|
|
|
/// taken.
|
2016-10-17 13:45:04 +00:00
|
|
|
pub struct FilterLinksIter<'a>(GetIter<'a>, Box<Fn(&[Link]) -> bool>);
|
|
|
|
|
|
|
|
impl<'a> FilterLinksIter<'a> {
|
|
|
|
pub fn new(gi: GetIter<'a>, f: Box<Fn(&[Link]) -> bool>) -> FilterLinksIter<'a> {
|
|
|
|
FilterLinksIter(gi, f)
|
|
|
|
}
|
|
|
|
}
|
2016-10-17 07:25:41 +00:00
|
|
|
|
2016-10-17 13:45:04 +00:00
|
|
|
impl<'a> Iterator for FilterLinksIter<'a> {
|
2016-10-17 11:51:40 +00:00
|
|
|
type Item = Result<FileLockEntry<'a>>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
use internal::InternalLinker;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match self.0.next() {
|
|
|
|
Some(Ok(fle)) => {
|
2017-09-23 13:03:29 +00:00
|
|
|
let links = match fle.get_internal_links() {
|
2016-10-17 11:51:40 +00:00
|
|
|
Err(e) => return Some(Err(e)),
|
2016-10-17 13:45:04 +00:00
|
|
|
Ok(links) => links.collect::<Vec<_>>(),
|
2016-10-17 11:51:40 +00:00
|
|
|
};
|
2016-10-17 13:45:04 +00:00
|
|
|
if !(self.1)(&links) {
|
2016-10-17 11:51:40 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
return Some(Ok(fle));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Some(Err(e)) => return Some(Err(e)),
|
2016-10-17 07:25:41 +00:00
|
|
|
None => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-10-13 16:50:41 +00:00
|
|
|
/// An iterator that removes all Items from the iterator that are not linked anymore by calling
|
|
|
|
/// `Store::delete()` on them.
|
|
|
|
///
|
|
|
|
/// It yields only items which are somehow linked to another entry
|
2016-10-17 07:25:41 +00:00
|
|
|
///
|
|
|
|
/// # Warning
|
|
|
|
///
|
|
|
|
/// Deletes entries from the store.
|
|
|
|
///
|
2016-10-17 07:23:20 +00:00
|
|
|
pub struct DeleteUnlinkedIter<'a>(GetIter<'a>);
|
2016-10-13 16:50:41 +00:00
|
|
|
|
2016-10-17 07:23:20 +00:00
|
|
|
impl<'a> Iterator for DeleteUnlinkedIter<'a> {
|
2016-10-13 16:50:41 +00:00
|
|
|
type Item = Result<FileLockEntry<'a>>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
use internal::InternalLinker;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match self.0.next() {
|
|
|
|
Some(Ok(fle)) => {
|
2017-09-23 13:03:29 +00:00
|
|
|
let links = match fle.get_internal_links() {
|
2016-10-13 16:50:41 +00:00
|
|
|
Err(e) => return Some(Err(e)),
|
|
|
|
Ok(links) => links,
|
|
|
|
};
|
|
|
|
if links.count() == 0 {
|
2017-09-23 13:03:29 +00:00
|
|
|
match self.0.store().delete(fle.get_location().clone()) {
|
2016-10-13 16:50:41 +00:00
|
|
|
Ok(x) => x,
|
2017-09-23 13:03:29 +00:00
|
|
|
Err(e) => return Some(Err(e).map_err(From::from)),
|
2016-10-13 16:50:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Some(Ok(fle));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Some(Err(e)) => return Some(Err(e)),
|
|
|
|
None => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-10-13 16:28:15 +00:00
|
|
|
}
|
|
|
|
|
2016-02-21 13:48:21 +00:00
|
|
|
impl InternalLinker for Entry {
|
2016-02-15 12:26:00 +00:00
|
|
|
|
2016-10-13 16:28:15 +00:00
|
|
|
fn get_internal_links(&self) -> Result<LinkIter> {
|
2018-04-30 13:48:08 +00:00
|
|
|
debug!("Getting internal links");
|
|
|
|
trace!("Getting internal links from header of '{}' = {:?}", self.get_location(), self.get_header());
|
2017-08-26 15:53:08 +00:00
|
|
|
let res = self
|
|
|
|
.get_header()
|
2017-09-07 20:13:00 +00:00
|
|
|
.read("links.internal")
|
2018-10-30 17:40:51 +00:00
|
|
|
.map_err(Error::from)
|
|
|
|
.context(EM::EntryHeaderReadError)
|
|
|
|
.context(EM::EntryHeaderError)
|
|
|
|
.map_err(Error::from)
|
2017-08-26 15:53:08 +00:00
|
|
|
.map(|r| r.cloned());
|
|
|
|
process_rw_result(res)
|
2016-02-15 12:26:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the links in a header and return the old links, if any.
|
2016-10-13 16:28:15 +00:00
|
|
|
fn set_internal_links(&mut self, links: Vec<&mut Entry>) -> Result<LinkIter> {
|
|
|
|
use internal::iter::IntoValues;
|
|
|
|
|
2018-04-30 13:48:08 +00:00
|
|
|
debug!("Setting internal links");
|
|
|
|
|
2016-02-21 13:48:21 +00:00
|
|
|
let self_location = self.get_location().clone();
|
|
|
|
let mut new_links = vec![];
|
|
|
|
|
|
|
|
for link in links {
|
|
|
|
if let Err(e) = add_foreign_link(link, self_location.clone()) {
|
|
|
|
return Err(e);
|
|
|
|
}
|
2017-02-22 09:53:27 +00:00
|
|
|
new_links.push(link.get_location().clone().into());
|
2016-02-21 13:48:21 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 19:17:21 +00:00
|
|
|
let new_links = LinkIter::new(new_links)
|
2016-10-13 16:28:15 +00:00
|
|
|
.into_values()
|
2017-02-22 09:53:27 +00:00
|
|
|
.into_iter()
|
2018-10-30 17:40:51 +00:00
|
|
|
.fold(Ok(vec![]), |acc: Result<Vec<_>>, elem| {
|
2016-08-03 09:20:25 +00:00
|
|
|
acc.and_then(move |mut v| {
|
2018-10-30 17:40:51 +00:00
|
|
|
v.push(elem.context(EM::ConversionError)?);
|
|
|
|
Ok(v)
|
2016-08-03 09:20:25 +00:00
|
|
|
})
|
2017-10-30 19:17:21 +00:00
|
|
|
})?;
|
2017-08-26 15:53:08 +00:00
|
|
|
let res = self
|
|
|
|
.get_header_mut()
|
2017-09-21 10:23:32 +00:00
|
|
|
.insert("links.internal", Value::Array(new_links))
|
2018-10-30 17:40:51 +00:00
|
|
|
.map_err(Error::from)
|
|
|
|
.context(EM::EntryHeaderReadError)
|
|
|
|
.map_err(Error::from);
|
2017-08-26 15:53:08 +00:00
|
|
|
process_rw_result(res)
|
2016-02-15 12:26:00 +00:00
|
|
|
}
|
|
|
|
|
2016-02-21 13:48:21 +00:00
|
|
|
fn add_internal_link(&mut self, link: &mut Entry) -> Result<()> {
|
2018-04-30 13:48:08 +00:00
|
|
|
debug!("Adding internal link: {:?}", link);
|
2017-02-22 16:40:28 +00:00
|
|
|
let location = link.get_location().clone().into();
|
|
|
|
add_internal_link_with_instance(self, link, location)
|
2016-02-15 12:26:00 +00:00
|
|
|
}
|
|
|
|
|
2016-02-21 13:48:21 +00:00
|
|
|
fn remove_internal_link(&mut self, link: &mut Entry) -> Result<()> {
|
2018-04-30 13:48:08 +00:00
|
|
|
debug!("Removing internal link: {:?}", link);
|
2016-10-15 09:24:42 +00:00
|
|
|
let own_loc = self.get_location().clone().without_base();
|
|
|
|
let other_loc = link.get_location().clone().without_base();
|
2016-02-21 13:48:21 +00:00
|
|
|
|
2016-10-15 09:24:14 +00:00
|
|
|
debug!("Removing internal link from {:?} to {:?}", own_loc, other_loc);
|
|
|
|
|
2016-02-21 13:48:21 +00:00
|
|
|
link.get_internal_links()
|
2016-04-08 21:57:16 +00:00
|
|
|
.and_then(|links| {
|
2016-10-15 09:24:14 +00:00
|
|
|
debug!("Rewriting own links for {:?}, without {:?}", other_loc, own_loc);
|
2017-02-22 09:53:27 +00:00
|
|
|
let links = links.filter(|l| !l.eq_store_id(&own_loc));
|
|
|
|
rewrite_links(link.get_header_mut(), links)
|
2016-02-21 13:48:21 +00:00
|
|
|
})
|
|
|
|
.and_then(|_| {
|
|
|
|
self.get_internal_links()
|
2016-04-08 21:57:16 +00:00
|
|
|
.and_then(|links| {
|
2016-10-15 09:24:14 +00:00
|
|
|
debug!("Rewriting own links for {:?}, without {:?}", own_loc, other_loc);
|
2017-02-22 09:53:27 +00:00
|
|
|
let links = links.filter(|l| !l.eq_store_id(&other_loc));
|
|
|
|
rewrite_links(self.get_header_mut(), links)
|
2016-02-21 13:48:21 +00:00
|
|
|
})
|
2016-02-15 12:26:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-25 16:04:05 +00:00
|
|
|
fn unlink(&mut self, store: &Store) -> Result<()> {
|
|
|
|
for id in self.get_internal_links()?.map(|l| l.get_store_id().clone()) {
|
2018-10-30 17:40:51 +00:00
|
|
|
match store.get(id).map_err(Error::from)? {
|
2018-02-25 16:04:05 +00:00
|
|
|
Some(mut entry) => self.remove_internal_link(&mut entry)?,
|
2018-10-30 17:40:51 +00:00
|
|
|
None => return Err(err_msg("Link target does not exist")),
|
2018-02-25 16:04:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-02-22 16:40:28 +00:00
|
|
|
fn add_internal_annotated_link(&mut self, link: &mut Entry, annotation: String) -> Result<()> {
|
|
|
|
let new_link = Link::Annotated {
|
|
|
|
link: link.get_location().clone(),
|
|
|
|
annotation: annotation,
|
|
|
|
};
|
|
|
|
|
|
|
|
add_internal_link_with_instance(self, link, new_link)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_internal_link_with_instance(this: &mut Entry, link: &mut Entry, instance: Link) -> Result<()> {
|
|
|
|
debug!("Adding internal link from {:?} to {:?}", this.get_location(), instance);
|
|
|
|
|
|
|
|
add_foreign_link(link, this.get_location().clone())
|
|
|
|
.and_then(|_| {
|
|
|
|
this.get_internal_links()
|
|
|
|
.and_then(|links| {
|
|
|
|
let links = links.chain(LinkIter::new(vec![instance]));
|
|
|
|
rewrite_links(this.get_header_mut(), links)
|
|
|
|
})
|
|
|
|
})
|
2016-02-15 12:19:34 +00:00
|
|
|
}
|
|
|
|
|
2016-11-17 10:32:59 +00:00
|
|
|
fn rewrite_links<I: Iterator<Item = Link>>(header: &mut Value, links: I) -> Result<()> {
|
2017-10-30 19:17:21 +00:00
|
|
|
let links = links.into_values()
|
2017-02-22 09:53:27 +00:00
|
|
|
.into_iter()
|
2018-10-30 17:40:51 +00:00
|
|
|
.fold(Ok(vec![]), |acc: Result<Vec<_>>, elem| {
|
2016-08-03 09:29:38 +00:00
|
|
|
acc.and_then(move |mut v| {
|
2018-10-30 17:40:51 +00:00
|
|
|
v.push(elem.context(EM::ConversionError)?);
|
|
|
|
Ok(v)
|
2016-08-03 09:29:38 +00:00
|
|
|
})
|
2017-10-30 19:17:21 +00:00
|
|
|
})?;
|
2016-08-03 09:29:38 +00:00
|
|
|
|
2016-10-15 09:24:14 +00:00
|
|
|
debug!("Setting new link array: {:?}", links);
|
2017-08-26 15:53:08 +00:00
|
|
|
let process = header
|
2017-09-07 20:13:00 +00:00
|
|
|
.insert("links.internal", Value::Array(links))
|
2018-10-30 17:40:51 +00:00
|
|
|
.map_err(Error::from)
|
|
|
|
.context(EM::EntryHeaderReadError)
|
|
|
|
.map_err(Error::from);
|
2016-08-03 09:29:38 +00:00
|
|
|
process_rw_result(process).map(|_| ())
|
2016-04-08 22:07:30 +00:00
|
|
|
}
|
|
|
|
|
2016-02-21 13:48:21 +00:00
|
|
|
/// When Linking A -> B, the specification wants us to link back B -> A.
|
|
|
|
/// This is a helper function which does this.
|
|
|
|
fn add_foreign_link(target: &mut Entry, from: StoreId) -> Result<()> {
|
2016-10-15 09:24:14 +00:00
|
|
|
debug!("Linking back from {:?} to {:?}", target.get_location(), from);
|
2016-02-21 13:48:21 +00:00
|
|
|
target.get_internal_links()
|
2016-10-13 16:28:15 +00:00
|
|
|
.and_then(|links| {
|
2017-10-30 19:17:21 +00:00
|
|
|
let links = links
|
2017-02-22 09:53:27 +00:00
|
|
|
.chain(LinkIter::new(vec![from.into()]))
|
2016-10-13 16:28:15 +00:00
|
|
|
.into_values()
|
2017-02-22 09:53:27 +00:00
|
|
|
.into_iter()
|
2018-10-30 17:40:51 +00:00
|
|
|
.fold(Ok(vec![]), |acc: Result<Vec<_>>, elem| {
|
2016-08-03 09:30:23 +00:00
|
|
|
acc.and_then(move |mut v| {
|
2018-10-30 17:40:51 +00:00
|
|
|
v.push(elem.context(EM::ConversionError)?);
|
|
|
|
Ok(v)
|
2016-08-03 09:30:23 +00:00
|
|
|
})
|
2017-10-30 19:17:21 +00:00
|
|
|
})?;
|
2016-10-15 09:24:14 +00:00
|
|
|
debug!("Setting links in {:?}: {:?}", target.get_location(), links);
|
2017-08-26 15:53:08 +00:00
|
|
|
|
|
|
|
let res = target
|
|
|
|
.get_header_mut()
|
2017-09-07 20:13:00 +00:00
|
|
|
.insert("links.internal", Value::Array(links))
|
2018-10-30 17:40:51 +00:00
|
|
|
.map_err(Error::from)
|
|
|
|
.context(EM::EntryHeaderReadError)
|
|
|
|
.map_err(Error::from);
|
2017-08-26 15:53:08 +00:00
|
|
|
|
|
|
|
process_rw_result(res).map(|_| ())
|
2016-02-21 13:48:21 +00:00
|
|
|
})
|
2016-02-15 12:27:40 +00:00
|
|
|
}
|
|
|
|
|
2017-08-26 15:53:08 +00:00
|
|
|
fn process_rw_result(links: Result<Option<Value>>) -> Result<LinkIter> {
|
2016-08-25 17:00:42 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2016-06-11 04:50:58 +00:00
|
|
|
let links = match links {
|
|
|
|
Err(e) => {
|
|
|
|
debug!("RW action on store failed. Generating LinkError");
|
2018-10-30 17:40:51 +00:00
|
|
|
return Err(e).context(EM::EntryHeaderReadError).map_err(Error::from)
|
2016-06-11 04:50:58 +00:00
|
|
|
},
|
|
|
|
Ok(None) => {
|
|
|
|
debug!("We got no value from the header!");
|
2016-10-13 16:28:15 +00:00
|
|
|
return Ok(LinkIter::new(vec![]))
|
2016-06-11 04:50:58 +00:00
|
|
|
},
|
|
|
|
Ok(Some(Value::Array(l))) => l,
|
|
|
|
Ok(Some(_)) => {
|
|
|
|
debug!("We expected an Array for the links, but there was a non-Array!");
|
2018-10-30 17:40:51 +00:00
|
|
|
return Err(err_msg("Link type error"));
|
2016-04-12 19:55:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-22 16:20:23 +00:00
|
|
|
if !links.iter().all(|l| is_match!(*l, Value::String(_)) || is_match!(*l, Value::Table(_))) {
|
|
|
|
debug!("At least one of the Values which were expected in the Array of links is not a String or a Table!");
|
2016-04-12 19:55:30 +00:00
|
|
|
debug!("Generating LinkError");
|
2018-10-30 17:40:51 +00:00
|
|
|
return Err(err_msg("Existing Link type error"));
|
2016-02-14 16:50:06 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 19:17:21 +00:00
|
|
|
let links : Vec<Link> = links.into_iter()
|
2016-02-14 16:50:06 +00:00
|
|
|
.map(|link| {
|
2017-02-22 16:20:23 +00:00
|
|
|
debug!("Matching the link: {:?}", link);
|
2016-02-14 16:50:06 +00:00
|
|
|
match link {
|
2016-08-25 17:00:42 +00:00
|
|
|
Value::String(s) => StoreId::new_baseless(PathBuf::from(s))
|
2017-02-22 09:53:27 +00:00
|
|
|
.map(|s| Link::Id { link: s })
|
2017-09-23 13:03:29 +00:00
|
|
|
.map_err(From::from)
|
2017-02-22 09:53:27 +00:00
|
|
|
,
|
2017-02-22 16:20:23 +00:00
|
|
|
Value::Table(mut tab) => {
|
|
|
|
debug!("Destructuring table");
|
|
|
|
if !tab.contains_key("link")
|
|
|
|
|| !tab.contains_key("annotation") {
|
|
|
|
debug!("Things missing... returning Error instance");
|
2018-10-30 17:40:51 +00:00
|
|
|
Err(err_msg("Link parser error"))
|
2017-02-22 16:20:23 +00:00
|
|
|
} else {
|
2017-10-30 19:17:21 +00:00
|
|
|
let link = tab.remove("link")
|
2018-10-30 17:40:51 +00:00
|
|
|
.ok_or(err_msg("Link parser: field missing"))?;
|
2017-02-22 16:20:23 +00:00
|
|
|
|
2017-10-30 19:17:21 +00:00
|
|
|
let anno = tab.remove("annotation")
|
2018-10-30 17:40:51 +00:00
|
|
|
.ok_or(err_msg("Link parser: Field missing"))?;
|
2017-02-22 16:20:23 +00:00
|
|
|
|
|
|
|
debug!("Ok, here we go with building a Link::Annotated");
|
|
|
|
match (link, anno) {
|
|
|
|
(Value::String(link), Value::String(anno)) => {
|
|
|
|
StoreId::new_baseless(PathBuf::from(link))
|
2017-09-23 13:03:29 +00:00
|
|
|
.map_err(From::from)
|
2017-02-22 16:20:23 +00:00
|
|
|
.map(|link| {
|
|
|
|
Link::Annotated {
|
|
|
|
link: link,
|
|
|
|
annotation: anno,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2018-10-30 17:40:51 +00:00
|
|
|
_ => Err(err_msg("Link parser: Field type error")),
|
2017-02-22 16:20:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-14 16:50:06 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
})
|
2017-10-30 19:17:21 +00:00
|
|
|
.collect::<Result<Vec<Link>>>()?;
|
2016-02-14 16:50:06 +00:00
|
|
|
|
2016-04-12 19:55:30 +00:00
|
|
|
debug!("Ok, the RW action was successful, returning link vector now!");
|
2016-10-13 16:28:15 +00:00
|
|
|
Ok(LinkIter::new(links))
|
2016-02-14 16:50:06 +00:00
|
|
|
}
|
|
|
|
|
2017-06-04 14:52:32 +00:00
|
|
|
pub mod store_check {
|
2017-06-06 13:15:08 +00:00
|
|
|
use libimagstore::store::Store;
|
2018-10-30 17:40:51 +00:00
|
|
|
|
|
|
|
use failure::ResultExt;
|
|
|
|
use failure::Fallible as Result;
|
|
|
|
use failure::Error;
|
|
|
|
use failure::err_msg;
|
2017-06-06 13:52:13 +00:00
|
|
|
|
2017-06-04 14:52:32 +00:00
|
|
|
pub trait StoreLinkConsistentExt {
|
2017-06-06 13:15:08 +00:00
|
|
|
fn check_link_consistency(&self) -> Result<()>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StoreLinkConsistentExt for Store {
|
2017-06-04 14:52:32 +00:00
|
|
|
fn check_link_consistency(&self) -> Result<()> {
|
2017-06-06 13:15:08 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use internal::InternalLinker;
|
|
|
|
|
|
|
|
use libimagstore::storeid::StoreId;
|
2017-09-20 17:07:05 +00:00
|
|
|
use libimagutil::debug_result::DebugResult;
|
2017-06-06 13:15:08 +00:00
|
|
|
|
|
|
|
// Helper data structure to collect incoming and outgoing links for each StoreId
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
struct Linking {
|
|
|
|
outgoing: Vec<StoreId>,
|
|
|
|
incoming: Vec<StoreId>,
|
|
|
|
}
|
|
|
|
|
2018-01-04 18:34:55 +00:00
|
|
|
// Helper function to aggregate the Link network
|
|
|
|
//
|
|
|
|
// This function aggregates a HashMap which maps each StoreId object in the store onto
|
|
|
|
// a Linking object, which contains a list of StoreIds which this entry links to and a
|
|
|
|
// list of StoreIds which link to the current one.
|
|
|
|
//
|
|
|
|
// The lambda returns an error if something fails
|
2017-06-06 13:15:08 +00:00
|
|
|
let aggregate_link_network = |store: &Store| -> Result<HashMap<StoreId, Linking>> {
|
2018-01-28 20:05:05 +00:00
|
|
|
store
|
2017-09-23 13:03:29 +00:00
|
|
|
.entries()?
|
2018-01-28 20:24:02 +00:00
|
|
|
.into_get_iter()
|
2018-01-28 20:05:05 +00:00
|
|
|
.fold(Ok(HashMap::new()), |map, element| {
|
|
|
|
map.and_then(|mut map| {
|
|
|
|
debug!("Checking element = {:?}", element);
|
2018-10-30 17:40:51 +00:00
|
|
|
let entry = element?.ok_or_else(|| err_msg("TODO: Not yet handled"))?;
|
2017-09-21 14:28:35 +00:00
|
|
|
|
2018-01-28 20:05:05 +00:00
|
|
|
debug!("Checking entry = {:?}", entry.get_location());
|
2017-09-21 14:28:35 +00:00
|
|
|
|
2018-01-28 20:05:05 +00:00
|
|
|
let internal_links = entry
|
|
|
|
.get_internal_links()?
|
|
|
|
.into_getter(store); // get the FLEs from the Store
|
2017-09-21 14:28:35 +00:00
|
|
|
|
2018-01-28 20:05:05 +00:00
|
|
|
let mut linking = Linking::default();
|
|
|
|
for internal_link in internal_links {
|
|
|
|
debug!("internal link = {:?}", internal_link);
|
2017-09-21 14:28:35 +00:00
|
|
|
|
2018-01-28 20:05:05 +00:00
|
|
|
linking.outgoing.push(internal_link?.get_location().clone());
|
|
|
|
linking.incoming.push(entry.get_location().clone());
|
|
|
|
}
|
2017-09-21 14:28:35 +00:00
|
|
|
|
2018-01-28 20:05:05 +00:00
|
|
|
map.insert(entry.get_location().clone(), linking);
|
|
|
|
Ok(map)
|
|
|
|
})
|
|
|
|
})
|
2017-06-06 13:15:08 +00:00
|
|
|
};
|
|
|
|
|
2018-01-04 18:34:55 +00:00
|
|
|
// Helper to check whethre all StoreIds in the network actually exists
|
|
|
|
//
|
|
|
|
// Because why not?
|
2018-10-30 17:40:51 +00:00
|
|
|
let all_collected_storeids_exist = |network: &HashMap<StoreId, Linking>| -> Result<()> {
|
2017-09-28 21:08:07 +00:00
|
|
|
for (id, _) in network.iter() {
|
|
|
|
if is_match!(self.get(id.clone()), Ok(Some(_))) {
|
|
|
|
debug!("Exists in store: {:?}", id);
|
|
|
|
|
2017-10-30 19:17:21 +00:00
|
|
|
if !id.exists()? {
|
2017-09-28 21:08:07 +00:00
|
|
|
warn!("Does exist in store but not on FS: {:?}", id);
|
2018-10-30 17:40:51 +00:00
|
|
|
return Err(err_msg("Link target does not exist"))
|
2017-06-06 13:15:08 +00:00
|
|
|
}
|
2017-09-28 21:08:07 +00:00
|
|
|
} else {
|
|
|
|
warn!("Does not exist in store: {:?}", id);
|
2018-10-30 17:40:51 +00:00
|
|
|
return Err(err_msg("Link target does not exist"))
|
2017-09-28 21:08:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2017-06-06 13:15:08 +00:00
|
|
|
};
|
|
|
|
|
2018-01-04 18:34:55 +00:00
|
|
|
// Helper function to create a SLCECD::OneDirectionalLink error object
|
2018-10-30 17:40:51 +00:00
|
|
|
let mk_one_directional_link_err = |src: StoreId, target: StoreId| -> Error {
|
|
|
|
Error::from(format_err!("Dead link: {} -> {}",
|
|
|
|
src.local_display_string(),
|
|
|
|
target.local_display_string()))
|
2017-06-06 13:15:08 +00:00
|
|
|
};
|
|
|
|
|
2018-01-04 18:34:55 +00:00
|
|
|
// Helper lambda to check whether the _incoming_ links of each entry actually also
|
|
|
|
// appear in the _outgoing_ list of the linked entry
|
2017-06-06 13:15:08 +00:00
|
|
|
let incoming_links_exists_as_outgoing_links =
|
|
|
|
|src: &StoreId, linking: &Linking, network: &HashMap<StoreId, Linking>| -> Result<()> {
|
2017-09-28 21:08:07 +00:00
|
|
|
for link in linking.incoming.iter() {
|
|
|
|
// Check whether the links which are _incoming_ on _src_ are outgoing
|
|
|
|
// in each of the links in the incoming list.
|
|
|
|
let incoming_consistent = network.get(link)
|
|
|
|
.map(|l| l.outgoing.contains(src))
|
|
|
|
.unwrap_or(false);
|
|
|
|
|
|
|
|
if !incoming_consistent {
|
|
|
|
return Err(mk_one_directional_link_err(src.clone(), link.clone()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2017-06-06 13:15:08 +00:00
|
|
|
};
|
|
|
|
|
2018-01-04 18:34:55 +00:00
|
|
|
// Helper lambda to check whether the _outgoing links of each entry actually also
|
|
|
|
// appear in the _incoming_ list of the linked entry
|
2017-06-06 13:15:08 +00:00
|
|
|
let outgoing_links_exist_as_incoming_links =
|
|
|
|
|src: &StoreId, linking: &Linking, network: &HashMap<StoreId, Linking>| -> Result<()> {
|
2017-09-28 21:08:07 +00:00
|
|
|
for link in linking.outgoing.iter() {
|
|
|
|
// Check whether the links which are _outgoing_ on _src_ are incoming
|
|
|
|
// in each of the links in the outgoing list.
|
|
|
|
let outgoing_consistent = network.get(link)
|
|
|
|
.map(|l| l.incoming.contains(src))
|
|
|
|
.unwrap_or(false);
|
|
|
|
|
|
|
|
if !outgoing_consistent {
|
|
|
|
return Err(mk_one_directional_link_err(link.clone(), src.clone()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2017-06-06 13:15:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
aggregate_link_network(&self)
|
2017-09-20 17:07:05 +00:00
|
|
|
.map_dbg_str("Aggregated")
|
|
|
|
.map_dbg(|nw| {
|
|
|
|
let mut s = String::new();
|
|
|
|
for (k, v) in nw {
|
|
|
|
s.push_str(&format!("{}\n in: {:?}\n out: {:?}", k, v.incoming, v.outgoing));
|
|
|
|
}
|
|
|
|
s
|
|
|
|
})
|
2017-06-06 13:15:08 +00:00
|
|
|
.and_then(|nw| {
|
|
|
|
all_collected_storeids_exist(&nw)
|
|
|
|
.map(|_| nw)
|
2018-10-30 17:40:51 +00:00
|
|
|
.context(err_msg("Link handling error"))
|
|
|
|
.map_err(Error::from)
|
2017-06-06 13:15:08 +00:00
|
|
|
})
|
|
|
|
.and_then(|nw| {
|
2017-09-28 21:08:07 +00:00
|
|
|
for (id, linking) in nw.iter() {
|
2017-10-30 19:17:21 +00:00
|
|
|
incoming_links_exists_as_outgoing_links(id, linking, &nw)?;
|
|
|
|
outgoing_links_exist_as_incoming_links(id, linking, &nw)?;
|
2017-09-28 21:08:07 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
2017-06-06 13:15:08 +00:00
|
|
|
})
|
|
|
|
.map(|_| ())
|
2017-06-04 14:52:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-10-15 08:33:30 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use std::path::PathBuf;
|
2018-05-01 18:53:07 +00:00
|
|
|
use std::sync::Arc;
|
2016-10-15 08:33:30 +00:00
|
|
|
|
|
|
|
use libimagstore::store::Store;
|
|
|
|
|
|
|
|
use super::InternalLinker;
|
2017-09-23 12:13:16 +00:00
|
|
|
use super::Link;
|
2016-10-15 08:33:30 +00:00
|
|
|
|
2016-10-15 08:58:50 +00:00
|
|
|
fn setup_logging() {
|
2017-12-07 21:07:01 +00:00
|
|
|
let _ = ::env_logger::try_init();
|
2016-10-15 08:58:50 +00:00
|
|
|
}
|
|
|
|
|
2016-10-15 08:33:30 +00:00
|
|
|
pub fn get_store() -> Store {
|
2017-09-05 20:00:58 +00:00
|
|
|
use libimagstore::file_abstraction::InMemoryFileAbstraction;
|
2018-05-01 18:53:07 +00:00
|
|
|
let backend = Arc::new(InMemoryFileAbstraction::default());
|
2017-12-22 10:24:04 +00:00
|
|
|
Store::new_with_backend(PathBuf::from("/"), &None, backend).unwrap()
|
2016-10-15 08:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_new_entry_no_links() {
|
2016-10-15 08:58:50 +00:00
|
|
|
setup_logging();
|
2016-10-15 08:33:30 +00:00
|
|
|
let store = get_store();
|
|
|
|
let entry = store.create(PathBuf::from("test_new_entry_no_links")).unwrap();
|
|
|
|
let links = entry.get_internal_links();
|
|
|
|
assert!(links.is_ok());
|
|
|
|
let links = links.unwrap();
|
|
|
|
assert_eq!(links.collect::<Vec<_>>().len(), 0);
|
|
|
|
}
|
|
|
|
|
2016-10-15 08:41:13 +00:00
|
|
|
#[test]
|
|
|
|
fn test_link_two_entries() {
|
2016-10-15 08:58:50 +00:00
|
|
|
setup_logging();
|
2016-10-15 08:41:13 +00:00
|
|
|
let store = get_store();
|
|
|
|
let mut e1 = store.create(PathBuf::from("test_link_two_entries1")).unwrap();
|
|
|
|
assert!(e1.get_internal_links().is_ok());
|
|
|
|
|
|
|
|
let mut e2 = store.create(PathBuf::from("test_link_two_entries2")).unwrap();
|
|
|
|
assert!(e2.get_internal_links().is_ok());
|
|
|
|
|
|
|
|
{
|
|
|
|
assert!(e1.add_internal_link(&mut e2).is_ok());
|
|
|
|
|
|
|
|
let e1_links = e1.get_internal_links().unwrap().collect::<Vec<_>>();
|
|
|
|
let e2_links = e2.get_internal_links().unwrap().collect::<Vec<_>>();
|
|
|
|
|
2016-10-15 09:24:14 +00:00
|
|
|
debug!("1 has links: {:?}", e1_links);
|
|
|
|
debug!("2 has links: {:?}", e2_links);
|
|
|
|
|
2016-10-15 08:41:13 +00:00
|
|
|
assert_eq!(e1_links.len(), 1);
|
|
|
|
assert_eq!(e2_links.len(), 1);
|
|
|
|
|
2017-02-22 10:04:22 +00:00
|
|
|
assert!(e1_links.first().map(|l| l.clone().with_base(store.path().clone()).eq_store_id(e2.get_location())).unwrap_or(false));
|
|
|
|
assert!(e2_links.first().map(|l| l.clone().with_base(store.path().clone()).eq_store_id(e1.get_location())).unwrap_or(false));
|
2016-10-15 08:41:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
assert!(e1.remove_internal_link(&mut e2).is_ok());
|
|
|
|
|
2018-11-01 10:51:07 +00:00
|
|
|
debug!("{:?}", e2.to_str());
|
2016-10-15 08:41:13 +00:00
|
|
|
let e2_links = e2.get_internal_links().unwrap().collect::<Vec<_>>();
|
|
|
|
assert_eq!(e2_links.len(), 0, "Expected [], got: {:?}", e2_links);
|
|
|
|
|
2018-11-01 10:51:07 +00:00
|
|
|
debug!("{:?}", e1.to_str());
|
2016-10-15 08:41:13 +00:00
|
|
|
let e1_links = e1.get_internal_links().unwrap().collect::<Vec<_>>();
|
|
|
|
assert_eq!(e1_links.len(), 0, "Expected [], got: {:?}", e1_links);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-15 09:31:30 +00:00
|
|
|
#[test]
|
|
|
|
fn test_multiple_links() {
|
|
|
|
setup_logging();
|
|
|
|
let store = get_store();
|
|
|
|
|
|
|
|
let mut e1 = store.retrieve(PathBuf::from("1")).unwrap();
|
|
|
|
let mut e2 = store.retrieve(PathBuf::from("2")).unwrap();
|
|
|
|
let mut e3 = store.retrieve(PathBuf::from("3")).unwrap();
|
|
|
|
let mut e4 = store.retrieve(PathBuf::from("4")).unwrap();
|
|
|
|
let mut e5 = store.retrieve(PathBuf::from("5")).unwrap();
|
|
|
|
|
|
|
|
assert!(e1.add_internal_link(&mut e2).is_ok());
|
|
|
|
|
|
|
|
assert_eq!(e1.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e2.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e3.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e4.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e5.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
|
|
|
|
assert!(e1.add_internal_link(&mut e3).is_ok());
|
|
|
|
|
|
|
|
assert_eq!(e1.get_internal_links().unwrap().collect::<Vec<_>>().len(), 2);
|
|
|
|
assert_eq!(e2.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e3.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e4.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e5.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
|
|
|
|
assert!(e1.add_internal_link(&mut e4).is_ok());
|
|
|
|
|
|
|
|
assert_eq!(e1.get_internal_links().unwrap().collect::<Vec<_>>().len(), 3);
|
|
|
|
assert_eq!(e2.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e3.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e4.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e5.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
|
|
|
|
assert!(e1.add_internal_link(&mut e5).is_ok());
|
|
|
|
|
|
|
|
assert_eq!(e1.get_internal_links().unwrap().collect::<Vec<_>>().len(), 4);
|
|
|
|
assert_eq!(e2.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e3.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e4.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e5.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
|
|
|
|
assert!(e5.remove_internal_link(&mut e1).is_ok());
|
|
|
|
|
|
|
|
assert_eq!(e1.get_internal_links().unwrap().collect::<Vec<_>>().len(), 3);
|
|
|
|
assert_eq!(e2.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e3.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e4.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e5.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
|
|
|
|
assert!(e4.remove_internal_link(&mut e1).is_ok());
|
|
|
|
|
|
|
|
assert_eq!(e1.get_internal_links().unwrap().collect::<Vec<_>>().len(), 2);
|
|
|
|
assert_eq!(e2.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e3.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e4.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e5.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
|
|
|
|
assert!(e3.remove_internal_link(&mut e1).is_ok());
|
|
|
|
|
|
|
|
assert_eq!(e1.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e2.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e3.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e4.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e5.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
|
|
|
|
assert!(e2.remove_internal_link(&mut e1).is_ok());
|
|
|
|
|
|
|
|
assert_eq!(e1.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e2.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e3.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e4.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e5.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-07-14 21:17:32 +00:00
|
|
|
#[test]
|
|
|
|
fn test_link_deleting() {
|
|
|
|
setup_logging();
|
|
|
|
let store = get_store();
|
|
|
|
|
|
|
|
let mut e1 = store.retrieve(PathBuf::from("1")).unwrap();
|
|
|
|
let mut e2 = store.retrieve(PathBuf::from("2")).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(e1.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e2.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
|
|
|
|
assert!(e1.add_internal_link(&mut e2).is_ok());
|
|
|
|
|
|
|
|
assert_eq!(e1.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e2.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
|
|
|
|
assert!(e1.remove_internal_link(&mut e2).is_ok());
|
|
|
|
|
|
|
|
assert_eq!(e1.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e2.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
}
|
|
|
|
|
2017-07-14 21:21:38 +00:00
|
|
|
#[test]
|
|
|
|
fn test_link_deleting_multiple_links() {
|
|
|
|
setup_logging();
|
|
|
|
let store = get_store();
|
|
|
|
|
|
|
|
let mut e1 = store.retrieve(PathBuf::from("1")).unwrap();
|
|
|
|
let mut e2 = store.retrieve(PathBuf::from("2")).unwrap();
|
|
|
|
let mut e3 = store.retrieve(PathBuf::from("3")).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(e1.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e2.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e3.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
|
|
|
|
assert!(e1.add_internal_link(&mut e2).is_ok()); // 1-2
|
|
|
|
assert!(e1.add_internal_link(&mut e3).is_ok()); // 1-2, 1-3
|
|
|
|
|
|
|
|
assert_eq!(e1.get_internal_links().unwrap().collect::<Vec<_>>().len(), 2);
|
|
|
|
assert_eq!(e2.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e3.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
|
|
|
|
assert!(e2.add_internal_link(&mut e3).is_ok()); // 1-2, 1-3, 2-3
|
|
|
|
|
|
|
|
assert_eq!(e1.get_internal_links().unwrap().collect::<Vec<_>>().len(), 2);
|
|
|
|
assert_eq!(e2.get_internal_links().unwrap().collect::<Vec<_>>().len(), 2);
|
|
|
|
assert_eq!(e3.get_internal_links().unwrap().collect::<Vec<_>>().len(), 2);
|
|
|
|
|
|
|
|
assert!(e1.remove_internal_link(&mut e2).is_ok()); // 1-3, 2-3
|
|
|
|
|
|
|
|
assert_eq!(e1.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e2.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e3.get_internal_links().unwrap().collect::<Vec<_>>().len(), 2);
|
|
|
|
|
|
|
|
assert!(e1.remove_internal_link(&mut e3).is_ok()); // 2-3
|
|
|
|
|
|
|
|
assert_eq!(e1.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e2.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e3.get_internal_links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
|
|
|
|
assert!(e2.remove_internal_link(&mut e3).is_ok());
|
|
|
|
|
|
|
|
assert_eq!(e1.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e2.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e3.get_internal_links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
}
|
|
|
|
|
2017-09-23 12:13:16 +00:00
|
|
|
#[test]
|
|
|
|
fn test_link_annotating() {
|
|
|
|
setup_logging();
|
|
|
|
let store = get_store();
|
|
|
|
let mut entry1 = store.create(PathBuf::from("test_link_annotating-1")).unwrap();
|
|
|
|
let mut entry2 = store.create(PathBuf::from("test_link_annotating-2")).unwrap();
|
|
|
|
|
|
|
|
let res = entry1.add_internal_annotated_link(&mut entry2, String::from("annotation"));
|
|
|
|
assert!(res.is_ok());
|
|
|
|
|
|
|
|
{
|
|
|
|
for link in entry1.get_internal_links().unwrap() {
|
|
|
|
match link {
|
|
|
|
Link::Annotated {annotation, ..} => assert_eq!(annotation, "annotation"),
|
|
|
|
_ => assert!(false, "Non-annotated link found"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
for link in entry2.get_internal_links().unwrap() {
|
|
|
|
match link {
|
|
|
|
Link::Id {..} => {},
|
|
|
|
Link::Annotated {..} => assert!(false, "Annotated link found"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-15 08:33:30 +00:00
|
|
|
}
|
|
|
|
|