redb/
lib.rs

1#![deny(clippy::all, clippy::pedantic, clippy::disallowed_methods)]
2// TODO: revisit this list and see if we can enable some
3// TODO: we should enable result_large_err for perf reasons
4#![allow(
5    let_underscore_drop,
6    clippy::default_trait_access,
7    clippy::if_not_else,
8    clippy::inline_always,
9    clippy::iter_not_returning_iterator,
10    clippy::manual_let_else,
11    clippy::missing_errors_doc,
12    clippy::missing_panics_doc,
13    clippy::module_name_repetitions,
14    clippy::must_use_candidate,
15    clippy::needless_pass_by_value,
16    clippy::option_option,
17    clippy::redundant_closure_for_method_calls,
18    clippy::result_large_err,
19    clippy::similar_names,
20    clippy::too_many_lines,
21    clippy::unnecessary_wraps,
22    clippy::unreadable_literal,
23    clippy::wildcard_imports
24)]
25// TODO remove this once wasi no longer requires nightly
26#![cfg_attr(target_os = "wasi", feature(wasi_ext))]
27
28//! # redb
29//!
30//! A simple, portable, high-performance, ACID, embedded key-value store.
31//!
32//! redb is written in pure Rust and is loosely inspired by [lmdb][lmdb]. Data is stored in a collection
33//! of copy-on-write B-trees. For more details, see the [design doc][design].
34//!
35//! # Features
36//!
37//! - Zero-copy, thread-safe, `BTreeMap` based API
38//! - Fully ACID-compliant transactions
39//! - MVCC support for concurrent readers & writer, without blocking
40//! - Crash-safe by default
41//! - Savepoints and rollbacks
42//!
43//! # Example
44//!
45//! ```
46//! use redb::{Database, Error, ReadableTable, TableDefinition};
47//!
48//! const TABLE: TableDefinition<&str, u64> = TableDefinition::new("my_data");
49//!
50//! #[cfg(not(target_os = "wasi"))]
51//! fn main() -> Result<(), Error> {
52//!     let file = tempfile::NamedTempFile::new().unwrap();
53//!     let db = Database::create(file.path())?;
54//!     let write_txn = db.begin_write()?;
55//!     {
56//!         let mut table = write_txn.open_table(TABLE)?;
57//!         table.insert("my_key", &123)?;
58//!     }
59//!     write_txn.commit()?;
60//!
61//!     let read_txn = db.begin_read()?;
62//!     let table = read_txn.open_table(TABLE)?;
63//!     assert_eq!(table.get("my_key")?.unwrap().value(), 123);
64//!
65//!     Ok(())
66//! }
67//! ```
68//!
69//! [lmdb]: https://www.lmdb.tech/doc/
70//! [design]: https://github.com/cberner/redb/blob/master/docs/design.md
71
72pub use db::{
73    Builder, CacheStats, Database, MultimapTableDefinition, MultimapTableHandle, RepairSession,
74    StorageBackend, TableDefinition, TableHandle, UntypedMultimapTableHandle, UntypedTableHandle,
75};
76pub use error::{
77    CommitError, CompactionError, DatabaseError, Error, SavepointError, StorageError, TableError,
78    TransactionError, UpgradeError,
79};
80pub use multimap_table::{
81    MultimapRange, MultimapTable, MultimapValue, ReadOnlyMultimapTable,
82    ReadOnlyUntypedMultimapTable, ReadableMultimapTable,
83};
84pub use table::{
85    ExtractIf, Range, ReadOnlyTable, ReadOnlyUntypedTable, ReadableTable, ReadableTableMetadata,
86    Table, TableStats,
87};
88pub use transactions::{DatabaseStats, Durability, ReadTransaction, WriteTransaction};
89pub use tree_store::{AccessGuard, AccessGuardMut, Savepoint};
90pub use types::{Key, MutInPlaceValue, TypeName, Value};
91
92pub type Result<T = (), E = StorageError> = std::result::Result<T, E>;
93
94#[cfg(feature = "python")]
95pub use crate::python::redb;
96
97pub mod backends;
98mod complex_types;
99mod db;
100mod error;
101mod multimap_table;
102#[cfg(feature = "python")]
103mod python;
104mod sealed;
105mod table;
106mod transaction_tracker;
107mod transactions;
108mod tree_store;
109mod tuple_types;
110mod types;
111
112#[cfg(test)]
113fn create_tempfile() -> tempfile::NamedTempFile {
114    if cfg!(target_os = "wasi") {
115        tempfile::NamedTempFile::new_in("/tmp").unwrap()
116    } else {
117        tempfile::NamedTempFile::new().unwrap()
118    }
119}