emdb 0.7.1

A lightweight, high-performance embedded database for Rust.
Documentation
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Copyright 2026 James Gober. Licensed under Apache-2.0.

//! In-memory sharded hash index. Maps `(ns_id, key)` → file offset of
//! the most recent live record for that key.
//!
//! Uses 64 shards with FxHash + an [`IdentityHasher`] over the precomputed
//! 64-bit hash so the per-op work is a single mod-64 + one HashMap probe.
//! No allocations on the hot lookup path; insert allocates only on
//! collision (when the same hash maps to multiple distinct keys).
//!
//! The index does **not** store the keys themselves — it stores file
//! offsets and lets the storage layer resolve hash collisions by reading
//! the on-disk record's key. This keeps RAM use proportional to the
//! number of records, not their total size.

use std::collections::HashMap;
use std::hash::{BuildHasherDefault, Hasher};
use std::sync::RwLock;

use crate::{Error, Result};

/// Number of shards. Power of two so the shard selector is a bitmask.
const SHARDS: usize = 64;
const SHARD_MASK: u64 = (SHARDS as u64) - 1;

/// `Hasher` that returns the input `u64` unchanged. Saves the cost of
/// re-hashing a key that already arrived as a 64-bit hash from FxHash.
#[derive(Default)]
pub(crate) struct IdentityHasher(u64);

impl Hasher for IdentityHasher {
    #[inline]
    fn finish(&self) -> u64 {
        self.0
    }

    #[inline]
    fn write(&mut self, _bytes: &[u8]) {
        // The HashMap dispatches via `write_u64` for u64 keys; this
        // path should not be exercised. Be defensive: leave the state
        // unchanged so a stray caller gets a deterministic (but
        // useless) hash rather than a panic.
    }

    #[inline]
    fn write_u64(&mut self, value: u64) {
        self.0 = value;
    }
}

type IdentityBuildHasher = BuildHasherDefault<IdentityHasher>;
type ShardMap = HashMap<u64, Slot, IdentityBuildHasher>;

/// Per-shard slot. `Single` is the common case; `Multi` handles 64-bit
/// hash collisions (extremely rare for typical workloads but must be
/// correct when they happen). Each `Multi` entry carries the key bytes
/// so the storage layer can disambiguate without re-reading the file.
#[derive(Debug, Clone)]
enum Slot {
    /// Single offset for this hash. Hot-path: ~99.99...% of slots.
    Single(u64),
    /// Multiple `(key, offset)` pairs that collided to the same 64-bit
    /// hash. Disambiguated by exact key compare on lookup.
    Multi(Vec<(Vec<u8>, u64)>),
}

/// FxHash-port for keys. Good enough avalanche for short strings, much
/// faster than SipHash. The actual FxHash impl lives in
/// [`Self::hash_key`]; this is just the type alias.
pub(crate) type KeyHash = u64;

/// Sharded index. One per namespace.
#[derive(Debug)]
pub(crate) struct Index {
    shards: Box<[RwLock<ShardMap>; SHARDS]>,
}

impl Default for Index {
    fn default() -> Self {
        Self::new()
    }
}

impl Index {
    /// Construct an empty index with all shards initialised.
    #[must_use]
    pub(crate) fn new() -> Self {
        let shards: [RwLock<ShardMap>; SHARDS] =
            std::array::from_fn(|_| RwLock::new(ShardMap::default()));
        Self {
            shards: Box::new(shards),
        }
    }

    /// Compute the FxHash of a key. Same algorithm as
    /// `rustc-hash` / Firefox's hasher; ~2-3x faster than SipHash for
    /// short keys, with adequate avalanche for hash-table use.
    #[inline]
    #[must_use]
    pub(crate) fn hash_key(key: &[u8]) -> KeyHash {
        const SEED: u64 = 0x51_7c_c1_b7_27_22_0a_95;
        const ROTATE: u32 = 5;

        let mut hash = 0_u64;
        let mut bytes = key;

        while bytes.len() >= 8 {
            let mut block = [0_u8; 8];
            block.copy_from_slice(&bytes[..8]);
            let word = u64::from_le_bytes(block);
            hash = (hash.rotate_left(ROTATE) ^ word).wrapping_mul(SEED);
            bytes = &bytes[8..];
        }
        if bytes.len() >= 4 {
            let mut block = [0_u8; 4];
            block.copy_from_slice(&bytes[..4]);
            let word = u32::from_le_bytes(block) as u64;
            hash = (hash.rotate_left(ROTATE) ^ word).wrapping_mul(SEED);
            bytes = &bytes[4..];
        }
        for &b in bytes {
            hash = (hash.rotate_left(ROTATE) ^ (b as u64)).wrapping_mul(SEED);
        }
        hash
    }

