1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//! Idiomatic and safe Rust bindings for [libmdbx].
//!
//! # Overview
//!
//! [libmdbx] is a high-performance embedded key-value database based on
//! LMDB, with additional features like nested transactions, automatic
//! compaction, and improved durability options.
//!
//! This crate provides a safe, idiomatic Rust interface for:
//! - Creating and managing memory-mapped database environments
//! - Performing transactional read and write operations
//! - Iterating over key-value pairs with cursors
//! - Custom deserialization via the [`TableObject`] trait
//!
//! # Quick Start
//!
//! Databases are stored in a directory on disk. The following example
//! demonstrates creating an environment, writing a key-value pair, and
//! reading it back.
//!
//! ```no_run
//! use signet_libmdbx::{
//! Environment, DatabaseFlags, WriteFlags, Geometry, MdbxResult,
//! };
//! use std::path::Path;
//!
//! fn main() -> MdbxResult<()> {
//! // Open an environment (creates directory if needed)
//! let env = Environment::builder()
//! .set_geometry(Geometry {
//! size: Some(0..(1024 * 1024 * 1024)), // up to 1GB
//! ..Default::default()
//! })
//! .open(Path::new("/tmp/my_database"))?;
//!
//! // Write data in a read-write transaction
//! let txn = env.begin_rw_sync()?;
//! let db = txn.create_db(None, DatabaseFlags::empty())?;
//! txn.put(db, b"hello", b"world", WriteFlags::empty())?;
//! txn.commit()?;
//!
//! // Read data in a read-only transaction
//! let txn = env.begin_ro_sync()?;
//! let db = txn.open_db(None)?;
//! let value: Option<Vec<u8>> = txn.get(db.dbi(), b"hello").expect("read failed");
//! assert_eq!(value.as_deref(), Some(b"world".as_slice()));
//!
//! Ok(())
//! }
//! ```
//! # Imports
//!
//! For most use cases, import from the crate root:
//! ```rust,ignore
//! use signet_libmdbx::{Environment, DatabaseFlags, WriteFlags, Geometry, MdbxResult};
//! ```
//!
//! Transaction and cursor types are returned from `Environment` and transaction
//! methods - you rarely need to import them directly.
//!
//! For advanced usage, import from submodules:
//! - [`tx`] - Transaction type aliases (`RoTxSync`, `RwTxUnsync`, etc.) and
//! cursor type aliases
//! - [`tx::iter`] - Iterator types for cursor iteration
//! - [`sys`] - Environment internals (`EnvironmentKind`, `PageSize`, etc.)
//!
//! # Key Concepts
//!
//! - [`Environment`] - A directory containing one or more databases. Created
//! via [`Environment::builder()`].
//! - [`TxSync`] and [`TxUnsync`] - Transactions for performing database
//! operations.
//! - Synchronized transactions (`TxSync`) can be shared between
//! threads.
//! - Unsynchronized transactions (`TxUnsync`) offer better
//! performance for single-threaded use cases.
//! - [`Ro`] and [`Rw`] - Marker types indicating read-only (`Ro`) or
//! read-write (`Rw`) transactions.
//! - These also exist in sync flavors: [`RoSync`] and [`RwSync`].
//! - [`Database`] - A named or unnamed key-value store within an environment.
//! - Accessed with [`Tx::open_db`].
//! - or created via [`Tx::create_db`].
//! - [`Cursor`]: Enables iteration and positioned access within a database.
//! Created via [`TxSync::cursor()`] or [`TxUnsync::cursor()`].
//!
//! [`Tx::open_db`]: crate::tx::Tx::open_db
//! [`Tx::create_db`]: crate::tx::Tx::create_db
//!
//! # Cursor Iterators
//!
//! Cursors provide several iterator types for traversing databases. The
//! iterator to use depends on your database flags and access pattern.
//!
//! | Iterator | Cursor Methods | Yields | Description |
//! |----------|----------------|--------|-------------|
//! | [`Iter`] | `iter_start`, `iter_from` | `(Key, Value)` | Forward iteration over all key-value pairs. |
//! | [`IterDup`] | `iter_dup_start`, `iter_dup_from` | [`DupItem`] | Flat iteration over DUPSORT tables. Yields `NewKey` for first value of each key, `SameKey` for subsequent. |
//! | [`IterDupOfKey`] | `iter_dup_of` | `Value` | Single-key iteration over DUPSORT duplicate values. |
//! | [`IterDupFixed`] | `iter_dupfixed_start`, `iter_dupfixed_from` | [`DupItem`] | Flat iteration over DUPFIXED tables using page-based access. |
//! | [`IterDupFixedOfKey`] | `iter_dupfixed_of` | `Value` | Single-key iteration over DUPFIXED values. Exact `size_hint()`. |
//!
//! [`Iter`]: crate::tx::iter::Iter
//! [`IterDup`]: crate::tx::iter::IterDup
//! [`IterDupOfKey`]: crate::tx::iter::IterDupOfKey
//! [`IterDupFixed`]: crate::tx::iter::IterDupFixed
//! [`IterDupFixedOfKey`]: crate::tx::iter::IterDupFixedOfKey
//! [`DupItem`]: crate::tx::iter::DupItem
//!
//! # Custom Zero-copy Deserialization with [`TableObject`]
//!
//! Implement [`TableObject`] to decode custom types directly from the
//! database:
//!
//! ```
//! # use std::borrow::Cow;
//! use signet_libmdbx::{TableObject, ReadResult, MdbxError};
//!
//! struct MyKey([u8; 32]);
//!
//! impl TableObject<'_> for MyKey {
//! fn decode_borrow(data: Cow<'_, [u8]>) -> ReadResult<Self> {
//! let arr: [u8; 32] = data.as_ref().try_into()
//! .map_err(|_| MdbxError::DecodeErrorLenDiff)?;
//! Ok(Self(arr))
//! }
//! }
//! ```
//!
//! See the [`TableObject`] docs for more examples.
//!
//! # Debug assertions
//!
//! When compiled with debug assertions enabled (the default for
//! `cargo build`), this crate performs additional runtime checks to
//! catch common mistakes.
//!
//! 1. Key sizes are checked against the database's configured
//! `pagesize` and `DatabaseFlags` (e.g. `INTEGERKEY`).
//! 2. Value sizes are checked against the database's configured
//! `pagesize` and `DatabaseFlags` (e.g. `INTEGERDUP`).
//! 3. For `append` operations, it checks that the key being appended is
//! greater than the current last key using lexicographic comparison.
//! This check is skipped for `REVERSE_KEY` and `REVERSE_DUP` databases
//! since they use different comparison semantics (comparing bytes from
//! end to beginning).
//!
//! # Provenance
//!
//! Forked from [reth-libmdbx], which was forked from an earlier Apache
//! licensed version of the `libmdbx-rs` crate. Original LMDB bindings from
//! [lmdb-rs].
//!
//! [libmdbx]: https://github.com/erthink/libmdbx
//! [reth-libmdbx]: https://github.com/paradigmxyz/reth
//! [lmdb-rs]: https://github.com/mozilla/lmdb-rs
pub extern crate signet_mdbx_sys as ffi;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use DupItem;
pub use ;