timely/dataflow/channels/pushers/
buffer.rs1use crate::communication::Push;
5use crate::container::{ContainerBuilder, CapacityContainerBuilder, PushInto};
6use crate::dataflow::channels::Message;
7use crate::dataflow::operators::Capability;
8use crate::progress::Timestamp;
9use crate::{Container, Data};
10
11#[derive(Debug)]
16pub struct Buffer<T, CB, P> {
17 time: Option<T>,
19 builder: CB,
21 pusher: P,
23}
24
25impl<T, CB: Default, P> Buffer<T, CB, P> {
26 pub fn new(pusher: P) -> Self {
28 Self {
29 time: None,
30 builder: Default::default(),
31 pusher,
32 }
33 }
34
35 pub fn inner(&mut self) -> &mut P { &mut self.pusher }
39
40 pub fn builder(&self) -> &CB {
43 &self.builder
44 }
45}
46
47impl<T, C: Container + Data, P: Push<Message<T, C>>> Buffer<T, CapacityContainerBuilder<C>, P> where T: Eq+Clone {
48 #[inline]
50 pub fn session(&mut self, time: &T) -> Session<T, CapacityContainerBuilder<C>, P> {
51 self.session_with_builder(time)
52 }
53
54 #[inline]
56 pub fn autoflush_session(&mut self, cap: Capability<T>) -> AutoflushSession<T, CapacityContainerBuilder<C>, P> where T: Timestamp {
57 self.autoflush_session_with_builder(cap)
58 }
59}
60
61impl<T, CB: ContainerBuilder, P: Push<Message<T, CB::Container>>> Buffer<T, CB, P> where T: Eq+Clone {
62 pub fn session_with_builder(&mut self, time: &T) -> Session<T, CB, P> {
64 if let Some(true) = self.time.as_ref().map(|x| x != time) { self.flush(); }
65 self.time = Some(time.clone());
66 Session { buffer: self }
67 }
68
69 pub fn autoflush_session_with_builder(&mut self, cap: Capability<T>) -> AutoflushSession<T, CB, P> where T: Timestamp {
71 if let Some(true) = self.time.as_ref().map(|x| x != cap.time()) { self.flush(); }
72 self.time = Some(cap.time().clone());
73 AutoflushSession {
74 buffer: self,
75 _capability: cap,
76 }
77 }
78}
79
80impl<T, CB: ContainerBuilder, P: Push<Message<T, CB::Container>>> Buffer<T, CB, P> where T: Eq+Clone {
81 pub fn cease(&mut self) {
83 self.flush();
84 self.pusher.push(&mut None);
85 }
86
87 #[inline]
89 fn extract_and_send(&mut self) {
90 while let Some(container) = self.builder.extract() {
91 let time = self.time.as_ref().unwrap().clone();
92 Message::push_at(container, time, &mut self.pusher);
93 }
94 }
95
96 #[inline]
98 fn flush(&mut self) {
99 while let Some(container) = self.builder.finish() {
100 let time = self.time.as_ref().unwrap().clone();
101 Message::push_at(container, time, &mut self.pusher);
102 }
103 }
104
105 fn give_container(&mut self, container: &mut CB::Container) {
111 if !container.is_empty() {
112 self.flush();
113 let time = self.time.as_ref().unwrap().clone();
114 Message::push_at(container, time, &mut self.pusher);
115 }
116 }
117
118 #[inline]
120 fn push_internal<D>(&mut self, item: D) where CB: PushInto<D> {
121 self.builder.push_into(item);
122 self.extract_and_send();
123 }
124}
125
126pub struct Session<'a, T, CB, P> {
132 buffer: &'a mut Buffer<T, CB, P>,
133}
134
135impl<'a, T, CB: ContainerBuilder, P> Session<'a, T, CB, P>
136where
137 T: Eq + Clone + 'a,
138 P: Push<Message<T, CB::Container>> + 'a,
139{
140 pub fn give_container(&mut self, container: &mut CB::Container) {
143 self.buffer.give_container(container)
144 }
145}
146
147impl<'a, T, CB, P> Session<'a, T, CB, P>
148where
149 T: Eq + Clone + 'a,
150 CB: ContainerBuilder + 'a,
151 P: Push<Message<T, CB::Container>> + 'a
152{
153 pub fn builder(&self) -> &CB {
156 self.buffer.builder()
157 }
158
159 #[inline]
161 pub fn give<D>(&mut self, data: D) where CB: PushInto<D> {
162 self.push_into(data);
163 }
164
165 #[inline]
167 pub fn give_iterator<I>(&mut self, iter: I)
168 where
169 I: Iterator,
170 CB: PushInto<I::Item>,
171 {
172 for item in iter {
173 self.push_into(item);
174 }
175 }
176}
177
178impl<'a, T, CB, P, D> PushInto<D> for Session<'a, T, CB, P>
179where
180 T: Eq + Clone + 'a,
181 CB: ContainerBuilder + PushInto<D> + 'a,
182 P: Push<Message<T, CB::Container>> + 'a,
183{
184 #[inline]
185 fn push_into(&mut self, item: D) {
186 self.buffer.push_internal(item);
187 }
188}
189
190pub struct AutoflushSession<'a, T, CB, P>
192where
193 T: Timestamp + 'a,
194 CB: ContainerBuilder + 'a,
195 P: Push<Message<T, CB::Container>> + 'a,
196{
197 buffer: &'a mut Buffer<T, CB, P>,
199 _capability: Capability<T>,
201}
202
203impl<'a, T, CB, P> AutoflushSession<'a, T, CB, P>
204where
205 T: Timestamp + 'a,
206 CB: ContainerBuilder + 'a,
207 P: Push<Message<T, CB::Container>> + 'a,
208{
209 #[inline]
211 pub fn give<D>(&mut self, data: D)
212 where
213 CB: PushInto<D>,
214 {
215 self.push_into(data);
216 }
217
218 #[inline]
220 pub fn give_iterator<I, D>(&mut self, iter: I)
221 where
222 I: Iterator<Item=D>,
223 CB: PushInto<D>,
224 {
225 for item in iter {
226 self.push_into(item);
227 }
228 }
229}
230impl<'a, T, CB, P, D> PushInto<D> for AutoflushSession<'a, T, CB, P>
231where
232 T: Timestamp + 'a,
233 CB: ContainerBuilder + PushInto<D> + 'a,
234 P: Push<Message<T, CB::Container>> + 'a,
235{
236 #[inline]
237 fn push_into(&mut self, item: D) {
238 self.buffer.push_internal(item);
239 }
240}
241
242impl<'a, T, CB, P> Drop for AutoflushSession<'a, T, CB, P>
243where
244 T: Timestamp + 'a,
245 CB: ContainerBuilder + 'a,
246 P: Push<Message<T, CB::Container>> + 'a,
247{
248 fn drop(&mut self) {
249 self.buffer.cease();
250 }
251}