turnframe-store 0.1.0

Object-safe persistence traits and the deterministic in-memory store for Turnframe
Documentation
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
//! [`Stores`]: one value carrying an implementation of every persistence trait.
//!
//! The runtime needs all seven stores and should not care whether they come
//! from one backend or seven. `Stores` holds them behind `Arc<dyn …>`, so it is
//! cheap to clone, shareable across tasks, and mixable: a deployment may keep
//! conversations in PostgreSQL and the outbox somewhere else without any type
//! elsewhere in the library learning about it.
//!
//! # Building one
//!
//! * [`Stores::in_memory`] for tests, examples and single-process runs.
//! * [`Stores::from_memory`] when the test needs to keep the
//!   [`MemoryStores`] handle too, to drive its clock or arm a failure.
//! * [`Stores::builder`] when the implementations differ.
//!
//! ```rust
//! use std::sync::Arc;
//!
//! use turnframe_store::memory::MemoryStores;
//! use turnframe_store::stores::Stores;
//!
//! # fn main() -> Result<(), turnframe_store::stores::StoresBuilderError> {
//! let backend = Arc::new(MemoryStores::new());
//! let stores = Stores::builder()
//!     .conversations(backend.clone())
//!     .interactions(backend.clone())
//!     .journal(backend.clone())
//!     .events(backend.clone())
//!     .outbox(backend.clone())
//!     .replay(backend.clone())
//!     .commit(backend)
//!     .build()?;
//! assert!(format!("{stores:?}").contains("conversations"));
//! # Ok(())
//! # }
//! ```

use std::fmt;
use std::sync::Arc;

use crate::commit::CommitStore;
use crate::conversation::{ConversationReader, ConversationStore};
use crate::events::{EventJournal, EventJournalReader};
use crate::interaction::{InteractionReader, InteractionStore};
use crate::journal::{CommandJournal, CommandJournalReader};
use crate::memory::MemoryStores;
use crate::outbox::{OutboxReader, OutboxStore};
use crate::replay::{ReplayReader, ReplayStore};

/// A complete set of stores.
///
/// Cloning shares the same underlying implementations; it never copies data.
#[derive(Clone)]
pub struct Stores {
    conversations: Arc<dyn ConversationStore>,
    interactions: Arc<dyn InteractionStore>,
    journal: Arc<dyn CommandJournal>,
    events: Arc<dyn EventJournal>,
    outbox: Arc<dyn OutboxStore>,
    replay: Arc<dyn ReplayStore>,
    commit: Arc<dyn CommitStore>,
}

impl Stores {
    /// An empty [`MemoryStores`] behind every trait.
    #[must_use]
    pub fn in_memory() -> Self {
        Self::from_memory(Arc::new(MemoryStores::new()))
    }

    /// Every trait served by one [`MemoryStores`] the caller keeps a handle to.
    ///
    /// Use it when the test also has to drive the store's clock or arm a
    /// [`FailurePoint`](crate::memory::FailurePoint).
    #[must_use]
    pub fn from_memory(backend: Arc<MemoryStores>) -> Self {
        Self {
            conversations: backend.clone(),
            interactions: backend.clone(),
            journal: backend.clone(),
            events: backend.clone(),
            outbox: backend.clone(),
            replay: backend.clone(),
            commit: backend,
        }
    }

    /// A builder that requires every store to be supplied.
    #[must_use]
    pub fn builder() -> StoresBuilder {
        StoresBuilder::default()
    }

    /// Conversations, turns and phase markers.
    #[must_use]
    pub fn conversations(&self) -> &Arc<dyn ConversationStore> {
        &self.conversations
    }

    /// Persisted interactions.
    #[must_use]
    pub fn interactions(&self) -> &Arc<dyn InteractionStore> {
        &self.interactions
    }

    /// The command journal.
    #[must_use]
    pub fn journal(&self) -> &Arc<dyn CommandJournal> {
        &self.journal
    }

