diff options
Diffstat (limited to 'cache/src/backends')
| -rw-r--r-- | cache/src/backends/dummy.rs | 18 | ||||
| -rw-r--r-- | cache/src/backends/filesystem.rs | 50 | ||||
| -rw-r--r-- | cache/src/backends/mod.rs | 28 | ||||
| -rw-r--r-- | cache/src/backends/rocksdb.rs | 26 |
4 files changed, 0 insertions, 122 deletions
diff --git a/cache/src/backends/dummy.rs b/cache/src/backends/dummy.rs deleted file mode 100644 index 7b0efa5..0000000 --- a/cache/src/backends/dummy.rs +++ /dev/null @@ -1,18 +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 <metamuffin.org> -*/ - -use crate::backends::CacheStorage; -use anyhow::Result; - -pub struct Dummy; -impl CacheStorage for Dummy { - fn store(&self, _key: String, _value: &[u8]) -> Result<()> { - Ok(()) - } - fn read(&self, _key: &str) -> Result<Option<Vec<u8>>> { - Ok(None) // sorry forgot - } -} diff --git a/cache/src/backends/filesystem.rs b/cache/src/backends/filesystem.rs deleted file mode 100644 index f1bbdf9..0000000 --- a/cache/src/backends/filesystem.rs +++ /dev/null @@ -1,50 +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 <metamuffin.org> -*/ - -use crate::{Config, backends::CacheStorage}; -use anyhow::{Result, bail}; -use rand::random; -use std::{ - fs::{File, create_dir_all, rename}, - io::{ErrorKind, Read, Write}, - path::PathBuf, -}; - -pub struct Filesystem(PathBuf); - -impl Filesystem { - pub fn new(config: &Config) -> Self { - Self(config.path.clone()) - } - fn temp_path(&self) -> PathBuf { - self.0.join(format!("temp-{:016x}", random::<u128>())) - } -} - -impl CacheStorage for Filesystem { - fn store(&self, key: String, value: &[u8]) -> Result<()> { - let temp = self.temp_path(); - let out = self.0.join(&key); - create_dir_all(out.parent().unwrap())?; - File::create(&temp)?.write_all(value)?; - rename(temp, out)?; - Ok(()) - } - fn read(&self, key: &str) -> Result<Option<Vec<u8>>> { - if key.contains("..") || key.starts_with("/") { - bail!("invalid key") - } - match File::open(self.0.join(key)) { - Ok(mut f) => { - let mut data = Vec::new(); - f.read_to_end(&mut data)?; - Ok(Some(data)) - } - Err(e) if e.kind() == ErrorKind::NotFound => Ok(None), - Err(e) => Err(e.into()), - } - } -} diff --git a/cache/src/backends/mod.rs b/cache/src/backends/mod.rs deleted file mode 100644 index 52a954b..0000000 --- a/cache/src/backends/mod.rs +++ /dev/null @@ -1,28 +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 <metamuffin.org> -*/ -pub mod dummy; -pub mod filesystem; -pub mod rocksdb; - -use crate::{ - CONF, - backends::{dummy::Dummy, filesystem::Filesystem, rocksdb::Rocksdb}, -}; -use anyhow::{Result, bail}; - -pub(crate) trait CacheStorage: Send + Sync + 'static { - fn store(&self, key: String, value: &[u8]) -> Result<()>; - fn read(&self, key: &str) -> Result<Option<Vec<u8>>>; -} - -pub fn init_backend() -> Result<Box<dyn CacheStorage>> { - Ok(match CONF.driver.as_str() { - "filesystem" => Box::new(Filesystem::new(&CONF)), - "rocksdb" => Box::new(Rocksdb::new(&CONF)?), - "dummy" => Box::new(Dummy), - _ => bail!("unknown driver"), - }) -} diff --git a/cache/src/backends/rocksdb.rs b/cache/src/backends/rocksdb.rs deleted file mode 100644 index 9db86dd..0000000 --- a/cache/src/backends/rocksdb.rs +++ /dev/null @@ -1,26 +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 <metamuffin.org> -*/ - -use crate::{Config, backends::CacheStorage}; -use anyhow::Result; -use rocksdb::DB; - -pub struct Rocksdb(DB); - -impl Rocksdb { - pub fn new(config: &Config) -> Result<Self> { - Ok(Self(rocksdb::DB::open_default(config.path.clone())?)) - } -} - -impl CacheStorage for Rocksdb { - fn store(&self, key: String, value: &[u8]) -> Result<()> { - Ok(self.0.put(key, value)?) - } - fn read(&self, key: &str) -> Result<Option<Vec<u8>>> { - Ok(self.0.get(key)?) - } -} |