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