1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
/*
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::{Db, ReadTransaction, ReadTxnFunction, WriteTransaction, WriteTxnFunction};
use anyhow::Result;
use redb::{Database, ReadableDatabase, ReadableTable, TableDefinition};
use std::path::Path;
pub struct Redb {
db: Database,
}
const TABLE: TableDefinition<&[u8], &[u8]> = TableDefinition::new("kv");
impl Redb {
pub fn new(path: &Path) -> Result<Self> {
Ok(Self {
db: Database::create(path)?,
})
}
}
impl Db for Redb {
fn write_transaction(&self, f: &mut WriteTxnFunction) -> Result<()> {
let mut txn = self.db.begin_write()?;
f(&mut txn)?;
txn.commit()?;
Ok(())
}
fn read_transaction(&self, f: &mut ReadTxnFunction) -> Result<()> {
let mut txn = self.db.begin_read()?;
f(&mut txn)?;
Ok(())
}
}
impl WriteTransaction for redb::WriteTransaction {
fn set(&mut self, key: &[u8], value: &[u8]) -> Result<()> {
self.open_table(TABLE)?.insert(key, value)?;
Ok(())
}
fn del(&mut self, key: &[u8]) -> Result<()> {
self.open_table(TABLE)?.remove(key)?;
Ok(())
}
}
impl ReadTransaction for redb::WriteTransaction {
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 next(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
let table = self.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 table = self.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),
}
}
}
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 next(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
let table = self.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 table = self.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),
}
}
}
|