diff options
| author | metamuffin <metamuffin@disroot.org> | 2026-01-24 04:31:48 +0100 |
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2026-01-24 04:31:48 +0100 |
| commit | b2e88a8beabf04adc28947cf82996e8692a68b71 (patch) | |
| tree | 23d66c8672b69cce7835ffabae4092669062ada8 /database/src/backends/redb.rs | |
| parent | 774f64c0789529884dd7a5232f190e347ad29532 (diff) | |
| download | jellything-b2e88a8beabf04adc28947cf82996e8692a68b71.tar jellything-b2e88a8beabf04adc28947cf82996e8692a68b71.tar.bz2 jellything-b2e88a8beabf04adc28947cf82996e8692a68b71.tar.zst | |
move things around; kv crate
Diffstat (limited to 'database/src/backends/redb.rs')
| -rw-r--r-- | database/src/backends/redb.rs | 93 |
1 files changed, 0 insertions, 93 deletions
diff --git a/database/src/backends/redb.rs b/database/src/backends/redb.rs deleted file mode 100644 index c0887c8..0000000 --- a/database/src/backends/redb.rs +++ /dev/null @@ -1,93 +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::{Database, ReadTransaction, WriteTransaction}; -use anyhow::Result; -use redb::{AccessGuard, ReadableDatabase, ReadableTable, StorageError, Table, TableDefinition}; -use std::path::Path; - -const TABLE: TableDefinition<&[u8], &[u8]> = TableDefinition::new("kv"); - -pub fn new(path: &Path) -> Result<redb::Database> { - Ok(redb::Database::create(path)?) -} - -impl Database for redb::Database { - fn write_transaction( - &self, - f: &mut dyn FnMut(&mut dyn WriteTransaction) -> Result<()>, - ) -> Result<()> { - let txn = self.begin_write()?; - let mut table = txn.open_table(TABLE)?; - f(&mut table)?; - drop(table); - txn.commit()?; - Ok(()) - } - fn read_transaction( - &self, - f: &mut dyn FnMut(&dyn ReadTransaction) -> Result<()>, - ) -> Result<()> { - let mut txn = self.begin_read()?; - f(&mut txn)?; - Ok(()) - } -} - -impl WriteTransaction for Table<'_, &[u8], &[u8]> { - fn set(&mut self, key: &[u8], value: &[u8]) -> Result<()> { - self.insert(key, value)?; - Ok(()) - } - fn del(&mut self, key: &[u8]) -> Result<()> { - self.remove(key)?; - Ok(()) - } -} -impl ReadTransaction for Table<'_, &[u8], &[u8]> { - fn get<'a>(&'a self, key: &[u8]) -> Result<Option<Vec<u8>>> { - match <Self as ReadableTable<_, _>>::get(self, key)? { - Some(v) => Ok(Some(v.value().to_vec())), - None => Ok(None), - } - } - fn iter<'a>( - &'a self, - key: &[u8], - reverse: bool, - ) -> Result<Box<dyn Iterator<Item = Result<Vec<u8>>> + 'a>> { - let k = |e: Result<(AccessGuard<'_, &[u8]>, AccessGuard<'_, &[u8]>), StorageError>| { - e.map(|e| e.0.value().to_vec()).map_err(|e| e.into()) - }; - Ok(if reverse { - Box::new(self.range(..=key)?.rev().map(k)) - } else { - Box::new(self.range(key..)?.map(k)) - }) - } -} -impl ReadTransaction for redb::ReadTransaction { - fn get<'a>(&'a self, key: &[u8]) -> Result<Option<Vec<u8>>> { - match self.open_table(TABLE)?.get(key)? { - Some(v) => Ok(Some(v.value().to_vec())), - None => Ok(None), - } - } - fn iter<'a>( - &'a self, - key: &[u8], - reverse: bool, - ) -> Result<Box<dyn Iterator<Item = Result<Vec<u8>>> + 'a>> { - let k = |e: Result<(AccessGuard<'_, &[u8]>, AccessGuard<'_, &[u8]>), StorageError>| { - e.map(|e| e.0.value().to_vec()).map_err(|e| e.into()) - }; - Ok(if reverse { - Box::new(self.open_table(TABLE)?.range(..=key)?.rev().map(k)) - } else { - Box::new(self.open_table(TABLE)?.range(key..)?.map(k)) - }) - } -} |