differential_dataflow/operators/arrange/
writer.rs

1//! Write endpoint for a sequence of batches.
2//!
3//! A `TraceWriter` accepts a sequence of batches and distributes them
4//! to both a shared trace and to a sequence of private queues.
5
6use std::rc::{Rc, Weak};
7use std::cell::RefCell;
8
9use timely::progress::Antichain;
10
11use crate::trace::{Trace, Batch, BatchReader};
12use crate::trace::wrappers::rc::TraceBox;
13
14
15use super::TraceAgentQueueWriter;
16use super::TraceReplayInstruction;
17
18/// Write endpoint for a sequence of batches.
19///
20/// A `TraceWriter` accepts a sequence of batches and distributes them
21/// to both a shared trace and to a sequence of private queues.
22pub struct TraceWriter<Tr: Trace> {
23    /// Current upper limit.
24    upper: Antichain<Tr::Time>,
25    /// Shared trace, possibly absent (due to weakness).
26    trace: Weak<RefCell<TraceBox<Tr>>>,
27    /// A sequence of private queues into which batches are written.
28    queues: Rc<RefCell<Vec<TraceAgentQueueWriter<Tr>>>>,
29}
30
31impl<Tr: Trace> TraceWriter<Tr> {
32    /// Creates a new `TraceWriter`.
33    pub fn new(
34        upper: Vec<Tr::Time>,
35        trace: Weak<RefCell<TraceBox<Tr>>>,
36        queues: Rc<RefCell<Vec<TraceAgentQueueWriter<Tr>>>>
37    ) -> Self
38    {
39        let mut temp = Antichain::new();
40        temp.extend(upper);
41        Self { upper: temp, trace, queues }
42    }
43
44    /// Exerts merge effort, even without additional updates.
45    pub fn exert(&mut self) {
46        if let Some(trace) = self.trace.upgrade() {
47            trace.borrow_mut().trace.exert();
48        }
49    }
50
51    /// Advances the trace by `batch`.
52    ///
53    /// The `hint` argument is either `None` in the case of an empty batch,
54    /// or is `Some(time)` for a time less or equal to all updates in the
55    /// batch and which is suitable for use as a capability.
56    pub fn insert(&mut self, batch: Tr::Batch, hint: Option<Tr::Time>) {
57
58        // Something is wrong if not a sequence.
59        if !(&self.upper == batch.lower()) {
60            println!("{:?} vs {:?}", self.upper, batch.lower());
61        }
62        assert!(&self.upper == batch.lower());
63        assert!(batch.lower() != batch.upper());
64
65        self.upper.clone_from(batch.upper());
66
67        // push information to each listener that still exists.
68        let mut borrow = self.queues.borrow_mut();
69        for queue in borrow.iter_mut() {
70            if let Some(pair) = queue.upgrade() {
71                pair.1.borrow_mut().push_back(TraceReplayInstruction::Batch(batch.clone(), hint.clone()));
72                pair.1.borrow_mut().push_back(TraceReplayInstruction::Frontier(batch.upper().clone()));
73                pair.0.activate();
74            }
75        }
76        borrow.retain(|w| w.upgrade().is_some());
77
78        // push data to the trace, if it still exists.
79        if let Some(trace) = self.trace.upgrade() {
80            trace.borrow_mut().trace.insert(batch);
81        }
82
83    }
84
85    /// Inserts an empty batch up to `upper`.
86    pub fn seal(&mut self, upper: Antichain<Tr::Time>) {
87        if self.upper != upper {
88            self.insert(Tr::Batch::empty(self.upper.clone(), upper), None);
89        }
90    }
91}
92
93impl<Tr: Trace> Drop for TraceWriter<Tr> {
94    fn drop(&mut self) {
95        self.seal(Antichain::new())
96    }
97}