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
impl MmapReader
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Check if the memory-mapped file is empty.
§Returns
true if the file has zero length, false otherwise.
Sourcepub fn slice(&self, offset: usize, len: usize) -> &[u8] ⓘ
pub fn slice(&self, offset: usize, len: usize) -> &[u8] ⓘ
Get a slice of bytes at the specified offset.
§Arguments
offset- Starting byte offsetlen- 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 headerSourcepub fn try_slice(&self, offset: usize, len: usize) -> Option<&[u8]>
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 offsetlen- 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());
}Sourcepub fn as_f32_slice(&self, offset: usize, count: usize) -> &[f32]
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
Sourcepub fn try_as_f32_slice(&self, offset: usize, count: usize) -> Result<&[f32]>
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 offsetcount- 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),
}Sourcepub fn as_f64_slice(&self, offset: usize, count: usize) -> &[f64]
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
Sourcepub fn try_as_f64_slice(&self, offset: usize, count: usize) -> Result<&[f64]>
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 offsetcount- 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),
}Sourcepub fn as_i32_slice(&self, offset: usize, count: usize) -> &[i32]
pub fn as_i32_slice(&self, offset: usize, count: usize) -> &[i32]
Sourcepub fn as_i64_slice(&self, offset: usize, count: usize) -> &[i64]
pub fn as_i64_slice(&self, offset: usize, count: usize) -> &[i64]
Sourcepub fn as_ptr(&self) -> *const u8
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.