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
use chrono::{DateTime, Utc};
use serde::{de::DeserializeOwned, Serialize};
use sqlx::Row;

use super::error::EntityError;
use crate::primitives::DataSourceId;

#[derive(sqlx::FromRow)]
pub struct GenericEvent {
    pub id: uuid::Uuid,
    pub sequence: i32,
    pub event: serde_json::Value,
    pub entity_created_at: DateTime<Utc>,
    pub event_recorded_at: DateTime<Utc>,
}

pub trait EntityEvent: DeserializeOwned + Serialize {
    type EntityId: Into<uuid::Uuid> + From<uuid::Uuid> + Copy;

    fn event_table_name() -> &'static str
    where
        Self: Sized;
}

pub trait Entity: TryFrom<EntityEvents<<Self as Entity>::Event>, Error = EntityError> {
    type Event: EntityEvent;
}

pub struct EntityEvents<T: EntityEvent> {
    pub entity_first_persisted_at: Option<chrono::DateTime<chrono::Utc>>,
    pub latest_event_persisted_at: Option<chrono::DateTime<chrono::Utc>>,
    entity_id: <T as EntityEvent>::EntityId,
    persisted_events: Vec<T>,
    new_events: Vec<T>,
}

impl<T> EntityEvents<T>
where
    T: DeserializeOwned + Serialize + 'static + EntityEvent,
{
    pub fn init(
        id: <T as EntityEvent>::EntityId,
        initial_events: impl IntoIterator<Item = T>,
    ) -> Self {
        Self {
            entity_first_persisted_at: None,
            latest_event_persisted_at: None,
            entity_id: id,
            persisted_events: Vec::new(),
            new_events: initial_events.into_iter().collect(),
        }
    }

    pub fn push(&mut self, event: T) {
        self.new_events.push(event);
    }

    pub async fn persist(
        &mut self,
        db: &mut sqlx::Transaction<'_, sqlx::Postgres>,
    ) -> Result<usize, sqlx::Error> {
        self.persist_inner(db, None, None).await
    }

    pub async fn persisted_at(
        &mut self,
        db: &mut sqlx::Transaction<'_, sqlx::Postgres>,
        data_source: DataSourceId,
        recorded_at: DateTime<Utc>,
    ) -> Result<usize, sqlx::Error> {
        self.persist_inner(db, data_source, Some(recorded_at)).await
    }

    async fn persist_inner(
        &mut self,
        db: &mut sqlx::Transaction<'_, sqlx::Postgres>,
        data_source: impl Into<Option<DataSourceId>>,
        recorded_at: Option<DateTime<Utc>>,
    ) -> Result<usize, sqlx::Error> {
        let uuid: uuid::Uuid = self.entity_id.into();
        let source_id = data_source.into();

        if self.new_events.is_empty() {
            return Ok(0);
        }

        let mut query_builder = sqlx::QueryBuilder::new(format!(
            "INSERT INTO {} ({}id, sequence, event_type, event{})",
            <T as EntityEvent>::event_table_name(),
            source_id.map(|_| "data_source_id, ").unwrap_or(""),
            recorded_at.map(|_| ", recorded_at").unwrap_or(""),
        ));

        let sequence = self.persisted_events.len() + 1;

        query_builder.push_values(
            self.new_events.iter().enumerate(),
            |mut builder, (offset, event)| {
                let event_json = serde_json::to_value(event).expect("Could not serialize event");
                let event_type = event_json
                    .get("type")
                    .and_then(serde_json::Value::as_str)
                    .expect("Could not get type")
                    .to_owned();
                if let Some(source_id) = source_id {
                    builder.push_bind(source_id);
                }
                builder.push_bind(uuid);
                builder.push_bind((sequence + offset) as i32);
                builder.push_bind(event_type);
                builder.push_bind(event_json);
                if let Some(recorded_at) = recorded_at {
                    builder.push_bind(recorded_at);
                }
            },
        );
        query_builder.push("RETURNING recorded_at");
        let query = query_builder.build();

        let rows = query.fetch_all(&mut **db).await?;

        let recorded_at: chrono::DateTime<chrono::Utc> = rows
            .last()
            .map(|row| row.get::<chrono::DateTime<chrono::Utc>, _>("recorded_at"))
            .expect("Could not get recorded_at");

        self.latest_event_persisted_at = Some(recorded_at);
        if self.entity_first_persisted_at.is_none() {
            self.entity_first_persisted_at = Some(recorded_at);
        }

        let n_persisted = self.new_events.len();
        self.persisted_events.append(&mut self.new_events);
        Ok(n_persisted)
    }

    pub async fn batch_persist(
        db: &mut sqlx::Transaction<'_, sqlx::Postgres>,
        entities: impl IntoIterator<Item = Self>,
    ) -> Result<(), sqlx::Error> {
        let mut query_builder = sqlx::QueryBuilder::new(format!(
            "INSERT INTO {} (id, sequence, event_type, event)",
            <T as EntityEvent>::event_table_name(),
        ));

        query_builder.push_values(
            entities.into_iter().flat_map(|entity| {
                let uuid: uuid::Uuid = entity.entity_id.into();
                let sequence = entity.persisted_events.len() + 1;
                entity
                    .new_events
                    .into_iter()
                    .enumerate()
                    .map(move |(offset, event)| (uuid, (sequence + offset) as i32, event))
            }),
            |mut builder, (uuid, sequence, event)| {
                let event_json = serde_json::to_value(event).expect("Could not serialize event");
                let event_type = event_json
                    .get("type")
                    .and_then(serde_json::Value::as_str)
                    .expect("Could not get type")
                    .to_owned();
                builder.push_bind(uuid);
                builder.push_bind(sequence);
                builder.push_bind(event_type);
                builder.push_bind(event_json);
            },
        );

        let query = query_builder.build();
        query.execute(&mut **db).await?;

        Ok(())
    }

    pub fn load_first<E: Entity<Event = T>>(
        events: impl IntoIterator<Item = GenericEvent>,
    ) -> Result<E, EntityError> {
        let mut current_id = None;
        let mut current = None;
        for e in events {
            if current_id.is_none() {
                current_id = Some(e.id);
                current = Some(Self {
                    entity_first_persisted_at: Some(e.entity_created_at),
                    latest_event_persisted_at: None,
                    entity_id: e.id.into(),
                    persisted_events: Vec::new(),
                    new_events: Vec::new(),
                });
            }
            if current_id != Some(e.id) {
                break;
            }
            let cur = current.as_mut().expect("Could not get current");
            cur.latest_event_persisted_at = Some(e.event_recorded_at);
            cur.persisted_events
                .push(serde_json::from_value(e.event).expect("Could not deserialize event"));
        }
        if let Some(current) = current {
            E::try_from(current)
        } else {
            Err(EntityError::NoEntityEventsPresent)
        }
    }

    pub fn load_n<E: Entity<Event = T>>(
        events: impl IntoIterator<Item = GenericEvent>,
        n: usize,
    ) -> Result<(Vec<E>, bool), EntityError> {
        let mut ret: Vec<E> = Vec::new();
        let mut current_id = None;
        let mut current = None;
        for e in events {
            if current_id != Some(e.id) {
                if let Some(current) = current.take() {
                    ret.push(E::try_from(current)?);
                    if ret.len() == n {
                        return Ok((ret, true));
                    }
                }

                current_id = Some(e.id);
                current = Some(Self {
                    entity_first_persisted_at: Some(e.entity_created_at),
                    latest_event_persisted_at: None,
                    entity_id: e.id.into(),
                    persisted_events: Vec::new(),
                    new_events: Vec::new(),
                });
            }
            let cur = current.as_mut().expect("Could not get current");
            cur.latest_event_persisted_at = Some(e.event_recorded_at);
            cur.persisted_events
                .push(serde_json::from_value(e.event).expect("Could not deserialize event"));
        }
        if let Some(current) = current.take() {
            ret.push(E::try_from(current)?);
        }
        Ok((ret, false))
    }

    pub fn last_persisted(&self) -> impl Iterator<Item = &T> {
        std::iter::once(self.persisted_events.last().expect("No persisted events"))
    }

    pub fn iter(&self) -> impl DoubleEndedIterator<Item = &T> {
        self.persisted_events.iter().chain(self.new_events.iter())
    }
}