    /// The claim ledger.
    #[must_use]
    pub fn events(&self) -> &Arc<dyn EventJournal> {
        &self.events
    }

    /// The external-effect outbox.
    #[must_use]
    pub fn outbox(&self) -> &Arc<dyn OutboxStore> {
        &self.outbox
    }

    /// Replay records.
    #[must_use]
    pub fn replay(&self) -> &Arc<dyn ReplayStore> {
        &self.replay
    }

    /// The all-or-nothing bundle writer.
    #[must_use]
    pub fn commit(&self) -> &Arc<dyn CommitStore> {
        &self.commit
    }

    /// The same persistence layer with the write side removed
    /// (spec §23, plan-only path).
    ///
    /// Each store is upcast to its read half, so the returned value has no
    /// method that changes anything and no way to recover one: there is no
    /// downcast back to [`Stores`], and [`CommitStore`] — which is write-only —
    /// is simply absent. It is what a caller hands to a path that must be
    /// unable to write, such as
    /// [`Orchestrator::plan_turn`](https://docs.rs/turnframe-runtime), so that
    /// "this path does not persist anything" is checked by the compiler rather
    /// than by review.
    #[must_use]
    pub fn read_only(&self) -> ReadOnlyStores {
        ReadOnlyStores {
            conversations: self.conversations.clone(),
            interactions: self.interactions.clone(),
            journal: self.journal.clone(),
            events: self.events.clone(),
            outbox: self.outbox.clone(),
            replay: self.replay.clone(),
        }
    }
}

/// The read half of a persistence layer: six readers, no writer of any kind.
///
/// Build one with [`Stores::read_only`], or with [`ReadOnlyStores::builder`]
/// when the reading implementations are not backed by a full [`Stores`] at all.
/// Cloning shares the same underlying implementations.
///
/// [`CommitStore`] has no read half — a commit is
/// the write — so it has no counterpart here.
#[derive(Clone)]
pub struct ReadOnlyStores {
    conversations: Arc<dyn ConversationReader>,
    interactions: Arc<dyn InteractionReader>,
    journal: Arc<dyn CommandJournalReader>,
    events: Arc<dyn EventJournalReader>,
    outbox: Arc<dyn OutboxReader>,
    replay: Arc<dyn ReplayReader>,
}

impl ReadOnlyStores {
    /// An empty [`MemoryStores`] behind every reader.
    #[must_use]
    pub fn in_memory() -> Self {
        Stores::in_memory().read_only()
    }

    /// A builder that requires every reader to be supplied.
    #[must_use]
    pub fn builder() -> ReadOnlyStoresBuilder {
        ReadOnlyStoresBuilder::default()
    }

    /// Conversations, turns and phase markers.
    #[must_use]
    pub fn conversations(&self) -> &Arc<dyn ConversationReader> {
        &self.conversations
    }

    /// Persisted interactions.
    #[must_use]
    pub fn interactions(&self) -> &Arc<dyn InteractionReader> {
        &self.interactions
    }

    /// The command journal.
    #[must_use]
    pub fn journal(&self) -> &Arc<dyn CommandJournalReader> {
        &self.journal
    }

    /// The claim ledger.
    #[must_use]
    pub fn events(&self) -> &Arc<dyn EventJournalReader> {
        &self.events
    }

    /// The external-effect outbox.
    #[must_use]
    pub fn outbox(&self) -> &Arc<dyn OutboxReader> {
        &self.outbox
    }

    /// Replay records.
    #[must_use]
    pub fn replay(&self) -> &Arc<dyn ReplayReader> {
        &self.replay
    }
}

impl From<&Stores> for ReadOnlyStores {
    fn from(stores: &Stores) -> Self {
        stores.read_only()
    }
}

impl fmt::Debug for ReadOnlyStores {
    /// Names the roles, never the implementations.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ReadOnlyStores")
            .field("roles", &ReadOnlyStoresBuilder::ROLES)
            .finish()
    }
}

