pub struct MmapReader { /* private fields */ }Expand description
A memory-mapped reader for binary sequence files
This reader provides efficient access to binary sequence files by memory-mapping them instead of performing traditional I/O operations. It supports both sequential access to individual records and parallel processing of records across multiple threads.
The reader ensures thread-safety through the use of Arc for sharing the
memory-mapped data between threads.
Records are returned as RefRecord which implement the BinseqRecord trait.
§Examples
use binseq::bq::MmapReader;
use binseq::Result;
fn main() -> Result<()> {
let path = "./data/subset.bq";
let reader = MmapReader::new(path)?;
// Calculate the number of records in the file
let num_records = reader.num_records();
println!("Number of records: {}", num_records);
// Get the record at index 20 (0-indexed)
let record = reader.get(20)?;
Ok(())
}Implementations§
Source§impl MmapReader
impl MmapReader
Sourcepub fn new<P>(path: P) -> Result<MmapReader, Error>
pub fn new<P>(path: P) -> Result<MmapReader, Error>
Creates a new memory-mapped reader for a binary sequence file
This method opens the file, memory-maps its contents, and validates the file structure to ensure it contains valid binary sequence data.
§Arguments
path- Path to the binary sequence file
§Returns
Ok(MmapReader)- A new reader if the file is validErr(Error)- If the file is invalid or cannot be opened
§Errors
Returns an error if:
- The file cannot be opened
- The file is not a regular file
- The file header is invalid
- The file size doesn’t match the expected size based on the header
Sourcepub fn num_records(&self) -> usize
pub fn num_records(&self) -> usize
Returns the total number of records in the file
This is calculated by subtracting the header size from the total file size and dividing by the size of each record.
Sourcepub fn header(&self) -> FileHeader
pub fn header(&self) -> FileHeader
Returns a copy of the binary sequence file header
The header contains format information and sequence length specifications.
Sourcepub fn set_default_quality_score(&mut self, score: u8)
pub fn set_default_quality_score(&mut self, score: u8)
Sets the default quality score for records without quality information
Sourcepub fn build_qbuf(&self) -> Vec<u8> ⓘ
pub fn build_qbuf(&self) -> Vec<u8> ⓘ
Creates a new quality score buffer
Trait Implementations§
Source§impl ParallelReader for MmapReader
Parallel processing implementation for memory-mapped readers
impl ParallelReader for MmapReader
Parallel processing implementation for memory-mapped readers
Source§fn process_parallel<P>(
self,
processor: P,
num_threads: usize,
) -> Result<(), Error>where
P: ParallelProcessor + Clone + 'static,
fn process_parallel<P>(
self,
processor: P,
num_threads: usize,
) -> Result<(), Error>where
P: ParallelProcessor + Clone + 'static,
Processes all records in parallel using multiple threads
This method distributes the records across the specified number of threads and processes them using the provided processor. Each thread receives its own clone of the processor and processes a contiguous chunk of records.
§Arguments
processor- The processor to use for handling recordsnum_threads- The number of threads to use for processing
§Type Parameters
P- A type that implementsParallelProcessorand can be cloned
§Returns
Ok(())- If all records were processed successfullyErr(Error)- If an error occurred during processing
Source§fn process_parallel_range<P>(
self,
processor: P,
num_threads: usize,
range: Range<usize>,
) -> Result<(), Error>where
P: ParallelProcessor + Clone + 'static,
fn process_parallel_range<P>(
self,
processor: P,
num_threads: usize,
range: Range<usize>,
) -> Result<(), Error>where
P: ParallelProcessor + Clone + 'static,
Process records in parallel within a specified range
This method allows parallel processing of a subset of records within the file, defined by a start and end index. The range is distributed across the specified number of threads.
§Arguments
processor- The processor to use for each recordnum_threads- The number of threads to spawnrange- The range of record indices to process
§Type Parameters
P- A type that implementsParallelProcessorand can be cloned
§Returns
Ok(())- If all records were processed successfullyErr(Error)- If an error occurred during processing
Auto Trait Implementations§
impl Freeze for MmapReader
impl RefUnwindSafe for MmapReader
impl Send for MmapReader
impl Sync for MmapReader
impl Unpin for MmapReader
impl UnwindSafe for MmapReader
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 more