Show folder sizes for movies, shows and seasons. Closes #5
CI / test (push) Successful in 1m12s
CI / test (push) Successful in 1m12s
The library lists now show how much space each movie and show takes on disk, counting everything a move copies, with a per-season breakdown for shows, a running total and a "Largest first" sort. - sizes.rs: a single background worker measures one folder at a time. Listing returns cached sizes straight away and queues folders; the UI polls until every size has arrived. - Cached in a new folder_sizes table (store migration v2). A size is kept while the folder's signature (mtimes of the folder and its direct subfolders) is unchanged, checked at most hourly and re-measured after 7 days. - Each agent measures its own folders, so peer listings carry the peer's sizes. - Tests for measuring, signatures, natural season order, the migration, and an e2e test covering both sides and a moved item.
This commit is contained in:
1 parent
4c6217bd40
commit
25a743b350
11 files changed
+672
-26
No files matched your search
@@ -3,6 +3,7 @@
|
||||
//! Jobs and exports are stored as JSON documents with a few indexed columns.
|
||||
//! The schema is deliberately small; the job struct is the source of truth.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
@@ -33,6 +34,11 @@ const MIGRATIONS: &[&str] = &[
|
||||
id TEXT PRIMARY KEY,
|
||||
body TEXT NOT NULL
|
||||
);",
|
||||
// v2: cached sizes of item folders, keyed by local path.
|
||||
"CREATE TABLE folder_sizes (
|
||||
path TEXT PRIMARY KEY,
|
||||
body TEXT NOT NULL
|
||||
);",
|
||||
];
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -161,6 +167,54 @@ impl Store {
|
||||
body.map(|b| serde_json::from_str(&b).map_err(Into::into))
|
||||
.transpose()
|
||||
}
|
||||
|
||||
/// Cached folder sizes for `paths`. Paths with no record are left out.
|
||||
pub async fn get_folder_sizes<T: DeserializeOwned>(
|
||||
&self,
|
||||
paths: Vec<String>,
|
||||
) -> Result<HashMap<String, T>> {
|
||||
let rows: Vec<(String, String)> = self
|
||||
.with(move |c| {
|
||||
let mut out = Vec::new();
|
||||
let mut stmt = c.prepare_cached("SELECT path, body FROM folder_sizes WHERE path = ?1")?;
|
||||
for p in &paths {
|
||||
if let Some(row) = stmt
|
||||
.query_row([p], |r| Ok((r.get(0)?, r.get(1)?)))
|
||||
.optional()?
|
||||
{
|
||||
out.push(row);
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
})
|
||||
.await?;
|
||||
rows.into_iter()
|
||||
.map(|(p, b)| Ok((p, serde_json::from_str(&b)?)))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub async fn put_folder_size<T: Serialize>(&self, path: &str, size: &T) -> Result<()> {
|
||||
let path = path.to_string();
|
||||
let body = serde_json::to_string(size)?;
|
||||
self.with(move |c| {
|
||||
c.execute(
|
||||
"INSERT INTO folder_sizes (path, body) VALUES (?1, ?2)
|
||||
ON CONFLICT(path) DO UPDATE SET body = excluded.body",
|
||||
params![path, body],
|
||||
)?;
|
||||
Ok(())
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn delete_folder_size(&self, path: &str) -> Result<()> {
|
||||
let path = path.to_string();
|
||||
self.with(move |c| {
|
||||
c.execute("DELETE FROM folder_sizes WHERE path = ?1", [path])?;
|
||||
Ok(())
|
||||
})
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -205,4 +259,50 @@ mod tests {
|
||||
let e: serde_json::Value = s.get_export("e1").await.unwrap().unwrap();
|
||||
assert_eq!(e["x"], 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn folder_sizes_roundtrip() {
|
||||
let store = Store::in_memory().unwrap();
|
||||
store
|
||||
.put_folder_size("/m/A", &serde_json::json!({"bytes": 1}))
|
||||
.await
|
||||
.unwrap();
|
||||
store
|
||||
.put_folder_size("/m/A", &serde_json::json!({"bytes": 2}))
|
||||
.await
|
||||
.unwrap();
|
||||
store
|
||||
.put_folder_size("/m/B", &serde_json::json!({"bytes": 3}))
|
||||
.await
|
||||
.unwrap();
|
||||
let got: HashMap<String, serde_json::Value> = store
|
||||
.get_folder_sizes(vec!["/m/A".into(), "/m/B".into(), "/m/C".into()])
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(got.len(), 2);
|
||||
assert_eq!(got["/m/A"]["bytes"], 2);
|
||||
store.delete_folder_size("/m/A").await.unwrap();
|
||||
let got: HashMap<String, serde_json::Value> =
|
||||
store.get_folder_sizes(vec!["/m/A".into()]).await.unwrap();
|
||||
assert!(got.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn upgrades_a_v1_database() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("state.db");
|
||||
{
|
||||
let c = Connection::open(&path).unwrap();
|
||||
c.execute_batch(MIGRATIONS[0]).unwrap();
|
||||
c.pragma_update(None, "user_version", 1).unwrap();
|
||||
}
|
||||
Store::open(&path).unwrap();
|
||||
let c = Connection::open(&path).unwrap();
|
||||
let v: i64 = c
|
||||
.pragma_query_value(None, "user_version", |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!(v as usize, MIGRATIONS.len());
|
||||
c.execute("INSERT INTO folder_sizes (path, body) VALUES ('p', '{}')", [])
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user