pub struct BlockStreamIterator {
pub all_blocks: Vec<(String, Vec<u8>)>,
pub position: usize,
pub config: StreamConfig,
pub buffer: VecDeque<BlockChunk>,
pub state: BlockStreamState,
pub stats: StreamStats,
}Expand description
Synchronous, single-threaded streaming iterator over a block collection.
§Backpressure
If the internal buffer reaches StreamConfig::max_buffer_chunks chunks,
next_chunk returns None and
increments StreamStats::backpressure_events. The caller must call
drain_one before production can resume.
§Example
use ipfrs_storage::block_stream::{BlockStreamIterator, StreamConfig};
let blocks: Vec<(String, Vec<u8>)> = (0u8..10)
.map(|i| (format!("cid-{i}"), vec![i; 32]))
.collect();
let mut iter = BlockStreamIterator::new(blocks, StreamConfig::default());
while !iter.is_exhausted() {
if let Some(chunk) = iter.next_chunk() {
println!("chunk {}: {} blocks", chunk.chunk_index, chunk.len());
} else {
// backpressure — drain before continuing
iter.drain_one();
}
}
// drain any remaining buffered chunks
while let Some(_chunk) = iter.drain_one() {}Fields§
§all_blocks: Vec<(String, Vec<u8>)>Source (CID, data) pairs.
position: usizeCurrent read position in all_blocks.
config: StreamConfigStream configuration.
buffer: VecDeque<BlockChunk>Buffered but not yet consumed chunks.
state: BlockStreamStateCurrent state of the iterator.
stats: StreamStatsAccumulated statistics.
Implementations§
Source§impl BlockStreamIterator
impl BlockStreamIterator
Sourcepub fn new(blocks: Vec<(String, Vec<u8>)>, config: StreamConfig) -> Self
pub fn new(blocks: Vec<(String, Vec<u8>)>, config: StreamConfig) -> Self
Construct a new iterator over blocks with the given config.
Sourcepub fn next_chunk(&mut self) -> Option<BlockChunk>
pub fn next_chunk(&mut self) -> Option<BlockChunk>
Attempt to produce the next chunk.
Returns None when:
- The buffer is already at capacity (backpressure — call
drain_one). - The iterator is exhausted.
On success, advances position, pushes a BlockChunk onto the
buffer, and returns a clone of that chunk.
Sourcepub fn drain_one(&mut self) -> Option<BlockChunk>
pub fn drain_one(&mut self) -> Option<BlockChunk>
Remove and return the oldest buffered chunk.
Updates StreamStats::chunks_drained and StreamStats::bytes_streamed.
If the buffer becomes empty and all source blocks have been consumed,
transitions to BlockStreamState::Exhausted.
Sourcepub fn fill_buffer(&mut self)
pub fn fill_buffer(&mut self)
Fill the buffer from source blocks up to max_buffer_chunks.
Calls next_chunk repeatedly until the buffer is
full, backpressure fires, or the source is exhausted.
Sourcepub fn is_exhausted(&self) -> bool
pub fn is_exhausted(&self) -> bool
Returns true when all blocks have been produced and the buffer
has been fully drained.
Sourcepub fn remaining_blocks(&self) -> usize
pub fn remaining_blocks(&self) -> usize
Number of source blocks not yet chunked.
Sourcepub fn buffered_chunks(&self) -> usize
pub fn buffered_chunks(&self) -> usize
Number of chunks currently sitting in the buffer.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for BlockStreamIterator
impl RefUnwindSafe for BlockStreamIterator
impl Send for BlockStreamIterator
impl Sync for BlockStreamIterator
impl Unpin for BlockStreamIterator
impl UnsafeUnpin for BlockStreamIterator
impl UnwindSafe for BlockStreamIterator
Blanket Implementations§
impl<T> Allocation for T
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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 more