pub struct MultiBufBatch<K: Debug + Ord + Hash, I: Debug> { /* private fields */ }Expand description
Collects items into multiple batches based on stream key.
A batch may become ready after collecting max_size number of items or until max_duration has elapsed
since first item was appended to the batch.
Batch item buffers are cached and reused to avoid allocations.
This base implementation does not handle actual awaiting for batch duration timeout.
Implementations§
Source§impl<K, I> MultiBufBatch<K, I>
impl<K, I> MultiBufBatch<K, I>
Sourcepub fn new(max_size: usize, max_duration: Duration) -> MultiBufBatch<K, I>
pub fn new(max_size: usize, max_duration: Duration) -> MultiBufBatch<K, I>
Crates new instance with given maximum batch size (max_size) and maximum duration (max_duration) that
batch can last since first item appended to it.
Panics if max_size == 0.
Sourcepub fn poll(&self) -> PollResult<K>
pub fn poll(&self) -> PollResult<K>
Checks if batch has reached one of its limits.
Returns:
PollResult::Ready(K)- batch for stream keyKhas reached one of its limit and is ready to be consumed,PollResult::NotReady(None)- batch is not ready yet and has no items appeded yet,PollResult::NotReady(Some(duration))- batch is not ready yet but it will be ready after time duration due to duration limit.
Sourcepub fn append(&mut self, key: K, item: I)
pub fn append(&mut self, key: K, item: I)
Appends item to batch with given stream key.
It is an contract error to append batch that is ready according to self.poll().
Panics if batch has already reached its max_size limit.
Sourcepub fn outstanding(&self) -> impl Iterator<Item = &K>
pub fn outstanding(&self) -> impl Iterator<Item = &K>
Lists keys of outstanding batches.
Sourcepub fn drain(&mut self, key: &K) -> Option<Drain<'_, I>>
pub fn drain(&mut self, key: &K) -> Option<Drain<'_, I>>
Consumes batch by draining items from internal buffer.
Sourcepub fn flush(&mut self) -> Vec<(K, Vec<I>)>
pub fn flush(&mut self) -> Vec<(K, Vec<I>)>
Flushes all outstanding batches starting from oldest.
Sourcepub fn get(&self, key: &K) -> Option<&[I]>
pub fn get(&self, key: &K) -> Option<&[I]>
Returns slice of internal item buffer of given outstanding batch.
Sourcepub fn clear_cache(&mut self)
pub fn clear_cache(&mut self)
Drops cached batch buffers.