aboutsummaryrefslogtreecommitdiff
path: root/database
diff options
context:
space:
mode:
Diffstat (limited to 'database')
-rw-r--r--database/src/backends/memory.rs31
-rw-r--r--database/src/backends/mod.rs18
-rw-r--r--database/src/backends/redb.rs49
-rw-r--r--database/src/backends/rocksdb.rs35
4 files changed, 133 insertions, 0 deletions
diff --git a/database/src/backends/memory.rs b/database/src/backends/memory.rs
new file mode 100644
index 0000000..97c8c2c
--- /dev/null
+++ b/database/src/backends/memory.rs
@@ -0,0 +1,31 @@
+/*
+ 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) 2025 metamuffin <metamuffin.org>
+*/
+
+use crate::backends::DatabaseStorage;
+use anyhow::Result;
+use std::{collections::BTreeMap, sync::RwLock};
+
+pub struct Memory(RwLock<BTreeMap<Vec<u8>, Vec<u8>>>);
+
+impl DatabaseStorage for Memory {
+ fn set(&self, key: &[u8], value: &[u8]) -> Result<()> {
+ self.0.write().unwrap().insert(key.to_vec(), value.to_vec());
+ Ok(())
+ }
+ fn get<'a>(&'a self, key: &[u8]) -> Result<Option<Vec<u8>>> {
+ Ok(self.0.read().unwrap().get(key).cloned())
+ }
+ 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()))
+ }
+ fn prev(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
+ let r = self.0.read().unwrap();
+ Ok(r.range(..key.to_vec())
+ .next_back()
+ .map(|(k, _)| k.to_owned()))
+ }
+}
diff --git a/database/src/backends/mod.rs b/database/src/backends/mod.rs
new file mode 100644
index 0000000..1240ac1
--- /dev/null
+++ b/database/src/backends/mod.rs
@@ -0,0 +1,18 @@
+/*
+ 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) 2025 metamuffin <metamuffin.org>
+*/
+
+pub mod redb;
+pub mod rocksdb;
+pub mod memory;
+
+use anyhow::Result;
+
+pub trait DatabaseStorage {
+ fn set(&self, key: &[u8], value: &[u8]) -> Result<()>;
+ fn get<'a>(&'a self, key: &[u8]) -> Result<Option<Vec<u8>>>;
+ 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
new file mode 100644
index 0000000..39fe532
--- /dev/null
+++ b/database/src/backends/redb.rs
@@ -0,0 +1,49 @@
+/*
+ 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) 2025 metamuffin <metamuffin.org>
+*/
+
+use crate::backends::DatabaseStorage;
+use anyhow::Result;
+use redb::{Database, ReadableDatabase, TableDefinition};
+
+pub struct Redb {
+ db: Database,
+}
+
+const TABLE: TableDefinition<&[u8], &[u8]> = TableDefinition::new("kv");
+
+impl DatabaseStorage 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 get<'a>(&'a self, key: &[u8]) -> Result<Option<Vec<u8>>> {
+ let txn = self.db.begin_read()?;
+ match txn.open_table(TABLE)?.get(key)? {
+ Some(v) => Ok(Some(v.value().to_vec())),
+ None => Ok(None),
+ }
+ }
+ fn next(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
+ let txn = self.db.begin_read()?;
+ let table = txn.open_table(TABLE)?;
+ let mut iter = table.range(key..)?;
+ match iter.next() {
+ Some(k) => Ok(Some(k?.0.value().to_vec())),
+ None => Ok(None),
+ }
+ }
+ fn prev(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
+ let txn = self.db.begin_read()?;
+ let table = txn.open_table(TABLE)?;
+ let mut iter = table.range(..key)?;
+ match iter.next_back() {
+ Some(k) => Ok(Some(k?.0.value().to_vec())),
+ None => Ok(None),
+ }
+ }
+}
diff --git a/database/src/backends/rocksdb.rs b/database/src/backends/rocksdb.rs
new file mode 100644
index 0000000..7c6f5f3
--- /dev/null
+++ b/database/src/backends/rocksdb.rs
@@ -0,0 +1,35 @@
+/*
+ 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) 2025 metamuffin <metamuffin.org>
+*/
+
+use crate::backends::DatabaseStorage;
+use anyhow::Result;
+use rocksdb::DB;
+
+pub struct Rocksdb {
+ db: DB,
+}
+
+impl DatabaseStorage for Rocksdb {
+ fn set(&self, key: &[u8], value: &[u8]) -> Result<()> {
+ self.db.put(key, value)?;
+ Ok(())
+ }
+ fn get<'a>(&'a self, key: &[u8]) -> Result<Option<Vec<u8>>> {
+ Ok(self.db.get(key)?)
+ }
+ fn next(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
+ let mut it = self.db.raw_iterator();
+ it.seek_for_prev(key);
+ it.next();
+ Ok(it.key().map(Vec::from))
+ }
+ fn prev(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
+ let mut it = self.db.raw_iterator();
+ it.seek(key);
+ it.prev();
+ Ok(it.key().map(Vec::from))
+ }
+}