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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use dataflow::channels::Content;
use progress::Timestamp;
use dataflow::operators::Capability;
use timely_communication::Push;
pub struct Buffer<T, D, P: Push<(T, Content<D>)>> {
time: Option<T>,
buffer: Vec<D>,
pusher: P,
}
impl<T, D, P: Push<(T, Content<D>)>> Buffer<T, D, P> where T: Eq+Clone {
pub fn new(pusher: P) -> Buffer<T, D, P> {
Buffer {
time: None,
buffer: Vec::with_capacity(Content::<D>::default_length()),
pusher: pusher,
}
}
pub fn session(&mut self, time: &T) -> Session<T, D, P> {
if let Some(true) = self.time.as_ref().map(|x| x != time) { self.flush(); }
self.time = Some(time.clone());
Session { buffer: self }
}
pub fn autoflush_session(&mut self, cap: Capability<T>) -> AutoflushSession<T, D, P> where T: Timestamp {
if let Some(true) = self.time.as_ref().map(|x| x != cap.time()) { self.flush(); }
self.time = Some(cap.time().clone());
AutoflushSession {
buffer: self,
_capability: cap,
}
}
pub fn inner(&mut self) -> &mut P { &mut self.pusher }
pub fn cease(&mut self) {
self.flush();
self.pusher.push(&mut None);
}
fn flush(&mut self) {
if !self.buffer.is_empty() {
let time = self.time.as_ref().unwrap().clone();
Content::push_at(&mut self.buffer, time, &mut self.pusher);
}
}
fn give(&mut self, data: D) {
self.buffer.push(data);
if self.buffer.len() == self.buffer.capacity() {
self.flush();
}
}
fn give_content(&mut self, content: &mut Content<D>) {
if !self.buffer.is_empty() {
self.flush();
}
let time = self.time.as_ref().unwrap().clone();
let data = ::std::mem::replace(content, Content::Typed(Vec::new()));
let mut message = Some((time, data));
self.pusher.push(&mut message);
if let Some((_, data)) = message {
*content = data;
}
}
}
pub struct Session<'a, T, D, P: Push<(T, Content<D>)>+'a> where T: Eq+Clone+'a, D: 'a {
buffer: &'a mut Buffer<T, D, P>,
}
impl<'a, T, D, P: Push<(T, Content<D>)>+'a> Session<'a, T, D, P> where T: Eq+Clone+'a, D: 'a {
#[inline(always)]
pub fn give(&mut self, data: D) {
self.buffer.give(data);
}
#[inline(always)]
pub fn give_iterator<I: Iterator<Item=D>>(&mut self, iter: I) {
for item in iter {
self.give(item);
}
}
#[inline(always)]
pub fn give_content(&mut self, message: &mut Content<D>) {
if message.len() > 0 {
self.buffer.give_content(message);
}
}
}
pub struct AutoflushSession<'a, T: Timestamp, D, P: Push<(T, Content<D>)>+'a> where
T: Eq+Clone+'a, D: 'a {
buffer: &'a mut Buffer<T, D, P>,
_capability: Capability<T>,
}
impl<'a, T: Timestamp, D, P: Push<(T, Content<D>)>+'a> AutoflushSession<'a, T, D, P> where T: Eq+Clone+'a, D: 'a {
#[inline(always)]
pub fn give(&mut self, data: D) {
self.buffer.give(data);
}
#[inline(always)]
pub fn give_iterator<I: Iterator<Item=D>>(&mut self, iter: I) {
for item in iter {
self.give(item);
}
}
#[inline(always)]
pub fn give_content(&mut self, message: &mut Content<D>) {
if message.len() > 0 {
self.buffer.give_content(message);
}
}
}
impl<'a, T: Timestamp, D, P: Push<(T, Content<D>)>+'a> Drop for AutoflushSession<'a, T, D, P> where T: Eq+Clone+'a, D: 'a {
fn drop(&mut self) {
self.buffer.cease();
}
}