iT邦幫忙

2023 iThome 鐵人賽

DAY 23
0
Software Development

當rust 遇上 cqrs & es系列 第 23

D23 snapshot size

  • 分享至 

  • xImage
  •  

之前snapshot是每一個事件就snapshot一份,如果改用new_snapshot_store可以設定多少事件snapshot一份,有時候如果想取折衷可以使用:

let reader_store: PersistedEventStore<SurrealEventRepository, Reader> 
    = PersistedEventStore::new_snapshot_store(repo.clone(), 2);
let book_store: PersistedEventStore<SurrealEventRepository, Book>
    = PersistedEventStore::new_snapshot_store(repo.clone(), 2);

還是昨天的範例,結果稍稍不同:

======== [Source of Truth] Snapshots ========
[
    SerializedSnapshot {
        aggregate_id: "test-book-1",
        aggregate: Object {
            "copies": Number(2),
            "description": String("使用rust,併同cqrs框架,實現event sourcing"),
            "id": String("test-book-1"),
            "isbn10": String("1234567890"),
            "lending_records": Array [
                Object {
                    "due_date": String("2023-10-15T01:46:03.015040909Z"),
                    "lent_date": String("2023-10-08T01:46:03.015040029Z"),
                    "reader_id": String("test-reader-1"),
                },
                Object {
                    "due_date": String("2023-10-15T01:46:03.023748102Z"),
                    "lent_date": String("2023-10-08T01:46:03.023733261Z"),
                    "reader_id": String("test-reader-2"),
                },
            ],
            "lent_history": Array [],
            "title": String("Rust 語言開發實戰"),
        },
        current_sequence: 4,
        current_snapshot: 2,
    },
    SerializedSnapshot {
        aggregate_id: "test-reader-1",
        aggregate: Object {
            "borrowed_books": Array [
                Object {
                    "book_id": String("test-book-1"),
                    "borrowed_date": String("2023-10-08T01:46:03.010734607Z"),
                    "due_date": String("2023-10-15T01:46:03.010743051Z"),
                    "return_date": Null,
                },
            ],
            "borrowed_history": Array [],
            "id": String("520ada11-720e-47e7-a55d-44934d85686e"),
            "name": String("John Smith"),
        },
        current_sequence: 2,
        current_snapshot: 1,
    },
    SerializedSnapshot {
        aggregate_id: "test-reader-2",
        aggregate: Object {
            "borrowed_books": Array [
                Object {
                    "book_id": String("test-book-1"),
                    "borrowed_date": String("2023-10-08T01:46:03.018704556Z"),
                    "due_date": String("2023-10-15T01:46:03.018705390Z"),
                    "return_date": Null,
                },
            ],
            "borrowed_history": Array [],
            "id": String("8c71be17-42f5-453d-ac9b-f63e7f81f9b3"),
            "name": String("Mary Ann"),
        },
        current_sequence: 2,
        current_snapshot: 1,
    },
]

可以看到把snapshot的size設為2,就會每2個event 存到soucing中產生一個snapshot,不過實務上感覺用次數切比較不實用,一般用日期切分或許比較合理,不過這可能要改動套件的原始碼才能做到。

Query的Dto都和昨天一樣:

======== [Query] Book ========
[
    BookDto {
        book_id: "test-book-1",
        title: "Rust 語言開發實戰",
        isbn10: "1234567890",
        description: "使用rust,併同cqrs框架,實現event sourcing",
        copies: 2,
        available_copies: 1,
    },
]
======== [Query] Readers ========
[
    ReaderDto {
        reader_id: "test-reader-1",
        name: "John Smith",
        books_borrowed: 0,
        books_borrows: [],
        next_due_date: None,
    },
    ReaderDto {
        reader_id: "test-reader-2",
        name: "Mary Ann",
        books_borrowed: 1,
        books_borrows: [
            ReaderBorrows {
                book_id: "test-book-1",
                book_name: "Rust 語言開發實戰",
                borrowed_date: 2023-10-08T01:46:03.018704556Z,
                due_date: 2023-10-15T01:46:03.018705390Z,
            },
        ],
        next_due_date: Some(
            2023-10-15T01:46:03.018705390Z,
        ),
    },
]

上一篇
D22 test snapshot
下一篇
D24 upcaster
系列文
當rust 遇上 cqrs & es30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言