/// Collects one reader per role.
#[derive(Clone, Default)]
pub struct ReadOnlyStoresBuilder {
    conversations: Option<Arc<dyn ConversationReader>>,
    interactions: Option<Arc<dyn InteractionReader>>,
    journal: Option<Arc<dyn CommandJournalReader>>,
    events: Option<Arc<dyn EventJournalReader>>,
    outbox: Option<Arc<dyn OutboxReader>>,
    replay: Option<Arc<dyn ReplayReader>>,
}

impl ReadOnlyStoresBuilder {
    /// The six role names, in the order [`ReadOnlyStoresBuilder::build`] checks
    /// them.
    pub const ROLES: [&'static str; 6] = [
        "conversations",
        "interactions",
        "journal",
        "events",
        "outbox",
        "replay",
    ];

    /// An empty builder.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the conversation reader.
    #[must_use]
    pub fn conversations(mut self, store: Arc<dyn ConversationReader>) -> Self {
        self.conversations = Some(store);
        self
    }

    /// Sets the interaction reader.
    #[must_use]
    pub fn interactions(mut self, store: Arc<dyn InteractionReader>) -> Self {
        self.interactions = Some(store);
        self
    }

    /// Sets the command journal reader.
    #[must_use]
    pub fn journal(mut self, store: Arc<dyn CommandJournalReader>) -> Self {
        self.journal = Some(store);
        self
    }

    /// Sets the event journal reader.
    #[must_use]
    pub fn events(mut self, store: Arc<dyn EventJournalReader>) -> Self {
        self.events = Some(store);
        self
    }

    /// Sets the outbox reader.
    #[must_use]
    pub fn outbox(mut self, store: Arc<dyn OutboxReader>) -> Self {
        self.outbox = Some(store);
        self
    }

    /// Sets the replay reader.
    #[must_use]
    pub fn replay(mut self, store: Arc<dyn ReplayReader>) -> Self {
        self.replay = Some(store);
        self
    }

    /// Builds the set.
    ///
    /// # Errors
    /// * [`StoresBuilderError::MissingStore`] naming the first role that was
    ///   never supplied.
    pub fn build(self) -> Result<ReadOnlyStores, StoresBuilderError> {
        fn required<T: ?Sized>(
            store: Option<Arc<T>>,
            role: &'static str,
        ) -> Result<Arc<T>, StoresBuilderError> {
            store.ok_or(StoresBuilderError::MissingStore { role })
        }

        Ok(ReadOnlyStores {
            conversations: required(self.conversations, Self::ROLES[0])?,
            interactions: required(self.interactions, Self::ROLES[1])?,
            journal: required(self.journal, Self::ROLES[2])?,
            events: required(self.events, Self::ROLES[3])?,
            outbox: required(self.outbox, Self::ROLES[4])?,
            replay: required(self.replay, Self::ROLES[5])?,
        })
    }
}

impl fmt::Debug for ReadOnlyStoresBuilder {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let supplied: Vec<&'static str> = Self::ROLES
            .iter()
            .zip([
                self.conversations.is_some(),
                self.interactions.is_some(),
                self.journal.is_some(),
                self.events.is_some(),
                self.outbox.is_some(),
                self.replay.is_some(),
            ])
            .filter_map(|(role, present)| present.then_some(*role))
            .collect();
        f.debug_struct("ReadOnlyStoresBuilder")
            .field("supplied", &supplied)
            .finish()
    }
}

impl fmt::Debug for Stores {
    /// Names the roles, never the implementations: a store may hold a
    /// connection string and this output is safe to log.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Stores")
            .field("roles", &StoresBuilder::ROLES)
            .finish()
    }
}

/// Why a [`Stores`] could not be built.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum StoresBuilderError {
    /// One of the seven roles was not supplied.
    #[error("no implementation supplied for the {role} store")]
    MissingStore {
        /// The role, one of [`StoresBuilder::ROLES`].
        role: &'static str,
    },
}

