vector_stream/batcher/
config.rs1use std::time::Duration;
2
3use data::BatchData;
4use limiter::BatchLimiter;
5
6use super::{data, limiter};
7
8pub struct BatchConfigParts<L, D> {
9 pub batch_limiter: L,
10 pub batch_data: D,
11 pub timeout: Duration,
12}
13
14pub trait BatchConfig<T> {
15 type ItemMetadata;
16 type Batch;
17
18 fn len(&self) -> usize;
20
21 fn is_empty(&self) -> bool {
23 self.len() == 0
24 }
25
26 fn take_batch(&mut self) -> Self::Batch;
28
29 fn push(&mut self, item: T, metadata: Self::ItemMetadata);
31
32 fn is_batch_full(&self) -> bool;
34
35 fn item_fits_in_batch(&self, item: &T) -> (bool, Self::ItemMetadata);
40
41 fn timeout(&self) -> Duration;
44}
45
46impl<T, L, B> BatchConfig<T> for BatchConfigParts<L, B>
47where
48 L: BatchLimiter<T, B>,
49 B: BatchData<T>,
50{
51 type ItemMetadata = L::ItemMetadata;
52 type Batch = B::Batch;
53
54 fn len(&self) -> usize {
55 self.batch_data.len()
56 }
57
58 fn take_batch(&mut self) -> Self::Batch {
59 self.batch_limiter.reset();
60 self.batch_data.take_batch()
61 }
62
63 fn push(&mut self, item: T, metadata: Self::ItemMetadata) {
64 self.batch_data.push_item(item);
65 self.batch_limiter.push_item(metadata);
66 }
67
68 fn is_batch_full(&self) -> bool {
69 self.batch_limiter.is_batch_full(&self.batch_data)
70 }
71
72 fn item_fits_in_batch(&self, item: &T) -> (bool, Self::ItemMetadata) {
73 self.batch_limiter
74 .item_fits_in_batch(item, &self.batch_data)
75 }
76
77 fn timeout(&self) -> Duration {
78 self.timeout
79 }
80}