pub struct MmapStorage { /* private fields */ }Expand description
Memory-mapped file storage for vectors.
Uses a combination of an index file (ID -> offset) and a data file (raw vectors). Also implements a simple WAL for durability.
Implementations§
Source§impl MmapStorage
impl MmapStorage
Sourcepub fn reserve_capacity(&mut self, vector_count: usize) -> Result<()>
pub fn reserve_capacity(&mut self, vector_count: usize) -> Result<()>
Pre-allocates storage capacity for a known number of vectors.
Call this before bulk imports to avoid blocking resize operations during insertion. This is especially useful when the final dataset size is known in advance.
§P2 Optimization
This allows users to pre-allocate once and avoid all resize locks during bulk import operations.
§Arguments
vector_count- Expected number of vectors to store
§Example
// Pre-allocate for 1 million vectors before bulk import
storage.reserve_capacity(1_000_000)?;§Errors
Returns an error if file operations fail.
Sourcepub fn metrics(&self) -> &StorageMetrics
pub fn metrics(&self) -> &StorageMetrics
Returns a reference to the storage metrics.
§P0 Audit: Latency Monitoring
Use this to monitor ensure_capacity latency, especially P99.
High P99 latency indicates “stop-the-world” pauses during mmap resizes.
§Example
let storage = MmapStorage::new(path, 768)?;
// ... perform operations ...
let stats = storage.metrics().ensure_capacity_latency_stats();
if stats.p99_exceeds(Duration::from_millis(100)) {
warn!("High P99 latency detected: {:?}", stats.p99());
}Sourcepub fn compact(&mut self) -> Result<usize>
pub fn compact(&mut self) -> Result<usize>
Compacts the storage by rewriting only active vectors.
This reclaims disk space from deleted vectors by:
- Writing all active vectors to a new temporary file
- Atomically replacing the old file with the new one
§TS-CORE-004: Storage Compaction
This operation is quasi-atomic via rename() for crash safety.
Reads remain available during compaction (copy-on-write pattern).
§Returns
The number of bytes reclaimed.
§Errors
Returns an error if file operations fail.
Sourcepub fn fragmentation_ratio(&self) -> f64
pub fn fragmentation_ratio(&self) -> f64
Returns the fragmentation ratio (0.0 = no fragmentation, 1.0 = 100% fragmented).
Use this to decide when to trigger compaction. A ratio > 0.3 (30% fragmentation) is a good threshold.
Sourcepub fn retrieve_ref(&self, id: u64) -> Result<Option<VectorSliceGuard<'_>>>
pub fn retrieve_ref(&self, id: u64) -> Result<Option<VectorSliceGuard<'_>>>
Retrieves a vector by ID without copying (zero-copy).
Returns a guard that provides direct access to the mmap’d data.
This is significantly faster than retrieve() for read-heavy workloads
as it eliminates heap allocation and memory copy.
§Arguments
id- The vector ID to retrieve
§Returns
Ok(Some(guard))- Guard providing zero-copy access to the vectorOk(None)- Vector with this ID doesn’t existErr(...)- I/O error (e.g., corrupted data)
§Example
let guard = storage.retrieve_ref(id)?.unwrap();
let slice: &[f32] = guard.as_ref();
// Use slice directly - no allocation occurred§Performance
Compared to retrieve():
- No heap allocation - data accessed directly from mmap
- No memcpy - pointer arithmetic only
- Lock held - guard must be dropped to release read lock
§Errors
Returns an error if the stored offset is out of bounds (corrupted index).
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for MmapStorage
impl !RefUnwindSafe for MmapStorage
impl Send for MmapStorage
impl Sync for MmapStorage
impl Unpin for MmapStorage
impl UnwindSafe for MmapStorage
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);