/// Collects one implementation per role.
///
/// Every role is mandatory: a half-configured persistence layer is a bug that
/// should surface at start-up, not at the first turn that needs the missing
/// store.
#[derive(Clone, Default)]
pub struct StoresBuilder {
    conversations: Option<Arc<dyn ConversationStore>>,
    interactions: Option<Arc<dyn InteractionStore>>,
    journal: Option<Arc<dyn CommandJournal>>,
    events: Option<Arc<dyn EventJournal>>,
    outbox: Option<Arc<dyn OutboxStore>>,
    replay: Option<Arc<dyn ReplayStore>>,
    commit: Option<Arc<dyn CommitStore>>,
}

impl StoresBuilder {
    /// The seven role names, in the order [`StoresBuilder::build`] checks them.
    pub const ROLES: [&'static str; 7] = [
        "conversations",
        "interactions",
        "journal",
        "events",
        "outbox",
        "replay",
        "commit",
    ];

    /// An empty builder.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the conversation store.
    #[must_use]
    pub fn conversations(mut self, store: Arc<dyn ConversationStore>) -> Self {
        self.conversations = Some(store);
        self
    }

    /// Sets the interaction store.
    #[must_use]
    pub fn interactions(mut self, store: Arc<dyn InteractionStore>) -> Self {
        self.interactions = Some(store);
        self
    }

    /// Sets the command journal.
    #[must_use]
    pub fn journal(mut self, store: Arc<dyn CommandJournal>) -> Self {
        self.journal = Some(store);
        self
    }

    /// Sets the event journal.
    #[must_use]
    pub fn events(mut self, store: Arc<dyn EventJournal>) -> Self {
        self.events = Some(store);
        self
    }

    /// Sets the outbox.
    #[must_use]
    pub fn outbox(mut self, store: Arc<dyn OutboxStore>) -> Self {
        self.outbox = Some(store);
        self
    }

    /// Sets the replay store.
    #[must_use]
    pub fn replay(mut self, store: Arc<dyn ReplayStore>) -> Self {
        self.replay = Some(store);
        self
    }

    /// Sets the commit store.
    #[must_use]
    pub fn commit(mut self, store: Arc<dyn CommitStore>) -> Self {
        self.commit = Some(store);
        self
    }

    /// Builds the set.
    ///
    /// # Errors
    /// * [`StoresBuilderError::MissingStore`] naming the first role that was
    ///   never supplied.
    pub fn build(self) -> Result<Stores, StoresBuilderError> {
        fn required<T: ?Sized>(
            store: Option<Arc<T>>,
            role: &'static str,
        ) -> Result<Arc<T>, StoresBuilderError> {
            store.ok_or(StoresBuilderError::MissingStore { role })
        }

        Ok(Stores {
            conversations: required(self.conversations, Self::ROLES[0])?,
            interactions: required(self.interactions, Self::ROLES[1])?,
            journal: required(self.journal, Self::ROLES[2])?,
            events: required(self.events, Self::ROLES[3])?,
            outbox: required(self.outbox, Self::ROLES[4])?,
            replay: required(self.replay, Self::ROLES[5])?,
            commit: required(self.commit, Self::ROLES[6])?,
        })
    }
}

impl fmt::Debug for StoresBuilder {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let supplied: Vec<&'static str> = Self::ROLES
            .iter()
            .zip([
                self.conversations.is_some(),
                self.interactions.is_some(),
                self.journal.is_some(),
                self.events.is_some(),
                self.outbox.is_some(),
                self.replay.is_some(),
                self.commit.is_some(),
            ])
            .filter_map(|(role, present)| present.then_some(*role))
            .collect();
        f.debug_struct("StoresBuilder")
            .field("supplied", &supplied)
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use turnframe_core::ids::{AccountId, ConversationId};

