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
|
/*
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, WriteTransaction, WriteTxnFunction};
use anyhow::Result;
use rocksdb::{ErrorKind, OptimisticTransactionDB};
use std::path::Path;
pub struct Rocksdb {
db: OptimisticTransactionDB,
}
impl Rocksdb {
pub fn new(path: &Path) -> Result<Self> {
Ok(Self {
db: OptimisticTransactionDB::open_default(path)?,
})
}
}
impl Db for Rocksdb {
fn write_transaction(&self, f: &mut WriteTxnFunction) -> Result<()> {
loop {
let mut txn = self.db.transaction();
f(&mut txn)?;
match txn.commit() {
Ok(()) => break Ok(()),
Err(e) if e.kind() == ErrorKind::Busy => continue,
Err(e) => return Err(e.into()),
}
}
}
fn read_transaction(&self, f: &mut super::ReadTxnFunction) -> Result<()> {
loop {
let txn = self.db.transaction();
f(&txn)?;
match txn.commit() {
Ok(()) => break Ok(()),
Err(e) if e.kind() == ErrorKind::Busy => continue,
Err(e) => return Err(e.into()),
}
}
}
}
impl WriteTransaction for rocksdb::Transaction<'_, OptimisticTransactionDB> {
fn set(&mut self, key: &[u8], value: &[u8]) -> Result<()> {
Ok(self.put(key, value)?)
}
fn del(&mut self, key: &[u8]) -> Result<()> {
Ok(self.delete(key)?)
}
}
impl ReadTransaction for rocksdb::Transaction<'_, OptimisticTransactionDB> {
fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
Ok(self.get(key)?)
}
fn next(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
let mut it = self.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.raw_iterator();
it.seek(key);
it.prev();
Ok(it.key().map(Vec::from))
}
}
|