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
use crate::{EntityId, Event, EventBus, Model};
use chrono::prelude::*;
use riker::actors::*;
use std::collections::HashMap;
use std::ops::RangeTo;

/// An actor that handles the persistance of events of entities
/// using "commits" to track who made a change and why.  
#[derive(Debug)]
pub struct Store<T: Model> {
    entities: HashMap<EntityId, (Commit<T>, Vec<Commit<T>>)>,
    bus: Option<EventBus<T>>,
}
impl<T: Model> Default for Store<T> {
    fn default() -> Self {
        Self {
            bus: None,
            entities: HashMap::new(),
        }
    }
}

type Author = Option<String>;
type Reason = Option<String>;

/// Commits represent changes to the system about to be persisted
#[derive(Debug, Clone)]
pub struct Commit<T: Model> {
    event: Event<T>,
    when: DateTime<Utc>,
    who: Author,
    why: Reason,
}
impl<T: Model> Commit<T> {
    pub fn new(event: Event<T>, who: Author, why: Reason) -> Self {
        Commit {
            event,
            when: Utc::now(),
            who,
            why,
        }
    }

    pub fn entity_id(&self) -> EntityId {
        self.event.entity_id()
    }
}
impl<T: Model> From<Event<T>> for Commit<T> {
    fn from(e: Event<T>) -> Self {
        Commit::new(e, None, None)
    }
}

impl<T: Model> Store<T> {
    fn make_snapshot(&self, id: EntityId, _until: DateTime<Utc>) -> Option<T> {
        let (commit, updates) = self.entities.get(&id)?;
        let entity = match commit.event.clone() {
            Event::Create(e) => e,
            Event::Change(_, _) => unreachable!(),
        };
        let snapshot = updates.iter().fold(entity, |mut e, up| {
            let update = match up.event.clone() {
                Event::Create(_) => unreachable!(),
                Event::Change(_, u) => u,
            };
            T::apply_change(&mut e, update);
            e
        });
        Some(snapshot)
    }
}

impl<T: Model> Actor for Store<T> {
    type Msg = StoreMsg<T>;
    fn recv(&mut self, cx: &Context<Self::Msg>, msg: Self::Msg, sender: Sender) {
        match msg {
            StoreMsg::Commit(msg) => self.receive(cx, msg, sender),
            StoreMsg::Subscribe(msg) => self.receive(cx, msg, sender),
            StoreMsg::Snapshot(msg) => self.receive(cx, msg, sender),
            StoreMsg::SnapshotList(msg) => self.receive(cx, msg, sender),
        };
    }
}

impl<T: Model> ActorFactoryArgs<EventBus<T>> for Store<T> {
    fn create_args(bus: EventBus<T>) -> Self {
        Store {
            bus: Some(bus),
            ..Default::default()
        }
    }
}

impl<T: Model> Receive<Commit<T>> for Store<T> {
    type Msg = StoreMsg<T>;
    fn receive(&mut self, cx: &Context<Self::Msg>, c: Commit<T>, _sender: Sender) {
        trace!("storing {:?}", c);
        let id = c.event.entity_id();
        let commit = c.clone();
        match c.event {
            Event::Create(_) => {
                self.entities.insert(id, (commit, vec![]));
            }
            Event::Change(_, _) => {
                let (_, updates) = self.entities.get_mut(&id).expect("entity exists");
                updates.push(commit);
            }
        }
        if self.bus.is_some() {
            self.bus.as_ref().unwrap().tell(
                Publish {
                    topic: cx.myself.name().into(),
                    msg: c.event,
                },
                None,
            );
        }
        debug!("saved commit for {}", id);
    }
}

impl<T: Model> Receive<(EntityId, DateTime<Utc>)> for Store<T> {
    type Msg = StoreMsg<T>;
    fn receive(
        &mut self,
        _cx: &Context<Self::Msg>,
        (id, until): (EntityId, DateTime<Utc>),
        sender: Sender,
    ) {
        let snapshot = self.make_snapshot(id, until);
        if snapshot.is_some() {
            debug!("loaded snapshot for {}", id);
        } else {
            debug!("entity {} not in store", id);
        }
        sender
            .unwrap()
            .try_tell(snapshot, None)
            .expect("can receive snapshot");
    }
}

impl<T: Model> Receive<RangeTo<DateTime<Utc>>> for Store<T> {
    type Msg = StoreMsg<T>;
    fn receive(
        &mut self,
        _ctx: &Context<Self::Msg>,
        range: RangeTo<DateTime<Utc>>,
        sender: Sender,
    ) {
        let snaps = self
            .entities
            .keys()
            .map(|id| self.make_snapshot(*id, range.end).unwrap())
            .collect::<Vec<_>>();
        trace!("{:?}", snaps);
        debug!("loaded list of snapshots until {}", range.end);
        sender
            .unwrap()
            .try_tell(snaps, None)
            .expect("can receive snapshot list");
    }
}

impl<T: Model> Receive<EntityId> for Store<T> {
    type Msg = StoreMsg<T>;
    fn receive(&mut self, _cx: &Context<Self::Msg>, _id: EntityId, _sender: Sender) {
        todo!();
    }
}

