-
-
Notifications
You must be signed in to change notification settings - Fork 378
/
subscriber.rs
360 lines (317 loc) · 10.2 KB
/
subscriber.rs
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
use std::{
future::Future,
pin::Pin,
sync::{
atomic::{AtomicBool, Ordering::Relaxed},
mpsc::{sync_channel, Receiver, SyncSender, TryRecvError},
},
task::{Context, Poll, Waker},
time::{Duration, Instant},
};
use crate::*;
static ID_GEN: AtomicUsize = AtomicUsize::new(0);
/// An event that happened to a key that a subscriber is interested in.
#[derive(Debug, Clone)]
pub struct Event {
/// A map of batches for each tree written to in a transaction,
/// only one of which will be the one subscribed to.
pub(crate) batches: Arc<[(Tree, Batch)]>,
}
impl Event {
pub(crate) fn single_update(
tree: Tree,
key: IVec,
value: Option<IVec>,
) -> Event {
Event::single_batch(
tree,
Batch { writes: vec![(key, value)].into_iter().collect() },
)
}
pub(crate) fn single_batch(tree: Tree, batch: Batch) -> Event {
Event::from_batches(vec![(tree, batch)])
}
pub(crate) fn from_batches(batches: Vec<(Tree, Batch)>) -> Event {
Event { batches: Arc::from(batches.into_boxed_slice()) }
}
/// Iterate over each Tree, key, and optional value in this `Event`
pub fn iter<'a>(
&'a self,
) -> Box<dyn 'a + Iterator<Item = (&'a Tree, &'a IVec, &'a Option<IVec>)>>
{
self.into_iter()
}
}
impl<'a> IntoIterator for &'a Event {
type Item = (&'a Tree, &'a IVec, &'a Option<IVec>);
type IntoIter = Box<dyn 'a + Iterator<Item = Self::Item>>;
fn into_iter(self) -> Self::IntoIter {
Box::new(self.batches.iter().flat_map(|(ref tree, ref batch)| {
batch.writes.iter().map(move |(k, v_opt)| (tree, k, v_opt))
}))
}
}
type Senders = Map<usize, (Option<Waker>, SyncSender<OneShot<Option<Event>>>)>;
/// A subscriber listening on a specified prefix
///
/// `Subscriber` implements both `Iterator<Item = Event>`
/// and `Future<Output=Option<Event>>`
///
/// # Examples
///
/// Synchronous, blocking subscriber:
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use sled::{Config, Event};
/// let config = Config::new().temporary(true);
///
/// let tree = config.open()?;
///
/// // watch all events by subscribing to the empty prefix
/// let mut subscriber = tree.watch_prefix(vec![]);
///
/// let tree_2 = tree.clone();
/// let thread = std::thread::spawn(move || {
/// tree.insert(vec![0], vec![1])
/// });
///
/// // `Subscription` implements `Iterator<Item=Event>`
/// for event in subscriber.take(1) {
/// // Events occur due to single key operations,
/// // batches, or transactions. The tree is included
/// // so that you may perform a new transaction or
/// // operation in response to the event.
/// for (tree, key, value_opt) in &event {
/// if let Some(value) = value_opt {
/// // key `key` was set to value `value`
/// } else {
/// // key `key` was removed
/// }
/// }
/// }
///
/// # thread.join().unwrap();
/// # Ok(())
/// # }
/// ```
/// Aynchronous, non-blocking subscriber:
///
/// `Subscription` implements `Future<Output=Option<Event>>`.
///
/// `while let Some(event) = (&mut subscriber).await { /* use it */ }`
pub struct Subscriber {
id: usize,
rx: Receiver<OneShot<Option<Event>>>,
existing: Option<OneShot<Option<Event>>>,
home: Arc<RwLock<Senders>>,
}
impl Drop for Subscriber {
fn drop(&mut self) {
let mut w_senders = self.home.write();
w_senders.remove(&self.id);
}
}
impl Subscriber {
/// Attempts to wait for a value on this `Subscriber`, returning
/// an error if no event arrives within the provided `Duration`
/// or if the backing `Db` shuts down.
pub fn next_timeout(
&mut self,
mut timeout: Duration,
) -> std::result::Result<Event, std::sync::mpsc::RecvTimeoutError> {
loop {
let start = Instant::now();
let mut future_rx = if let Some(future_rx) = self.existing.take() {
future_rx
} else {
self.rx.recv_timeout(timeout)?
};
timeout =
if let Some(timeout) = timeout.checked_sub(start.elapsed()) {
timeout
} else {
Duration::from_nanos(0)
};
let start = Instant::now();
match future_rx.wait_timeout(timeout) {
Ok(Some(event)) => return Ok(event),
Ok(None) => (),
Err(timeout_error) => {
self.existing = Some(future_rx);
return Err(timeout_error);
}
}
timeout =
if let Some(timeout) = timeout.checked_sub(start.elapsed()) {
timeout
} else {
Duration::from_nanos(0)
};
}
}
}
impl Future for Subscriber {
type Output = Option<Event>;
fn poll(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Self::Output> {
loop {
let mut future_rx = if let Some(future_rx) = self.existing.take() {
future_rx
} else {
match self.rx.try_recv() {
Ok(future_rx) => future_rx,
Err(TryRecvError::Empty) => break,
Err(TryRecvError::Disconnected) => {
return Poll::Ready(None)
}
}
};
match Future::poll(Pin::new(&mut future_rx), cx) {
Poll::Ready(Some(event)) => return Poll::Ready(event),
Poll::Ready(None) => continue,
Poll::Pending => {
self.existing = Some(future_rx);
return Poll::Pending;
}
}
}
let mut home = self.home.write();
let entry = home.get_mut(&self.id).unwrap();
entry.0 = Some(cx.waker().clone());
Poll::Pending
}
}
impl Iterator for Subscriber {
type Item = Event;
fn next(&mut self) -> Option<Event> {
loop {
let future_rx = self.rx.recv().ok()?;
match future_rx.wait() {
Some(Some(event)) => return Some(event),
Some(None) => return None,
None => continue,
}
}
}
}
#[derive(Debug, Default)]
pub(crate) struct Subscribers {
watched: RwLock<BTreeMap<Vec<u8>, Arc<RwLock<Senders>>>>,
ever_used: AtomicBool,
}
impl Drop for Subscribers {
fn drop(&mut self) {
let watched = self.watched.read();
for senders in watched.values() {
let senders = std::mem::take(&mut *senders.write());
for (_, (waker, sender)) in senders {
drop(sender);
if let Some(waker) = waker {
waker.wake();
}
}
}
}
}
impl Subscribers {
pub(crate) fn register(&self, prefix: &[u8]) -> Subscriber {
self.ever_used.store(true, Relaxed);
let r_mu = {
let r_mu = self.watched.read();
if r_mu.contains_key(prefix) {
r_mu
} else {
drop(r_mu);
let mut w_mu = self.watched.write();
if !w_mu.contains_key(prefix) {
let old = w_mu.insert(
prefix.to_vec(),
Arc::new(RwLock::new(Map::default())),
);
assert!(old.is_none());
}
drop(w_mu);
self.watched.read()
}
};
let (tx, rx) = sync_channel(1024);
let arc_senders = &r_mu[prefix];
let mut w_senders = arc_senders.write();
let id = ID_GEN.fetch_add(1, Relaxed);
w_senders.insert(id, (None, tx));
Subscriber { id, rx, existing: None, home: arc_senders.clone() }
}
pub(crate) fn reserve_batch(
&self,
batch: &Batch,
) -> Option<ReservedBroadcast> {
if !self.ever_used.load(Relaxed) {
return None;
}
let r_mu = self.watched.read();
let mut skip_indices = std::collections::HashSet::new();
let mut subscribers = vec![];
for key in batch.writes.keys() {
for (idx, (prefix, subs_rwl)) in r_mu.iter().enumerate() {
if key.starts_with(prefix) && !skip_indices.contains(&idx) {
skip_indices.insert(idx);
let subs = subs_rwl.read();
for (_id, (waker, sender)) in subs.iter() {
let (tx, rx) = OneShot::pair();
if sender.send(rx).is_err() {
continue;
}
subscribers.push((waker.clone(), tx));
}
}
}
}
if subscribers.is_empty() {
None
} else {
Some(ReservedBroadcast { subscribers })
}
}
pub(crate) fn reserve<R: AsRef<[u8]>>(
&self,
key: R,
) -> Option<ReservedBroadcast> {
if !self.ever_used.load(Relaxed) {
return None;
}
let r_mu = self.watched.read();
let prefixes = r_mu.iter().filter(|(k, _)| key.as_ref().starts_with(k));
let mut subscribers = vec![];
for (_, subs_rwl) in prefixes {
let subs = subs_rwl.read();
for (_id, (waker, sender)) in subs.iter() {
let (tx, rx) = OneShot::pair();
if sender.send(rx).is_err() {
continue;
}
subscribers.push((waker.clone(), tx));
}
}
if subscribers.is_empty() {
None
} else {
Some(ReservedBroadcast { subscribers })
}
}
}
pub(crate) struct ReservedBroadcast {
subscribers: Vec<(Option<Waker>, OneShotFiller<Option<Event>>)>,
}
impl ReservedBroadcast {
pub fn complete(self, event: &Event) {
let iter = self.subscribers.into_iter();
for (waker, tx) in iter {
tx.fill(Some(event.clone()));
if let Some(waker) = waker {
waker.wake();
}
}
}
}