From 23b304fdbdf3e9417b615b906ab9dd41799d6c9f Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Sun, 23 Mar 2025 22:43:33 +0100 Subject: [PATCH] create interface ProjectionSnapshotRepository --- .../ProjectionSnapshotRepository.kt | 28 +++++++++++++++++++ .../ProjectionSnapshotRepositoryInMemory.kt | 10 +++---- 2 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/eventDemo/libs/event/projection/ProjectionSnapshotRepository.kt diff --git a/src/main/kotlin/eventDemo/libs/event/projection/ProjectionSnapshotRepository.kt b/src/main/kotlin/eventDemo/libs/event/projection/ProjectionSnapshotRepository.kt new file mode 100644 index 0000000..b14d363 --- /dev/null +++ b/src/main/kotlin/eventDemo/libs/event/projection/ProjectionSnapshotRepository.kt @@ -0,0 +1,28 @@ +package eventDemo.libs.event.projection + +import eventDemo.libs.event.AggregateId +import eventDemo.libs.event.Event + +interface ProjectionSnapshotRepository, P : Projection, ID : AggregateId> { + /** + * Create a snapshot for the event + */ + fun applyAndPutToCache(event: E): P + + /** + * Build the list of all [Projections][Projection] + */ + fun getList(): List

+ + /** + * Build the last version of the [Projection] from the cache. + */ + fun getLast(aggregateId: ID): P + + /** + * Build the [Projection] to the specific [event][Event]. + * + * It does not contain the [events][Event] it after this one. + */ + fun getUntil(event: E): P +} diff --git a/src/main/kotlin/eventDemo/libs/event/projection/ProjectionSnapshotRepositoryInMemory.kt b/src/main/kotlin/eventDemo/libs/event/projection/ProjectionSnapshotRepositoryInMemory.kt index fe5200f..3ab7850 100644 --- a/src/main/kotlin/eventDemo/libs/event/projection/ProjectionSnapshotRepositoryInMemory.kt +++ b/src/main/kotlin/eventDemo/libs/event/projection/ProjectionSnapshotRepositoryInMemory.kt @@ -16,7 +16,7 @@ class ProjectionSnapshotRepositoryInMemory, P : Projection, ID private val initialStateBuilder: (aggregateId: ID) -> P, private val snapshotCacheConfig: SnapshotConfig = SnapshotConfig(), private val applyToProjection: P.(event: E) -> P, -) { +) : ProjectionSnapshotRepository { private val projectionsSnapshot: ConcurrentHashMap>> = ConcurrentHashMap() /** @@ -29,7 +29,7 @@ class ProjectionSnapshotRepositoryInMemory, P : Projection, ID * 5. save it * 6. remove old one */ - fun applyAndPutToCache(event: E): P = + override fun applyAndPutToCache(event: E): P = getUntil(event) .also { save(it) @@ -39,7 +39,7 @@ class ProjectionSnapshotRepositoryInMemory, P : Projection, ID /** * Build the list of all [Projections][Projection] */ - fun getList(): List

= + override fun getList(): List

= projectionsSnapshot.map { (id, b) -> getLast(id) } @@ -51,7 +51,7 @@ class ProjectionSnapshotRepositoryInMemory, P : Projection, ID * 2. get the missing event to the snapshot * 3. apply the missing events to the snapshot */ - fun getLast(aggregateId: ID): P { + override fun getLast(aggregateId: ID): P { val lastSnapshot = getLastSnapshot(aggregateId)?.first val missingEventOfSnapshot = getEventAfterTheSnapshot(aggregateId, lastSnapshot) return lastSnapshot.applyEvents(aggregateId, missingEventOfSnapshot) @@ -66,7 +66,7 @@ class ProjectionSnapshotRepositoryInMemory, P : Projection, ID * 2. get the events with a greater version of the snapshot but lower of passed event * 3. apply the events to the snapshot */ - fun getUntil(event: E): P { + override fun getUntil(event: E): P { val lastSnapshot = getLastSnapshotBeforeOrEqualEvent(event)?.first if (lastSnapshot?.lastEventVersion == event.version) { return lastSnapshot