#[derive(Debug, Clone)]
pub enum StoreMsg<T: Model> {
    Commit(Commit<T>),
    Snapshot((EntityId, DateTime<Utc>)),
    SnapshotList(RangeTo<DateTime<Utc>>),
    Subscribe(EntityId),
}
impl<T: Model> From<Event<T>> for StoreMsg<T> {
    fn from(msg: Event<T>) -> Self {
        StoreMsg::Commit(msg.into())
    }
}
impl<T: Model> From<RangeTo<DateTime<Utc>>> for StoreMsg<T> {
    fn from(range: RangeTo<DateTime<Utc>>) -> Self {
        StoreMsg::SnapshotList(range)
    }
}
impl<T: Model> From<Commit<T>> for StoreMsg<T> {
    fn from(msg: Commit<T>) -> Self {
        StoreMsg::Commit(msg)
    }
}
impl<T: Model> From<EntityId> for StoreMsg<T> {
    fn from(id: EntityId) -> Self {
        StoreMsg::Subscribe(id)
    }
}
impl<T: Model> From<(EntityId, DateTime<Utc>)> for StoreMsg<T> {
    fn from(snap: (EntityId, DateTime<Utc>)) -> Self {
        StoreMsg::Snapshot(snap)
    }
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;
    use futures::executor::block_on;
    use riker_patterns::ask::ask;

    #[derive(Default, Clone, Debug)]
    pub struct TestCount {
        id: EntityId,
        pub count: i16,
    }
    impl TestCount {
        pub fn new(c: i16) -> Self {
            Self {
                count: c,
                id: EntityId::new(),
            }
        }
    }
    #[derive(Clone, Debug)]
    pub enum Op {
        Add(i16),
        Sub(i16),
    }
    impl Model for TestCount {
        type Change = Op;
        fn id(&self) -> EntityId {
            self.id
        }
        fn apply_change(&mut self, change: Self::Change) {
            match change {
                Op::Add(n) => self.count += n,
                Op::Sub(n) => self.count -= n,
            };
        }
    }

    #[test]
    fn load_snapshot() {
        let sys = ActorSystem::new().unwrap();
        let store = sys.actor_of::<Store<TestCount>>("test-counts").unwrap();

        let test = TestCount::default();
        let id = test.id();
        store.tell(Event::Create(test), None);
        store.tell(Event::Change(id, Op::Add(15)), None);
        store.tell(Event::Change(id, Op::Add(5)), None);
        store.tell(Event::Change(id, Op::Sub(9)), None);
        store.tell(Event::Change(id, Op::Add(31)), None);

        let result: Option<TestCount> = block_on(ask(&sys, &store, (id, Utc::now())));
        assert_eq!(result.unwrap().count, 42);
    }

    #[test]
    fn non_existing_entity() {
        let sys = ActorSystem::new().unwrap();
        let store = sys.actor_of::<Store<TestCount>>("test-counts").unwrap();

        let result: Option<TestCount> = block_on(ask(&sys, &store, ("123".into(), Utc::now())));
        assert!(result.is_none());
    }

    #[test]
    fn load_list_of_snapshots() {
        let sys = ActorSystem::new().unwrap();
        let store = sys.actor_of::<Store<TestCount>>("test-counts").unwrap();

        let some_counter = TestCount {
            id: "123".into(),
            count: 42,
        };
        store.tell(Event::Create(some_counter), None);
        store.tell(Event::Create(TestCount::default()), None);
        store.tell(Event::Create(TestCount::default()), None);
        store.tell(Event::Change("123".into(), Op::Add(8)), None);

        let result: Vec<TestCount> = block_on(ask(&sys, &store, ..Utc::now()));
        assert_eq!(result.len(), 3);
        let some_counter_snapshot = result.iter().find(|s| s.id() == "123".into()).unwrap();
        assert_eq!(some_counter_snapshot.count, 50);
    }

    #[test]
    fn broadcast_event() {
        let sys = ActorSystem::new().unwrap();
        let bus: EventBus<_> = channel("bus", &sys).unwrap();
        let store_name = "test-counts";
        let store = sys
            .actor_of_args::<Store<TestCount>, _>(store_name, bus.clone())
            .unwrap();

        #[derive(Clone, Debug)]
        pub struct Get;
        #[derive(Clone, Debug)]
        enum TestSubMsg {
            Event(Event<TestCount>),
            Get,
        }
        impl From<Event<TestCount>> for TestSubMsg {
            fn from(event: Event<TestCount>) -> Self {
                TestSubMsg::Event(event)
            }
        }

        #[derive(Default)]
        struct TestSub(Option<Event<TestCount>>);
        impl Actor for TestSub {
            type Msg = TestSubMsg;
            fn recv(&mut self, _cx: &Context<Self::Msg>, msg: Self::Msg, sender: Sender) {
                match msg {
                    TestSubMsg::Get => {
                        sender.unwrap().try_tell(self.0.clone(), None).unwrap();
                    }
                    TestSubMsg::Event(e) => {
                        self.0 = Some(e);
                    }
                }
            }
        }

        let sub = sys.actor_of::<TestSub>("subscriber").unwrap();
        bus.tell(
            Subscribe {
                topic: store_name.into(),
                actor: Box::new(sub.clone()),
            },
            None,
        );

        store.tell(Event::Create(TestCount::default()), None);

        let result: Option<Event<TestCount>> = block_on(ask(&sys, &sub, TestSubMsg::Get));

        assert!(result.is_some());
    }
}