From 2bcccb18a6cb8bf836f57c3d86f759b19699def2 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sat, 24 Jan 2026 23:06:33 +0100 Subject: cache as object --- cache/src/bin/cache_fs_to_rocksdb.rs | 38 ---------------------------- cache/src/bin/cache_rocksdb_delete_prefix.rs | 25 ------------------ cache/src/lib.rs | 8 ++---- cache/tools/Cargo.toml | 16 ++++++++++++ cache/tools/cache_fs_to_rocksdb.rs | 38 ++++++++++++++++++++++++++++ cache/tools/cache_rocksdb_delete_prefix.rs | 25 ++++++++++++++++++ 6 files changed, 81 insertions(+), 69 deletions(-) delete mode 100644 cache/src/bin/cache_fs_to_rocksdb.rs delete mode 100644 cache/src/bin/cache_rocksdb_delete_prefix.rs create mode 100644 cache/tools/Cargo.toml create mode 100644 cache/tools/cache_fs_to_rocksdb.rs create mode 100644 cache/tools/cache_rocksdb_delete_prefix.rs (limited to 'cache') diff --git a/cache/src/bin/cache_fs_to_rocksdb.rs b/cache/src/bin/cache_fs_to_rocksdb.rs deleted file mode 100644 index d283dcb..0000000 --- a/cache/src/bin/cache_fs_to_rocksdb.rs +++ /dev/null @@ -1,38 +0,0 @@ -/* - This file is part of jellything (https://codeberg.org/metamuffin/jellything) - which is licensed under the GNU Affero General Public License (version 3); see /COPYING. - Copyright (C) 2026 metamuffin -*/ - -use anyhow::{Result, bail}; -use rocksdb::DB; -use std::{env::args, fs::File, io::Read, path::Path}; - -fn main() -> Result<()> { - let in_path = args().nth(1).unwrap(); - let out_path = args().nth(2).unwrap(); - let db = DB::open_default(out_path)?; - if !in_path.ends_with("/") { - bail!("path needs to end with /") - } - traverse(&db, &in_path, in_path.as_ref())?; - db.flush()?; - Ok(()) -} - -fn traverse(db: &DB, prefix: &str, path: &Path) -> Result<()> { - if path.is_dir() { - for e in path.read_dir()? { - traverse(db, prefix, &e?.path())?; - } - } - if path.is_file() { - let key = path.to_string_lossy(); - let key = key.strip_prefix(prefix).unwrap(); - let mut value = Vec::new(); - File::open(path)?.read_to_end(&mut value)?; - println!("{key}"); - db.put(key, value)?; - } - Ok(()) -} diff --git a/cache/src/bin/cache_rocksdb_delete_prefix.rs b/cache/src/bin/cache_rocksdb_delete_prefix.rs deleted file mode 100644 index e09ce61..0000000 --- a/cache/src/bin/cache_rocksdb_delete_prefix.rs +++ /dev/null @@ -1,25 +0,0 @@ -/* - This file is part of jellything (https://codeberg.org/metamuffin/jellything) - which is licensed under the GNU Affero General Public License (version 3); see /COPYING. - Copyright (C) 2026 metamuffin -*/ - -use anyhow::Result; -use rocksdb::DB; -use std::env::args; - -fn main() -> Result<()> { - let db = DB::open_default(args().nth(1).unwrap())?; - let prefix = args().nth(2).unwrap(); - for r in db.prefix_iterator(&prefix) { - let key = r?.0; - let key_s = String::from_utf8_lossy(&key); - if !key_s.starts_with(&prefix) { - break; - } - println!("{key_s}"); - db.delete(key)?; - } - db.flush()?; - Ok(()) -} diff --git a/cache/src/lib.rs b/cache/src/lib.rs index be2b331..4d6bbcf 100644 --- a/cache/src/lib.rs +++ b/cache/src/lib.rs @@ -81,14 +81,10 @@ impl Cache { Ok(out) } - pub fn cache_read(&self, key: &str) -> Result>> { + pub fn read(&self, key: &str) -> Result>> { self.storage.read(key) } - pub fn cache_store( - &self, - key: String, - generate: impl FnOnce() -> Result>, - ) -> Result { + pub fn store(&self, key: String, generate: impl FnOnce() -> Result>) -> Result { self.cache(&key, generate)?; Ok(key) } diff --git a/cache/tools/Cargo.toml b/cache/tools/Cargo.toml new file mode 100644 index 0000000..8f249b5 --- /dev/null +++ b/cache/tools/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "tools" +version = "0.1.0" +edition = "2024" + +[dependencies] +rocksdb = { version = "0.24.0", features = ["multi-threaded-cf"] } +anyhow = "1.0.100" + +[[bin]] +name = "cache_fs_to_rocksdb" +path = "cache_fs_to_rocksdb.rs" + +[[bin]] +name = "cache_rocksdb_delete_prefix" +path = "cache_rocksdb_delete_prefix.rs" diff --git a/cache/tools/cache_fs_to_rocksdb.rs b/cache/tools/cache_fs_to_rocksdb.rs new file mode 100644 index 0000000..d283dcb --- /dev/null +++ b/cache/tools/cache_fs_to_rocksdb.rs @@ -0,0 +1,38 @@ +/* + This file is part of jellything (https://codeberg.org/metamuffin/jellything) + which is licensed under the GNU Affero General Public License (version 3); see /COPYING. + Copyright (C) 2026 metamuffin +*/ + +use anyhow::{Result, bail}; +use rocksdb::DB; +use std::{env::args, fs::File, io::Read, path::Path}; + +fn main() -> Result<()> { + let in_path = args().nth(1).unwrap(); + let out_path = args().nth(2).unwrap(); + let db = DB::open_default(out_path)?; + if !in_path.ends_with("/") { + bail!("path needs to end with /") + } + traverse(&db, &in_path, in_path.as_ref())?; + db.flush()?; + Ok(()) +} + +fn traverse(db: &DB, prefix: &str, path: &Path) -> Result<()> { + if path.is_dir() { + for e in path.read_dir()? { + traverse(db, prefix, &e?.path())?; + } + } + if path.is_file() { + let key = path.to_string_lossy(); + let key = key.strip_prefix(prefix).unwrap(); + let mut value = Vec::new(); + File::open(path)?.read_to_end(&mut value)?; + println!("{key}"); + db.put(key, value)?; + } + Ok(()) +} diff --git a/cache/tools/cache_rocksdb_delete_prefix.rs b/cache/tools/cache_rocksdb_delete_prefix.rs new file mode 100644 index 0000000..e09ce61 --- /dev/null +++ b/cache/tools/cache_rocksdb_delete_prefix.rs @@ -0,0 +1,25 @@ +/* + This file is part of jellything (https://codeberg.org/metamuffin/jellything) + which is licensed under the GNU Affero General Public License (version 3); see /COPYING. + Copyright (C) 2026 metamuffin +*/ + +use anyhow::Result; +use rocksdb::DB; +use std::env::args; + +fn main() -> Result<()> { + let db = DB::open_default(args().nth(1).unwrap())?; + let prefix = args().nth(2).unwrap(); + for r in db.prefix_iterator(&prefix) { + let key = r?.0; + let key_s = String::from_utf8_lossy(&key); + if !key_s.starts_with(&prefix) { + break; + } + println!("{key_s}"); + db.delete(key)?; + } + db.flush()?; + Ok(()) +} -- cgit v1.3