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