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
use parking_lot::RwLock;

use crate::{ChunkStore, ChunkStoreEvent};

// ---

// TODO(cmc): Not sure why I need the extra Box here, RwLock should be `?Sized`.
type SharedStoreSubscriber = RwLock<Box<dyn ChunkStoreSubscriber>>;

/// A [`ChunkStoreSubscriber`] subscribes to atomic changes from all [`ChunkStore`]s
/// through [`ChunkStoreEvent`]s.
///
/// [`ChunkStoreSubscriber`]s can be used to build both secondary indices and trigger systems.
//
// TODO(#4204): StoreSubscriber should require SizeBytes so they can be part of memstats.
pub trait ChunkStoreSubscriber: std::any::Any + Send + Sync {
    /// Arbitrary name for the subscriber.
    ///
    /// Does not need to be unique.
    fn name(&self) -> String;

    /// Workaround for downcasting support, simply return `self`:
    /// ```ignore
    /// fn as_any(&self) -> &dyn std::any::Any {
    ///     self
    /// }
    /// ```
    fn as_any(&self) -> &dyn std::any::Any;

    /// Workaround for downcasting support, simply return `self`:
    /// ```ignore
    /// fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
    ///     self
    /// }
    /// ```
    fn as_any_mut(&mut self) -> &mut dyn std::any::Any;

    /// The core of this trait: get notified of changes happening in all [`ChunkStore`]s.
    ///
    /// This will be called automatically by the [`ChunkStore`] itself if the subscriber has been
    /// registered: [`ChunkStore::register_subscriber`].
    /// Or you might want to feed it [`ChunkStoreEvent`]s manually, depending on your use case.
    ///
    /// ## Example
    ///
    /// ```ignore
    /// fn on_events(&mut self, events: &[ChunkStoreEvent]) {
    ///     use re_chunk_store::ChunkStoreDiffKind;
    ///     for event in events {
    ///         match event.kind {
    ///             ChunkStoreDiffKind::Addition => println!("Row added: {}", event.row_id),
    ///             ChunkStoreDiffKind::Deletion => println!("Row removed: {}", event.row_id),
    ///         }
    ///     }
    /// }
    /// ```
    fn on_events(&mut self, events: &[ChunkStoreEvent]);
}

/// All registered [`ChunkStoreSubscriber`]s.
static SUBSCRIBERS: once_cell::sync::Lazy<RwLock<Vec<SharedStoreSubscriber>>> =
    once_cell::sync::Lazy::new(|| RwLock::new(Vec::new()));

#[derive(Debug, Clone, Copy)]
pub struct ChunkStoreSubscriberHandle(u32);

impl ChunkStore {
    /// Registers a [`ChunkStoreSubscriber`] so it gets automatically notified when data gets added and/or
    /// removed to/from a [`ChunkStore`].
    ///
    /// Refer to [`ChunkStoreEvent`]'s documentation for more information about these events.
    ///
    /// ## Scope
    ///
    /// Registered [`ChunkStoreSubscriber`]s are global scope: they get notified of all events from all
    /// existing [`ChunkStore`]s, including [`ChunkStore`]s created after the subscriber was registered.
    ///
    /// Use [`ChunkStoreEvent::store_id`] to identify the source of an event.
    ///
    /// ## Late registration
    ///
    /// Subscribers must be registered before a store gets created to guarantee that no events
    /// were missed.
    ///
    /// [`ChunkStoreEvent::event_id`] can be used to identify missing events.
    ///
    /// ## Ordering
    ///
    /// The order in which registered subscribers are notified is undefined and will likely become
    /// concurrent in the future.
    ///
    /// If you need a specific order across multiple subscribers, embed them into an orchestrating
    /// subscriber.
    //
    // TODO(cmc): send a compacted snapshot to late registerers for bootstrapping
    pub fn register_subscriber(
        subscriber: Box<dyn ChunkStoreSubscriber>,
    ) -> ChunkStoreSubscriberHandle {
        let mut subscribers = SUBSCRIBERS.write();
        subscribers.push(RwLock::new(subscriber));
        ChunkStoreSubscriberHandle(subscribers.len() as u32 - 1)
    }

    /// Passes a reference to the downcasted subscriber to the given `FnMut` callback.
    ///
    /// Returns `None` if the subscriber doesn't exist or downcasting failed.
    pub fn with_subscriber<V: ChunkStoreSubscriber, T, F: FnMut(&V) -> T>(
        ChunkStoreSubscriberHandle(handle): ChunkStoreSubscriberHandle,
        mut f: F,
    ) -> Option<T> {
        let subscribers = SUBSCRIBERS.read();
        subscribers.get(handle as usize).and_then(|subscriber| {
            let subscriber = subscriber.read();
            subscriber.as_any().downcast_ref::<V>().map(&mut f)
        })
    }

    /// Passes a reference to the downcasted subscriber to the given `FnOnce` callback.
    ///
    /// Returns `None` if the subscriber doesn't exist or downcasting failed.
    pub fn with_subscriber_once<V: ChunkStoreSubscriber, T, F: FnOnce(&V) -> T>(
        ChunkStoreSubscriberHandle(handle): ChunkStoreSubscriberHandle,
        f: F,
    ) -> Option<T> {
        let subscribers = SUBSCRIBERS.read();
        subscribers.get(handle as usize).and_then(|subscriber| {
            let subscriber = subscriber.read();
            subscriber.as_any().downcast_ref::<V>().map(f)
        })
    }

