use std::time::Duration;
use data::BatchData;
use limiter::BatchLimiter;
use super::{data, limiter};
pub struct BatchConfigParts<L, D> {
pub batch_limiter: L,
pub batch_data: D,
pub timeout: Duration,
}
pub trait BatchConfig<T> {
type ItemMetadata;
type Batch;
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
fn take_batch(&mut self) -> Self::Batch;
fn push(&mut self, item: T, metadata: Self::ItemMetadata);
fn is_batch_full(&self) -> bool;
fn item_fits_in_batch(&self, item: &T) -> (bool, Self::ItemMetadata);
fn timeout(&self) -> Duration;
}
impl<T, L, B> BatchConfig<T> for BatchConfigParts<L, B>
where
L: BatchLimiter<T, B>,
B: BatchData<T>,
{
type ItemMetadata = L::ItemMetadata;
type Batch = B::Batch;
fn len(&self) -> usize {
self.batch_data.len()
}
fn take_batch(&mut self) -> Self::Batch {
self.batch_limiter.reset();
self.batch_data.take_batch()
}
fn push(&mut self, item: T, metadata: Self::ItemMetadata) {
self.batch_data.push_item(item);
self.batch_limiter.push_item(metadata);
}
fn is_batch_full(&self) -> bool {
self.batch_limiter.is_batch_full(&self.batch_data)
}
fn item_fits_in_batch(&self, item: &T) -> (bool, Self::ItemMetadata) {
self.batch_limiter
.item_fits_in_batch(item, &self.batch_data)
}
fn timeout(&self) -> Duration {
self.timeout
}
}