Skip to content

Commit

Permalink
Remove use of Compaction term re #447
Browse files Browse the repository at this point in the history
  • Loading branch information
bartelink committed Aug 19, 2024
1 parent 34c1f79 commit 728695e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
6 changes: 3 additions & 3 deletions samples/Store/Domain.Tests/SavedForLaterTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ let ``Event aggregation should carry set semantics`` (commands: Command list) =
// fold over events using regular set semantics and compare outcomes
let evolveSet (state: HashSet<SkuId>) (event: Events.Event) =
match event with
| Events.Snapshotted items ->
state.Clear()
state.UnionWith(items.items |> Seq.map _.skuId)
| Events.Added appended -> state.UnionWith appended.skus
| Events.Removed removed -> state.ExceptWith removed.skus
| Events.Merged merged -> state.UnionWith(merged.items |> Seq.map _.skuId)
| Events.Compacted compacted ->
state.Clear()
state.UnionWith(compacted.items |> Seq.map _.skuId)
state

let state',events = establish commands
Expand Down
19 changes: 8 additions & 11 deletions samples/Store/Domain/SavedForLater.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@ module Events =
type Added = { skus: SkuId []; dateSaved: DateTimeOffset }
type Removed = { skus: SkuId [] }
type Merged = { items: Item [] }
module Compaction =
type Compacted = { items: Item [] }
// NB need to revise this tag if you break the unfold schema
let [<Literal>] EventType = "compacted"
type Snapshotted = { items: Item [] }

type Event =
/// Checkpoint with snapshot of entire preceding event fold, avoiding need for any further reads
| [<System.Runtime.Serialization.DataMember(Name=Compaction.EventType)>] Compacted of Compaction.Compacted
| [<System.Runtime.Serialization.DataMember(Name="Snapshotted")>] Snapshotted of Snapshotted

/// Inclusion of another set of state in this one
| Merged of Merged
Expand Down Expand Up @@ -55,17 +52,17 @@ module Fold =

module Snapshot =

let generate state = Compacted { items = state }
let isOrigin = function Compacted _ -> true | _ -> false
let generate state = Snapshotted { items = state }
let isOrigin = function Snapshotted _ -> true | _ -> false
let config = isOrigin, generate

let fold (state: State) (events: seq<Event>): State =
let index = InternalState state
for event in events do
match event with
| Compacted { items = skus } -> index.Replace skus
| Merged { items = skus} -> index.Append skus
| Removed { skus = skus } -> index.Remove skus
| Snapshotted { items = skus } -> index.Replace skus
| Merged { items = skus} -> index.Append skus
| Removed { skus = skus } -> index.Remove skus
| Added { dateSaved = d; skus = skus } ->
index.Append(seq { for sku in skus -> { skuId = sku; dateSaved = d }})
index.ToExternalState()
Expand All @@ -79,7 +76,7 @@ type private Index(state: Events.Item seq) =

member _.DoesNotAlreadyContainSameOrMoreRecent effectiveDate sku =
match index.TryGetValue sku with
| true,item when item.dateSaved >= effectiveDate -> false
| true, item when item.dateSaved >= effectiveDate -> false
| _ -> true
member this.DoesNotAlreadyContainItem(item: Events.Item) =
this.DoesNotAlreadyContainSameOrMoreRecent item.dateSaved item.skuId
Expand Down

0 comments on commit 728695e

Please sign in to comment.