Skip to main content

diskann_rs/
storage.rs

1//! # Storage Abstraction
2//!
3//! Provides a unified `Storage` enum that backs index data from different sources:
4//! - Memory-mapped files (`Mmap`)
5//! - Owned byte buffers (`Vec<u8>`)
6//! - Shared byte buffers (`Arc<[u8]>`)
7//!
8//! All variants deref to `&[u8]`, so callers index into storage uniformly.
9
10use memmap2::Mmap;
11use std::ops::Deref;
12use std::sync::Arc;
13
14/// Backing store for index data.
15///
16/// `Storage` replaces the bare `Mmap` field so that indexes can be loaded
17/// from files *or* from in-memory byte buffers without duplicating search logic.
18pub enum Storage {
19    /// Memory-mapped file (zero-copy, OS-managed paging).
20    Mmap(Mmap),
21    /// Owned byte buffer (e.g. from `to_bytes()` round-trip or network).
22    Owned(Vec<u8>),
23    /// Reference-counted shared buffer (cheap clone, multi-reader).
24    Shared(Arc<[u8]>),
25}
26
27impl Deref for Storage {
28    type Target = [u8];
29
30    #[inline]
31    fn deref(&self) -> &[u8] {
32        match self {
33            Storage::Mmap(m) => m,
34            Storage::Owned(v) => v,
35            Storage::Shared(a) => a,
36        }
37    }
38}
39
40impl AsRef<[u8]> for Storage {
41    #[inline]
42    fn as_ref(&self) -> &[u8] {
43        self
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_owned_deref() {
53        let data = vec![1u8, 2, 3, 4];
54        let s = Storage::Owned(data.clone());
55        assert_eq!(&*s, &data[..]);
56    }
57
58    #[test]
59    fn test_shared_deref() {
60        let data: Arc<[u8]> = Arc::from(vec![10u8, 20, 30]);
61        let s = Storage::Shared(data.clone());
62        assert_eq!(&*s, &*data);
63    }
64}