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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright 2026 James Gober.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//! # emdb
//!
//! A high-performance embedded key-value database for Rust.
//!
//! ## Architecture
//!
//! emdb is an **fsys-journal-backed append-only KV** with a sharded
//! in-memory hash index. Writes go through `fsys::JournalHandle`'s
//! lock-free LSN reservation + group-commit fsync; reads slice
//! directly into a kernel-managed memory map of the same file
//! (zero-copy). Crash safety is delegated to fsys's CRC-32C frame
//! validation and five-state tail-truncation taxonomy.
//!
//! This is the Bitcask family of storage engines (one append-only
//! log + an in-memory index), built on top of fsys for the
//! filesystem substrate. fsys handles platform-specific durability
//! (NVMe passthrough flush on Linux + Windows, io_uring on Linux,
//! `WRITE_THROUGH` where appropriate); emdb handles the
//! engine-level concerns (per-namespace sharded indices,
//! encryption, range scans, TTL).
//!
//! **Reads** are lock-free — the 64-shard primary index plus the
//! `Arc<Mmap>` zero-copy read path scale to many millions of
//! operations per second on a single open handle. **Writes** are
//! lock-free via fsys's atomic LSN reservation; no writer mutex
//! on the hot append path. Producers can still batch through
//! [`Emdb::insert_many`] or [`Emdb::transaction`] when group
//! semantics matter.
//!
//! ## Quick start
//!
//! ```rust
//! use emdb::Emdb;
//!
//! let db = Emdb::open_in_memory();
//! db.insert("name", "emdb")?;
//! assert_eq!(db.get("name")?, Some(b"emdb".to_vec()));
//! # Ok::<(), emdb::Error>(())
//! ```
//!
//! Persistent file-backed:
//!
//! ```no_run
//! use emdb::Emdb;
//!
//! let path = std::env::temp_dir().join("emdb-doc-example.emdb");
//! {
//! let db = Emdb::open(&path)?;
//! db.insert("name", "emdb")?;
//! db.flush()?; // make record bytes durable
//! db.checkpoint()?; // persist tail_hint for fast reopen
//! }
//! let db = Emdb::open(&path)?;
//! assert_eq!(db.get("name")?, Some(b"emdb".to_vec()));
//! # let _cleanup = std::fs::remove_file(path);
//! # Ok::<(), emdb::Error>(())
//! ```
//!
//! TTL:
//!
//! ```no_run
//! # #[cfg(feature = "ttl")]
//! # {
//! use std::time::Duration;
//!
//! use emdb::{Emdb, Ttl};
//!
//! let path = std::env::temp_dir().join("emdb-doc-ttl.emdb");
//! let db = Emdb::builder()
//! .path(&path)
//! .default_ttl(Duration::from_secs(60))
//! .build()?;
//! db.insert_with_ttl("session", "token", Ttl::Default)?;
//! assert!(db.ttl("session")?.is_some());
//! # let _cleanup = std::fs::remove_file(path);
//! # }
//! # Ok::<(), emdb::Error>(())
//! ```
//!
//! ## Zero-copy reads
//!
//! [`Emdb::get_zerocopy`] returns a [`ValueRef`] that points directly
//! into the kernel-managed mmap region — no allocation, no copy.
//! Encrypted databases fall back to an owned plaintext buffer inside
//! the same [`ValueRef`] type.
//!
//! ```rust
//! use emdb::Emdb;
//!
//! let db = Emdb::open_in_memory();
//! db.insert("k", "v")?;
//! if let Some(v) = db.get_zerocopy("k")? {
//! let want: &[u8] = b"v";
//! assert!(v == want);
//! }
//! # Ok::<(), emdb::Error>(())
//! ```
//!
//! ## Streaming iteration
//!
//! [`Emdb::iter`] / [`Emdb::keys`] yield records lazily, decoding one
//! record per `next()` call from a snapshot of offsets captured at
//! construction time. Memory use scales with the offset count, not
//! the total value size.
//!
//! Range queries are opt-in via
//! [`EmdbBuilder::enable_range_scans`]; once enabled,
//! [`Emdb::range_iter`] / [`Emdb::range_prefix_iter`] return streaming
//! iterators backed by a lock-free `crossbeam_skiplist::SkipMap`
//! secondary index — inserts and range scans run concurrently
//! without a global lock.
//!
//! ## Group-commit durability
//!
//! Per-record `flush()` workloads with concurrent writers can opt
//! into the group-commit pipeline so multiple in-flight `flush()`
//! calls share a single `fdatasync`:
//!
//! ```no_run
//! use emdb::{Emdb, FlushPolicy};
//!
//! let db = Emdb::builder()
//! .flush_policy(FlushPolicy::Group)
//! .build()?;
//! # Ok::<(), emdb::Error>(())
//! ```
//!
//! Default policy is [`FlushPolicy::OnEachFlush`], which performs one
//! `fdatasync` per call — the right choice when there is only one
//! writer thread or when durability is already batched at the
//! application layer.
//!
//! ## Storage path resolution
//!
//! emdb does not pick a default path for you. You either pass an
//! explicit path, or opt into OS-aware resolution via the builder.
//!
//! ```no_run
//! use emdb::Emdb;
//!
//! // Resolves to:
//! // Linux: $XDG_DATA_HOME/hivedb-kv/sessions.emdb
//! // macOS: ~/Library/Application Support/hivedb-kv/sessions.emdb
//! // Windows: %LOCALAPPDATA%\hivedb-kv\sessions.emdb
//! let db = Emdb::builder()
//! .app_name("hivedb-kv")
//! .database_name("sessions.emdb")
//! .build()?;
//! # Ok::<(), emdb::Error>(())
//! ```
//!
//! ## Operational APIs
//!
//! - [`Emdb::stats`] — point-in-time database introspection
//! (record counts, file size, namespace count). Cheap to call
//! from a per-second health-check loop.
//! - [`Emdb::backup_to`] — atomic snapshot to a sibling file. The
//! result is a normal openable database, not a dump format.
//! - [`Emdb::lock_holder`] / [`Emdb::break_lock`] — diagnose and
//! recover from stuck advisory lockfiles when a holder dies
//! without releasing.
//! - [`Emdb::checkpoint`] — explicit fast-reopen checkpoint that
//! persists the file header's `tail_hint`.
//!
//! ## Async surface
//!
//! Opt-in via the `async` feature. Wraps the sync API in
//! `tokio::task::spawn_blocking` so blocking I/O never stalls the
//! async-task scheduler. Exposes `AsyncEmdb` and `AsyncNamespace`,
//! plus `EmdbBuilder::build_async` for the builder path.
//!
//! ```ignore
//! # // gated behind `async` feature
//! use emdb::{AsyncEmdb, Emdb};
//!
//! # async fn ex() -> Result<(), emdb::Error> {
//! // Open via the simple constructor.
//! let db = AsyncEmdb::open("/tmp/users.emdb").await?;
//! db.insert("alice", "active").await?;
//! let value = db.get("alice").await?;
//!
//! // Or build with explicit configuration.
//! let configured = Emdb::builder()
//! .path("/tmp/configured.emdb")
//! .enable_range_scans(true)
//! .build_async()
//! .await?;
//! # let _ = (db, configured); Ok(())
//! # }
//! ```
//!
//! Every async method clones the underlying `Arc<Emdb>` into a
//! `spawn_blocking` closure; cheap, but each call allocates owned
//! `Vec<u8>` copies for key/value bytes so the closure can take
//! them by value. For latency-sensitive workloads where the
//! spawn dispatch overhead exceeds the sync cost (e.g. tight
//! `get` loops on a hot in-memory key), reach for the sync
//! handle via `AsyncEmdb::sync_handle` and batch via
//! `insert_many` / `range`.
//!
//! Large iterations come in two flavours. `iter` / `keys` / `range`
//! / `range_prefix` / `iter_from` / `iter_after` materialise the
//! full result into an owned `Vec` before resolving — convenient
//! for small queries. The `*_stream` variants
//! (`iter_stream`, `keys_stream`, `range_stream`,
//! `range_prefix_stream`, `iter_from_stream`, `iter_after_stream`)
//! return a `tokio_stream::wrappers::ReceiverStream` backed by a
//! bounded mpsc channel: records arrive incrementally, the
//! blocking pump task respects the consumer's backpressure, and
//! memory in flight is bounded by the channel depth rather than
//! the namespace size.
//!
//! ## Cargo features
//!
//! - `ttl` *(default)* — per-record expiration and `default_ttl`.
//! - `nested` — dotted-prefix group operations and `Focus` handles.
//! - `encrypt` — AES-256-GCM + ChaCha20-Poly1305 at-rest encryption
//! with raw-key or Argon2id-derived passphrase.
//! - `async` — `AsyncEmdb` / `AsyncNamespace` wrappers via
//! tokio's `spawn_blocking`, plus streaming-iterator variants
//! backed by `tokio_stream::wrappers::ReceiverStream`. Pulls in
//! `tokio` (`rt` + `rt-multi-thread` + `macros` + `sync`) and
//! `tokio-stream`.
//! - `bench-compare`, `bench-rocksdb`, `bench-redis` — comparative
//! bench peers (dev-only, never required by application builds).
// Test code is allowed to use the convenience panickers — the strict
// lint profile above is for production library code, not assertion
// scaffolding inside `#[cfg(test)] mod tests` blocks.
pub use ;
pub use EmdbBuilder;
pub use ;
pub use ;
pub use ;
pub use LockHolder;
pub use ;
pub use Focus;
pub use EmdbStats;
pub use FlushPolicy;
pub use Transaction;
pub use Ttl;
pub use ValueRef;