    /// Passes a mutable reference to the downcasted subscriber to the given callback.
    ///
    /// Returns `None` if the subscriber doesn't exist or downcasting failed.
    pub fn with_subscriber_mut<V: ChunkStoreSubscriber, T, F: FnMut(&mut V) -> T>(
        ChunkStoreSubscriberHandle(handle): ChunkStoreSubscriberHandle,
        mut f: F,
    ) -> Option<T> {
        let subscribers = SUBSCRIBERS.read();
        subscribers.get(handle as usize).and_then(|subscriber| {
            let mut subscriber = subscriber.write();
            subscriber.as_any_mut().downcast_mut::<V>().map(&mut f)
        })
    }

    /// Called by [`ChunkStore`]'s mutating methods to notify subscriber subscribers of upcoming events.
    pub(crate) fn on_events(events: &[ChunkStoreEvent]) {
        re_tracing::profile_function!();
        let subscribers = SUBSCRIBERS.read();
        // TODO(cmc): might want to parallelize at some point.
        for subscriber in subscribers.iter() {
            subscriber.write().on_events(events);
        }
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use ahash::HashSet;

    use re_chunk::{Chunk, RowId};
    use re_log_types::{
        example_components::{MyColor, MyIndex, MyPoint},
        StoreId, TimePoint, Timeline,
    };

    use crate::{ChunkStore, ChunkStoreSubscriber, GarbageCollectionOptions};

    use super::*;

    /// A simple [`ChunkStoreSubscriber`] for test purposes that just accumulates [`ChunkStoreEvent`]s.
    #[derive(Debug)]
    struct AllEvents {
        store_ids: HashSet<StoreId>,
        events: Vec<ChunkStoreEvent>,
    }

    impl AllEvents {
        fn new(store_ids: impl IntoIterator<Item = StoreId>) -> Self {
            Self {
                store_ids: store_ids.into_iter().collect(),
                events: Vec::new(),
            }
        }
    }

    impl ChunkStoreSubscriber for AllEvents {
        fn name(&self) -> String {
            "rerun.testing.store_subscribers.AllEvents".into()
        }

        fn as_any(&self) -> &dyn std::any::Any {
            self
        }

        fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
            self
        }

        fn on_events(&mut self, events: &[ChunkStoreEvent]) {
            self.events.extend(
                events
                    .iter()
                    // NOTE: `cargo` implicitly runs tests in parallel!
                    .filter(|e| self.store_ids.contains(&e.store_id))
                    .cloned(),
            );
        }
    }

    #[test]
    fn store_subscriber() -> anyhow::Result<()> {
        let mut store1 = ChunkStore::new(
            re_log_types::StoreId::random(re_log_types::StoreKind::Recording),
            Default::default(),
        );
        let mut store = ChunkStore::new(
            re_log_types::StoreId::random(re_log_types::StoreKind::Recording),
            Default::default(),
        );

        let mut expected_events = Vec::new();

        let view = AllEvents::new([store1.id().clone(), store.id().clone()]);
        let view_handle = ChunkStore::register_subscriber(Box::new(view));

        let timeline_frame = Timeline::new_sequence("frame");
        let timeline_other = Timeline::new_temporal("other");
        let timeline_yet_another = Timeline::new_sequence("yet_another");

        let chunk = Chunk::builder("entity_a".into())
            .with_component_batch(
                RowId::new(),
                TimePoint::from_iter([
                    (timeline_frame, 42),      //
                    (timeline_other, 666),     //
                    (timeline_yet_another, 1), //
                ]),
                &MyIndex::from_iter(0..10),
            )
            .build()?;

        expected_events.extend(store1.insert_chunk(&Arc::new(chunk))?);

        let chunk = {
            let num_instances = 3;
            let points: Vec<_> = (0..num_instances)
                .map(|i| MyPoint::new(0.0, i as f32))
                .collect();
            let colors = vec![MyColor::from(0xFF0000FF)];
            Chunk::builder("entity_b".into())
                .with_component_batches(
                    RowId::new(),
                    TimePoint::from_iter([
                        (timeline_frame, 42),      //
                        (timeline_yet_another, 1), //
                    ]),
                    [&points as _, &colors as _],
                )
                .build()?
        };

        expected_events.extend(store.insert_chunk(&Arc::new(chunk))?);

        let chunk = {
            let num_instances = 6;
            let colors = vec![MyColor::from(0x00DD00FF); num_instances];
            Chunk::builder("entity_b".into())
                .with_component_batches(
                    RowId::new(),
                    TimePoint::default(),
                    [
                        &MyIndex::from_iter(0..num_instances as _) as _,
                        &colors as _,
                    ],
                )
                .build()?
        };

        expected_events.extend(store1.insert_chunk(&Arc::new(chunk))?);

        expected_events.extend(store1.gc(&GarbageCollectionOptions::gc_everything()).0);
        expected_events.extend(store.gc(&GarbageCollectionOptions::gc_everything()).0);

        ChunkStore::with_subscriber::<AllEvents, _, _>(view_handle, |got| {
            similar_asserts::assert_eq!(expected_events.len(), got.events.len());
            similar_asserts::assert_eq!(expected_events, got.events);
        });

        Ok(())
    }
}