masstree 0.9.5

A high-performance concurrent ordered map (trie of B+trees)
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! Entry API for conditional key access
//!
//! Provides [`Entry`], [`OccupiedEntry`], and [`VacantEntry`] types for
//! ergonomic conditional insertion and modification patterns.

use std::fmt::{self as StdFmt, Debug, Formatter};

use seize::LocalGuard;

use crate::{MassTreeGeneric, TreeAllocator, policy::LeafPolicy, tree::RemoveError};

/// Result type for [`OccupiedEntry::try_remove_entry`].
pub type RemoveEntryResult<O> = Result<Option<(Vec<u8>, O)>, RemoveError>;

/// A view into a single entry in a tree, which may either be vacant or occupied.
///
/// # Concurrency
///
/// Classification (Occupied vs Vacant) is a point-in-time snapshot taken
/// without holding a lock. A concurrent insert or remove between
/// classification and the subsequent mutation can cause:
///
/// - `and_modify`: lost updates (computation based on stale value).
/// - `VacantEntry::insert`: silent overwrite of a concurrently inserted key.
///
/// For atomic read-modify-write, use `insert_with_guard` directly or an
/// external synchronization strategy.
///
/// # Example
///
/// ```rust
/// use masstree::MassTree;
///
/// let tree: MassTree<u64> = MassTree::new();
/// let guard = tree.guard();
///
/// // Insert if absent, get value either way
/// let _value = tree.entry_with_guard(b"counter", &guard)
///     .or_insert(0);
///
/// // Modify existing or insert default
/// let _value = tree.entry_with_guard(b"counter", &guard)
///     .and_modify(|v| v + 1)
///     .or_insert(0);
/// ```
pub enum Entry<'t, 'e, P, A>
where
    P: LeafPolicy,
    A: TreeAllocator<P>,
{
    /// An occupied entry (key exists in tree at classification time).
    Occupied(OccupiedEntry<'t, 'e, P, A>),

    /// A vacant entry (key does not exist classification time).
    Vacant(VacantEntry<'t, 'e, P, A>),
}

/// A view into an occupied entry in a tree.
pub struct OccupiedEntry<'t, 'e, P, A>
where
    P: LeafPolicy,
    A: TreeAllocator<P>,
{
    /// The key (borrowed from caller, zero allocation).
    key: &'e [u8],

    /// The current value (snapshot at entry creation time).
    value: P::Output,

    /// Reference to the tree for insertion.
    tree: &'t MassTreeGeneric<P, A>,

    /// Guard for concurrent access.
    guard: &'e LocalGuard<'t>,
}

/// A view into a vacant entry in a tree.
pub struct VacantEntry<'t, 'e, P, A>
where
    P: LeafPolicy,
    A: TreeAllocator<P>,
{
    /// The key (borrowed from caller, zero alloc)
    key: &'e [u8],

    /// Reference to the tree for insertion.
    tree: &'t MassTreeGeneric<P, A>,

    /// Guard for concurrent access.
    guard: &'e LocalGuard<'t>,
}

// ============================================================================
//  Entry Implementation
// ============================================================================

