list_path also fetches attrs of attrs

This commit is contained in:
Tomáš Mládek 2021-12-17 19:44:00 +01:00
parent bf4e0efd2c
commit 1194a4439e

View file

@ -9,7 +9,7 @@ use lru::LruCache;
use serde_json::Value; use serde_json::Value;
use uuid::Uuid; use uuid::Uuid;
use crate::addressing::Address; use crate::addressing::{Address, Addressable};
use crate::database::constants::{ use crate::database::constants::{
HIER_ADDR, HIER_HAS_ATTR, HIER_INVARIANT, IS_OF_TYPE_ATTR, LABEL_ATTR, TYPE_ADDR, TYPE_HAS_ATTR, HIER_ADDR, HIER_HAS_ATTR, HIER_INVARIANT, IS_OF_TYPE_ATTR, LABEL_ATTR, TYPE_ADDR, TYPE_HAS_ATTR,
}; };
@ -78,15 +78,15 @@ impl std::fmt::Display for UPath {
} }
trait EntryList { trait EntryList {
fn extract_addresses(&self) -> Vec<Address>; fn extract_pointers(&self) -> Vec<(Address, Address)>;
} }
impl EntryList for Vec<Entry> { impl EntryList for Vec<Entry> {
fn extract_addresses(&self) -> Vec<Address> { fn extract_pointers(&self) -> Vec<(Address, Address)> {
self.iter() self.iter()
.filter_map(|e| { .filter_map(|e| {
if let EntryValue::Address(address) = &e.value { if let EntryValue::Address(address) = &e.value {
Some(address.clone()) Some((e.address().unwrap(), address.clone()))
} else { } else {
None None
} }
@ -113,7 +113,10 @@ pub fn list_orphans<C: Connection<Backend = Sqlite>>(connection: &C) -> Result<V
value: QueryComponent::Any, value: QueryComponent::Any,
})), })),
)? )?
.extract_addresses(); .extract_pointers()
.into_iter()
.map(|(_, val)| val)
.collect();
Ok(all_directories Ok(all_directories
.into_iter() .into_iter()
@ -128,16 +131,19 @@ pub async fn list_path<C: Connection<Backend = Sqlite>>(
) -> Result<Vec<Entry>> { ) -> Result<Vec<Entry>> {
let resolved_path: Vec<Address> = resolve_path(connection, path, false)?; let resolved_path: Vec<Address> = resolve_path(connection, path, false)?;
let entry_addresses = match resolved_path.last() { let entry_addresses = match resolved_path.last() {
Some(last) => Some(last) => query(
query( connection,
connection, Query::SingleQuery(QueryPart::Matches(EntryQuery {
Query::SingleQuery(QueryPart::Matches(EntryQuery { entity: QueryComponent::Exact(last.clone()),
entity: QueryComponent::Exact(last.clone()), attribute: QueryComponent::Exact(HIER_HAS_ATTR.to_string()),
attribute: QueryComponent::Exact(HIER_HAS_ATTR.to_string()), value: QueryComponent::Any,
value: QueryComponent::Any, })),
})), )?
)? .extract_pointers()
.extract_addresses(), .into_iter()
.map(|(addr, val)| [addr, val])
.flatten()
.collect(),
None => list_orphans(connection)?, None => list_orphans(connection)?,
}; };
@ -189,7 +195,10 @@ pub fn fetch_or_create_dir<C: Connection<Backend = Sqlite>>(
value: QueryComponent::Any, value: QueryComponent::Any,
})), })),
)? )?
.extract_addresses(), .extract_pointers()
.into_iter()
.map(|(_, val)| val)
.collect(),
None => list_orphans(connection)?, None => list_orphans(connection)?,
}; };