nitrite 0.4.0

An embedded NoSQL document database for Rust with collections, repositories, indexing, and ACID transactions
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
use crate::common::{AttributeAware, Key, Value};
use crate::errors::NitriteResult;
use crate::store::iters::{
    EntryIterator, KeyIterator, ValueIterator,
};
use crate::store::NitriteStore;
use std::fmt::Debug;
use std::iter::Rev;
use std::ops::Deref;
use std::sync::Arc;

/// Low-level interface for key-value store implementations in Nitrite.
///
/// # Purpose
/// Defines the contract that all key-value store backends must implement.
/// Implementers provide concrete storage operations for Nitrite, such as in-memory
/// or persistent storage via adapters like ReDB or Fjall.
///
/// # Key Methods
/// - **Basic Operations**: `put()`, `get()`, `remove()`, `contains_key()`
/// - **Bulk Operations**: `put_all()` for atomic batch inserts
/// - **Iteration**: `keys()`, `values()`, `entries()` with bidirectional traversal
/// - **Navigation**: `first_key()`, `last_key()`, `higher_key()`, `ceiling_key()`, `lower_key()`, `floor_key()`
/// - **Lifecycle**: `open()`, `close()`, `clear()`, `dispose()`
/// - **State**: `is_closed()`, `is_dropped()`, `size()`, `is_empty()`
/// - **Attributes**: Support for metadata via `AttributeAware`
///
/// # Implementations
/// - `InMemoryMapProvider`: In-memory hash-based storage
/// - `ReDBMapProvider`: Persistent storage via ReDB
/// - `FjallMapProvider`: Persistent storage via Fjall
/// - `TransactionMapProvider`: Transactional view over map data
///
/// # Thread Safety
/// Implementers must be `Send + Sync` for safe use in concurrent contexts.
pub trait NitriteMapProvider: AttributeAware + Send + Sync {
    /// Checks whether the map contains a key.
    ///
    /// # Arguments
    /// * `key` - The key to check for existence
    ///
    /// # Returns
    /// * `Ok(true)` if the key exists
    /// * `Ok(false)` if the key does not exist
    /// * `Err(NitriteError)` if the operation fails
    fn contains_key(&self, key: &Key) -> NitriteResult<bool>;

    /// Retrieves the value associated with a key.
    ///
    /// # Arguments
    /// * `key` - The key to retrieve
    ///
    /// # Returns
    /// * `Ok(Some(value))` if the key exists
    /// * `Ok(None)` if the key does not exist
    /// * `Err(NitriteError)` if the operation fails
    fn get(&self, key: &Key) -> NitriteResult<Option<Value>>;

    /// Clears all entries from the map.
    ///
    /// # Returns
    /// * `Ok(())` if the map was successfully cleared
    /// * `Err(NitriteError)` if the operation fails
    fn clear(&self) -> NitriteResult<()>;

    /// Checks if the map is closed.
    ///
    /// A closed map cannot be used for further operations.
    ///
    /// # Returns
    /// * `Ok(true)` if the map is closed
    /// * `Ok(false)` if the map is open
    /// * `Err(NitriteError)` if the operation fails
    fn is_closed(&self) -> NitriteResult<bool>;

    /// Closes the map.
    ///
    /// After closing, no further operations should be performed on this map.
    /// For persistent storage backends, this may flush pending data.
    ///
    /// # Returns
    /// * `Ok(())` if the map was successfully closed
    /// * `Err(NitriteError)` if the operation fails
    fn close(&self) -> NitriteResult<()>;

    /// Retrieves an iterator over all values in the map.
    ///
    /// The returned iterator supports bidirectional traversal using the `Iterator`
    /// and `DoubleEndedIterator` traits.
    ///
    /// # Returns
    /// * `Ok(ValueIterator)` with an iterator over all values
    /// * `Err(NitriteError)` if the operation fails
    fn values(&self) -> NitriteResult<ValueIterator>;

