Skip to content

Commit c180268

Browse files
committed
feat.: Impl. caching for owner_id
Signed-off-by: Dheshan Mohandass <dheshan@mohandass.com>
1 parent df3c475 commit c180268

File tree

3 files changed

+56
-21
lines changed

3 files changed

+56
-21
lines changed

Cargo.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ async-trait = "0.1"
3636
chrono = { version = "0.4", features = ["serde"] }
3737
rand = "0.8"
3838
bigdecimal = "0.4"
39+
dashmap = "5.5"

src/bin/diskusage/main.rs

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,58 @@
1-
mod models;
1+
mod counter;
22
mod filesystem;
3+
mod models;
34
mod users;
4-
mod counter;
55

6+
use crate::models::definitions::DbModel;
67
use clap::Parser;
78
use rayon::prelude::*;
8-
use crate::models::definitions::DbModel;
99

1010
#[derive(clap::Parser, Default, Debug)]
11-
#[clap(
12-
author = "Dheshan Mohandass",
13-
version,
14-
about
15-
)]
11+
#[clap(author = "Dheshan Mohandass", version, about)]
1612
/// A CLI tool for tracking disk usage.
1713
struct Arguments {
1814
/// The root directory to track.
1915
#[clap(short, long)]
20-
root_dir: String,
16+
root_dir: String,
2117
/// Enable debug mode.
2218
#[clap(short, long)]
23-
debug: bool,
19+
debug: bool,
2420
}
2521

26-
async fn ensure_user_exists(owner: Option<i32>, pool: std::sync::Arc<sqlx::Pool<sqlx::Postgres>>) -> Result<(), sqlx::Error> {
22+
async fn ensure_user_exists(
23+
owner: Option<i32>,
24+
pool: std::sync::Arc<sqlx::Pool<sqlx::Postgres>>,
25+
cache: std::sync::Arc<dashmap::DashSet<i32>>,
26+
) -> Result<(), sqlx::Error> {
2727
if let Some(owner_id) = owner {
28+
// Check if the user exists in the cache
29+
if let Some(_) = cache.get(&owner_id) {
30+
return Ok(());
31+
}
2832
let select_user_where_clause = format!("WHERE user_id = {}", owner_id);
29-
let user = models::definitions::User::select_where(&pool, &select_user_where_clause).await.unwrap_or_default();
30-
33+
let user = models::definitions::User::select_where(&pool, &select_user_where_clause)
34+
.await
35+
.unwrap_or_default();
36+
3137
if user.is_empty() {
3238
let user = models::definitions::User {
3339
user_id: owner_id,
34-
username: users::username::get_username(owner_id as u32)
40+
username: users::username::get_username(owner_id as u32),
3541
};
3642
user.insert(&pool).await?;
3743
}
44+
45+
cache.insert(owner_id);
3846
}
3947
Ok(())
4048
}
4149

42-
fn process_directory(entry: walkdir::DirEntry, pool: std::sync::Arc<sqlx::Pool<sqlx::Postgres>>, handle: tokio::runtime::Handle) {
50+
fn process_directory(
51+
entry: walkdir::DirEntry,
52+
pool: std::sync::Arc<sqlx::Pool<sqlx::Postgres>>,
53+
handle: tokio::runtime::Handle,
54+
cache: std::sync::Arc<dashmap::DashSet<i32>>,
55+
) {
4356
let dir_path = entry.path();
4457
let owner = filesystem::fetch::owner(dir_path).map(|x| x as i32);
4558
let parent_dir = dir_path.parent().unwrap_or(std::path::Path::new("/"));
@@ -51,7 +64,7 @@ fn process_directory(entry: walkdir::DirEntry, pool: std::sync::Arc<sqlx::Pool<s
5164
};
5265

5366
handle.block_on(async move {
54-
if let Err(e) = ensure_user_exists(owner, pool.clone()).await {
67+
if let Err(e) = ensure_user_exists(owner, pool.clone(), cache.clone()).await {
5568
log::error!("Failed to insert user: {:?}", e);
5669
return;
5770
}
@@ -61,7 +74,12 @@ fn process_directory(entry: walkdir::DirEntry, pool: std::sync::Arc<sqlx::Pool<s
6174
});
6275
}
6376

64-
fn process_file(entry: walkdir::DirEntry, pool: std::sync::Arc<sqlx::Pool<sqlx::Postgres>>, handle: tokio::runtime::Handle) {
77+
fn process_file(
78+
entry: walkdir::DirEntry,
79+
pool: std::sync::Arc<sqlx::Pool<sqlx::Postgres>>,
80+
handle: tokio::runtime::Handle,
81+
cache: std::sync::Arc<dashmap::DashSet<i32>>,
82+
) {
6583
let file_path = entry.path();
6684
let owner = filesystem::fetch::owner(file_path).map(|x| x as i32);
6785
let file_size = filesystem::fetch::file_size(file_path).unwrap_or_default();
@@ -78,7 +96,7 @@ fn process_file(entry: walkdir::DirEntry, pool: std::sync::Arc<sqlx::Pool<sqlx::
7896
};
7997

8098
handle.block_on(async move {
81-
if let Err(e) = ensure_user_exists(owner, pool.clone()).await {
99+
if let Err(e) = ensure_user_exists(owner, pool.clone(), cache.clone()).await {
82100
log::error!("Failed to insert user: {:?}", e);
83101
return;
84102
}
@@ -143,7 +161,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
143161
let pool_c = std::sync::Arc::clone(&pool);
144162
let handle_c: tokio::runtime::Handle = handle.clone();
145163
counter::logger::logger_thread(handle_c, pool_c).await;
146-
164+
165+
let cache: std::sync::Arc<dashmap::DashSet<i32>> = std::sync::Arc::new(dashmap::DashSet::new());
166+
147167
log::info!("Starting disk usage tracking for: {}", root_dir);
148168
walkdir::WalkDir::new(root_dir)
149169
.into_iter()
@@ -153,9 +173,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
153173
let pool = std::sync::Arc::clone(&pool);
154174
let handle = handle.clone();
155175
if entry.file_type().is_dir() {
156-
process_directory(entry, pool, handle);
176+
process_directory(entry, pool, handle, cache.clone());
157177
} else if entry.file_type().is_file() {
158-
process_file(entry, pool, handle);
178+
process_file(entry, pool, handle, cache.clone());
159179
}
160180
});
161181

0 commit comments

Comments
 (0)