seekr_code/index/
mmap_store.rs1use std::path::Path;
7
8use memmap2::Mmap;
9
10use crate::error::IndexError;
11
12pub struct MmapIndex {
14 mmap: Mmap,
16}
17
18impl MmapIndex {
19 pub fn open(path: &Path) -> Result<Self, IndexError> {
24 let file = std::fs::File::open(path)?;
25
26 let mmap = unsafe { Mmap::map(&file)? };
28
29 Ok(Self { mmap })
30 }
31
32 pub fn as_bytes(&self) -> &[u8] {
34 &self.mmap
35 }
36
37 pub fn len(&self) -> usize {
39 self.mmap.len()
40 }
41
42 pub fn is_empty(&self) -> bool {
44 self.mmap.is_empty()
45 }
46}
47
48impl std::fmt::Debug for MmapIndex {
49 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50 f.debug_struct("MmapIndex")
51 .field("len", &self.mmap.len())
52 .finish()
53 }
54}
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 #[test]
61 fn test_mmap_open_nonexistent() {
62 let result = MmapIndex::open(Path::new("/nonexistent/path/index.dat"));
63 assert!(result.is_err());
64 }
65
66 #[test]
67 fn test_mmap_open_existing() {
68 let dir = tempfile::tempdir().unwrap();
69 let file_path = dir.path().join("test.dat");
70 std::fs::write(&file_path, b"hello world").unwrap();
71
72 let mmap = MmapIndex::open(&file_path).unwrap();
73 assert_eq!(mmap.len(), 11);
74 assert_eq!(mmap.as_bytes(), b"hello world");
75 }
76}