-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#202 Introducing snapshotting event sourced aggregate - draft
- Loading branch information
Showing
4 changed files
with
237 additions
and
2 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
...in/kotlin/com/fraktalio/fmodel/application/EventSourcingSnapshottingAggregateExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright (c) 2023 Fraktalio D.O.O. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.fraktalio.fmodel.application | ||
|
||
import kotlinx.coroutines.flow.* | ||
|
||
/** | ||
* Extension function - Handles the command message of type [C] | ||
* | ||
* @param command Command message of type [C] | ||
* @return State of type [S] | ||
* | ||
* @author Иван Дугалић / Ivan Dugalic / @idugalic | ||
*/ | ||
fun <C, S, E, I> I.handle(command: C): Flow<E> where I : StateComputation<C, S, E>, | ||
I : EventComputation<C, S, E>, | ||
I : StateRepository<C, S>, | ||
I : EventSnapshottingRepository<C, S, E> = | ||
flow { | ||
// 1. Fetch the latest state snapshot or NULL | ||
val latestSnapshotState = command.fetchState() | ||
// 2. Fetch the latest events, since the latest state snapshot | ||
val latestEvents = command.fetchEvents(latestSnapshotState).toList() | ||
// 3. Compute the current state, based on the latest state snapshot and the latest events | ||
val currentState = latestEvents.fold(latestSnapshotState ?: initialState) { s, e -> evolve(s, e) } | ||
// 4. Compute the new events, based on the latest events, latest snapshot state and the command, and save it | ||
val newEvents = latestEvents.asFlow() | ||
.computeNewEvents(command, latestSnapshotState) | ||
.save() | ||
// 5. Compute the new state, based on the current state and the command and save it conditionally | ||
with(currentState.computeNewState(command)) { | ||
if (shouldCreateNewSnapshot(latestSnapshotState)) { | ||
save() | ||
} | ||
} | ||
emitAll(newEvents) | ||
} | ||
|
||
/** | ||
* Extension function - Handles the command message of type [C] to the locking state stored aggregate, optimistically | ||
* | ||
* @param command Command message of type [C] | ||
* @return State of type [Pair]<[S], [V]>, in which [V] is the type of the Version (optimistic locking) | ||
* | ||
* @author Иван Дугалић / Ivan Dugalic / @idugalic | ||
*/ | ||
suspend fun <C, S, E, V, I> I.handleOptimistically(command: C): Flow<Pair<E, V>> where I : StateComputation<C, S, E>, | ||
I : EventComputation<C, S, E>, | ||
I : StateLockingRepository<C, S, V>, | ||
I : EventSnapshottingLockingRepository<C, S, E, V> = | ||
flow { | ||
// 1. Fetch the latest state snapshot or NULL | ||
val (latestSnapshotState, latestSnapshotVersion) = command.fetchState() | ||
// 2. Fetch the latest events, since the latest state snapshot | ||
val latestEvents = command.fetchEvents(Pair(latestSnapshotState, latestSnapshotVersion)).toList() | ||
// 3. Compute the current state, based on the latest state snapshot and the latest events | ||
val currentState = latestEvents.fold(latestSnapshotState ?: initialState) { s, e -> evolve(s, e.first) } | ||
// 4. Get the latest event version | ||
val latestEventVersion = latestEvents.map { it.second }.lastOrNull() | ||
// 5. Compute the new events, based on the latest events, latest snapshot state and the command, and save it | ||
val newEvents = latestEvents.asFlow() | ||
.map { it.first } | ||
.computeNewEvents(command, latestSnapshotState) | ||
.save(latestEventVersion) | ||
// 6. Get the new snapshot version = the last/latest event version | ||
val newSnapshotVersion = newEvents.map { it.second }.lastOrNull() | ||
// 7. Compute the new state, based on the current state and the command and save it conditionally | ||
with(currentState.computeNewState(command)) { | ||
if (shouldCreateNewSnapshot( | ||
latestSnapshotState, | ||
latestSnapshotVersion, | ||
newSnapshotVersion | ||
) | ||
) | ||
save(latestSnapshotVersion, newSnapshotVersion) | ||
} | ||
emitAll(newEvents) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters