1//! A reference-counted wrapper sharing one owned trace.
2//!
3//! The types in this module, `TraceBox` and `TraceRc` and meant to parallel `RcBox` and `Rc` in `std::rc`.
4//!
5//! The first typee is an owned trace with some information about the cumulative requirements of the shared
6//! handles. This is roughly how much progress has each made, so we know which "read capabilities" they have
7//! collectively dropped, and when it is safe to inform the trace of such progress.
8//!
9//! The second type is a wrapper which presents as a `TraceReader`, but whose methods for advancing its read
10//! capabilities interact with the `TraceBox` rather than directly with the owned trace. Ideally, instances
11//! `TraceRc` should appear indistinguishable from the underlying trace from a reading perspective, with the
12//! exception that the trace may not compact its representation as fast as if it were exclusively owned.
1314use std::rc::Rc;
15use std::cell::RefCell;
1617use timely::progress::{Antichain, frontier::{AntichainRef, MutableAntichain}};
1819use crate::trace::TraceReader;
2021/// A wrapper around a trace which tracks the frontiers of all referees.
22///
23/// This is an internal type, unlikely to be useful to higher-level programs, but exposed just in case.
24/// This type is equivalent to a `RefCell`, in that it wraps the mutable state that multiple referrers
25/// may influence.
26pub struct TraceBox<Tr: TraceReader> {
27/// accumulated holds on times for advancement.
28pub logical_compaction: MutableAntichain<Tr::Time>,
29/// accumulated holds on times for distinction.
30pub physical_compaction: MutableAntichain<Tr::Time>,
31/// The wrapped trace.
32pub trace: Tr,
33}
3435impl<Tr: TraceReader> TraceBox<Tr> {
36/// Moves an existing trace into a shareable trace wrapper.
37 ///
38 /// The trace may already exist and have non-initial advance and distinguish frontiers. The boxing
39 /// process will fish these out and make sure that they are used for the initial read capabilities.
40pub fn new(mut trace: Tr) -> Self {
4142let mut logical_compaction = MutableAntichain::new();
43 logical_compaction.update_iter(trace.get_logical_compaction().iter().cloned().map(|t| (t,1)));
44let mut physical_compaction = MutableAntichain::new();
45 physical_compaction.update_iter(trace.get_physical_compaction().iter().cloned().map(|t| (t,1)));
4647 TraceBox {
48 logical_compaction,
49 physical_compaction,
50 trace,
51 }
52 }
53/// Replaces elements of `lower` with those of `upper`.
54#[inline]
55pub fn adjust_logical_compaction(&mut self, lower: AntichainRef<Tr::Time>, upper: AntichainRef<Tr::Time>) {
56self.logical_compaction.update_iter(upper.iter().cloned().map(|t| (t,1)));
57self.logical_compaction.update_iter(lower.iter().cloned().map(|t| (t,-1)));
58self.trace.set_logical_compaction(self.logical_compaction.frontier());
59 }
60/// Replaces elements of `lower` with those of `upper`.
61#[inline]
62pub fn adjust_physical_compaction(&mut self, lower: AntichainRef<Tr::Time>, upper: AntichainRef<Tr::Time>) {
63self.physical_compaction.update_iter(upper.iter().cloned().map(|t| (t,1)));
64self.physical_compaction.update_iter(lower.iter().cloned().map(|t| (t,-1)));
65self.trace.set_physical_compaction(self.physical_compaction.frontier());
66 }
67}
6869/// A handle to a shared trace.
70///
71/// As long as the handle exists, the wrapped trace should continue to exist and will not advance its
72/// timestamps past the frontier maintained by the handle. The intent is that such a handle appears as
73/// if it is a privately maintained trace, despite being backed by shared resources.
74pub struct TraceRc<Tr: TraceReader> {
75 logical_compaction: Antichain<Tr::Time>,
76 physical_compaction: Antichain<Tr::Time>,
77/// Wrapped trace. Please be gentle when using.
78pub wrapper: Rc<RefCell<TraceBox<Tr>>>,
79}
8081impl<Tr: TraceReader> TraceReader for TraceRc<Tr> {
82type Key<'a> = Tr::Key<'a>;
83type Val<'a> = Tr::Val<'a>;
84type Time = Tr::Time;
85type TimeGat<'a> = Tr::TimeGat<'a>;
86type Diff = Tr::Diff;
87type DiffGat<'a> = Tr::DiffGat<'a>;
8889type Batch = Tr::Batch;
90type Storage = Tr::Storage;
91type Cursor = Tr::Cursor;
9293/// Sets frontier to now be elements in `frontier`.
94 ///
95 /// This change may not have immediately observable effects. It informs the shared trace that this
96 /// handle no longer requires access to times other than those in the future of `frontier`, but if
97 /// there are other handles to the same trace, it may not yet be able to compact.
98fn set_logical_compaction(&mut self, frontier: AntichainRef<Tr::Time>) {
99self.wrapper.borrow_mut().adjust_logical_compaction(self.logical_compaction.borrow(), frontier);
100self.logical_compaction = frontier.to_owned();
101 }
102fn get_logical_compaction(&mut self) -> AntichainRef<Tr::Time> { self.logical_compaction.borrow() }
103/// Allows the trace to compact batches of times before `frontier`.
104fn set_physical_compaction(&mut self, frontier: AntichainRef<Tr::Time>) {
105self.wrapper.borrow_mut().adjust_physical_compaction(self.physical_compaction.borrow(), frontier);
106self.physical_compaction = frontier.to_owned();
107 }
108fn get_physical_compaction(&mut self) -> AntichainRef<Tr::Time> { self.physical_compaction.borrow() }
109/// Creates a new cursor over the wrapped trace.
110fn cursor_through(&mut self, frontier: AntichainRef<Tr::Time>) -> Option<(Tr::Cursor, Tr::Storage)> {
111 ::std::cell::RefCell::borrow_mut(&self.wrapper).trace.cursor_through(frontier)
112 }
113114fn map_batches<F: FnMut(&Self::Batch)>(&self, f: F) {
115 ::std::cell::RefCell::borrow(&self.wrapper).trace.map_batches(f)
116 }
117}
118119impl<Tr: TraceReader> TraceRc<Tr> {
120/// Allocates a new handle from an existing wrapped wrapper.
121pub fn make_from(trace: Tr) -> (Self, Rc<RefCell<TraceBox<Tr>>>) {
122123let wrapped = Rc::new(RefCell::new(TraceBox::new(trace)));
124125let handle = TraceRc {
126 logical_compaction: wrapped.borrow().logical_compaction.frontier().to_owned(),
127 physical_compaction: wrapped.borrow().physical_compaction.frontier().to_owned(),
128 wrapper: wrapped.clone(),
129 };
130131 (handle, wrapped)
132 }
133}
134135impl<Tr: TraceReader> Clone for TraceRc<Tr> {
136fn clone(&self) -> Self {
137// increase ref counts for this frontier
138self.wrapper.borrow_mut().adjust_logical_compaction(Antichain::new().borrow(), self.logical_compaction.borrow());
139self.wrapper.borrow_mut().adjust_physical_compaction(Antichain::new().borrow(), self.physical_compaction.borrow());
140 TraceRc {
141 logical_compaction: self.logical_compaction.clone(),
142 physical_compaction: self.physical_compaction.clone(),
143 wrapper: self.wrapper.clone(),
144 }
145 }
146}
147148impl<Tr: TraceReader> Drop for TraceRc<Tr> {
149fn drop(&mut self) {
150self.wrapper.borrow_mut().adjust_logical_compaction(self.logical_compaction.borrow(), Antichain::new().borrow());
151self.wrapper.borrow_mut().adjust_physical_compaction(self.physical_compaction.borrow(), Antichain::new().borrow());
152self.logical_compaction = Antichain::new();
153self.physical_compaction = Antichain::new();
154 }
155}