impl<T> IntoIterator for EntityEvents<T>
where
    T: DeserializeOwned + Serialize + 'static + EntityEvent,
{
    type Item = T;
    type IntoIter = std::iter::Chain<std::vec::IntoIter<T>, std::vec::IntoIter<T>>;

    fn into_iter(self) -> Self::IntoIter {
        self.persisted_events.into_iter().chain(self.new_events)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Debug, serde::Serialize, serde::Deserialize)]
    enum DummyEvent {
        Created(String),
    }
    impl EntityEvent for DummyEvent {
        type EntityId = uuid::Uuid;

        fn event_table_name() -> &'static str {
            "dummy_events"
        }
    }
    struct DummyEntity {
        name: String,
    }
    impl Entity for DummyEntity {
        type Event = DummyEvent;
    }
    impl TryFrom<EntityEvents<DummyEvent>> for DummyEntity {
        type Error = EntityError;
        fn try_from(events: EntityEvents<DummyEvent>) -> Result<Self, Self::Error> {
            let name = events
                .into_iter()
                .map(|e| match e {
                    DummyEvent::Created(name) => name,
                })
                .next()
                .expect("Could not find name");
            Ok(Self { name })
        }
    }

    #[test]
    fn load_zero_events() {
        let generic_events = vec![];
        let res = EntityEvents::load_first::<DummyEntity>(generic_events);
        assert!(matches!(res, Err(EntityError::NoEntityEventsPresent)));
    }

    #[test]
    fn load_first() {
        let generic_events = vec![GenericEvent {
            id: uuid::Uuid::new_v4(),
            sequence: 1,
            event: serde_json::to_value(DummyEvent::Created("dummy-name".to_owned()))
                .expect("Could not serialize"),
            entity_created_at: chrono::Utc::now(),
            event_recorded_at: chrono::Utc::now(),
        }];
        let entity: DummyEntity = EntityEvents::load_first(generic_events).expect("Could not load");
        assert!(entity.name == "dummy-name");
    }

    #[test]
    fn load_n() {
        let generic_events = vec![
            GenericEvent {
                id: uuid::Uuid::new_v4(),
                sequence: 1,
                event: serde_json::to_value(DummyEvent::Created("dummy-name".to_owned()))
                    .expect("Could not serialize"),
                entity_created_at: chrono::Utc::now(),
                event_recorded_at: chrono::Utc::now(),
            },
            GenericEvent {
                id: uuid::Uuid::new_v4(),
                sequence: 1,
                event: serde_json::to_value(DummyEvent::Created("other-name".to_owned()))
                    .expect("Could not serialize"),
                entity_created_at: chrono::Utc::now(),
                event_recorded_at: chrono::Utc::now(),
            },
        ];
        let (entity, more): (Vec<DummyEntity>, _) =
            EntityEvents::load_n(generic_events, 2).expect("Could not load");
        assert!(!more);
        assert_eq!(entity.len(), 2);
    }
}