btree-store 0.1.1

A persistent, embedded key-value storage engine in Rust featuring a Copy-On-Write (COW) B-Tree, ACID compliance, and crash safety with multi-bucket support
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
use parking_lot::Mutex;
use std::{
    collections::HashMap,
    collections::HashSet,
    collections::hash_map::DefaultHasher,
    fs::{File, OpenOptions},
    hash::{Hash, Hasher},
    io::{self, Read, Write},
    path::{Path, PathBuf},
    sync::Arc,
};

use crate::{
    Error, FreeNode, MetaNode, Result,
    node::{Node, PAGE_SIZE},
};

#[cfg(unix)]
fn fsync_parent_dir(path: &Path) -> io::Result<()> {
    if let Some(parent) = path.parent() {
        let parent = if parent.as_os_str().is_empty() {
            Path::new(".")
        } else {
            parent
        };
        File::open(parent)?.sync_all()?;
    }
    Ok(())
}

#[cfg(not(unix))]
fn fsync_parent_dir(_path: &Path) -> io::Result<()> {
    Ok(())
}

/// Abstract Trait for Positional I/O to support Linux, Windows, macOS, FreeBSD.
pub(crate) trait FileIO {
    fn pread_exact(&self, buf: &mut [u8], offset: u64) -> io::Result<()>;
    fn pwrite_all(&self, buf: &[u8], offset: u64) -> io::Result<()>;
    fn psync_all(&self) -> io::Result<()>;
}

impl FileIO for File {
    #[cfg(unix)]
    fn pread_exact(&self, buf: &mut [u8], offset: u64) -> io::Result<()> {
        use std::os::unix::fs::FileExt;
        self.read_exact_at(buf, offset)
    }

    #[cfg(unix)]
    fn pwrite_all(&self, buf: &[u8], offset: u64) -> io::Result<()> {
        use std::os::unix::fs::FileExt;
        self.write_all_at(buf, offset)
    }

    #[cfg(windows)]
    fn pread_exact(&self, mut buf: &mut [u8], mut offset: u64) -> io::Result<()> {
        use std::os::windows::fs::FileExt;
        while !buf.is_empty() {
            match self.seek_read(buf, offset) {
                Ok(0) => {
                    return Err(io::Error::new(
                        io::ErrorKind::UnexpectedEof,
                        "failed to fill whole buffer",
                    ));
                }
                Ok(n) => {
                    let tmp = buf;
                    buf = &mut tmp[n..];
                    offset += n as u64;
                }
                Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
                Err(e) => return Err(e),
            }
        }
        Ok(())
    }

    #[cfg(windows)]
    fn pwrite_all(&self, mut buf: &[u8], mut offset: u64) -> io::Result<()> {
        use std::os::windows::fs::FileExt;
        while !buf.is_empty() {
            match self.seek_write(buf, offset) {
                Ok(0) => {
                    return Err(io::Error::new(
                        io::ErrorKind::WriteZero,
                        "failed to write whole buffer",
                    ));
                }
                Ok(n) => {
                    buf = &buf[n..];
                    offset += n as u64;
                }
                Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
                Err(e) => return Err(e),
            }
        }
        Ok(())
    }

    fn psync_all(&self) -> io::Result<()> {
        self.sync_all()
    }
}
const FREE_NODE_SIZE: usize = std::mem::size_of::<FreeNode>();

#[repr(C)]
#[derive(Clone, Copy)]
struct PendingLogHeader {
    seq: u64,
    nr_freed: u32,
    nr_alloc: u32,
    checksum: u32,
    _padding: u32,
}

#[repr(C)]
#[derive(Clone, Copy)]
struct PendingEntry {
    page_id: u64,
    nr_pages: u32,
    _padding: u32,
}

pub struct Store {
    file: Arc<Mutex<File>>,
    sb: Mutex<MetaNode>,
    pending_path: PathBuf,
    /// contention is concentrated at the B+ Tree's root
    cache: NodeCache,
}

impl Store {
    pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
        let path = path.as_ref();

