pub struct MemoryMappedArray<A>{
pub shape: Vec<usize>,
pub file_path: PathBuf,
pub mode: AccessMode,
pub offset: usize,
pub size: usize,
/* private fields */
}Expand description
Memory-mapped array that efficiently maps file data directly into memory
Fields§
§shape: Vec<usize>The shape of the array
file_path: PathBufThe path to the mapped file
mode: AccessModeThe access mode
offset: usizeThe offset in the file where the data starts (in bytes)
size: usizeThe total number of elements
Implementations§
Source§impl<A> MemoryMappedArray<A>
impl<A> MemoryMappedArray<A>
Sourcepub fn clone_ref(&self) -> CoreResult<Self>
pub fn clone_ref(&self) -> CoreResult<Self>
Create a new reference to the same memory-mapped file
Sourcepub fn path(filepath: &Path, shape: &[usize]) -> Result<Self, CoreError>
pub fn path(filepath: &Path, shape: &[usize]) -> Result<Self, CoreError>
Open an existing memory-mapped array file
Sourcepub fn new<S, D>(
data: Option<&ArrayBase<S, D>>,
file_path: &Path,
mode: AccessMode,
offset: usize,
) -> Result<Self, CoreError>
pub fn new<S, D>( data: Option<&ArrayBase<S, D>>, file_path: &Path, mode: AccessMode, offset: usize, ) -> Result<Self, CoreError>
Sourcepub fn new_temp<S, D>(
data: &ArrayBase<S, D>,
mode: AccessMode,
offset: usize,
) -> Result<Self, CoreError>
pub fn new_temp<S, D>( data: &ArrayBase<S, D>, mode: AccessMode, offset: usize, ) -> Result<Self, CoreError>
Sourcepub fn as_array<D>(&self) -> Result<Array<A, D>, CoreError>where
D: Dimension,
pub fn as_array<D>(&self) -> Result<Array<A, D>, CoreError>where
D: Dimension,
Get a view of the array data as an ndarray Array with the given dimension
§Returns
An ndarray Array view of the memory-mapped data
Sourcepub fn as_array_mut<D>(&mut self) -> Result<ArrayViewMut<'_, A, D>, CoreError>where
D: Dimension,
pub fn as_array_mut<D>(&mut self) -> Result<ArrayViewMut<'_, A, D>, CoreError>where
D: Dimension,
Sourcepub fn reload(&mut self) -> Result<(), CoreError>
pub fn reload(&mut self) -> Result<(), CoreError>
Reload the memory mapping from disk
This function is useful when changes have been made to the underlying file by other processes or by direct file I/O operations.
§Returns
Ok(()) if the reload succeeded, or an error
Sourcepub fn is_temp(&self) -> bool
pub fn is_temp(&self) -> bool
Check if the array is temporary
§Returns
true if the array is backed by a temporary file, false otherwise
Source§impl<A: ZeroCopySerializable> MemoryMappedArray<A>
impl<A: ZeroCopySerializable> MemoryMappedArray<A>
Sourcepub fn save_array<S, D>(
data: &ArrayBase<S, D>,
file_path: impl AsRef<Path>,
metadata: Option<Value>,
) -> CoreResult<Self>
pub fn save_array<S, D>( data: &ArrayBase<S, D>, file_path: impl AsRef<Path>, metadata: Option<Value>, ) -> CoreResult<Self>
Create a new memory-mapped array from an existing array and save with zero-copy serialization.
This method provides a convenient way to convert a standard ndarray to a memory-mapped array with zero-copy serialization in a single operation. It’s particularly useful for initializing memory-mapped arrays with data.
§Arguments
data- The source ndarray to be converted and savedfilepath- Path where the memory-mapped array will be savedmetadata- Optional metadata to include with the array
§Returns
A new memory-mapped array with read-write access to the saved data
§Example
// Create an ndarray
let data = Array2::<f64>::from_shape_fn((100, 100), |(i, j)| (i * 100 + j) as f64);
// Create metadata
let metadata = json!({"description": "Temperature data", "units": "Celsius"});
// Convert to memory-mapped array and save
let mmap = MemoryMappedArray::<f64>::save_array(&data, "temperature.bin", Some(metadata))?;Sourcepub fn open_zero_copy(
filepath: impl AsRef<Path>,
mode: AccessMode,
) -> CoreResult<Self>
pub fn open_zero_copy( filepath: impl AsRef<Path>, mode: AccessMode, ) -> CoreResult<Self>
Open a zero-copy serialized memory-mapped array from a file.
This is a convenient wrapper around the load_zero_copy method with a more intuitive name.
It memory-maps a file containing a previously serialized array, providing efficient access
to the data.
§Arguments
filepath- Path to the file containing the serialized arraymode- Access mode (ReadOnly, ReadWrite, or CopyOnWrite)
§Returns
A memory-mapped array or an error with context
§Example
// Open a memory-mapped array with read-only access
let array = MemoryMappedArray::<f64>::open_zero_copy("data/temperature.bin", AccessMode::ReadOnly)?;
// Access the array
let ndarray = array.readonlyarray::<scirs2_core::ndarray::Ix2>()?;
println!("First value: {}", ndarray[[0, 0]]);Sourcepub fn read_metadata(filepath: impl AsRef<Path>) -> CoreResult<Value>
pub fn read_metadata(filepath: impl AsRef<Path>) -> CoreResult<Value>
Read the metadata from a zero-copy serialized file without loading the entire array.
This method efficiently extracts just the metadata from a file without memory-mapping the entire array data. This is useful for checking array properties or file information before deciding whether to load the full array.
§Arguments
filepath- Path to the file containing the serialized array
§Returns
The metadata as a JSON value or an empty JSON object if no metadata was stored
§Example
// Read metadata without loading the array
let metadata = MemoryMappedArray::<f64>::read_metadata("data/large_dataset.bin")?;
// Check properties
if let Some(created) = metadata.get("created") {
println!("Dataset created on: {}", created);
}
if let Some(dimensions) = metadata.get("dimensions") {
println!("Dataset dimensions: {}", dimensions);
}Sourcepub fn readonlyarray<D>(&self) -> CoreResult<Array<A, D>>where
D: Dimension,
pub fn readonlyarray<D>(&self) -> CoreResult<Array<A, D>>where
D: Dimension,
Get a read-only view of the array as an ndarray Array.
This method provides a convenient way to access the memory-mapped array as a standard ndarray Array with the specified dimensionality.
§Type Parameters
D- The dimensionality for the returned array (e.g., Ix1, Ix2, IxDyn)
§Returns
A read-only ndarray Array view of the memory-mapped data
§Example
let array = MemoryMappedArray::<f64>::open_zero_copy("matrix.bin", AccessMode::ReadOnly)?;
// Access as a 2D ndarray
let ndarray = array.readonlyarray::<Ix2>()?;
// Now you can use all the ndarray methods
let sum = ndarray.sum();
let mean = ndarray.mean_or(0.0);
println!("Matrix sum: {}, mean: {}", sum, mean);Sourcepub fn update_metadata(
file_path: impl AsRef<Path>,
metadata: Value,
) -> CoreResult<()>
pub fn update_metadata( file_path: impl AsRef<Path>, metadata: Value, ) -> CoreResult<()>
Update metadata in a zero-copy serialized file without rewriting the entire array.
This method efficiently updates just the metadata portion of a serialized array file without touching the actual array data. When possible, it performs the update in place to avoid creating a new file.
§Arguments
filepath- Path to the file containing the serialized arraymetadata- The new metadata to store (as JSON)
§Returns
CoreResult<()> indicating success or an error with context
§Behavior
- If the new metadata is the same size or smaller than the original, the update is done in-place
- If the new metadata is larger, the entire file is rewritten to maintain proper alignment
§Example
// Add processing information to the metadata
let updated_metadata = json!({
"description": "Temperature dataset",
"processed": true,
"processing_date": "2023-05-21",
"normalization_applied": true,
"outliers_removed": 12
});
// Update the metadata without affecting the array data
MemoryMappedArray::<f64>::update_metadata("data/temperature.bin", updated_metadata)?;Trait Implementations§
Source§impl<A: Clone + Copy + 'static + Send + Sync> AdaptiveChunking<A> for MemoryMappedArray<A>
impl<A: Clone + Copy + 'static + Send + Sync> AdaptiveChunking<A> for MemoryMappedArray<A>
Source§fn adaptive_chunking(
&self,
params: AdaptiveChunkingParams,
) -> CoreResult<AdaptiveChunkingResult>
fn adaptive_chunking( &self, params: AdaptiveChunkingParams, ) -> CoreResult<AdaptiveChunkingResult>
Source§fn process_chunks_adaptive<F, R>(
&self,
params: AdaptiveChunkingParams,
f: F,
) -> CoreResult<Vec<R>>
fn process_chunks_adaptive<F, R>( &self, params: AdaptiveChunkingParams, f: F, ) -> CoreResult<Vec<R>>
Source§fn process_chunks_mut_adaptive<F>(
&mut self,
params: AdaptiveChunkingParams,
f: F,
) -> CoreResult<()>
fn process_chunks_mut_adaptive<F>( &mut self, params: AdaptiveChunkingParams, f: F, ) -> CoreResult<()>
Source§fn process_chunks_parallel_adaptive<F, R>(
&self,
params: AdaptiveChunkingParams,
f: F,
) -> CoreResult<Vec<R>>
fn process_chunks_parallel_adaptive<F, R>( &self, params: AdaptiveChunkingParams, f: F, ) -> CoreResult<Vec<R>>
Source§impl<A: Clone + Copy + 'static + Send + Sync + Zero> ArithmeticOps<A> for MemoryMappedArray<A>
impl<A: Clone + Copy + 'static + Send + Sync + Zero> ArithmeticOps<A> for MemoryMappedArray<A>
Source§fn add(&self, other: &Self) -> CoreResult<MemoryMappedArray<A>>where
A: Add<Output = A>,
fn add(&self, other: &Self) -> CoreResult<MemoryMappedArray<A>>where
A: Add<Output = A>,
Source§fn sub(&self, other: &Self) -> CoreResult<MemoryMappedArray<A>>where
A: Sub<Output = A>,
fn sub(&self, other: &Self) -> CoreResult<MemoryMappedArray<A>>where
A: Sub<Output = A>,
Source§fn mul(&self, other: &Self) -> CoreResult<MemoryMappedArray<A>>where
A: Mul<Output = A>,
fn mul(&self, other: &Self) -> CoreResult<MemoryMappedArray<A>>where
A: Mul<Output = A>,
Source§fn div(&self, other: &Self) -> CoreResult<MemoryMappedArray<A>>where
A: Div<Output = A>,
fn div(&self, other: &Self) -> CoreResult<MemoryMappedArray<A>>where
A: Div<Output = A>,
Source§impl<A: Clone + Copy + 'static + Send + Sync + Zero> BroadcastOps<A> for MemoryMappedArray<A>
impl<A: Clone + Copy + 'static + Send + Sync + Zero> BroadcastOps<A> for MemoryMappedArray<A>
Source§fn broadcast_op<F>(
&self,
other: &Self,
f: F,
) -> CoreResult<MemoryMappedArray<A>>
fn broadcast_op<F>( &self, other: &Self, f: F, ) -> CoreResult<MemoryMappedArray<A>>
Source§impl<A> Clone for MemoryMappedArray<A>
impl<A> Clone for MemoryMappedArray<A>
Source§impl<A> DataExchange<A> for MemoryMappedArray<A>
impl<A> DataExchange<A> for MemoryMappedArray<A>
Source§fn export_data(
&self,
interface: &ZeroCopyInterface,
name: &str,
) -> CoreResult<DataId>
fn export_data( &self, interface: &ZeroCopyInterface, name: &str, ) -> CoreResult<DataId>
Source§fn from_interface(interface: &ZeroCopyInterface, name: &str) -> CoreResult<Self>
fn from_interface(interface: &ZeroCopyInterface, name: &str) -> CoreResult<Self>
Source§impl<A> Debug for MemoryMappedArray<A>
impl<A> Debug for MemoryMappedArray<A>
Source§impl<A> Drop for MemoryMappedArray<A>
impl<A> Drop for MemoryMappedArray<A>
Source§impl<A: Clone + Copy + 'static + Send + Sync> MemoryMappedChunkIter<A> for MemoryMappedArray<A>
impl<A: Clone + Copy + 'static + Send + Sync> MemoryMappedChunkIter<A> for MemoryMappedArray<A>
Source§impl<A: Clone + Copy + 'static + Send + Sync> MemoryMappedChunks<A> for MemoryMappedArray<A>
impl<A: Clone + Copy + 'static + Send + Sync> MemoryMappedChunks<A> for MemoryMappedArray<A>
Source§fn chunk_count(&self, strategy: ChunkingStrategy) -> usize
fn chunk_count(&self, strategy: ChunkingStrategy) -> usize
Source§fn process_chunks<F, R>(&self, strategy: ChunkingStrategy, f: F) -> Vec<R>
fn process_chunks<F, R>(&self, strategy: ChunkingStrategy, f: F) -> Vec<R>
Source§fn process_chunks_mut<F>(&mut self, strategy: ChunkingStrategy, f: F)
fn process_chunks_mut<F>(&mut self, strategy: ChunkingStrategy, f: F)
Source§impl<A: Clone + Copy + 'static + Send + Sync> MemoryMappedChunksParallel<A> for MemoryMappedArray<A>
Available on crate feature parallel only.
impl<A: Clone + Copy + 'static + Send + Sync> MemoryMappedChunksParallel<A> for MemoryMappedArray<A>
parallel only.Source§fn process_chunks_parallel<F, R>(
&self,
strategy: ChunkingStrategy,
f: F,
) -> Vec<R>
fn process_chunks_parallel<F, R>( &self, strategy: ChunkingStrategy, f: F, ) -> Vec<R>
Source§fn process_chunks_mut_parallel<F>(&mut self, strategy: ChunkingStrategy, f: F)
fn process_chunks_mut_parallel<F>(&mut self, strategy: ChunkingStrategy, f: F)
Source§impl<A: Clone + Copy + 'static + Send + Sync> MemoryMappedSlicing<A> for MemoryMappedArray<A>
impl<A: Clone + Copy + 'static + Send + Sync> MemoryMappedSlicing<A> for MemoryMappedArray<A>
Source§fn slice<I, E>(&self, sliceinfo: I) -> CoreResult<MemoryMappedSlice<A, E>>
fn slice<I, E>(&self, sliceinfo: I) -> CoreResult<MemoryMappedSlice<A, E>>
Source§fn slice_1d(
&self,
range: impl RangeBounds<usize>,
) -> CoreResult<MemoryMappedSlice<A, Ix1>>
fn slice_1d( &self, range: impl RangeBounds<usize>, ) -> CoreResult<MemoryMappedSlice<A, Ix1>>
Source§fn slice_2d(
&self,
row_range: impl RangeBounds<usize>,
col_range: impl RangeBounds<usize>,
) -> CoreResult<MemoryMappedSlice<A, Ix2>>
fn slice_2d( &self, row_range: impl RangeBounds<usize>, col_range: impl RangeBounds<usize>, ) -> CoreResult<MemoryMappedSlice<A, Ix2>>
Source§impl<A: Clone + Copy + 'static + Send + Sync + Zero> ZeroCopyOps<A> for MemoryMappedArray<A>
impl<A: Clone + Copy + 'static + Send + Sync + Zero> ZeroCopyOps<A> for MemoryMappedArray<A>
Source§fn map_zero_copy<F>(&self, f: F) -> CoreResult<MemoryMappedArray<A>>
fn map_zero_copy<F>(&self, f: F) -> CoreResult<MemoryMappedArray<A>>
Source§fn reduce_zero_copy<F>(&self, init: A, f: F) -> CoreResult<A>
fn reduce_zero_copy<F>(&self, init: A, f: F) -> CoreResult<A>
Source§fn combine_zero_copy<F>(
&self,
other: &Self,
f: F,
) -> CoreResult<MemoryMappedArray<A>>
fn combine_zero_copy<F>( &self, other: &Self, f: F, ) -> CoreResult<MemoryMappedArray<A>>
Source§fn filter_zero_copy<F>(&self, predicate: F) -> CoreResult<Vec<A>>
fn filter_zero_copy<F>(&self, predicate: F) -> CoreResult<Vec<A>>
Source§fn max_zero_copy(&self) -> CoreResult<A>where
A: PartialOrd,
fn max_zero_copy(&self) -> CoreResult<A>where
A: PartialOrd,
Source§fn min_zero_copy(&self) -> CoreResult<A>where
A: PartialOrd,
fn min_zero_copy(&self) -> CoreResult<A>where
A: PartialOrd,
Source§fn sum_zero_copy(&self) -> CoreResult<A>
fn sum_zero_copy(&self) -> CoreResult<A>
Source§fn product_zero_copy(&self) -> CoreResult<A>
fn product_zero_copy(&self) -> CoreResult<A>
Source§impl<A: ZeroCopySerializable> ZeroCopySerialization<A> for MemoryMappedArray<A>
impl<A: ZeroCopySerializable> ZeroCopySerialization<A> for MemoryMappedArray<A>
Source§fn save_zero_copy(
&self,
path: impl AsRef<Path>,
metadata: Option<Value>,
) -> CoreResult<()>
fn save_zero_copy( &self, path: impl AsRef<Path>, metadata: Option<Value>, ) -> CoreResult<()>
Source§fn load_zero_copy(
path: impl AsRef<Path>,
mode: AccessMode,
) -> CoreResult<MemoryMappedArray<A>>
fn load_zero_copy( path: impl AsRef<Path>, mode: AccessMode, ) -> CoreResult<MemoryMappedArray<A>>
Source§fn as_bytes_slice(&self) -> CoreResult<&[u8]>
fn as_bytes_slice(&self) -> CoreResult<&[u8]>
Source§fn as_bytes_slice_mut(&mut self) -> CoreResult<&mut [u8]>
fn as_bytes_slice_mut(&mut self) -> CoreResult<&mut [u8]>
Auto Trait Implementations§
impl<A> Freeze for MemoryMappedArray<A>
impl<A> RefUnwindSafe for MemoryMappedArray<A>where
A: RefUnwindSafe,
impl<A> Send for MemoryMappedArray<A>
impl<A> Sync for MemoryMappedArray<A>
impl<A> Unpin for MemoryMappedArray<A>where
A: Unpin,
impl<A> UnwindSafe for MemoryMappedArray<A>where
A: UnwindSafe,
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> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.