    /// Look up the offset for `key`. Returns `Ok(None)` for missing keys.
    ///
    /// `resolve_collision` is called only when the hash slot holds
    /// multiple entries; it should compare each candidate key against
    /// the user's key and return the matching offset (or `None`).
    /// Single-entry hits skip the callback entirely.
    ///
    /// # Errors
    ///
    /// Returns [`Error::LockPoisoned`] when a shard's `RwLock` was
    /// poisoned.
    pub(crate) fn get(&self, hash: KeyHash, key: &[u8]) -> Result<Option<u64>> {
        let shard_idx = (hash & SHARD_MASK) as usize;
        let shard = self.shards[shard_idx]
            .read()
            .map_err(|_poisoned| Error::LockPoisoned)?;
        match shard.get(&hash) {
            None => Ok(None),
            Some(Slot::Single(offset)) => Ok(Some(*offset)),
            Some(Slot::Multi(entries)) => {
                for (k, off) in entries {
                    if k.as_slice() == key {
                        return Ok(Some(*off));
                    }
                }
                Ok(None)
            }
        }
    }

    /// Replace the offset for `key`, with a resolver callback that the
    /// index uses to disambiguate hash collisions. The resolver is
    /// invoked only when an existing `Single` slot needs to be checked
    /// against the new key — it should return the key bytes stored at
    /// the supplied offset (i.e., decode the on-disk record at that
    /// offset and return its key).
    ///
    /// Behaviour:
    /// - Empty slot → `Single(offset)`, returns `Ok(None)`.
    /// - `Single(existing_offset)` and resolver says key matches →
    ///   in-place update, returns `Ok(Some(existing_offset))`.
    /// - `Single(existing_offset)` and resolver says key differs →
    ///   promote to `Multi` carrying both `(existing_key, existing_offset)`
    ///   and `(new_key, offset)`. Returns `Ok(None)`.
    /// - `Multi(_)` → linear scan; matching key is replaced, otherwise
    ///   the new entry is pushed.
    ///
    /// # Errors
    ///
    /// Returns [`Error::LockPoisoned`] on poisoned shard lock, or any
    /// error returned by `resolve_existing`.
    pub(crate) fn replace<F>(
        &self,
        hash: KeyHash,
        key: &[u8],
        offset: u64,
        mut resolve_existing: F,
    ) -> Result<Option<u64>>
    where
        F: FnMut(u64) -> Result<Option<Vec<u8>>>,
    {
        let shard_idx = (hash & SHARD_MASK) as usize;
        let mut shard = self.shards[shard_idx]
            .write()
            .map_err(|_poisoned| Error::LockPoisoned)?;
        match shard.get_mut(&hash) {
            None => {
                let _prev = shard.insert(hash, Slot::Single(offset));
                Ok(None)
            }
            Some(slot) => match slot {
                Slot::Single(existing_offset) => {
                    let existing = *existing_offset;
                    // Resolve the existing offset's key. If unresolvable
                    // (record gone / decode failed), treat as a stale
                    // entry and overwrite in place.
                    match resolve_existing(existing)? {
                        Some(existing_key) if existing_key.as_slice() == key => {
                            *existing_offset = offset;
                            Ok(Some(existing))
                        }
                        Some(existing_key) => {
                            *slot =
                                Slot::Multi(vec![(existing_key, existing), (key.to_vec(), offset)]);
                            Ok(None)
                        }
                        None => {
                            *existing_offset = offset;
                            Ok(Some(existing))
                        }
                    }
                }
                Slot::Multi(entries) => {
                    for entry in entries.iter_mut() {
                        if entry.0.as_slice() == key {
                            let prev = entry.1;
                            entry.1 = offset;
                            return Ok(Some(prev));
                        }
                    }
                    entries.push((key.to_vec(), offset));
                    Ok(None)
                }
            },
        }
    }

    /// Remove the entry for `key`. Returns the previous offset if any.
    ///
    /// # Errors
    ///
    /// Returns [`Error::LockPoisoned`] on poisoned shard lock.
    pub(crate) fn remove(&self, hash: KeyHash, key: &[u8]) -> Result<Option<u64>> {
        let shard_idx = (hash & SHARD_MASK) as usize;
        let mut shard = self.shards[shard_idx]
            .write()
            .map_err(|_poisoned| Error::LockPoisoned)?;
        match shard.get_mut(&hash) {
            None => Ok(None),
            Some(slot) => match slot {
                Slot::Single(offset) => {
                    let prev = *offset;
                    let _removed = shard.remove(&hash);
                    Ok(Some(prev))
                }
                Slot::Multi(entries) => {
                    let mut matched: Option<u64> = None;
                    entries.retain(|(k, off)| {
                        if matched.is_none() && k.as_slice() == key {
                            matched = Some(*off);
                            false
                        } else {
                            true
                        }
                    });
                    if entries.is_empty() {
                        let _removed = shard.remove(&hash);
                    } else if entries.len() == 1 {
                        let (_k, off) = entries[0].clone();
                        *slot = Slot::Single(off);
                    }
                    Ok(matched)
                }
            },
        }
    }

