use std::ops::Bound;
use std::path::{Path, PathBuf};
use std::sync::RwLock;
use redb::{Database, Durability, ReadableDatabase, TableDefinition, TableError};
use super::api::{
BFut, Backend, CommitOptions, Durability as CommitDurability, KeyRange, Op, ScanPage,
ScanRequest, Table, Usage,
};
use crate::error::{Error, Result};
type Def = TableDefinition<'static, &'static [u8], &'static [u8]>;
const META: Def = TableDefinition::new("meta");
const RECORDS: Def = TableDefinition::new("records");
const CHUNKS: Def = TableDefinition::new("chunks");
fn definition(table: Table) -> Def {
match table {
Table::Meta => META,
Table::Records => RECORDS,
Table::Chunks => CHUNKS,
}
}
fn backend_err(context: &str, e: impl std::fmt::Display) -> Error {
let message = e.to_string();
if message.contains("No space left") || message.contains("ENOSPC") {
return Error::QuotaExceeded {
needed: 0,
available: Some(0),
};
}
Error::Backend(format!("{context}: {message}"))
}
#[derive(Debug)]
pub struct RedbBackend {
db: RwLock<Option<Database>>,
path: PathBuf,
}
impl RedbBackend {
pub fn open(path: impl AsRef<Path>) -> Result<Self> {
let path = path.as_ref().to_path_buf();
let db = Database::create(&path).map_err(|e| backend_err("opening the database", e))?;
let backend = Self {
db: RwLock::new(Some(db)),
path,
};
backend.create_tables()?;
Ok(backend)
}
fn with_db<T>(&self, f: impl FnOnce(&Database) -> Result<T>) -> Result<T> {
let guard = self
.db
.read()
.map_err(|_| Error::backend("the redb handle lock was poisoned"))?;
match guard.as_ref() {
Some(db) => f(db),
None => Err(Error::Closed),
}
}
fn create_tables(&self) -> Result<()> {
if !self.any_table_missing()? {
return Ok(());
}
self.with_db(|db| {
let txn = db
.begin_write()
.map_err(|e| backend_err("beginning the setup transaction", e))?;
{
for table in Table::ALL {
txn.open_table(definition(table))
.map_err(|e| backend_err("creating a table", e))?;
}
}
txn.commit()
.map_err(|e| backend_err("committing table creation", e))
})
}
fn any_table_missing(&self) -> Result<bool> {
self.with_db(|db| {
let txn = db
.begin_read()
.map_err(|e| backend_err("beginning the table probe", e))?;
for table in Table::ALL {
match txn.open_table(definition(table)) {
Ok(_) => {}
Err(TableError::TableDoesNotExist(_)) => return Ok(true),
Err(e) => return Err(backend_err("probing for a table", e)),
}
}
Ok(false)
})
}
fn read_one(&self, table: Table, key: &[u8]) -> Result<Option<Vec<u8>>> {
self.with_db(|db| {
let txn = db
.begin_read()
.map_err(|e| backend_err("beginning a read", e))?;
let handle = match txn.open_table(definition(table)) {
Ok(handle) => handle,
Err(TableError::TableDoesNotExist(_)) => return Ok(None),
Err(e) => return Err(backend_err("opening a table for reading", e)),
};
handle
.get(key)
.map_err(|e| backend_err("reading a key", e))
.map(|found| found.map(|guard| guard.value().to_vec()))
})
}
fn scan_page(&self, request: &ScanRequest) -> Result<ScanPage> {
self.with_db(|db| {
let txn = db
.begin_read()
.map_err(|e| backend_err("beginning a scan", e))?;
let handle = match txn.open_table(definition(request.table)) {
Ok(handle) => handle,
Err(TableError::TableDoesNotExist(_)) => return Ok(ScanPage::default()),
Err(e) => return Err(backend_err("opening a table for scanning", e)),
};
let bounds = as_ref_bounds(&request.range);
let iter = handle
.range::<&[u8]>(bounds)
.map_err(|e| backend_err("opening a range", e))?;
let mut items = Vec::new();
let mut resume = None;
let entries: Box<dyn Iterator<Item = _>> = if request.reverse {
Box::new(iter.rev())
} else {
Box::new(iter)
};
for entry in entries {
if items.len() >= request.limit {
break;
}
let (key, value) = entry.map_err(|e| backend_err("reading a range entry", e))?;
let key = key.value().to_vec();
let value = request.want_values.then(|| value.value().to_vec());
resume = Some(key.clone());
items.push((key, value));
}
let exhausted = items.len() < request.limit;
Ok(ScanPage {
items,
resume: if exhausted { None } else { resume },
})
})
}
fn apply(&self, ops: Vec<Op>, durability: CommitDurability) -> Result<()> {
self.with_db(|db| {
let mut txn = db
.begin_write()
.map_err(|e| backend_err("beginning a write", e))?;
let redb_durability = match durability {
CommitDurability::Immediate => Durability::Immediate,
CommitDurability::Eventual => Durability::None,
};
txn.set_durability(redb_durability)
.map_err(|e| backend_err("setting durability", e))?;
{
for op in ops {
match op {
Op::Put { table, key, value } => {
let mut handle = txn
.open_table(definition(table))
.map_err(|e| backend_err("opening a table for writing", e))?;
handle
.insert(key.as_slice(), value.as_slice())
.map_err(|e| backend_err("inserting a key", e))?;
}
Op::Delete { table, key } => {
let mut handle = txn
.open_table(definition(table))
.map_err(|e| backend_err("opening a table for deletion", e))?;
handle
.remove(key.as_slice())
.map_err(|e| backend_err("removing a key", e))?;
}
Op::DeleteRange { table, range } => {
let mut handle = txn.open_table(definition(table)).map_err(|e| {
backend_err("opening a table for range deletion", e)
})?;
let bounds = as_ref_bounds(&range);
handle
.retain_in::<&[u8], _>(bounds, |_, _| false)
.map_err(|e| backend_err("deleting a range", e))?;
}
}
}
}
txn.commit().map_err(|e| backend_err("committing", e))
})
}
}
fn borrow_bound(bound: &Bound<Vec<u8>>) -> Bound<&[u8]> {
match bound {
Bound::Unbounded => Bound::Unbounded,
Bound::Included(v) => Bound::Included(v.as_slice()),
Bound::Excluded(v) => Bound::Excluded(v.as_slice()),
}
}
fn as_ref_bounds(range: &KeyRange) -> (Bound<&[u8]>, Bound<&[u8]>) {
(borrow_bound(&range.start), borrow_bound(&range.end))
}
impl Backend for RedbBackend {
fn get<'a>(&'a self, table: Table, key: &'a [u8]) -> BFut<'a, Option<Vec<u8>>> {
Box::pin(async move { self.read_one(table, key) })
}
fn get_many<'a>(&'a self, table: Table, keys: Vec<Vec<u8>>) -> BFut<'a, Vec<Option<Vec<u8>>>> {
Box::pin(async move {
self.with_db(|db| {
let txn = db
.begin_read()
.map_err(|e| backend_err("beginning a batch read", e))?;
let handle = match txn.open_table(definition(table)) {
Ok(handle) => handle,
Err(TableError::TableDoesNotExist(_)) => return Ok(vec![None; keys.len()]),
Err(e) => return Err(backend_err("opening a table for batch reading", e)),
};
keys.iter()
.map(|key| {
handle
.get(key.as_slice())
.map_err(|e| backend_err("reading a key", e))
.map(|found| found.map(|guard| guard.value().to_vec()))
})
.collect()
})
})
}
fn scan_page_size(&self) -> usize {
1024
}
fn scan(&self, request: ScanRequest) -> BFut<'_, ScanPage> {
Box::pin(async move { self.scan_page(&request) })
}
fn commit(&self, ops: Vec<Op>) -> BFut<'_, ()> {
Box::pin(async move { self.apply(ops, CommitDurability::Immediate) })
}
fn commit_with(&self, ops: Vec<Op>, options: CommitOptions) -> BFut<'_, ()> {
Box::pin(async move { self.apply(ops, options.durability) })
}
fn usage(&self) -> BFut<'_, Option<Usage>> {
Box::pin(async move {
let used = std::fs::metadata(&self.path).map(|m| m.len()).unwrap_or(0);
Ok(Some(Usage {
used,
available: None,
persisted: true,
}))
})
}
fn close(&self) -> BFut<'_, ()> {
Box::pin(async move {
let taken = {
let mut guard = self
.db
.write()
.map_err(|_| Error::backend("the redb handle lock was poisoned"))?;
guard.take()
};
drop(taken);
Ok(())
})
}
fn flush(&self) -> BFut<'_, ()> {
Box::pin(async move { self.apply(Vec::new(), CommitDurability::Immediate) })
}
}
#[cfg(test)]
mod tests {
use super::*;
use futures::executor::block_on;
use tempfile::TempDir;
fn backend(dir: &TempDir) -> RedbBackend {
RedbBackend::open(dir.path().join("bank.redb")).unwrap()
}
fn put(key: &[u8], value: &[u8]) -> Op {
Op::Put {
table: Table::Records,
key: key.to_vec(),
value: value.to_vec(),
}
}
#[test]
fn a_reopened_database_still_has_its_data() {
let dir = TempDir::new().unwrap();
{
let b = backend(&dir);
block_on(b.commit(vec![put(b"k", b"survives")])).unwrap();
}
let b = backend(&dir);
assert_eq!(
block_on(b.get(Table::Records, b"k")).unwrap(),
Some(b"survives".to_vec())
);
}
#[test]
fn reads_work_before_anything_has_been_written() {
let dir = TempDir::new().unwrap();
let b = backend(&dir);
assert_eq!(block_on(b.get(Table::Records, b"nothing")).unwrap(), None);
assert!(block_on(b.scan(ScanRequest {
table: Table::Chunks,
range: KeyRange::all(),
reverse: false,
limit: 10,
want_values: true,
}))
.unwrap()
.items
.is_empty());
}
#[test]
fn an_empty_value_survives_a_reopen() {
let dir = TempDir::new().unwrap();
{
let b = backend(&dir);
block_on(b.commit(vec![put(b"empty", b"")])).unwrap();
}
let b = backend(&dir);
assert_eq!(
block_on(b.get(Table::Records, b"empty")).unwrap(),
Some(Vec::new()),
"an empty value must stay distinguishable from a missing key"
);
assert_eq!(block_on(b.get(Table::Records, b"other")).unwrap(), None);
}
#[test]
fn a_failed_op_list_leaves_the_previous_state_intact() {
let dir = TempDir::new().unwrap();
let b = backend(&dir);
block_on(b.commit(vec![put(b"a", b"1"), put(b"b", b"2")])).unwrap();
block_on(b.commit(vec![
Op::DeleteRange {
table: Table::Records,
range: KeyRange::prefix(b"a"),
},
put(b"c", b"3"),
]))
.unwrap();
assert_eq!(block_on(b.get(Table::Records, b"a")).unwrap(), None);
assert_eq!(
block_on(b.get(Table::Records, b"b")).unwrap(),
Some(b"2".to_vec())
);
assert_eq!(
block_on(b.get(Table::Records, b"c")).unwrap(),
Some(b"3".to_vec())
);
}
#[test]
fn tables_stay_independent_across_a_reopen() {
let dir = TempDir::new().unwrap();
{
let b = backend(&dir);
block_on(b.commit(vec![
Op::Put {
table: Table::Meta,
key: b"k".to_vec(),
value: b"meta".to_vec(),
},
Op::Put {
table: Table::Records,
key: b"k".to_vec(),
value: b"record".to_vec(),
},
]))
.unwrap();
}
let b = backend(&dir);
assert_eq!(
block_on(b.get(Table::Meta, b"k")).unwrap(),
Some(b"meta".to_vec())
);
assert_eq!(
block_on(b.get(Table::Records, b"k")).unwrap(),
Some(b"record".to_vec())
);
assert_eq!(block_on(b.get(Table::Chunks, b"k")).unwrap(), None);
}
#[test]
fn reopening_creates_no_tables_and_writes_nothing() {
let dir = TempDir::new().unwrap();
{
let b = backend(&dir);
assert!(
!b.any_table_missing().unwrap(),
"the first open must have created all three tables"
);
}
let b = backend(&dir);
assert!(
!b.any_table_missing().unwrap(),
"a reopened database must not need its tables creating again"
);
}
#[test]
fn usage_reports_a_real_file_size() {
let dir = TempDir::new().unwrap();
let b = backend(&dir);
block_on(b.commit(vec![put(b"k", &vec![0u8; 10_000])])).unwrap();
let usage = block_on(b.usage()).unwrap().expect("redb reports usage");
assert!(usage.used > 0, "a file with data must report a size");
assert!(usage.persisted, "a file on disk is not evicted behind us");
assert_eq!(
usage.available, None,
"free disk space is the filesystem's business, not ours"
);
}
}