Skip to content

Commit

Permalink
fix(ark-cli): update ark-cli to reflect fs-index changes
Browse files Browse the repository at this point in the history
Signed-off-by: Tarek <[email protected]>
  • Loading branch information
tareknaser committed Aug 8, 2024
1 parent b246256 commit 06fadc6
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 45 deletions.
16 changes: 12 additions & 4 deletions ark-cli/src/commands/link/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -28,8 +27,17 @@ pub fn load_link(
) -> Result<Link<ResourceId>, 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) {
Expand All @@ -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(),
))?,
Expand Down
30 changes: 14 additions & 16 deletions ark-cli/src/commands/list.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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| {
Expand All @@ -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::<u32>().unwrap_or(0)),
Expand All @@ -114,7 +115,7 @@ impl List {
let datetime = if self.modified {
let format = "%b %e %H:%M %Y";
Some(
DateTime::<Utc>::from(resource.modified)
DateTime::<Utc>::from(indexed_resource.last_modified())
.format(format)
.to_string(),
)
Expand All @@ -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 {
Expand Down
14 changes: 9 additions & 5 deletions ark-cli/src/index_registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -34,7 +36,9 @@ pub fn provide_index<P: AsRef<Path>>(
}

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"))
Expand Down
46 changes: 26 additions & 20 deletions ark-cli/src/util.rs
Original file line number Diff line number Diff line change
@@ -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<PathBuf>,
Expand Down Expand Up @@ -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());
}
}
}
Expand All @@ -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()
);
}
}
}
Expand Down

0 comments on commit 06fadc6

Please sign in to comment.