impl<'t, 'e, P, A> Entry<'t, 'e, P, A>
where
    P: LeafPolicy,
    A: TreeAllocator<P>,
{
    /// Returns the key for this entry.
    #[must_use]
    #[inline(always)]
    pub const fn key(&self) -> &[u8] {
        match self {
            Entry::Occupied(o) => o.key(),

            Entry::Vacant(v) => v.key(),
        }
    }

    /// Inserts a default value if the entry is vacant, then returns
    /// the entry's value.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use masstree::MassTree;
    /// # let tree: MassTree<u64> = MassTree::new();
    /// # let guard = tree.guard();
    /// let value = tree.entry_with_guard(b"key", &guard).or_insert(42);
    /// ```
    /// Returns the entry's value if occupied, or inserts the default and returns it.
    #[inline(always)]
    pub fn or_insert(self, default: P::Value) -> P::Output {
        match self {
            Entry::Occupied(o) => o.into_value(),
            Entry::Vacant(v) => v.insert(default),
        }
    }

    /// Returns the entry's value if occupied, or computes and inserts a default.
    #[inline(always)]
    pub fn or_insert_with<F>(self, default: F) -> P::Output
    where
        F: FnOnce() -> P::Value,
    {
        match self {
            Entry::Occupied(o) => o.into_value(),
            Entry::Vacant(v) => v.insert(default()),
        }
    }

    /// Returns the entry's value if occupied, or computes a default from the key.
    #[inline(always)]
    pub fn or_insert_with_key<F>(self, default: F) -> P::Output
    where
        F: FnOnce(&[u8]) -> P::Value,
    {
        match self {
            Entry::Occupied(o) => o.into_value(),
            Entry::Vacant(v) => {
                let value: P::Value = default(v.key());
                v.insert(value)
            }
        }
    }

    /// Inserts the default value if vacant, returns the entry's value.
    pub fn or_default(self) -> P::Output
    where
        P::Value: Default,
    {
        self.or_insert(Default::default())
    }

    /// Modifies the value if occupied using the provided function.
    ///
    /// The function receives a snapshot of the current value. Under concurrent
    /// writes, the snapshot may be stale and the resulting store can overwrite
    /// a newer value. See [`Entry`] concurrency notes.
    #[must_use]
    pub fn and_modify<F>(self, f: F) -> Self
    where
        F: FnOnce(&P::Output) -> P::Value,
    {
        match self {
            Entry::Occupied(mut o) => {
                let new_value: P::Value = f(&o.value);
                let new_output: P::Output = P::into_output(new_value);
                let return_output: P::Output = new_output.clone();

                let _old = o.tree.insert_output_with_guard(o.key, new_output, o.guard);
                o.value = return_output;

                Entry::Occupied(o)
            }

            Entry::Vacant(v) => Entry::Vacant(v),
        }
    }

    /// Inserts a value and returns an `OccupiedEntry`.
    #[inline]
    pub fn insert_entry(self, value: P::Value) -> OccupiedEntry<'t, 'e, P, A> {
        match self {
            Entry::Occupied(mut o) => {
                o.insert(value);
                o
            }

            Entry::Vacant(v) => {
                let key: &[u8] = v.key;
                let tree: &MassTreeGeneric<P, A> = v.tree;
                let guard: &LocalGuard<'_> = v.guard;

                // Convert to output before insert
                let output: P::Output = P::into_output(value);
                let return_output: P::Output = output.clone();
                let _old = tree.insert_output_with_guard(key, output, guard);

                OccupiedEntry {
                    key,
                    value: return_output,
                    tree,
                    guard,
                }
            }
        }
    }

    /// Returns a reference to the value if occupied, or `None` if vacant.
    #[must_use]
    #[inline(always)]
    pub const fn get(&self) -> Option<&P::Output> {
        match self {
            Entry::Occupied(o) => Some(o.get()),

            Entry::Vacant(_) => None,
        }
    }
}

// ============================================================================
//  OccupiedEntry Implementation
// ============================================================================

impl<P, A> OccupiedEntry<'_, '_, P, A>
where
    P: LeafPolicy,
    A: TreeAllocator<P>,
{
    /// Gets a reference to the key in the entry.
    #[must_use]
    #[inline(always)]
    pub const fn key(&self) -> &[u8] {
        self.key
    }

    /// Gets a reference to the value in the entry.
    #[inline(always)]
    pub const fn get(&self) -> &P::Output {
        &self.value
    }

    /// Converts the [`OccupiedEntry`] into the value
    #[inline(always)]
    pub fn into_value(self) -> P::Output {
        self.value
    }

    /// Sets the value of the entry, and returns the old value.
    #[inline(always)]
    pub fn insert(&mut self, value: P::Value) -> Option<P::Output> {
        let output = P::into_output(value);
        let old = self
            .tree
            .insert_output_with_guard(self.key, output.clone(), self.guard);

        self.value = output;

        old
    }

    /// Removes the entry from the tree and returns the actually removed value.
    ///
    /// # Panics
    ///
    /// Panics if removal fails (extremely rare - only on retry limit exceeded).
    /// Use [`try_remove`](Self::try_remove) for fallible operation.
    #[inline(always)]
    #[expect(
        clippy::panic,
        reason = "Convenience wrapper; use try_remove for fallible version"
    )]
    pub fn remove(self) -> Option<P::Output> {
        match self.try_remove() {
            Ok(value) => value,

            Err(e) => panic!("OccupiedEntry::remove failed: {e:?}"),
        }
    }

    /// Fallible version of [`remove`](Self::remove)
    ///
    /// # Errors
    ///
    /// Returns error if removal failed
    #[inline(always)]
    pub fn try_remove(self) -> Result<Option<P::Output>, RemoveError> {
        self.tree.remove_with_guard(self.key, self.guard)
    }

    /// Removes the entry from the tree and returns the key and removed value.
    ///
    /// # Panics
    ///
    /// Panics if removal fails. Use [`try_remove_entry`](Self::try_remove_entry)
    /// for fallible operation.
    #[inline(always)]
    #[expect(
        clippy::panic,
        reason = "Convenience wrapper; use try_remove_entry for fallible version"
    )]
    pub fn remove_entry(self) -> Option<(Vec<u8>, P::Output)> {
        match self.try_remove_entry() {
            Ok(result) => result,

            Err(e) => panic!("OccupiedEntry::remove_entry failed: {e:?}"),
        }
    }

    /// Fallible version of [`remove_entry`](Self::remove_entry).
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying tree operation encounters a failure.
    #[inline(always)]
    pub fn try_remove_entry(self) -> RemoveEntryResult<P::Output> {
        let key_owned: Vec<u8> = self.key.to_vec();

        match self.tree.remove_with_guard(self.key, self.guard)? {
            Some(value) => Ok(Some((key_owned, value))),
            None => Ok(None),
        }
    }
}

