From 06fadc614a7a02db748351d9d9cf78b2fcc11197 Mon Sep 17 00:00:00 2001 From: Tarek Date: Thu, 8 Aug 2024 23:00:11 +0300 Subject: [PATCH] fix(ark-cli): update ark-cli to reflect fs-index changes Signed-off-by: Tarek --- ark-cli/src/commands/link/utils.rs | 16 ++++++++--- ark-cli/src/commands/list.rs | 30 +++++++++---------- ark-cli/src/index_registrar.rs | 14 +++++---- ark-cli/src/util.rs | 46 +++++++++++++++++------------- 4 files changed, 61 insertions(+), 45 deletions(-) diff --git a/ark-cli/src/commands/link/utils.rs b/ark-cli/src/commands/link/utils.rs index 122f7f1c..2d27ca9a 100644 --- a/ark-cli/src/commands/link/utils.rs +++ b/ark-cli/src/commands/link/utils.rs @@ -3,8 +3,7 @@ use data_link::Link; use std::path::PathBuf; use url::Url; -use crate::error::AppError; -use crate::util::provide_index; // Import your custom AppError type +use crate::{error::AppError, util::provide_index}; // Import your custom AppError type pub async fn create_link( root: &PathBuf, @@ -28,8 +27,17 @@ pub fn load_link( ) -> Result, AppError> { let path_from_index = id.clone().map(|id| { let index = provide_index(root); - index.id2path[&id].as_path().to_path_buf() + index + .get_resources_by_id(&id) + .map(|r| r[0].path().to_owned()) + .ok_or_else(|| { + AppError::IndexError(format!( + "Resource with id {} not found", + id + )) + }) }); + let path_from_index = path_from_index.transpose()?; let path_from_user = file_path; let path = match (path_from_user, path_from_index) { @@ -46,7 +54,7 @@ pub fn load_link( } } (Some(path), None) => Ok(path.to_path_buf()), - (None, Some(path)) => Ok(path), + (None, Some(path)) => Ok(path.to_path_buf()), (None, None) => Err(AppError::LinkLoadError( "Provide a path or id for request.".to_owned(), ))?, diff --git a/ark-cli/src/commands/list.rs b/ark-cli/src/commands/list.rs index bc557b42..ee86152b 100644 --- a/ark-cli/src/commands/list.rs +++ b/ark-cli/src/commands/list.rs @@ -1,5 +1,4 @@ -use std::io::Read; -use std::path::PathBuf; +use std::{io::Read, path::PathBuf}; use crate::{ provide_index, provide_root, read_storage_value, AppError, DateTime, @@ -76,15 +75,17 @@ impl List { .map_err(|_| { AppError::IndexError("Could not read index".to_owned()) })? - .path2id + .resources() .iter() - .filter_map(|(path, resource)| { + .filter_map(|indexed_resource| { + let path = indexed_resource.path(); + let id = indexed_resource.id(); let tags = if self.tags { Some( read_storage_value( &root, "tags", - &resource.id.to_string(), + &id.to_string(), &None, ) .map_or(vec![], |s| { @@ -102,7 +103,7 @@ impl List { read_storage_value( &root, "scores", - &resource.id.to_string(), + &id.to_string(), &None, ) .map_or(0, |s| s.parse::().unwrap_or(0)), @@ -114,7 +115,7 @@ impl List { let datetime = if self.modified { let format = "%b %e %H:%M %Y"; Some( - DateTime::::from(resource.modified) + DateTime::::from(indexed_resource.last_modified()) .format(format) .to_string(), ) @@ -123,21 +124,18 @@ impl List { }; let (path, resource, content) = match entry_output { - EntryOutput::Both => ( - Some(path.to_owned().into_path_buf()), - Some(resource.clone().id), - None, - ), - EntryOutput::Path => { - (Some(path.to_owned().into_path_buf()), None, None) + EntryOutput::Both => { + (Some(path.to_owned()), Some(id.to_owned()), None) } - EntryOutput::Id => (None, Some(resource.clone().id), None), + EntryOutput::Path => (Some(path.to_owned()), None, None), + EntryOutput::Id => (None, Some(id.to_owned()), None), EntryOutput::Link => match File::open(path) { Ok(mut file) => { let mut contents = String::new(); match file.read_to_string(&mut contents) { Ok(_) => { - // Check if the content of the file is a valid url + // Check if the content + // of the file is a valid url let url = contents.trim(); let url = url::Url::parse(url); match url { diff --git a/ark-cli/src/index_registrar.rs b/ark-cli/src/index_registrar.rs index fc6a2e5b..a75df080 100644 --- a/ark-cli/src/index_registrar.rs +++ b/ark-cli/src/index_registrar.rs @@ -2,11 +2,13 @@ use lazy_static::lazy_static; extern crate canonical_path; use data_error::{ArklibError, Result}; -use fs_index::ResourceIndex; +use fs_index::{load_or_build_index, ResourceIndex}; -use std::collections::HashMap; -use std::path::Path; -use std::sync::{Arc, RwLock}; +use std::{ + collections::HashMap, + path::Path, + sync::{Arc, RwLock}, +}; use crate::ResourceId; @@ -34,7 +36,9 @@ pub fn provide_index>( } log::info!("Index has not been registered before"); - match ResourceIndex::provide(&root_path) { + // If the index has not been registered before, + // we need to load it, update it and register it + match load_or_build_index(&root_path, true) { Ok(index) => { let mut registrar = REGISTRAR.write().map_err(|_| { ArklibError::Other(anyhow::anyhow!("Failed to lock registrar")) diff --git a/ark-cli/src/util.rs b/ark-cli/src/util.rs index 9a370167..40eff290 100644 --- a/ark-cli/src/util.rs +++ b/ark-cli/src/util.rs @@ -1,24 +1,26 @@ use crate::ResourceId; -use fs_index::index::ResourceIndex; +use fs_index::ResourceIndex; use fs_metadata::METADATA_STORAGE_FOLDER; use fs_properties::PROPERTIES_STORAGE_FOLDER; use fs_storage::{ ARK_FOLDER, PREVIEWS_STORAGE_FOLDER, SCORE_STORAGE_FILE, STATS_FOLDER, TAG_STORAGE_FILE, THUMBNAILS_STORAGE_FOLDER, }; -use std::env::current_dir; -use std::fs::{canonicalize, metadata}; -use std::io::BufRead; -use std::io::BufReader; -use std::path::Path; -use std::str::FromStr; -use std::thread; -use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; -use std::{fs::File, path::PathBuf}; +use std::{ + env::current_dir, + fs::{canonicalize, metadata, File}, + io::{BufRead, BufReader}, + path::{Path, PathBuf}, + str::FromStr, + thread, + time::{Duration, Instant, SystemTime, UNIX_EPOCH}, +}; -use crate::error::AppError; -use crate::models::storage::{Storage, StorageType}; -use crate::ARK_CONFIG; +use crate::{ + error::AppError, + models::storage::{Storage, StorageType}, + ARK_CONFIG, +}; pub fn discover_roots( roots_cfg: &Option, @@ -111,11 +113,11 @@ pub fn monitor_index( let duration = start.elapsed(); println!("Updating succeeded in {:?}\n", duration); - if !diff.deleted.is_empty() { - println!("Deleted: {:?}", diff.deleted); + if !diff.removed().is_empty() { + println!("Deleted: {:?}", diff.removed()); } - if !diff.added.is_empty() { - println!("Added: {:?}", diff.added); + if !diff.added().is_empty() { + println!("Added: {:?}", diff.added()); } } } @@ -127,10 +129,14 @@ pub fn monitor_index( ) })?; - println!("Here are {} entries in the index", index.size()); + println!("Here are {} entries in the index", index.len()); - for (key, count) in index.collisions.iter() { - println!("Id {:?} calculated {} times", key, count); + for (key, resources) in index.collisions().iter() { + println!( + "Id {:?} calculated {} times", + key, + resources.len() + ); } } }