Split EventStream and EventStore
This commit is contained in:
@@ -13,7 +13,7 @@ import kotlin.concurrent.withLock
|
||||
*/
|
||||
class GameEventHandler(
|
||||
private val eventBus: GameEventBus,
|
||||
private val eventStream: GameEventStream,
|
||||
private val eventStore: GameEventStore,
|
||||
private val versionBuilder: VersionBuilder,
|
||||
) : EventHandler<GameEvent, GameId> {
|
||||
private val projectionsBuilders: ConcurrentLinkedQueue<(GameEvent) -> Unit> = ConcurrentLinkedQueue()
|
||||
@@ -31,7 +31,7 @@ class GameEventHandler(
|
||||
.computeIfAbsent(aggregateId) { ReentrantLock() }
|
||||
.withLock {
|
||||
buildEvent(versionBuilder.buildNextVersion(aggregateId))
|
||||
.also { eventStream.publish(it) }
|
||||
.also { eventStore.publish(it) }
|
||||
}.also { event ->
|
||||
projectionsBuilders.forEach { it(event) }
|
||||
eventBus.publish(event)
|
||||
|
||||
12
src/main/kotlin/eventDemo/app/event/GameEventStore.kt
Normal file
12
src/main/kotlin/eventDemo/app/event/GameEventStore.kt
Normal file
@@ -0,0 +1,12 @@
|
||||
package eventDemo.app.event
|
||||
|
||||
import eventDemo.app.entity.GameId
|
||||
import eventDemo.app.event.event.GameEvent
|
||||
import eventDemo.libs.event.EventStore
|
||||
|
||||
/**
|
||||
* A stream to publish and read the played card event.
|
||||
*/
|
||||
class GameEventStore(
|
||||
private val eventStore: EventStore<GameEvent, GameId>,
|
||||
) : EventStore<GameEvent, GameId> by eventStore
|
||||
@@ -1,6 +1,5 @@
|
||||
package eventDemo.app.event
|
||||
|
||||
import eventDemo.app.entity.GameId
|
||||
import eventDemo.app.event.event.GameEvent
|
||||
import eventDemo.libs.event.EventStream
|
||||
|
||||
@@ -8,8 +7,8 @@ import eventDemo.libs.event.EventStream
|
||||
* A stream to publish and read the played card event.
|
||||
*/
|
||||
class GameEventStream(
|
||||
private val eventStream: EventStream<GameEvent, GameId>,
|
||||
) : EventStream<GameEvent, GameId> by eventStream {
|
||||
private val eventStream: EventStream<GameEvent>,
|
||||
) : EventStream<GameEvent> by eventStream {
|
||||
override fun publish(event: GameEvent) {
|
||||
eventStream.publish(event)
|
||||
}
|
||||
|
||||
@@ -2,17 +2,17 @@ package eventDemo.app.event.projection
|
||||
|
||||
import eventDemo.app.entity.GameId
|
||||
import eventDemo.app.event.GameEventHandler
|
||||
import eventDemo.app.event.GameEventStream
|
||||
import eventDemo.app.event.GameEventStore
|
||||
import eventDemo.app.event.event.GameEvent
|
||||
|
||||
class GameStateRepository(
|
||||
eventStream: GameEventStream,
|
||||
eventStore: GameEventStore,
|
||||
eventHandler: GameEventHandler,
|
||||
snapshotConfig: SnapshotConfig = SnapshotConfig(),
|
||||
) {
|
||||
private val projectionsSnapshot =
|
||||
ProjectionSnapshotRepositoryInMemory(
|
||||
eventStream = eventStream,
|
||||
eventStore = eventStore,
|
||||
snapshotCacheConfig = snapshotConfig,
|
||||
applyToProjection = GameState::apply,
|
||||
initialStateBuilder = { aggregateId: GameId -> GameState(aggregateId) },
|
||||
|
||||
@@ -2,6 +2,7 @@ package eventDemo.app.event.projection
|
||||
|
||||
import eventDemo.libs.event.AggregateId
|
||||
import eventDemo.libs.event.Event
|
||||
import eventDemo.libs.event.EventStore
|
||||
import eventDemo.libs.event.EventStream
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import kotlinx.datetime.Clock
|
||||
@@ -21,7 +22,7 @@ data class SnapshotConfig(
|
||||
)
|
||||
|
||||
class ProjectionSnapshotRepositoryInMemory<E : Event<ID>, P : Projection<ID>, ID : AggregateId>(
|
||||
private val eventStream: EventStream<E, ID>,
|
||||
private val eventStore: EventStore<E, ID>,
|
||||
private val initialStateBuilder: (ID) -> P,
|
||||
private val snapshotCacheConfig: SnapshotConfig = SnapshotConfig(),
|
||||
private val applyToProjection: P.(event: E) -> P,
|
||||
@@ -77,10 +78,9 @@ class ProjectionSnapshotRepositoryInMemory<E : Event<ID>, P : Projection<ID>, ID
|
||||
}
|
||||
|
||||
val missingEventOfSnapshot =
|
||||
eventStream.readVersionBetween(
|
||||
event.aggregateId,
|
||||
(lastSnapshot?.lastEventVersion ?: 1)..event.version,
|
||||
)
|
||||
eventStore
|
||||
.getStream(event.aggregateId)
|
||||
.readVersionBetween((lastSnapshot?.lastEventVersion ?: 1)..event.version)
|
||||
|
||||
return if (lastSnapshot?.lastEventVersion == event.version) {
|
||||
lastSnapshot
|
||||
@@ -164,8 +164,9 @@ class ProjectionSnapshotRepositoryInMemory<E : Event<ID>, P : Projection<ID>, ID
|
||||
private fun getEventAfterTheSnapshot(
|
||||
aggregateId: ID,
|
||||
snapshot: P?,
|
||||
) = eventStream
|
||||
.readGreaterOfVersion(aggregateId, snapshot?.lastEventVersion ?: 0)
|
||||
) = eventStore
|
||||
.getStream(aggregateId)
|
||||
.readGreaterOfVersion(snapshot?.lastEventVersion ?: 0)
|
||||
|
||||
/**
|
||||
* Apply events to the projection.
|
||||
|
||||
Reference in New Issue
Block a user