        let (file, sb) = if !path.exists() {
            let file = OpenOptions::new()
                .read(true)
                .write(true)
                .create(true)
                .truncate(true)
                .open(path)?;

            let mut sb = MetaNode::new();

            file.pwrite_all(&sb.as_page_slice(), 0)?;

            sb.seq += 1;
            sb.update_checksum();
            file.pwrite_all(&sb.as_page_slice(), PAGE_SIZE as u64)?;

            file.psync_all()?;
            (file, sb)
        } else {
            let file = OpenOptions::new().read(true).write(true).open(path)?;

            let mut buf0 = vec![0u8; PAGE_SIZE];
            let mut buf1 = vec![0u8; PAGE_SIZE];

            let r0 = file.pread_exact(&mut buf0, 0);
            let r1 = file.pread_exact(&mut buf1, PAGE_SIZE as u64);

            let sb0 = if r0.is_ok() {
                let s = MetaNode::from_slice(&buf0);
                if s.validate().is_ok() { Some(s) } else { None }
            } else {
                None
            };

            let sb1 = if r1.is_ok() {
                let s = MetaNode::from_slice(&buf1);
                if s.validate().is_ok() { Some(s) } else { None }
            } else {
                None
            };

            let sb = match (sb0, sb1) {
                (Some(s0), Some(s1)) => {
                    if s0.seq >= s1.seq {
                        s0
                    } else {
                        s1
                    }
                }
                (Some(s0), None) => s0,
                (None, Some(s1)) => s1,
                (None, None) => return Err(Error::Corruption),
            };
            (file, sb)
        };

        let pending_path = path.with_extension("pending");
        let store = Self {
            file: Arc::new(Mutex::new(file)),
            sb: Mutex::new(sb),
            pending_path,
            cache: NodeCache::new(4096),
        };

        store.recover_pending()?;
        Ok(store)
    }

    fn recover_pending(&self) -> Result<()> {
        if !self.pending_path.exists() || std::fs::metadata(&self.pending_path)?.len() == 0 {
            return Ok(());
        }

        let mut f = File::open(&self.pending_path)?;
        let mut buf = Vec::new();
        f.read_to_end(&mut buf)?;

        let header_size = std::mem::size_of::<PendingLogHeader>();
        if buf.len() < header_size {
            let _ = self.clear_pending_log();
            return Ok(());
        }

        let header = unsafe { std::ptr::read_unaligned(buf.as_ptr() as *const PendingLogHeader) };
        let data_part = &buf[header_size..];

        if crc32c::crc32c(data_part) != header.checksum {
            let _ = self.clear_pending_log();
            return Ok(());
        }

        let entry_size = std::mem::size_of::<PendingEntry>();
        let (current_seq, next_id) = {
            let sb = self.sb.lock();
            (sb.seq, sb.next_page_id)
        };

        if header.seq == current_seq {
            // case 1: Redo-Free. SB was updated but old pages were not freed
            let mut pos = 0;
            for _ in 0..header.nr_freed {
                if pos + entry_size > data_part.len() {
                    break;
                }
                let entry = unsafe {
                    std::ptr::read_unaligned(data_part.as_ptr().add(pos) as *const PendingEntry)
                };
                self.free_pages(entry.page_id, entry.nr_pages)?;
                pos += entry_size;
            }
            let root = self.sb.lock().root_current;
            self.update_root(root)?;
        } else if header.seq == current_seq + 1 {
            // case 2: Undo-Alloc. transaction failed before SB update
            let mut pos = (header.nr_freed as usize) * entry_size;
            for _ in 0..header.nr_alloc {
                if pos + entry_size > data_part.len() {
                    break;
                }
                let entry = unsafe {
                    std::ptr::read_unaligned(data_part.as_ptr().add(pos) as *const PendingEntry)
                };
                if entry.page_id >= next_id {
                    self.free_pages(entry.page_id, entry.nr_pages)?;
                }
                pos += entry_size;
            }
            let root = self.sb.lock().root_current;
            self.update_root(root)?;
        } else {
            // stale/future/malformed log: ignore
        }

        let _ = self.clear_pending_log();
        Ok(())
    }

    pub fn write_pending_log(
        &self,
        seq: u64,
        freed: &[(u64, u32)],
        alloc: &HashSet<u64>,
    ) -> Result<()> {
        let temp_path = self.pending_path.with_extension("tmp");
        {
            let mut f = OpenOptions::new()
                .write(true)
                .create(true)
                .truncate(true)
                .open(&temp_path)?;

            let mut entries = Vec::with_capacity(freed.len() + alloc.len());
            for &(pid, nr) in freed {
                entries.push(PendingEntry {
                    page_id: pid,
                    nr_pages: nr,
                    _padding: 0,
                });
            }
            for &pid in alloc.iter() {
                entries.push(PendingEntry {
                    page_id: pid,
                    nr_pages: 1,
                    _padding: 0,
                });
            }

            let data_bytes = unsafe {
                std::slice::from_raw_parts(
                    entries.as_ptr() as *const u8,
                    entries.len() * std::mem::size_of::<PendingEntry>(),
                )
            };

            let header = PendingLogHeader {
                seq,
                nr_freed: freed.len() as u32,
                nr_alloc: alloc.len() as u32,
                checksum: crc32c::crc32c(data_bytes),
                _padding: 0,
            };

            let header_bytes = unsafe {
                std::slice::from_raw_parts(
                    &header as *const PendingLogHeader as *const u8,
                    std::mem::size_of::<PendingLogHeader>(),
                )
            };

            f.write_all(header_bytes)?;
            f.write_all(data_bytes)?;
            f.sync_all()?;
        }

        std::fs::rename(&temp_path, &self.pending_path)?;
        // After rename, we still need to fsync the parent dir to ensure the rename is permanent
        fsync_parent_dir(&self.pending_path)?;
        Ok(())
    }

    pub fn clear_pending_log(&self) -> Result<()> {
        if self.pending_path.exists() {
            let f = OpenOptions::new().write(true).open(&self.pending_path)?;
            f.set_len(0)?;
            f.sync_all()?;
        }
        Ok(())
    }

    pub fn get_seq(&self) -> u64 {
        self.sb.lock().seq
    }

    pub fn alloc_page(&self) -> Result<u64> {
        let pages = self.alloc_pages(1)?;
        Ok(pages[0])
    }

    pub fn alloc_pages(&self, nr_pages: u32) -> Result<Vec<u64>> {
        let mut sb_guard = self.sb.lock();
        let f = self.file.lock();

        let mut temp_sb = *sb_guard;
        let mut gathered = Vec::new();

        // 1. Try to allocate from the cached head chunk
        if temp_sb.nr_free >= nr_pages as u64 {
            let start_id = (temp_sb.free_list_head + temp_sb.nr_free) - nr_pages as u64;

            if start_id == temp_sb.free_list_head {
                // Allocating the head page itself. Read the next pointer first.
                let mut buf = [0u8; FREE_NODE_SIZE];
                f.pread_exact(&mut buf, temp_sb.free_list_head * PAGE_SIZE as u64)?;
                let free_node = FreeNode::from_slice(&buf);

                temp_sb.free_list_head = free_node.next;
                if temp_sb.free_list_head != 0 {
                    let mut next_buf = [0u8; FREE_NODE_SIZE];
                    f.pread_exact(&mut next_buf, temp_sb.free_list_head * PAGE_SIZE as u64)?;
                    let next_node = FreeNode::from_slice(&next_buf);
                    if !next_node.validate() {
                        return Err(Error::Corruption);
                    }
                    temp_sb.nr_free = next_node.nr_pages as u64;
                } else {
                    temp_sb.nr_free = 0;
                }
            } else {
                temp_sb.nr_free -= nr_pages as u64;
            }

            for i in 0..nr_pages {
                self.cache.invalidate(start_id + i as u64);
            }
            *sb_guard = temp_sb;
            return Ok((0..nr_pages).map(|i| start_id + i as u64).collect());
        }

        // 2. Gather discrete pages from free list
        while gathered.len() < nr_pages as usize && temp_sb.free_list_head != 0 {
            let to_take =
                std::cmp::min(nr_pages as usize - gathered.len(), temp_sb.nr_free as usize);
            let start_id = (temp_sb.free_list_head + temp_sb.nr_free) - to_take as u64;

            for i in 0..to_take {
                let pid = start_id + i as u64;
                self.cache.invalidate(pid);
                gathered.push(pid);
            }
            temp_sb.nr_free -= to_take as u64;

            if temp_sb.nr_free == 0 {
                let mut buf = [0u8; FREE_NODE_SIZE];
                f.pread_exact(&mut buf, temp_sb.free_list_head * PAGE_SIZE as u64)?;
                let h_node = FreeNode::from_slice(&buf);
                if !h_node.validate() {
                    return Err(Error::Corruption);
                }
                temp_sb.free_list_head = h_node.next;
                if temp_sb.free_list_head != 0 {
                    let mut n_buf = [0u8; FREE_NODE_SIZE];
                    f.pread_exact(&mut n_buf, temp_sb.free_list_head * PAGE_SIZE as u64)?;
                    let n_node = FreeNode::from_slice(&n_buf);
                    if !n_node.validate() {
                        return Err(Error::Corruption);
                    }
                    temp_sb.nr_free = n_node.nr_pages as u64;
                }
            }
        }

        // 3. Allocate from end of file for remaining
        if gathered.len() < nr_pages as usize {
            let needed = nr_pages as usize - gathered.len();
            let start_id = temp_sb.next_page_id;
            temp_sb.next_page_id += needed as u64;
            for i in 0..needed {
                let pid = start_id + i as u64;
                self.cache.invalidate(pid);
                gathered.push(pid);
            }
        }

        *sb_guard = temp_sb;
        Ok(gathered)
    }

    pub fn free_pages(&self, page_id: u64, nr_pages: u32) -> Result<()> {
        if page_id == 0 {
            return Ok(());
        }

        for i in 0..nr_pages {
            self.cache.invalidate(page_id + i as u64);
        }

        let mut sb = self.sb.lock();
        let f = self.file.lock();

        if sb.free_list_head == page_id {
            return Ok(());
        }

        // Ensure cached head's state is persisted before moving head
        if sb.free_list_head != 0 {
            let mut buf = vec![0u8; FREE_NODE_SIZE];
            if f.pread_exact(&mut buf, sb.free_list_head * PAGE_SIZE as u64)
                .is_ok()
            {
                let mut head_node = FreeNode::from_slice(&buf);
                if head_node.validate() && head_node.nr_pages as u64 != sb.nr_free {
                    head_node.nr_pages = sb.nr_free as u32;
                    head_node.update_checksum();
                    let _ = f.pwrite_all(
                        unsafe {
                            std::slice::from_raw_parts(
                                (&head_node as *const FreeNode) as *const u8,
                                FREE_NODE_SIZE,
                            )
                        },
                        sb.free_list_head * PAGE_SIZE as u64,
                    );
                }
            }
        }

        let mut new_node = FreeNode {
            next: sb.free_list_head,
            nr_pages,
            checksum: 0,
        };
        new_node.update_checksum();
        f.pwrite_all(
            unsafe {
                std::slice::from_raw_parts(
                    (&new_node as *const FreeNode) as *const u8,
                    FREE_NODE_SIZE,
                )
            },
            page_id * PAGE_SIZE as u64,
        )?;

        sb.free_list_head = page_id;
        sb.nr_free = nr_pages as u64;

        Ok(())
    }

    pub fn sync(&self) -> Result<()> {
        self.file.lock().psync_all().map_err(|_| Error::IoError)
    }

    pub fn load_node(&self, page_id: u64) -> Result<Arc<Node>> {
        if let Some(node) = self.cache.get(page_id) {
            return Ok(node);
        }

        let raw = self.load_page(page_id)?;
        let node = Arc::new(Node::from_raw(raw)?);

        self.cache.put(page_id, node.clone());
        Ok(node)
    }

    pub fn load_page(&self, page_id: u64) -> Result<Vec<u8>> {
        self.load_data(&[page_id], PAGE_SIZE)
    }

    pub fn load_data(&self, pages: &[u64], len: usize) -> Result<Vec<u8>> {
        let mut buf = vec![0u8; len];
        self.read_data(pages, &mut buf)?;
        Ok(buf)
    }

    pub fn read_data(&self, pages: &[u64], buf: &mut [u8]) -> Result<()> {
        let f = self.file.lock();
        for (i, &pid) in pages.iter().enumerate() {
            let offset = pid * PAGE_SIZE as u64;
            let start = i * PAGE_SIZE;
            if start >= buf.len() {
                break;
            }
            let end = std::cmp::min(start + PAGE_SIZE, buf.len());
            f.pread_exact(&mut buf[start..end], offset)?;
        }
        Ok(())
    }

    pub fn write_data(&self, pages: &[u64], data: &[u8]) -> Result<()> {
        let f = self.file.lock();
        for (i, &pid) in pages.iter().enumerate() {
            let offset = pid * PAGE_SIZE as u64;
            let start = i * PAGE_SIZE;
            if start >= data.len() {
                break;
            }
            let end = std::cmp::min(start + PAGE_SIZE, data.len());
            f.pwrite_all(&data[start..end], offset)?;
        }
        Ok(())
    }

    pub fn read_page(&self, page_id: u64, buf: &mut [u8]) -> Result<()> {
        self.read_data(&[page_id], buf)
    }

    pub fn write_page(&self, page_id: u64, data: &[u8]) -> Result<()> {
        self.write_data(&[page_id], data)
    }

    pub fn get_root_id(&self) -> u64 {
        self.sb.lock().root_current
    }

    pub fn refresh_sb(&self) -> Result<u64> {
        let file = self.file.lock();
        let mut buf0 = vec![0u8; PAGE_SIZE];
        let mut buf1 = vec![0u8; PAGE_SIZE];

        let r0 = file.pread_exact(&mut buf0, 0);
        let r1 = file.pread_exact(&mut buf1, PAGE_SIZE as u64);
        drop(file);

        let sb0 = if r0.is_ok() {
            let s = MetaNode::from_slice(&buf0);
            if s.validate().is_ok() { Some(s) } else { None }
        } else {
            None
        };

        let sb1 = if r1.is_ok() {
            let s = MetaNode::from_slice(&buf1);
            if s.validate().is_ok() { Some(s) } else { None }
        } else {
            None
        };

        let sb = match (sb0, sb1) {
            (Some(s0), Some(s1)) => {
                if s0.seq >= s1.seq {
                    s0
                } else {
                    s1
                }
            }
            (Some(s0), None) => s0,
            (None, Some(s1)) => s1,
            (None, None) => return Err(Error::Corruption),
        };

        let mut current_sb = self.sb.lock();
        if sb.seq > current_sb.seq {
            *current_sb = sb;
            Ok(sb.root_current)
        } else {
            Ok(current_sb.root_current)
        }
    }

    pub fn clear_cache(&self) {
        for shard in &self.cache.shards {
            let mut guard = shard.lock();
            guard.entries.iter_mut().for_each(|e| *e = None);
            guard.map.clear();
        }
    }

    pub(crate) fn update_root(&self, root_id: u64) -> Result<()> {
        let mut sb = self.sb.lock();

        sb.root_current = root_id;
        sb.seq += 1;
        sb.update_checksum();

        let write_offset = if sb.seq.is_multiple_of(2) {
            PAGE_SIZE as u64
        } else {
            0
        };

        let file = self.file.lock();
        file.pwrite_all(&sb.as_page_slice(), write_offset)?;
        file.psync_all()?;

        Ok(())
    }
}

struct CacheEntry {
    page_id: u64,
    node: Arc<Node>,
    usage: bool,
}

struct NodeCacheShard {
    entries: Vec<Option<CacheEntry>>,
    map: HashMap<u64, usize>,
    hand: usize,
    capacity: usize,
}

impl NodeCacheShard {
    fn new(capacity: usize) -> Self {
        Self {
            entries: (0..capacity).map(|_| None).collect(),
            map: HashMap::with_capacity(capacity),
            hand: 0,
            capacity,
        }
    }

    fn get(&mut self, page_id: u64) -> Option<Arc<Node>> {
        if let Some(&idx) = self.map.get(&page_id)
            && let Some(entry) = &mut self.entries[idx]
        {
            entry.usage = true;
            return Some(entry.node.clone());
        }
        None
    }

    fn put(&mut self, page_id: u64, node: Arc<Node>) {
        if let Some(&idx) = self.map.get(&page_id)
            && let Some(entry) = &mut self.entries[idx]
        {
            entry.usage = true;
            entry.node = node;
            return;
        }

        loop {
            let evict = match &mut self.entries[self.hand] {
                None => true,
                Some(entry) => {
                    if entry.usage {
                        entry.usage = false;
                        false
                    } else {
                        self.map.remove(&entry.page_id);
                        true
                    }
                }
            };

            if evict {
                self.entries[self.hand] = Some(CacheEntry {
                    page_id,
                    node,
                    usage: true,
                });
                self.map.insert(page_id, self.hand);
                self.hand = (self.hand + 1) % self.capacity;
                return;
            }
            self.hand = (self.hand + 1) % self.capacity;
        }
    }

    fn invalidate(&mut self, page_id: u64) {
        if let Some(&idx) = self.map.get(&page_id) {
            self.entries[idx] = None;
            self.map.remove(&page_id);
        }
    }
}

const NUM_SHARDS: usize = 64;

pub struct NodeCache {
    shards: Vec<Mutex<NodeCacheShard>>,
}

impl NodeCache {
    fn new(capacity: usize) -> Self {
        let shard_cap = std::cmp::max(1, capacity / NUM_SHARDS);
        let mut shards = Vec::with_capacity(NUM_SHARDS);
        for _ in 0..NUM_SHARDS {
            shards.push(Mutex::new(NodeCacheShard::new(shard_cap)));
        }
        Self { shards }
    }

    fn get_shard(&self, page_id: u64) -> &Mutex<NodeCacheShard> {
        let mut hasher = DefaultHasher::new();
        page_id.hash(&mut hasher);
        let hash = hasher.finish();
        &self.shards[(hash as usize) % NUM_SHARDS]
    }

    pub fn get(&self, page_id: u64) -> Option<Arc<Node>> {
        self.get_shard(page_id).lock().get(page_id)
    }

    pub fn put(&self, page_id: u64, node: Arc<Node>) {
        self.get_shard(page_id).lock().put(page_id, node)
    }

    pub fn invalidate(&self, page_id: u64) {
        self.get_shard(page_id).lock().invalidate(page_id)
    }
}