diff options
| author | metamuffin <metamuffin@disroot.org> | 2025-12-15 15:09:37 +0100 |
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2025-12-15 15:09:37 +0100 |
| commit | 0e48299889c3c2b81bf351ffe5da71e0bcd4c22a (patch) | |
| tree | 8a7ff2bd2330c206070b2062723ba471b2d62544 /database/src/backends | |
| parent | 7552a4ff0e027334398d28d5687a339ad77c0871 (diff) | |
| download | jellything-0e48299889c3c2b81bf351ffe5da71e0bcd4c22a.tar jellything-0e48299889c3c2b81bf351ffe5da71e0bcd4c22a.tar.bz2 jellything-0e48299889c3c2b81bf351ffe5da71e0bcd4c22a.tar.zst | |
db
Diffstat (limited to 'database/src/backends')
| -rw-r--r-- | database/src/backends/memory.rs | 13 | ||||
| -rw-r--r-- | database/src/backends/mod.rs | 5 | ||||
| -rw-r--r-- | database/src/backends/redb.rs | 19 | ||||
| -rw-r--r-- | database/src/backends/rocksdb.rs | 19 |
4 files changed, 46 insertions, 10 deletions
diff --git a/database/src/backends/memory.rs b/database/src/backends/memory.rs index 97c8c2c..2f19ce6 100644 --- a/database/src/backends/memory.rs +++ b/database/src/backends/memory.rs @@ -4,13 +4,18 @@ Copyright (C) 2025 metamuffin <metamuffin.org> */ -use crate::backends::DatabaseStorage; +use crate::backends::KV; use anyhow::Result; use std::{collections::BTreeMap, sync::RwLock}; pub struct Memory(RwLock<BTreeMap<Vec<u8>, Vec<u8>>>); -impl DatabaseStorage for Memory { +impl Memory { + pub fn new() -> Self { + Self(RwLock::new(BTreeMap::new())) + } +} +impl KV for Memory { fn set(&self, key: &[u8], value: &[u8]) -> Result<()> { self.0.write().unwrap().insert(key.to_vec(), value.to_vec()); Ok(()) @@ -18,6 +23,10 @@ impl DatabaseStorage for Memory { fn get<'a>(&'a self, key: &[u8]) -> Result<Option<Vec<u8>>> { Ok(self.0.read().unwrap().get(key).cloned()) } + fn del(&self, key: &[u8]) -> Result<()> { + self.0.write().unwrap().remove(key); + Ok(()) + } fn next(&self, key: &[u8]) -> Result<Option<Vec<u8>>> { let r = self.0.read().unwrap(); Ok(r.range(key.to_vec()..).next().map(|(k, _)| k.to_owned())) diff --git a/database/src/backends/mod.rs b/database/src/backends/mod.rs index 1240ac1..b6d3770 100644 --- a/database/src/backends/mod.rs +++ b/database/src/backends/mod.rs @@ -4,15 +4,16 @@ Copyright (C) 2025 metamuffin <metamuffin.org> */ +pub mod memory; pub mod redb; pub mod rocksdb; -pub mod memory; use anyhow::Result; -pub trait DatabaseStorage { +pub trait KV { fn set(&self, key: &[u8], value: &[u8]) -> Result<()>; fn get<'a>(&'a self, key: &[u8]) -> Result<Option<Vec<u8>>>; + fn del(&self, key: &[u8]) -> Result<()>; fn next(&self, key: &[u8]) -> Result<Option<Vec<u8>>>; fn prev(&self, key: &[u8]) -> Result<Option<Vec<u8>>>; } diff --git a/database/src/backends/redb.rs b/database/src/backends/redb.rs index 39fe532..1b672b6 100644 --- a/database/src/backends/redb.rs +++ b/database/src/backends/redb.rs @@ -4,7 +4,9 @@ Copyright (C) 2025 metamuffin <metamuffin.org> */ -use crate::backends::DatabaseStorage; +use std::path::Path; + +use crate::backends::KV; use anyhow::Result; use redb::{Database, ReadableDatabase, TableDefinition}; @@ -14,13 +16,26 @@ pub struct Redb { const TABLE: TableDefinition<&[u8], &[u8]> = TableDefinition::new("kv"); -impl DatabaseStorage for Redb { +impl Redb { + pub fn new(path: &Path) -> Result<Self> { + Ok(Self { + db: Database::create(path)?, + }) + } +} +impl KV for Redb { fn set(&self, key: &[u8], value: &[u8]) -> Result<()> { let txn = self.db.begin_write()?; txn.open_table(TABLE)?.insert(key, value)?; txn.commit()?; Ok(()) } + fn del(&self, key: &[u8]) -> Result<()> { + let txn = self.db.begin_write()?; + txn.open_table(TABLE)?.remove(key)?; + txn.commit()?; + Ok(()) + } fn get<'a>(&'a self, key: &[u8]) -> Result<Option<Vec<u8>>> { let txn = self.db.begin_read()?; match txn.open_table(TABLE)?.get(key)? { diff --git a/database/src/backends/rocksdb.rs b/database/src/backends/rocksdb.rs index 7c6f5f3..f4ed55b 100644 --- a/database/src/backends/rocksdb.rs +++ b/database/src/backends/rocksdb.rs @@ -4,7 +4,9 @@ Copyright (C) 2025 metamuffin <metamuffin.org> */ -use crate::backends::DatabaseStorage; +use std::path::Path; + +use crate::backends::KV; use anyhow::Result; use rocksdb::DB; @@ -12,14 +14,23 @@ pub struct Rocksdb { db: DB, } -impl DatabaseStorage for Rocksdb { +impl Rocksdb { + pub fn new(path: &Path) -> Result<Self> { + Ok(Self { + db: DB::open_default(path)?, + }) + } +} +impl KV for Rocksdb { fn set(&self, key: &[u8], value: &[u8]) -> Result<()> { - self.db.put(key, value)?; - Ok(()) + Ok(self.db.put(key, value)?) } fn get<'a>(&'a self, key: &[u8]) -> Result<Option<Vec<u8>>> { Ok(self.db.get(key)?) } + fn del(&self, key: &[u8]) -> Result<()> { + Ok(self.db.delete(key)?) + } fn next(&self, key: &[u8]) -> Result<Option<Vec<u8>>> { let mut it = self.db.raw_iterator(); it.seek_for_prev(key); |