Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(EventStoreDb): Add AccessStrategy.CustomOrigin #222

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/Equinox.EventStore/EventStore.fs
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,9 @@ type AccessStrategy<'event,'state> =
/// Scanning for events concludes when any event passes the <c>isOrigin</c> test.
/// See https://eventstore.org/docs/event-sourcing-basics/rolling-snapshots/index.html
| RollingSnapshots of isOrigin : ('event -> bool) * toSnapshot : ('state -> 'event)
/// Customize the loading process, walking events toward the the oldest one (in batches)
/// Short-circuits if the supplied `isOrigin` identifies the given event as being a valid alternate starting point
| CustomOrigin of isOrigin : ('event -> bool)

type private CompactionContext(eventsLen : int, capacityBeforeCompaction : int) =
/// Determines whether writing a Compaction event is warranted (based on the existing state and the current accumulated changes)
Expand All @@ -456,22 +459,21 @@ type private Category<'event, 'state, 'context>(context : Context, codec : FsCod

let compactionPredicate =
match access with
| None -> None
| None | Some (AccessStrategy.CustomOrigin _) -> None
| Some AccessStrategy.LatestKnownEvent -> Some (fun _ -> true)
| Some (AccessStrategy.RollingSnapshots (isValid, _)) -> Some isValid

let isOrigin =
match access with
| None | Some AccessStrategy.LatestKnownEvent -> fun _ -> true
| Some (AccessStrategy.RollingSnapshots (isValid, _)) -> isValid
| Some (AccessStrategy.RollingSnapshots (isValid, _)) | Some (AccessStrategy.CustomOrigin isValid) -> isValid

let loadAlgorithm load streamName initial log =
let batched = load initial (context.LoadBatched streamName log (tryDecode,None))
let compacted = load initial (context.LoadBackwardsStoppingAtCompactionEvent streamName log (tryDecode, isOrigin))
match access with
| None -> batched
| Some AccessStrategy.LatestKnownEvent
| Some (AccessStrategy.RollingSnapshots _) -> compacted
| Some AccessStrategy.LatestKnownEvent | Some (AccessStrategy.RollingSnapshots _) | Some (AccessStrategy.CustomOrigin _) -> compacted

let load (fold : 'state -> 'event seq -> 'state) initial f = async {
let! token, events = f
Expand All @@ -489,7 +491,7 @@ type private Category<'event, 'state, 'context>(context : Context, codec : FsCod
let encode e = codec.Encode(ctx, e)
let events =
match access with
| None | Some AccessStrategy.LatestKnownEvent -> events
| None | Some AccessStrategy.LatestKnownEvent | Some (AccessStrategy.CustomOrigin _) -> events
| Some (AccessStrategy.RollingSnapshots (_, compact)) ->
let cc = CompactionContext(List.length events, pos.batchCapacityLimit.Value)
if cc.IsCompactionDue then events @ [fold state events |> compact] else events
Expand Down