timely/dataflow/operators/
capability.rs1use std::{borrow, error::Error, fmt::Display, ops::Deref};
25use std::rc::Rc;
26use std::cell::RefCell;
27use std::fmt::{self, Debug};
28
29use crate::order::PartialOrder;
30use crate::progress::Timestamp;
31use crate::progress::ChangeBatch;
32use crate::progress::operate::PortConnectivity;
33use crate::scheduling::Activations;
34use crate::dataflow::channels::pullers::counter::ConsumedGuard;
35
36pub trait CapabilityTrait<T: Timestamp> {
38 fn time(&self) -> &T;
40 fn valid_for_output(&self, query_buffer: &Rc<RefCell<ChangeBatch<T>>>, port: usize) -> bool;
41}
42
43impl<T: Timestamp, C: CapabilityTrait<T>> CapabilityTrait<T> for &C {
44 fn time(&self) -> &T { (**self).time() }
45 fn valid_for_output(&self, query_buffer: &Rc<RefCell<ChangeBatch<T>>>, port: usize) -> bool {
46 (**self).valid_for_output(query_buffer, port)
47 }
48}
49impl<T: Timestamp, C: CapabilityTrait<T>> CapabilityTrait<T> for &mut C {
50 fn time(&self) -> &T { (**self).time() }
51 fn valid_for_output(&self, query_buffer: &Rc<RefCell<ChangeBatch<T>>>, port: usize) -> bool {
52 (**self).valid_for_output(query_buffer, port)
53 }
54}
55
56pub struct Capability<T: Timestamp> {
63 time: T,
64 internal: Rc<RefCell<ChangeBatch<T>>>,
65}
66
67impl<T: Timestamp> CapabilityTrait<T> for Capability<T> {
68 fn time(&self) -> &T { &self.time }
69 fn valid_for_output(&self, query_buffer: &Rc<RefCell<ChangeBatch<T>>>, _port: usize) -> bool {
70 Rc::ptr_eq(&self.internal, query_buffer)
71 }
72}
73
74impl<T: Timestamp> Capability<T> {
75 pub(crate) fn new(time: T, internal: Rc<RefCell<ChangeBatch<T>>>) -> Self {
78 internal.borrow_mut().update(time.clone(), 1);
79
80 Self {
81 time,
82 internal,
83 }
84 }
85
86 pub fn time(&self) -> &T {
88 &self.time
89 }
90
91 pub fn delayed(&self, new_time: &T) -> Capability<T> {
96 #[cold]
100 #[inline(never)]
101 fn delayed_panic(capability: &dyn Debug, invalid_time: &dyn Debug) -> ! {
102 panic!(
105 "Attempted to delay {:?} to {:?}, which is not beyond the capability's time.",
106 capability,
107 invalid_time,
108 )
109 }
110
111 self.try_delayed(new_time)
112 .unwrap_or_else(|| delayed_panic(self, new_time))
113 }
114
115 pub fn try_delayed(&self, new_time: &T) -> Option<Capability<T>> {
120 if self.time.less_equal(new_time) {
121 Some(Self::new(new_time.clone(), Rc::clone(&self.internal)))
122 } else {
123 None
124 }
125 }
126
127 pub fn downgrade(&mut self, new_time: &T) {
131 #[cold]
135 #[inline(never)]
136 fn downgrade_panic(capability: &dyn Debug, invalid_time: &dyn Debug) -> ! {
137 panic!(
140 "Attempted to downgrade {:?} to {:?}, which is not beyond the capability's time.",
141 capability,
142 invalid_time,
143 )
144 }
145
146 self.try_downgrade(new_time)
147 .unwrap_or_else(|_| downgrade_panic(self, new_time))
148 }
149
150 pub fn try_downgrade(&mut self, new_time: &T) -> Result<(), DowngradeError> {
154 if let Some(new_capability) = self.try_delayed(new_time) {
155 *self = new_capability;
156 Ok(())
157 } else {
158 Err(DowngradeError(()))
159 }
160 }
161}
162
163impl<T: Timestamp> Drop for Capability<T> {
167 fn drop(&mut self) {
168 self.internal.borrow_mut().update(self.time.clone(), -1);
169 }
170}
171
172impl<T: Timestamp> Clone for Capability<T> {
173 fn clone(&self) -> Capability<T> {
174 Self::new(self.time.clone(), Rc::clone(&self.internal))
175 }
176}
177
178impl<T: Timestamp> Deref for Capability<T> {
179 type Target = T;
180
181 fn deref(&self) -> &T {
182 &self.time
183 }
184}
185
186impl<T: Timestamp> Debug for Capability<T> {
187 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
188 f.debug_struct("Capability")
189 .field("time", &self.time)
190 .field("internal", &"...")
191 .finish()
192 }
193}
194
195impl<T: Timestamp> PartialEq for Capability<T> {
196 fn eq(&self, other: &Self) -> bool {
197 self.time() == other.time() && Rc::ptr_eq(&self.internal, &other.internal)
198 }
199}
200impl<T: Timestamp> Eq for Capability<T> { }
201
202impl<T: Timestamp> PartialOrder for Capability<T> {
203 fn less_equal(&self, other: &Self) -> bool {
204 self.time().less_equal(other.time()) && Rc::ptr_eq(&self.internal, &other.internal)
205 }
206}
207
208impl<T: Timestamp> ::std::hash::Hash for Capability<T> {
209 fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) {
210 self.time.hash(state);
211 }
212}
213
214#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
217pub struct DowngradeError(());
218
219impl Display for DowngradeError {
220 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
221 f.write_str("could not downgrade the given capability")
222 }
223}
224
225impl Error for DowngradeError {}
226
227type CapabilityUpdates<T> = Rc<RefCell<Vec<Rc<RefCell<ChangeBatch<T>>>>>>;
229
230pub struct InputCapability<T: Timestamp> {
238 internal: CapabilityUpdates<T>,
240 summaries: Rc<RefCell<PortConnectivity<T::Summary>>>,
242 consumed_guard: ConsumedGuard<T>,
244}
245
246impl<T: Timestamp> CapabilityTrait<T> for InputCapability<T> {
247 fn time(&self) -> &T { self.time() }
248 fn valid_for_output(&self, query_buffer: &Rc<RefCell<ChangeBatch<T>>>, port: usize) -> bool {
249 let summaries_borrow = self.summaries.borrow();
250 let internal_borrow = self.internal.borrow();
251 Rc::ptr_eq(&internal_borrow[port], query_buffer) &&
253 summaries_borrow.get(port).map_or(false, |path| path.elements() == [Default::default()])
254 }
255}
256
257impl<T: Timestamp> InputCapability<T> {
258 pub(crate) fn new(internal: CapabilityUpdates<T>, summaries: Rc<RefCell<PortConnectivity<T::Summary>>>, guard: ConsumedGuard<T>) -> Self {
261 InputCapability {
262 internal,
263 summaries,
264 consumed_guard: guard,
265 }
266 }
267
268 pub fn time(&self) -> &T {
270 self.consumed_guard.time()
271 }
272
273 pub fn delayed(&self, new_time: &T) -> Capability<T> {
278 self.delayed_for_output(new_time, 0)
279 }
280
281 pub fn delayed_for_output(&self, new_time: &T, output_port: usize) -> Capability<T> {
283 use crate::progress::timestamp::PathSummary;
284 if let Some(path) = self.summaries.borrow().get(output_port) {
285 if path.iter().flat_map(|summary| summary.results_in(self.time())).any(|time| time.less_equal(new_time)) {
286 Capability::new(new_time.clone(), Rc::clone(&self.internal.borrow()[output_port]))
287 } else {
288 panic!("Attempted to delay to a time ({:?}) not greater or equal to the operators input-output summary ({:?}) applied to the capabilities time ({:?})", new_time, path, self.time());
289 }
290 }
291 else {
292 panic!("Attempted to delay a capability for a disconnected output");
293 }
294 }
295
296 pub fn retain(self) -> Capability<T> {
304 self.retain_for_output(0)
305 }
306
307 pub fn retain_for_output(self, output_port: usize) -> Capability<T> {
311 use crate::progress::timestamp::PathSummary;
312 let self_time = self.time().clone();
313 if let Some(path) = self.summaries.borrow().get(output_port) {
314 if path.iter().flat_map(|summary| summary.results_in(&self_time)).any(|time| time.less_equal(&self_time)) {
315 Capability::new(self_time, Rc::clone(&self.internal.borrow()[output_port]))
316 }
317 else {
318 panic!("Attempted to retain a time ({:?}) not greater or equal to the operators input-output summary ({:?}) applied to the capabilities time ({:?})", self_time, path, self_time);
319 }
320 }
321 else {
322 panic!("Attempted to retain a capability for a disconnected output");
323 }
324 }
325}
326
327impl<T: Timestamp> Deref for InputCapability<T> {
328 type Target = T;
329
330 fn deref(&self) -> &T {
331 self.time()
332 }
333}
334
335impl<T: Timestamp> Debug for InputCapability<T> {
336 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
337 f.debug_struct("InputCapability")
338 .field("time", self.time())
339 .field("internal", &"...")
340 .finish()
341 }
342}
343
344#[derive(Clone, Debug)]
346pub struct ActivateCapability<T: Timestamp> {
347 pub(crate) capability: Capability<T>,
348 pub(crate) address: Rc<[usize]>,
349 pub(crate) activations: Rc<RefCell<Activations>>,
350}
351
352impl<T: Timestamp> CapabilityTrait<T> for ActivateCapability<T> {
353 fn time(&self) -> &T { self.capability.time() }
354 fn valid_for_output(&self, query_buffer: &Rc<RefCell<ChangeBatch<T>>>, port: usize) -> bool {
355 self.capability.valid_for_output(query_buffer, port)
356 }
357}
358
359impl<T: Timestamp> ActivateCapability<T> {
360 pub fn new(capability: Capability<T>, address: Rc<[usize]>, activations: Rc<RefCell<Activations>>) -> Self {
362 Self {
363 capability,
364 address,
365 activations,
366 }
367 }
368
369 pub fn time(&self) -> &T {
371 self.capability.time()
372 }
373
374 pub fn delayed(&self, time: &T) -> Self {
376 ActivateCapability {
377 capability: self.capability.delayed(time),
378 address: Rc::clone(&self.address),
379 activations: Rc::clone(&self.activations),
380 }
381 }
382
383 pub fn downgrade(&mut self, time: &T) {
385 self.capability.downgrade(time);
386 self.activations.borrow_mut().activate(&self.address);
387 }
388}
389
390impl<T: Timestamp> Drop for ActivateCapability<T> {
391 fn drop(&mut self) {
392 self.activations.borrow_mut().activate(&self.address);
393 }
394}
395
396#[derive(Clone, Debug)]
398pub struct CapabilitySet<T: Timestamp> {
399 elements: Vec<Capability<T>>,
400}
401
402impl<T: Timestamp> CapabilitySet<T> {
403
404 pub fn new() -> Self {
406 Self { elements: Vec::new() }
407 }
408
409 pub fn with_capacity(capacity: usize) -> Self {
411 Self { elements: Vec::with_capacity(capacity) }
412 }
413
414 pub fn from_elem(cap: Capability<T>) -> Self {
443 Self { elements: vec![cap] }
444 }
445
446 pub fn insert(&mut self, capability: Capability<T>) {
448 if !self.elements.iter().any(|c| c.less_equal(&capability)) {
449 self.elements.retain(|c| !capability.less_equal(c));
450 self.elements.push(capability);
451 }
452 }
453
454 pub fn delayed(&self, time: &T) -> Capability<T> {
458 #[cold]
462 #[inline(never)]
463 fn delayed_panic(invalid_time: &dyn Debug) -> ! {
464 panic!(
467 "failed to create a delayed capability, the current set does not \
468 have an element less than or equal to {:?}",
469 invalid_time,
470 )
471 }
472
473 self.try_delayed(time)
474 .unwrap_or_else(|| delayed_panic(time))
475 }
476
477 pub fn try_delayed(&self, time: &T) -> Option<Capability<T>> {
481 self.elements
482 .iter()
483 .find(|capability| capability.time().less_equal(time))
484 .and_then(|capability| capability.try_delayed(time))
485 }
486
487 pub fn downgrade<B, F>(&mut self, frontier: F)
491 where
492 B: borrow::Borrow<T>,
493 F: IntoIterator<Item = B>,
494 {
495 #[cold]
499 #[inline(never)]
500 fn downgrade_panic() -> ! {
501 panic!(
504 "Attempted to downgrade a CapabilitySet with a frontier containing an element \
505 that was not beyond an element within the set"
506 )
507 }
508
509 self.try_downgrade(frontier)
510 .unwrap_or_else(|_| downgrade_panic())
511 }
512
513 pub fn try_downgrade<B, F>(&mut self, frontier: F) -> Result<(), DowngradeError>
521 where
522 B: borrow::Borrow<T>,
523 F: IntoIterator<Item = B>,
524 {
525 let count = self.elements.len();
526 for time in frontier.into_iter() {
527 let capability = self.try_delayed(time.borrow()).ok_or(DowngradeError(()))?;
528 self.elements.push(capability);
529 }
530 self.elements.drain(..count);
531
532 Ok(())
533 }
534}
535
536impl<T> From<Vec<Capability<T>>> for CapabilitySet<T>
537where
538 T: Timestamp,
539{
540 fn from(capabilities: Vec<Capability<T>>) -> Self {
541 let mut this = Self::with_capacity(capabilities.len());
542 for capability in capabilities {
543 this.insert(capability);
544 }
545
546 this
547 }
548}
549
550impl<T: Timestamp> Default for CapabilitySet<T> {
551 fn default() -> Self {
552 Self::new()
553 }
554}
555
556impl<T: Timestamp> Deref for CapabilitySet<T> {
557 type Target=[Capability<T>];
558
559 fn deref(&self) -> &[Capability<T>] {
560 &self.elements
561 }
562}