    #[test]
    fn a_store_set_is_shareable_across_tasks() {
        // The runtime holds one `Stores` and hands it to every turn, on any
        // executor thread. If this stops compiling, a trait lost `Send + Sync`.
        fn assert_send_sync<T: Send + Sync + 'static>() {}
        assert_send_sync::<Stores>();
        assert_send_sync::<StoresBuilder>();
        assert_send_sync::<MemoryStores>();
    }

    #[test]
    fn builder_names_the_first_missing_role() {
        assert_eq!(
            Stores::builder().build().unwrap_err(),
            StoresBuilderError::MissingStore {
                role: "conversations"
            }
        );
        let backend = Arc::new(MemoryStores::new());
        let err = Stores::builder()
            .conversations(backend.clone())
            .interactions(backend.clone())
            .build()
            .unwrap_err();
        assert_eq!(err, StoresBuilderError::MissingStore { role: "journal" });
        assert!(
            err.to_string()
                .contains("no implementation supplied for the journal store")
        );
    }

    #[test]
    fn builder_accepts_one_backend_for_every_role() {
        let backend = Arc::new(MemoryStores::new());
        let stores = Stores::builder()
            .conversations(backend.clone())
            .interactions(backend.clone())
            .journal(backend.clone())
            .events(backend.clone())
            .outbox(backend.clone())
            .replay(backend.clone())
            .commit(backend)
            .build()
            .unwrap();
        assert!(format!("{stores:?}").contains("conversations"));
    }

    #[tokio::test]
    async fn the_read_only_view_answers_what_the_full_set_answers() {
        let backend = Arc::new(MemoryStores::new());
        let stores = Stores::from_memory(backend);
        let account = AccountId::from("a");
        let conversation = ConversationId::new();
        stores
            .conversations()
            .create_conversation(crate::conversation::ConversationRecord::new(
                conversation,
                account.clone(),
                chrono::Utc::now(),
            ))
            .await
            .unwrap();

        let reading = stores.read_only();
        assert_eq!(
            reading
                .conversations()
                .load_conversation(&account, &conversation)
                .await
                .unwrap()
                .account_id,
            account
        );
        // There is nothing to call here that writes: the view carries the six
        // readers and no commit store at all.
        assert!(format!("{reading:?}").contains("conversations"));
        assert!(!format!("{reading:?}").contains("commit"));
    }

    #[test]
    fn the_read_only_builder_names_the_first_missing_role() {
        assert_eq!(
            ReadOnlyStores::builder().build().unwrap_err(),
            StoresBuilderError::MissingStore {
                role: "conversations"
            }
        );
        let backend = Arc::new(MemoryStores::new());
        let reading = ReadOnlyStores::builder()
            .conversations(backend.clone())
            .interactions(backend.clone())
            .journal(backend.clone())
            .events(backend.clone())
            .outbox(backend.clone())
            .replay(backend)
            .build()
            .unwrap();
        assert!(format!("{reading:?}").contains("replay"));
    }

    #[test]
    fn a_read_only_view_is_shareable_across_tasks() {
        fn assert_send_sync<T: Send + Sync + 'static>() {}
        assert_send_sync::<ReadOnlyStores>();
        assert_send_sync::<ReadOnlyStoresBuilder>();
    }

    #[tokio::test]
    async fn in_memory_shares_one_state_across_roles() {
        let backend = Arc::new(MemoryStores::new());
        let stores = Stores::from_memory(backend.clone());
        let account = AccountId::from("a");
        let conversation = ConversationId::new();
        stores
            .conversations()
            .create_conversation(crate::conversation::ConversationRecord::new(
                conversation,
                account.clone(),
                backend.now(),
            ))
            .await
            .unwrap();
        // The same state answers through the aggregate and through the handle.
        assert!(
            crate::conversation::ConversationReader::load_conversation(
                backend.as_ref(),
                &account,
                &conversation
            )
            .await
            .is_ok()
        );
        let cloned = stores.clone();
        assert!(
            cloned
                .conversations()
                .load_conversation(&account, &conversation)
                .await
                .is_ok()
        );
    }
}