diff options
| author | metamuffin <metamuffin@disroot.org> | 2025-12-18 18:45:53 +0100 |
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2025-12-18 18:45:53 +0100 |
| commit | da985cc06e4caa7501222dbf54f212536fd42b0c (patch) | |
| tree | 106ebacb66abb8fd97a7be4e802ac45d8ce9852d /database/src/backends/memory.rs | |
| parent | fc7f3ae8e39a0398ceba7b9c44f58679c01a98da (diff) | |
| download | jellything-da985cc06e4caa7501222dbf54f212536fd42b0c.tar jellything-da985cc06e4caa7501222dbf54f212536fd42b0c.tar.bz2 jellything-da985cc06e4caa7501222dbf54f212536fd42b0c.tar.zst | |
transaction interface
Diffstat (limited to 'database/src/backends/memory.rs')
| -rw-r--r-- | database/src/backends/memory.rs | 59 |
1 files changed, 46 insertions, 13 deletions
diff --git a/database/src/backends/memory.rs b/database/src/backends/memory.rs index f1952a3..3c2fdea 100644 --- a/database/src/backends/memory.rs +++ b/database/src/backends/memory.rs @@ -4,10 +4,14 @@ Copyright (C) 2025 metamuffin <metamuffin.org> */ -use crate::backends::KV; +use crate::backends::{Db, ReadTransaction, ReadTxnFunction, WriteTransaction, WriteTxnFunction}; use anyhow::Result; -use std::{collections::BTreeMap, sync::RwLock}; +use std::{ + collections::BTreeMap, + sync::{RwLock, RwLockReadGuard, RwLockWriteGuard}, +}; +type Inner = BTreeMap<Vec<u8>, Vec<u8>>; pub struct Memory(RwLock<BTreeMap<Vec<u8>, Vec<u8>>>); impl Memory { @@ -15,25 +19,54 @@ impl Memory { 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()); +impl Db for Memory { + fn write_transaction(&self, f: &mut WriteTxnFunction) -> Result<()> { + f(&mut self.0.write().unwrap()) + } + fn read_transaction(&self, f: &mut ReadTxnFunction) -> Result<()> { + f(&self.0.read().unwrap()) + } +} +impl WriteTransaction for RwLockWriteGuard<'_, Inner> { + fn set(&mut self, key: &[u8], value: &[u8]) -> Result<()> { + (**self).insert(key.to_vec(), value.to_vec()); + Ok(()) + } + fn del(&mut self, key: &[u8]) -> Result<()> { + (**self).remove(key); Ok(()) } +} +impl ReadTransaction for RwLockWriteGuard<'_, Inner> { fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> { - Ok(self.0.read().unwrap().get(key).cloned()) + Ok((**self).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>>> { + Ok((**self) + .range(key.to_vec()..) + .next() + .map(|(k, _)| k.to_owned())) + } + fn prev(&self, key: &[u8]) -> Result<Option<Vec<u8>>> { + Ok((**self) + .range(..key.to_vec()) + .next_back() + .map(|(k, _)| k.to_owned())) + } +} +impl ReadTransaction for RwLockReadGuard<'_, Inner> { + fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> { + Ok((**self).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())) + Ok((**self) + .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()) + Ok((**self) + .range(..key.to_vec()) .next_back() .map(|(k, _)| k.to_owned())) } |