return to old (orphanized) multi-root hierarchy
parent
1df746596f
commit
da776bbcf3
|
@ -11,18 +11,10 @@ use crate::addressing::Address;
|
|||
use crate::database::constants::{
|
||||
HIER_ADDR, HIER_HAS_ATTR, HIER_INVARIANT, IS_OF_TYPE_ATTR, LABEL_ATTR, TYPE_ADDR, TYPE_HAS_ATTR,
|
||||
};
|
||||
use crate::database::entry::{Entry, EntryValue, InvariantEntry};
|
||||
use crate::database::entry::{Entry, EntryValue};
|
||||
use crate::database::lang::{EntryQuery, Query, QueryComponent, QueryPart};
|
||||
use crate::database::{bulk_retrieve_objects, insert_entry, query, DbPool};
|
||||
|
||||
lazy_static! {
|
||||
static ref HIER_ROOT_INVARIANT: InvariantEntry = InvariantEntry {
|
||||
attribute: String::from(LABEL_ATTR),
|
||||
value: EntryValue::Value(Value::from("/")),
|
||||
};
|
||||
static ref HIER_ROOT_INVARIANT_ADDR: Address = HIER_ROOT_INVARIANT.entity().unwrap();
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct UNode(String);
|
||||
|
||||
|
@ -101,32 +93,32 @@ impl EntryList for Vec<Entry> {
|
|||
}
|
||||
}
|
||||
|
||||
// pub fn list_orphans<C: Connection<Backend = Sqlite>>(connection: &C) -> Result<Vec<Address>> {
|
||||
// let all_directories: Vec<Entry> = query(
|
||||
// connection,
|
||||
// Query::SingleQuery(QueryPart::Matches(EntryQuery {
|
||||
// entity: QueryComponent::Any,
|
||||
// attribute: QueryComponent::Exact(IS_OF_TYPE_ATTR.to_string()),
|
||||
// value: QueryComponent::Exact(EntryValue::Address(HIER_ADDR.clone())),
|
||||
// })),
|
||||
// )?;
|
||||
//
|
||||
// let directories_with_parents: Vec<Address> = query(
|
||||
// connection,
|
||||
// Query::SingleQuery(QueryPart::Matches(EntryQuery {
|
||||
// entity: QueryComponent::Any,
|
||||
// attribute: QueryComponent::Exact(HIER_HAS_ATTR.to_string()),
|
||||
// value: QueryComponent::Any,
|
||||
// })),
|
||||
// )?
|
||||
// .extract_addresses();
|
||||
//
|
||||
// Ok(all_directories
|
||||
// .into_iter()
|
||||
// .filter(|entry| !directories_with_parents.contains(&entry.entity))
|
||||
// .map(|e| e.entity)
|
||||
// .collect())
|
||||
// }
|
||||
pub fn list_orphans<C: Connection<Backend = Sqlite>>(connection: &C) -> Result<Vec<Address>> {
|
||||
let all_directories: Vec<Entry> = query(
|
||||
connection,
|
||||
Query::SingleQuery(QueryPart::Matches(EntryQuery {
|
||||
entity: QueryComponent::Any,
|
||||
attribute: QueryComponent::Exact(IS_OF_TYPE_ATTR.to_string()),
|
||||
value: QueryComponent::Exact(EntryValue::Address(HIER_ADDR.clone())),
|
||||
})),
|
||||
)?;
|
||||
|
||||
let directories_with_parents: Vec<Address> = query(
|
||||
connection,
|
||||
Query::SingleQuery(QueryPart::Matches(EntryQuery {
|
||||
entity: QueryComponent::Any,
|
||||
attribute: QueryComponent::Exact(HIER_HAS_ATTR.to_string()),
|
||||
value: QueryComponent::Any,
|
||||
})),
|
||||
)?
|
||||
.extract_addresses();
|
||||
|
||||
Ok(all_directories
|
||||
.into_iter()
|
||||
.filter(|entry| !directories_with_parents.contains(&entry.entity))
|
||||
.map(|e| e.entity)
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub async fn list_node<C: Connection<Backend = Sqlite>>(
|
||||
connection: &C,
|
||||
|
@ -162,8 +154,6 @@ pub fn fetch_or_create_dir<C: Connection<Backend = Sqlite>>(
|
|||
None => trace!("FETCHING/CREATING /{:#}", directory),
|
||||
}
|
||||
|
||||
let parent = parent.unwrap_or_else(|| HIER_ROOT_INVARIANT_ADDR.clone());
|
||||
|
||||
let matching_directories = query(
|
||||
connection,
|
||||
Query::SingleQuery(QueryPart::Matches(EntryQuery {
|
||||
|
@ -177,15 +167,18 @@ pub fn fetch_or_create_dir<C: Connection<Backend = Sqlite>>(
|
|||
.into_iter()
|
||||
.map(|e: Entry| e.entity);
|
||||
|
||||
let parent_has: Vec<Address> = query(
|
||||
connection,
|
||||
Query::SingleQuery(QueryPart::Matches(EntryQuery {
|
||||
entity: QueryComponent::Exact(parent.clone()),
|
||||
attribute: QueryComponent::Exact(String::from(HIER_HAS_ATTR)),
|
||||
value: QueryComponent::Any,
|
||||
})),
|
||||
)?
|
||||
.extract_addresses();
|
||||
let parent_has: Vec<Address> = match parent.clone() {
|
||||
Some(parent) => query(
|
||||
connection,
|
||||
Query::SingleQuery(QueryPart::Matches(EntryQuery {
|
||||
entity: QueryComponent::Exact(parent),
|
||||
attribute: QueryComponent::Exact(String::from(HIER_HAS_ATTR)),
|
||||
value: QueryComponent::Any,
|
||||
})),
|
||||
)?
|
||||
.extract_addresses(),
|
||||
None => list_orphans(connection)?,
|
||||
};
|
||||
|
||||
let valid_directories: Vec<Address> = matching_directories
|
||||
.filter(|a| parent_has.contains(a))
|
||||
|
@ -209,12 +202,14 @@ pub fn fetch_or_create_dir<C: Connection<Backend = Sqlite>>(
|
|||
};
|
||||
insert_entry(connection, directory_entry)?;
|
||||
|
||||
let has_entry = Entry {
|
||||
entity: parent,
|
||||
attribute: String::from(HIER_HAS_ATTR),
|
||||
value: EntryValue::Address(new_directory_address.clone()),
|
||||
};
|
||||
insert_entry(connection, has_entry)?;
|
||||
if parent.is_some() {
|
||||
let has_entry = Entry {
|
||||
entity: parent.unwrap(),
|
||||
attribute: String::from(HIER_HAS_ATTR),
|
||||
value: EntryValue::Address(new_directory_address.clone()),
|
||||
};
|
||||
insert_entry(connection, has_entry)?;
|
||||
}
|
||||
|
||||
Ok(new_directory_address)
|
||||
} else {
|
||||
|
@ -233,7 +228,7 @@ pub fn resolve_path<C: Connection<Backend = Sqlite>>(
|
|||
path: &UPath,
|
||||
create: bool,
|
||||
) -> Result<Vec<Address>> {
|
||||
let mut result: Vec<Address> = vec![HIER_ROOT_INVARIANT_ADDR.clone()];
|
||||
let mut result: Vec<Address> = vec![];
|
||||
let mut path_stack = path.0.to_vec();
|
||||
|
||||
path_stack.reverse();
|
||||
|
|
Loading…
Reference in New Issue