diff options
-rw-r--r-- | import/src/lib.rs | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/import/src/lib.rs b/import/src/lib.rs index 4f83653..9ed786a 100644 --- a/import/src/lib.rs +++ b/import/src/lib.rs @@ -41,7 +41,9 @@ pub async fn import(db: &Database, fed: &Federation) -> anyhow::Result<()> { .context("indexing")?; info!("merging nodes..."); merge_nodes(db).context("merging nodes")?; - info!("clearing temporary node tree"); + info!("generating paths..."); + generate_node_paths(db).context("generating paths")?; + info!("clearing temporary node tree..."); db.node_import.clear()?; info!("import completed"); drop(permit); @@ -61,13 +63,37 @@ pub fn merge_nodes(db: &Database) -> anyhow::Result<()> { .unwrap(); node.public.id = Some(id.clone()); - node.public.path = vec!["library".to_string()]; // TODO reconstruct from children + node.public.path = vec![]; // will be reconstructed in the next pass db.node.insert(&id, &node)?; } Ok(()) } +pub fn generate_node_paths(db: &Database) -> anyhow::Result<()> { + fn traverse(db: &Database, c: String, mut path: Vec<String>) -> anyhow::Result<()> { + let node = db + .node + .update_and_fetch(&c, |mut nc| { + if let Some(nc) = &mut nc { + if nc.public.path.is_empty() { + nc.public.path = path.clone(); + } + } + nc + })? + .ok_or(anyhow!("node missing"))?; + + path.push(c); + for c in node.public.children { + traverse(db, c, path.clone())?; + } + Ok(()) + } + traverse(db, "library".to_string(), vec![])?; + Ok(()) +} + fn compare_index_path(x: &[usize], y: &[usize]) -> Ordering { if x.is_empty() { Ordering::Greater |