    /// Retrieves an iterator over all keys in the map.
    ///
    /// The returned iterator supports bidirectional traversal using the `Iterator`
    /// and `DoubleEndedIterator` traits.
    ///
    /// # Returns
    /// * `Ok(KeyIterator)` with an iterator over all keys
    /// * `Err(NitriteError)` if the operation fails
    fn keys(&self) -> NitriteResult<KeyIterator>;

    /// Removes a key-value pair from the map.
    ///
    /// # Arguments
    /// * `key` - The key to remove
    ///
    /// # Returns
    /// * `Ok(Some(value))` with the removed value if the key existed
    /// * `Ok(None)` if the key did not exist
    /// * `Err(NitriteError)` if the operation fails
    fn remove(&self, key: &Key) -> NitriteResult<Option<Value>>;

    /// Inserts or updates a key-value pair in the map.
    ///
    /// If the key already exists, the value is updated.
    ///
    /// # Arguments
    /// * `key` - The key to insert or update
    /// * `value` - The value to store
    ///
    /// # Returns
    /// * `Ok(())` if the operation was successful
    /// * `Err(NitriteError)` if the operation fails
    fn put(&self, key: Key, value: Value) -> NitriteResult<()>;

    /// Atomically inserts multiple key-value pairs in a single batch operation.
    ///
    /// This is more efficient than calling `put()` multiple times as it reduces
    /// journal writes and fsync overhead for persistent backends. The operation is atomic -
    /// all entries are committed together or none at all.
    ///
    /// # Arguments
    /// * `entries` - A vector of key-value pairs to insert
    ///
    /// # Returns
    /// * `Ok(())` if all entries were successfully inserted
    /// * `Err(NitriteError)` if the operation failed
    ///
    /// # Default Implementation
    /// The default implementation calls `put()` individually for each entry.
    /// Implementations should override this for better performance.
    fn put_all(&self, entries: Vec<(Key, Value)>) -> NitriteResult<()> {
        // Default implementation: fall back to individual puts
        for (key, value) in entries {
            self.put(key, value)?;
        }
        Ok(())
    }

    /// Returns the number of entries in the map.
    ///
    /// # Returns
    /// * `Ok(count)` with the total number of key-value pairs
    /// * `Err(NitriteError)` if the operation fails
    fn size(&self) -> NitriteResult<u64>;

    /// Inserts a key-value pair only if the key does not already exist.
    ///
    /// This is an atomic operation - either the key-value pair is inserted
    /// or the operation returns the existing value.
    ///
    /// # Arguments
    /// * `key` - The key to potentially insert
    /// * `value` - The value to store if the key does not exist
    ///
    /// # Returns
    /// * `Ok(None)` if the key did not exist and was successfully inserted
    /// * `Ok(Some(existing_value))` if the key already existed
    /// * `Err(NitriteError)` if the operation fails
    fn put_if_absent(&self, key: Key, value: Value) -> NitriteResult<Option<Value>>;

    /// Returns the first (lowest) key in the map, if it exists.
    ///
    /// # Returns
    /// * `Ok(Some(key))` with the first key in sort order
    /// * `Ok(None)` if the map is empty
    /// * `Err(NitriteError)` if the operation fails
    fn first_key(&self) -> NitriteResult<Option<Key>>;

    /// Returns the last (highest) key in the map, if it exists.
    ///
    /// # Returns
    /// * `Ok(Some(key))` with the last key in sort order
    /// * `Ok(None)` if the map is empty
    /// * `Err(NitriteError)` if the operation fails
    fn last_key(&self) -> NitriteResult<Option<Key>>;

    /// Returns the least key greater than the specified key, if it exists.
    ///
    /// # Arguments
    /// * `key` - The reference key
    ///
    /// # Returns
    /// * `Ok(Some(higher_key))` if a greater key exists
    /// * `Ok(None)` if no greater key exists
    /// * `Err(NitriteError)` if the operation fails
    fn higher_key(&self, key: &Key) -> NitriteResult<Option<Key>>;