    /// Total live entry count across every shard. O(shards).
    ///
    /// # Errors
    ///
    /// Returns [`Error::LockPoisoned`] on poisoned shard lock.
    pub(crate) fn len(&self) -> Result<usize> {
        let mut total = 0;
        for shard in self.shards.iter() {
            let guard = shard.read().map_err(|_poisoned| Error::LockPoisoned)?;
            for slot in guard.values() {
                total += match slot {
                    Slot::Single(_) => 1,
                    Slot::Multi(entries) => entries.len(),
                };
            }
        }
        Ok(total)
    }

    /// Drop every entry. O(total).
    ///
    /// # Errors
    ///
    /// Returns [`Error::LockPoisoned`] on poisoned shard lock.
    pub(crate) fn clear(&self) -> Result<()> {
        for shard in self.shards.iter() {
            let mut guard = shard.write().map_err(|_poisoned| Error::LockPoisoned)?;
            guard.clear();
        }
        Ok(())
    }

    /// Iterate every (key, offset) pair across every shard. The keys
    /// are reconstructed from the engine's record reads — this iter
    /// only emits offsets, and the engine resolves them to keys.
    ///
    /// # Errors
    ///
    /// Returns [`Error::LockPoisoned`] on poisoned shard lock.
    pub(crate) fn collect_offsets(&self) -> Result<Vec<u64>> {
        let mut out = Vec::new();
        for shard in self.shards.iter() {
            let guard = shard.read().map_err(|_poisoned| Error::LockPoisoned)?;
            for slot in guard.values() {
                match slot {
                    Slot::Single(off) => out.push(*off),
                    Slot::Multi(entries) => {
                        for (_, off) in entries {
                            out.push(*off);
                        }
                    }
                }
            }
        }
        Ok(out)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Test resolver that always reports the slot's existing key as
    /// the supplied `existing` for the test setup. Tests construct a
    /// closure-shaped resolver via [`make_resolver`].
    fn no_resolver(_offset: u64) -> Result<Option<Vec<u8>>> {
        Ok(None)
    }

    #[test]
    fn insert_and_get_round_trip() {
        let idx = Index::new();
        let h = Index::hash_key(b"alpha");
        assert!(idx
            .replace(h, b"alpha", 100, no_resolver)
            .unwrap()
            .is_none());
        assert_eq!(idx.get(h, b"alpha").unwrap(), Some(100));
    }

    #[test]
    fn replace_returns_previous_offset() {
        let idx = Index::new();
        let h = Index::hash_key(b"alpha");
        let _ = idx.replace(h, b"alpha", 100, no_resolver).unwrap();
        // Resolver returns the existing key so replace updates in-place.
        let resolver = |_off: u64| Ok(Some(b"alpha".to_vec()));
        let prev = idx.replace(h, b"alpha", 200, resolver).unwrap();
        assert_eq!(prev, Some(100));
        assert_eq!(idx.get(h, b"alpha").unwrap(), Some(200));
    }

    #[test]
    fn remove_drops_entry() {
        let idx = Index::new();
        let h = Index::hash_key(b"alpha");
        let _ = idx.replace(h, b"alpha", 100, no_resolver).unwrap();
        let prev = idx.remove(h, b"alpha").unwrap();
        assert_eq!(prev, Some(100));
        assert_eq!(idx.get(h, b"alpha").unwrap(), None);
    }

    #[test]
    fn hash_collision_disambiguates_by_key() {
        let idx = Index::new();
        // Force a collision by inserting two distinct keys at the same hash.
        // First insert lands as Single (no existing slot).
        let _ = idx.replace(42, b"first", 100, no_resolver).unwrap();
        // Second insert sees the Single and the resolver supplies the
        // existing key as "first" — the index promotes to Multi with both.
        let resolver = |_off: u64| Ok(Some(b"first".to_vec()));
        let _ = idx.replace(42, b"second", 200, resolver).unwrap();
        assert_eq!(idx.get(42, b"first").unwrap(), Some(100));
        assert_eq!(idx.get(42, b"second").unwrap(), Some(200));
        assert_eq!(idx.get(42, b"third").unwrap(), None);
    }

    #[test]
    fn len_reflects_total_entries_across_shards() {
        let idx = Index::new();
        for i in 0_u32..200 {
            let key = format!("k{i:04}");
            let h = Index::hash_key(key.as_bytes());
            let _ = idx
                .replace(h, key.as_bytes(), i as u64, no_resolver)
                .unwrap();
        }
        assert_eq!(idx.len().unwrap(), 200);
    }

    #[test]
    fn clear_empties_every_shard() {
        let idx = Index::new();
        for i in 0_u32..50 {
            let key = format!("k{i}");
            let h = Index::hash_key(key.as_bytes());
            let _ = idx
                .replace(h, key.as_bytes(), i as u64, no_resolver)
                .unwrap();
        }
        idx.clear().unwrap();
        assert_eq!(idx.len().unwrap(), 0);
    }

    #[test]
    fn fxhash_is_deterministic() {
        let h1 = Index::hash_key(b"deterministic");
        let h2 = Index::hash_key(b"deterministic");
        assert_eq!(h1, h2);
        let h3 = Index::hash_key(b"different");
        assert_ne!(h1, h3);
    }
}