/// Memory-mapped index loading.
///
/// Maps the .mvec file directly into the process address space.
/// The OS pages in only what is actually accessed — ideal for large indexes
/// on memory-constrained devices where the full index may not fit in RAM.
///
/// Note: vectors are still copied into a VectorStore on load. True zero-copy
/// access (where VectorStore::data directly aliases the mmap region) is a
/// future optimization requiring lifetime-annotated storage.

use std::path::Path;
use memmap2::Mmap;
use crate::error::Result;

/// Raw memory-mapped view of a .mvec file.
/// Returned by `MonaVec::open_mmap` for inspection; use `MonaVec::load_mmap`
/// for a fully-initialized index that owns the map.
pub struct MmapView {
    pub map: Mmap,
}

impl MmapView {
    pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
        let file = std::fs::File::open(path)?;
        let map  = unsafe { Mmap::map(&file)? };
        Ok(Self { map })
    }

    /// Byte slice starting at the vectors section (after the fixed header).
    pub fn vectors_slice(&self, header_size: usize) -> &[u8] {
        &self.map[header_size..]
    }
}