    /// Returns the least key greater than or equal to the specified key, if it exists.
    ///
    /// # Arguments
    /// * `key` - The reference key
    ///
    /// # Returns
    /// * `Ok(Some(ceiling_key))` if a key >= the given key exists
    /// * `Ok(None)` if no such key exists
    /// * `Err(NitriteError)` if the operation fails
    fn ceiling_key(&self, key: &Key) -> NitriteResult<Option<Key>>;

    /// Returns the greatest key less than the specified key, if it exists.
    ///
    /// # Arguments
    /// * `key` - The reference key
    ///
    /// # Returns
    /// * `Ok(Some(lower_key))` if a lesser key exists
    /// * `Ok(None)` if no lesser key exists
    /// * `Err(NitriteError)` if the operation fails
    fn lower_key(&self, key: &Key) -> NitriteResult<Option<Key>>;

    /// Returns the greatest key less than or equal to the specified key, if it exists.
    ///
    /// # Arguments
    /// * `key` - The reference key
    ///
    /// # Returns
    /// * `Ok(Some(floor_key))` if a key <= the given key exists
    /// * `Ok(None)` if no such key exists
    /// * `Err(NitriteError)` if the operation fails
    fn floor_key(&self, key: &Key) -> NitriteResult<Option<Key>>;

    /// Checks if the map is empty.
    ///
    /// # Returns
    /// * `Ok(true)` if the map contains no entries
    /// * `Ok(false)` if the map contains at least one entry
    /// * `Err(NitriteError)` if the operation fails
    fn is_empty(&self) -> NitriteResult<bool>;

    /// Returns a reference to the parent `NitriteStore` that owns this map.
    ///
    /// # Returns
    /// * `Ok(NitriteStore)` with the parent store
    /// * `Err(NitriteError)` if the operation fails
    fn get_store(&self) -> NitriteResult<NitriteStore>;

    /// Returns the name of this map.
    ///
    /// # Returns
    /// * `Ok(String)` with the map's identifier
    /// * `Err(NitriteError)` if the operation fails
    fn get_name(&self) -> NitriteResult<String>;

    /// Retrieves an iterator over all key-value entries in the map.
    ///
    /// The returned iterator supports bidirectional traversal using the `Iterator`
    /// and `DoubleEndedIterator` traits.
    ///
    /// # Returns
    /// * `Ok(EntryIterator)` with an iterator over all entries
    /// * `Err(NitriteError)` if the operation fails
    fn entries(&self) -> NitriteResult<EntryIterator>;

    /// Retrieves a reverse iterator over all key-value entries in the map.
    ///
    /// Iterates entries in reverse order (from last to first).
    ///
    /// # Returns
    /// * `Ok(Rev<EntryIterator>)` with a reverse iterator over all entries
    /// * `Err(NitriteError)` if the operation fails
    fn reverse_entries(&self) -> NitriteResult<Rev<EntryIterator>>;

    /// Disposes of the map's resources.
    ///
    /// This is typically called when the database is closing. Unlike `close()`,
    /// this performs cleanup and may be called even if already closed.
    ///
    /// # Returns
    /// * `Ok(())` if disposal was successful
    /// * `Err(NitriteError)` if the operation fails
    fn dispose(&self) -> NitriteResult<()>;

    /// Checks if the map has been dropped.
    ///
    /// A dropped map is no longer part of the database and should not be used.
    ///
    /// # Returns
    /// * `Ok(true)` if the map has been dropped
    /// * `Ok(false)` if the map is still valid
    /// * `Err(NitriteError)` if the operation fails
    fn is_dropped(&self) -> NitriteResult<bool>;
}

#[derive(Clone)]
pub struct NitriteMap {
    inner: Arc<dyn NitriteMapProvider>,
}

