MmapReader

Struct MmapReader 

Source
pub struct MmapReader { /* private fields */ }
Expand description

Memory-mapped database file for zero-copy reads.

This struct wraps a memory-mapped file and provides safe access to the underlying data. It’s particularly useful for reading large tensor data without copying.

§Examples

use synadb::mmap::MmapReader;

let reader = MmapReader::open("data.db").unwrap();
let data = reader.slice(0, 1024);
println!("Read {} bytes", data.len());

Implementations§

Source§

impl MmapReader

Source

pub fn open<P: AsRef<Path>>(path: P) -> Result<Self>

Open a file for memory-mapped reading.

§Arguments
  • path - Path to the file to memory-map
§Returns

A new MmapReader instance.

§Errors

Returns SynaError::Io if the file cannot be opened or mapped.

§Examples
use synadb::mmap::MmapReader;

let reader = MmapReader::open("data.db").unwrap();
Source

pub fn len(&self) -> usize

Get the total length of the memory-mapped file.

§Returns

The size of the file in bytes.

§Examples
use synadb::mmap::MmapReader;

let reader = MmapReader::open("data.db").unwrap();
println!("File size: {} bytes", reader.len());
Source

pub fn is_empty(&self) -> bool

Check if the memory-mapped file is empty.

§Returns

true if the file has zero length, false otherwise.

Source

pub fn slice(&self, offset: usize, len: usize) -> &[u8]

Get a slice of bytes at the specified offset.

§Arguments
  • offset - Starting byte offset
  • len - Number of bytes to read
§Returns

A byte slice referencing the memory-mapped data.

§Panics

Panics if offset + len exceeds the file size. Use try_slice for a non-panicking version.

§Examples
use synadb::mmap::MmapReader;

let reader = MmapReader::open("data.db").unwrap();
let header = reader.slice(0, 15); // Read 15-byte header
Source

pub fn try_slice(&self, offset: usize, len: usize) -> Option<&[u8]>

Try to get a slice of bytes at the specified offset.

This is a non-panicking version of slice.

§Arguments
  • offset - Starting byte offset
  • len - Number of bytes to read
§Returns

Some(&[u8]) if the range is valid, None otherwise.

§Examples
use synadb::mmap::MmapReader;

let reader = MmapReader::open("data.db").unwrap();
if let Some(data) = reader.try_slice(0, 100) {
    println!("Read {} bytes", data.len());
}
Source

pub fn as_f32_slice(&self, offset: usize, count: usize) -> &[f32]

Get tensor data as f32 slice (zero-copy).

This method reinterprets the raw bytes as a slice of f32 values without copying the data. The data must have been written as little-endian f32 values.

§Arguments
  • offset - Starting byte offset (must be 4-byte aligned for best performance)
  • count - Number of f32 elements to read
§Returns

A slice of f32 values referencing the memory-mapped data.

§Panics

Panics if the requested range exceeds the file size. Use try_as_f32_slice for a non-panicking version.

§Safety

This method uses unsafe code to reinterpret bytes as f32. It is safe when:

  • The data was originally written as f32 values
  • The platform uses little-endian byte order
§Examples
use synadb::mmap::MmapReader;

let reader = MmapReader::open("vectors.db").unwrap();
let floats = reader.as_f32_slice(1024, 768); // Read 768-dim vector
println!("First value: {}", floats[0]);

Requirements: 2.4

Source

pub fn try_as_f32_slice(&self, offset: usize, count: usize) -> Result<&[f32]>

Try to get tensor data as f32 slice (zero-copy).

This is a non-panicking version of as_f32_slice.

§Arguments
  • offset - Starting byte offset
  • count - Number of f32 elements to read
§Returns

Ok(&[f32]) if the range is valid, Err otherwise.

§Examples
use synadb::mmap::MmapReader;

let reader = MmapReader::open("vectors.db").unwrap();
match reader.try_as_f32_slice(1024, 768) {
    Ok(floats) => println!("Read {} floats", floats.len()),
    Err(e) => println!("Error: {}", e),
}
Source

pub fn as_f64_slice(&self, offset: usize, count: usize) -> &[f64]

Get tensor data as f64 slice (zero-copy).

This method reinterprets the raw bytes as a slice of f64 values without copying the data. The data must have been written as little-endian f64 values.

§Arguments
  • offset - Starting byte offset (must be 8-byte aligned for best performance)
  • count - Number of f64 elements to read
§Returns

A slice of f64 values referencing the memory-mapped data.

§Panics

Panics if the requested range exceeds the file size. Use try_as_f64_slice for a non-panicking version.

§Safety

This method uses unsafe code to reinterpret bytes as f64. It is safe when:

  • The data was originally written as f64 values
  • The platform uses little-endian byte order
§Examples
use synadb::mmap::MmapReader;

let reader = MmapReader::open("data.db").unwrap();
let doubles = reader.as_f64_slice(0, 100);
println!("Sum: {}", doubles.iter().sum::<f64>());

Requirements: 2.4

Source

pub fn try_as_f64_slice(&self, offset: usize, count: usize) -> Result<&[f64]>

Try to get tensor data as f64 slice (zero-copy).

This is a non-panicking version of as_f64_slice.

§Arguments
  • offset - Starting byte offset
  • count - Number of f64 elements to read
§Returns

Ok(&[f64]) if the range is valid, Err otherwise.

§Examples
use synadb::mmap::MmapReader;

let reader = MmapReader::open("data.db").unwrap();
match reader.try_as_f64_slice(0, 100) {
    Ok(doubles) => println!("Read {} doubles", doubles.len()),
    Err(e) => println!("Error: {}", e),
}
Source

pub fn as_i32_slice(&self, offset: usize, count: usize) -> &[i32]

Get tensor data as i32 slice (zero-copy).

§Arguments
  • offset - Starting byte offset
  • count - Number of i32 elements to read
§Returns

A slice of i32 values referencing the memory-mapped data.

§Panics

Panics if the requested range exceeds the file size.

Source

pub fn as_i64_slice(&self, offset: usize, count: usize) -> &[i64]

Get tensor data as i64 slice (zero-copy).

§Arguments
  • offset - Starting byte offset
  • count - Number of i64 elements to read
§Returns

A slice of i64 values referencing the memory-mapped data.

§Panics

Panics if the requested range exceeds the file size.

Source

pub fn as_ptr(&self) -> *const u8

Get the raw pointer to the memory-mapped data.

This is useful for advanced use cases where direct pointer access is needed, such as GPU memory transfers.

§Safety

The returned pointer is valid only as long as this MmapReader instance exists. Do not use the pointer after dropping the reader.

§Returns

A raw pointer to the start of the memory-mapped region.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.