// ============================================================================
//  VacantEntry Implementation
// ============================================================================

impl<P, A> VacantEntry<'_, '_, P, A>
where
    P: LeafPolicy,
    A: TreeAllocator<P>,
{
    /// Gets a reference to the key that would be used when inserting.
    #[must_use]
    #[inline(always)]
    pub const fn key(&self) -> &[u8] {
        self.key
    }

    /// Consumes the entry and returns the key as an owned [`Vec<u8>`]
    #[must_use]
    #[inline(always)]
    pub fn into_key(self) -> Vec<u8> {
        self.key.to_vec()
    }

    /// Inserts the value into the vacant entry and returns it.
    #[inline(always)]
    pub fn insert(self, value: P::Value) -> P::Output {
        // Convert to output before insert, clone for return value
        let output = P::into_output(value);
        let return_output = output.clone();

        // Insert using internal method that accepts P::Output
        // The return value (old value if any) is ignored since we're in a VacantEntry
        let _old = self
            .tree
            .insert_output_with_guard(self.key, output, self.guard);
        return_output
    }
}

// ============================================================================
//  Debug Implementations
// ============================================================================

impl<P, A> Debug for Entry<'_, '_, P, A>
where
    P: LeafPolicy,
    P::Output: Send + Sync + Debug,
    A: TreeAllocator<P>,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> StdFmt::Result {
        match self {
            Entry::Occupied(o) => f.debug_tuple("Occupied").field(&o.value).finish(),

            Entry::Vacant(v) => f.debug_tuple("Vacant").field(&v.key).finish(),
        }
    }
}

impl<P, A> Debug for OccupiedEntry<'_, '_, P, A>
where
    P: LeafPolicy,
    P::Output: Send + Sync + Debug,
    A: TreeAllocator<P>,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> StdFmt::Result {
        f.debug_struct("OccupiedEntry")
            .field("key", &self.key)
            .field("value", &self.value)
            .finish()
    }
}

impl<P, A> Debug for VacantEntry<'_, '_, P, A>
where
    P: LeafPolicy,
    P::Output: Send + Sync + Debug,
    A: TreeAllocator<P>,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> StdFmt::Result {
        f.debug_struct("VacantEntry")
            .field("key", &self.key)
            .finish()
    }
}

// ============================================================================
//  Constructor (internal)
// ============================================================================

impl<'t, 'e, P, A> Entry<'t, 'e, P, A>
where
    P: LeafPolicy,
    A: TreeAllocator<P>,
{
    /// Create an Entry by looking up the key.
    #[inline]
    pub(crate) fn new(
        tree: &'t MassTreeGeneric<P, A>,
        key: &'e [u8],
        guard: &'e LocalGuard<'t>,
    ) -> Self {
        match tree.get_with_guard(key, guard) {
            Some(value) => Entry::Occupied(OccupiedEntry {
                key,
                value,
                tree,
                guard,
            }),
            None => Entry::Vacant(VacantEntry { key, tree, guard }),
        }
    }
}

#[cfg(test)]
mod unit_tests;