impl Deref for NitriteMap {
    type Target = Arc<dyn NitriteMapProvider>;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl NitriteMap {
    /// Creates a new `NitriteMap` wrapping a provider implementation.
    ///
    /// # Arguments
    /// * `inner` - A concrete implementation of `NitriteMapProvider`
    ///
    /// # Returns
    /// A new `NitriteMap` that dereferences to `Arc<dyn NitriteMapProvider>`
    ///
    /// # Notes
    /// - The provider is wrapped in an `Arc` for efficient, thread-safe sharing
    /// - Cloning `NitriteMap` is cheap - it only increments the reference count
    /// - The same map can be safely shared across multiple threads
    pub fn new<T: NitriteMapProvider + 'static>(inner: T) -> Self {
        NitriteMap { inner: Arc::new(inner) }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::common::{Attributes, Key, Value};
    use crate::errors::NitriteError;
    use crate::store::iters::{EntryIterator, KeyIterator, ValueIterator};
    use crate::store::NitriteStore;

    #[derive(Copy, Clone)]
    struct MockNitriteMap;

    impl AttributeAware for MockNitriteMap {
        fn attributes(&self) -> NitriteResult<Option<Attributes>> {
            Ok(None)
        }

        fn set_attributes(&self, _attributes: Attributes) -> NitriteResult<()> {
            Ok(())
        }
    }

    impl NitriteMapProvider for MockNitriteMap {
        fn contains_key(&self, key: &Key) -> NitriteResult<bool> {
            Ok(key == &Key::from("key1"))
        }

        fn get(&self, key: &Key) -> NitriteResult<Option<Value>> {
            if key == &Key::from("key1") {
                Ok(Some(Value::from("value1")))
            } else {
                Ok(None)
            }
        }

        fn clear(&self) -> NitriteResult<()> {
            Ok(())
        }

        fn is_closed(&self) -> NitriteResult<bool> {
            Ok(false)
        }

        fn close(&self) -> NitriteResult<()> {
            Ok(())
        }

        fn values(&self) -> NitriteResult<ValueIterator> {
            Err(NitriteError::new("Invalid operation", crate::errors::ErrorKind::InvalidOperation))
        }

        fn keys(&self) -> NitriteResult<KeyIterator> {
            Err(NitriteError::new("Invalid operation", crate::errors::ErrorKind::InvalidOperation))
        }

        fn remove(&self, key: &Key) -> NitriteResult<Option<Value>> {
            if key == &Key::from("key1") {
                Ok(Some(Value::from("value1")))
            } else {
                Ok(None)
            }
        }

        fn put(&self, _key: Key, _value: Value) -> NitriteResult<()> {
            Ok(())
        }

        fn size(&self) -> NitriteResult<u64> {
            Ok(1)
        }

        fn put_if_absent(&self, key: Key, _value: Value) -> NitriteResult<Option<Value>> {
            if key == Key::from("key1") {
                Ok(Some(Value::from("value1")))
            } else {
                Ok(None)
            }
        }

        fn first_key(&self) -> NitriteResult<Option<Key>> {
            Ok(Some(Key::from("key1")))
        }

        fn last_key(&self) -> NitriteResult<Option<Key>> {
            Ok(Some(Key::from("key1")))
        }

        fn higher_key(&self, _key: &Key) -> NitriteResult<Option<Key>> {
            Ok(None)
        }

        fn ceiling_key(&self, key: &Key) -> NitriteResult<Option<Key>> {
            Ok(Some(key.clone()))
        }

        fn lower_key(&self, _key: &Key) -> NitriteResult<Option<Key>> {
            Ok(None)
        }

        fn floor_key(&self, key: &Key) -> NitriteResult<Option<Key>> {
            Ok(Some(key.clone()))
        }

        fn is_empty(&self) -> NitriteResult<bool> {
            Ok(false)
        }

        fn get_store(&self) -> NitriteResult<NitriteStore> {
            Err(NitriteError::new("Invalid operation", crate::errors::ErrorKind::InvalidOperation))
        }

        fn get_name(&self) -> NitriteResult<String> {
            Ok("test_map".to_string())
        }

        fn entries(&self) -> NitriteResult<EntryIterator> {
            Err(NitriteError::new("Invalid operation", crate::errors::ErrorKind::InvalidOperation))
        }

        fn reverse_entries(&self) -> NitriteResult<Rev<EntryIterator>> {
            Err(NitriteError::new("Invalid operation", crate::errors::ErrorKind::InvalidOperation))
        }

        fn dispose(&self) -> NitriteResult<()> {
            Ok(())
        }

        fn is_dropped(&self) -> NitriteResult<bool> {
            Ok(false)
        }
    }

    #[test]
    fn test_contains_key() {
        let map = NitriteMap::new(MockNitriteMap);
        assert!(map.contains_key(&Key::from("key1")).unwrap());
        assert!(!map.contains_key(&Key::from("key2")).unwrap());
    }

    #[test]
    fn test_get() {
        let map = NitriteMap::new(MockNitriteMap);
        assert_eq!(map.get(&Key::from("key1")).unwrap(), Some(Value::from("value1")));
        assert_eq!(map.get(&Key::from("key2")).unwrap(), None);
    }

    #[test]
    fn test_clear() {
        let map = NitriteMap::new(MockNitriteMap);
        assert!(map.clear().is_ok());
    }

    #[test]
    fn test_is_closed() {
        let map = NitriteMap::new(MockNitriteMap);
        assert!(!map.is_closed().unwrap());
    }

    #[test]
    fn test_close() {
        let map = NitriteMap::new(MockNitriteMap);
        assert!(map.close().is_ok());
    }

    #[test]
    fn test_values() {
        let map = NitriteMap::new(MockNitriteMap);
        assert!(map.values().is_err());
    }

    #[test]
    fn test_keys() {
        let map = NitriteMap::new(MockNitriteMap);
        assert!(map.keys().is_err());
    }

    #[test]
    fn test_remove() {
        let map = NitriteMap::new(MockNitriteMap);
        assert_eq!(map.remove(&Key::from("key1")).unwrap(), Some(Value::from("value1")));
        assert_eq!(map.remove(&Key::from("key2")).unwrap(), None);
    }

    #[test]
    fn test_put() {
        let map = NitriteMap::new(MockNitriteMap);
        assert!(map.put(Key::from("key1"), Value::from("value1")).is_ok());
    }

    #[test]
    fn test_size() {
        let map = NitriteMap::new(MockNitriteMap);
        assert_eq!(map.size().unwrap(), 1);
    }

    #[test]
    fn test_put_if_absent() {
        let map = NitriteMap::new(MockNitriteMap);
        assert_eq!(map.put_if_absent(Key::from("key1"), Value::from("value1")).unwrap(), Some(Value::from("value1")));
        assert_eq!(map.put_if_absent(Key::from("key2"), Value::from("value2")).unwrap(), None);
    }

    #[test]
    fn test_first_key() {
        let map = NitriteMap::new(MockNitriteMap);
        assert_eq!(map.first_key().unwrap(), Some(Key::from("key1")));
    }

    #[test]
    fn test_last_key() {
        let map = NitriteMap::new(MockNitriteMap);
        assert_eq!(map.last_key().unwrap(), Some(Key::from("key1")));
    }

    #[test]
    fn test_higher_key() {
        let map = NitriteMap::new(MockNitriteMap);
        assert_eq!(map.higher_key(&Key::from("key1")).unwrap(), None);
    }

    #[test]
    fn test_ceiling_key() {
        let map = NitriteMap::new(MockNitriteMap);
        assert_eq!(map.ceiling_key(&Key::from("key1")).unwrap(), Some(Key::from("key1")));
    }

    #[test]
    fn test_lower_key() {
        let map = NitriteMap::new(MockNitriteMap);
        assert_eq!(map.lower_key(&Key::from("key1")).unwrap(), None);
    }

    #[test]
    fn test_floor_key() {
        let map = NitriteMap::new(MockNitriteMap);
        assert_eq!(map.floor_key(&Key::from("key1")).unwrap(), Some(Key::from("key1")));
    }

    #[test]
    fn test_is_empty() {
        let map = NitriteMap::new(MockNitriteMap);
        assert!(!map.is_empty().unwrap());
    }

    #[test]
    fn test_get_store() {
        let map = NitriteMap::new(MockNitriteMap);
        assert!(map.get_store().is_err());
    }

    #[test]
    fn test_get_name() {
        let map = NitriteMap::new(MockNitriteMap);
        assert_eq!(map.get_name().unwrap(), "test_map");
    }

    #[test]
    fn test_entries() {
        let map = NitriteMap::new(MockNitriteMap);
        assert!(map.entries().is_err());
    }

    #[test]
    fn test_reverse_entries() {
        let map = NitriteMap::new(MockNitriteMap);
        assert!(map.reverse_entries().is_err());
    }

    #[test]
    fn test_dispose_map() {
        let map = NitriteMap::new(MockNitriteMap);
        assert!(map.dispose().is_ok());
    }

    #[test]
    fn test_is_dropped() {
        let map = NitriteMap::new(MockNitriteMap);
        assert!(!map.is_dropped().unwrap());
    }

    #[test]
    fn test_arc_cloning_efficiency() {
        // Test that NitriteMap cloning is efficient with Arc
        let map1 = NitriteMap::new(MockNitriteMap);
        let map2 = map1.clone();
        
        // Both should reference the same underlying Arc data
        assert_eq!(map1.get_name().unwrap(), map2.get_name().unwrap());
    }

    #[test]
    fn test_deref_access_efficiency() {
        // Test that Deref trait allows efficient access to Arc<dyn TNitriteMap>
        let map = NitriteMap::new(MockNitriteMap);
        
        // Deref should allow direct access without extra allocations
        let _deref_target = &*map;
        assert!(!map.is_empty().unwrap());
    }

    #[test]
    fn test_multiple_sequential_operations() {
        // Test efficiency of multiple sequential operations
        let map = NitriteMap::new(MockNitriteMap);
        
        // Multiple operations should not cause unnecessary overhead
        assert_eq!(map.size().unwrap(), 1);
        assert!(map.contains_key(&Key::from("key1")).unwrap());
        assert_eq!(map.get(&Key::from("key1")).unwrap(), Some(Value::from("value1")));
        assert!(!map.is_empty().unwrap());
    }

    #[test]
    fn test_put_if_absent_consistency() {
        // Test put_if_absent consistency across multiple calls
        let map = NitriteMap::new(MockNitriteMap);
        
        // First call should return existing value
        let result1 = map.put_if_absent(Key::from("key1"), Value::from("new_value1")).unwrap();
        assert_eq!(result1, Some(Value::from("value1")));
        
        // Calling again should return same result
        let result2 = map.put_if_absent(Key::from("key1"), Value::from("another_value")).unwrap();
        assert_eq!(result2, Some(Value::from("value1")));
    }

    #[test]
    fn test_key_operations_efficiency() {
        // Test efficiency of multiple key operations
        let map = NitriteMap::new(MockNitriteMap);
        
        assert_eq!(map.first_key().unwrap(), Some(Key::from("key1")));
        assert_eq!(map.last_key().unwrap(), Some(Key::from("key1")));
        assert_eq!(map.ceiling_key(&Key::from("key1")).unwrap(), Some(Key::from("key1")));
        assert_eq!(map.floor_key(&Key::from("key1")).unwrap(), Some(Key::from("key1")));
    }
}