1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use std::rc::Rc;
use std::cell::RefCell;
use progress::ChangeBatch;
use dataflow::channels::Content;
use Push;
pub struct Counter<T: Ord, D, P: Push<(T, Content<D>)>> {
pushee: P,
produced: Rc<RefCell<ChangeBatch<T>>>,
phantom: ::std::marker::PhantomData<D>,
}
impl<T: Ord, D, P: Push<(T, Content<D>)>> Push<(T, Content<D>)> for Counter<T, D, P> where T : Eq+Clone+'static {
#[inline(always)]
fn push(&mut self, message: &mut Option<(T, Content<D>)>) {
if let Some((ref time, ref data)) = *message {
self.produced.borrow_mut().update(time.clone(), data.len() as i64);
}
if message.is_some() || !self.produced.borrow_mut().is_empty() {
self.pushee.push(message);
}
}
}
impl<T, D, P: Push<(T, Content<D>)>> Counter<T, D, P> where T : Ord+Clone+'static {
pub fn new(pushee: P) -> Counter<T, D, P> {
Counter {
pushee: pushee,
produced: Rc::new(RefCell::new(ChangeBatch::new())),
phantom: ::std::marker::PhantomData,
}
}
#[inline(always)]
pub fn produced(&self) -> &Rc<RefCell<ChangeBatch<T>>> {
&self.produced
}
}