Skip to main content

redb_turbo/
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_turbo as redb;
47//! use redb::{Database, Error, ReadableTable, TableDefinition};
48//!
49//! const TABLE: TableDefinition<&str, u64> = TableDefinition::new("my_data");
50//!
51//! #[cfg(not(target_os = "wasi"))]
52//! fn main() -> Result<(), Error> {
53//!     let file = tempfile::NamedTempFile::new().unwrap();
54//!     let db = Database::create(file.path())?;
55//!     let write_txn = db.begin_write()?;
56//!     {
57//!         let mut table = write_txn.open_table(TABLE)?;
58//!         table.insert("my_key", &123)?;
59//!     }
60//!     write_txn.commit()?;
61//!
62//!     let read_txn = db.begin_read()?;
63//!     let table = read_txn.open_table(TABLE)?;
64//!     assert_eq!(table.get("my_key")?.unwrap().value(), 123);
65//!
66//!     Ok(())
67//! }
68//! ```
69//!
70//! [lmdb]: https://www.lmdb.tech/doc/
71//! [design]: https://github.com/cberner/redb/blob/master/docs/design.md
72
73pub use db::{
74    Builder, CacheStats, Database, MultimapTableDefinition, MultimapTableHandle, RepairSession,
75    StorageBackend, TableDefinition, TableHandle, UntypedMultimapTableHandle, UntypedTableHandle,
76};
77pub use error::{
78    CommitError, CompactionError, DatabaseError, Error, SavepointError, StorageError, TableError,
79    TransactionError, UpgradeError,
80};
81pub use multimap_table::{
82    MultimapRange, MultimapTable, MultimapValue, ReadOnlyMultimapTable,
83    ReadOnlyUntypedMultimapTable, ReadableMultimapTable,
84};
85pub use table::{
86    ExtractIf, Range, ReadOnlyTable, ReadOnlyUntypedTable, ReadableTable, ReadableTableMetadata,
87    Table, TableStats,
88};
89pub use transactions::{DatabaseStats, Durability, ReadTransaction, WriteTransaction};
90pub use tree_store::{AccessGuard, AccessGuardMut, Savepoint};
91pub use types::{Key, MutInPlaceValue, TypeName, Value};
92
93pub type Result<T = (), E = StorageError> = std::result::Result<T, E>;
94
95#[cfg(feature = "python")]
96pub use crate::python::redb;
97
98pub mod backends;
99mod complex_types;
100mod db;
101mod error;
102mod multimap_table;
103pub mod page_crypto;
104#[cfg(feature = "python")]
105mod python;
106mod sealed;
107mod table;
108mod transaction_tracker;
109mod transactions;
110mod tree_store;
111mod tuple_types;
112mod types;
113
114// Re-export page crypto and compression types
115pub use page_crypto::{
116    Aes256GcmPageCrypto, DictionaryTrainer, NoOpPageCompression, NoOpPageCrypto, PageCompression,
117    PageCrypto, ZstdDictPageCompression, ZstdPageCompression,
118};
119
120#[cfg(test)]
121fn create_tempfile() -> tempfile::NamedTempFile {
122    if cfg!(target_os = "wasi") {
123        tempfile::NamedTempFile::new_in("/tmp").unwrap()
124    } else {
125        tempfile::NamedTempFile::new().unwrap()
126    }
127}