-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryStorage.fs
698 lines (643 loc) · 36.9 KB
/
MemoryStorage.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
namespace Sharpino
open System.Runtime.CompilerServices
open FSharpPlus
open FSharpPlus.Operators
open System
open System.Collections
open Sharpino.Storage
open Microsoft.Extensions.Logging
open Microsoft.Extensions.Logging.Abstractions
open Sharpino.Definitions
// should be called like InMemoryEventStore
module MemoryStorage =
let logger: ILogger ref = ref NullLogger.Instance
let setLogger (newLogger: ILogger) =
logger := newLogger
type MemoryStorage() =
let event_id_seq_dic = Generic.Dictionary<Version, Generic.Dictionary<Name,int>>()
let event_aggregate_id_seq_dic = Generic.Dictionary<Version, Generic.Dictionary<Name,Generic.Dictionary<AggregateId,int>>>()
let snapshot_id_seq_dic = Generic.Dictionary<Version, Generic.Dictionary<Name,int>>()
let events_dic = Generic.Dictionary<Version, Generic.Dictionary<string, List<StoragePgEvent<_>>>>()
let aggregate_events_dic = Generic.Dictionary<Version, Generic.Dictionary<string, Generic.Dictionary<AggregateId, List<StorageEventJsonRef>>>>()
let snapshots_dic = Generic.Dictionary<Version, Generic.Dictionary<string, List<StorageSnapshot>>>()
let aggregate_snapshots_dic = Generic.Dictionary<Version, Generic.Dictionary<Name, Generic.Dictionary<AggregateId, List<StorageAggregateSnapshot>>>>()
let aggregate_undo_command_buffer = Generic.Dictionary<Version, Generic.Dictionary<Name, Generic.Dictionary<AggregateId, List<StorageEventJsonRef>>>>()
[<MethodImpl(MethodImplOptions.Synchronized)>]
let next_event_id version name =
logger.Value.LogDebug (sprintf "next_event_id %s %s" version name)
let event_id_seq =
if (event_id_seq_dic.ContainsKey version |> not) || (event_id_seq_dic.[version].ContainsKey name |> not) then
1
else
event_id_seq_dic.[version].[name]
if (event_id_seq_dic.ContainsKey version |> not) then
let dic = Generic.Dictionary<Name, int>()
dic.Add(name, event_id_seq + 1)
event_id_seq_dic.Add(version, dic)
else
let dic = event_id_seq_dic.[version]
if (dic.ContainsKey name |> not) then
dic.Add(name, event_id_seq + 1)
else
dic.[name] <- event_id_seq + 1
event_id_seq
[<MethodImpl(MethodImplOptions.Synchronized)>]
let next_aggregate_event_id version name aggregateId =
logger.Value.LogDebug (sprintf "next_aggregate_event_id %s %s %A" version name aggregateId)
let event_id_seq =
if (event_aggregate_id_seq_dic.ContainsKey version |> not) || (event_aggregate_id_seq_dic.[version].ContainsKey name |> not) || (event_aggregate_id_seq_dic.[version].[name].ContainsKey aggregateId |> not) then
1
else
event_aggregate_id_seq_dic.[version].[name].[aggregateId]
if (event_aggregate_id_seq_dic.ContainsKey version |> not) then
let dic = Generic.Dictionary<Name, Generic.Dictionary<AggregateId, int>>()
let dic2 = Generic.Dictionary<AggregateId, int>()
dic2.Add(aggregateId, event_id_seq + 1)
dic.Add(name, dic2)
event_aggregate_id_seq_dic.Add(version, dic)
else
let dic = event_aggregate_id_seq_dic.[version]
if (dic.ContainsKey name |> not) then
let dic2 = Generic.Dictionary<AggregateId, int>()
dic2.Add(aggregateId, event_id_seq + 1)
dic.Add(name, dic2)
else
let dic2 = dic.[name]
if (dic2.ContainsKey aggregateId |> not) then
dic2.Add(aggregateId, event_id_seq + 1)
else
dic2.[aggregateId] <- event_id_seq + 1
event_id_seq
[<MethodImpl(MethodImplOptions.Synchronized)>]
let next_snapshot_id version name =
logger.Value.LogDebug (sprintf "next_snapshot_id %s %s" version name)
let snapshot_id_seq =
if (snapshot_id_seq_dic.ContainsKey version |> not) || (snapshot_id_seq_dic.[version].ContainsKey name |> not) then
1
else
snapshot_id_seq_dic.[version].[name]
if (snapshot_id_seq_dic.ContainsKey version |> not) then
let dic = new Generic.Dictionary<Name, int>()
dic.Add(name, snapshot_id_seq + 1)
snapshot_id_seq_dic.Add(version, dic)
else
let dic = snapshot_id_seq_dic.[version]
if (dic.ContainsKey name |> not) then
dic.Add(name, snapshot_id_seq + 1)
else
dic.[name] <- snapshot_id_seq + 1
snapshot_id_seq
[<MethodImpl(MethodImplOptions.Synchronized)>]
let storeEvents version name events =
logger.Value.LogDebug (sprintf "storeEvents %s %s" version name)
if (events_dic.ContainsKey version |> not) then
let dic = new Generic.Dictionary<string, List<StoragePgEvent<_>>>()
dic.Add(name, events)
events_dic.Add(version, dic)
else
let dic = events_dic.[version]
if (dic.ContainsKey name |> not) then
dic.Add(name, events)
else
dic.[name] <- events
[<MethodImpl(MethodImplOptions.Synchronized)>]
let storeAggregateEvents version name aggregateId events =
logger.Value.LogDebug (sprintf "storeAggregateEvents %s %s %A" version name aggregateId)
if (aggregate_events_dic.ContainsKey version |> not) then
let dic = Generic.Dictionary<Name, Generic.Dictionary<AggregateId, List<StorageEventJsonRef>>>()
let dic2 = Generic.Dictionary<AggregateId, List<StorageEventJsonRef>>()
dic2.Add(aggregateId, events)
dic.Add(name, dic2)
aggregate_events_dic.Add(version, dic)
else
let dic = aggregate_events_dic.[version]
if (dic.ContainsKey name |> not) then
let dic2 = Generic.Dictionary<AggregateId, List<StorageEventJsonRef>>()
dic2.Add(aggregateId, events)
dic.Add(name, dic2)
else
let dic2 = dic.[name]
if (dic2.ContainsKey aggregateId |> not) then
dic2.Add(aggregateId, events)
else
dic2.[aggregateId] <- events
[<MethodImpl(MethodImplOptions.Synchronized)>]
let storeSnapshots version name snapshots =
logger.Value.LogDebug (sprintf "storeSnapshots %s %s" version name)
if (snapshots_dic.ContainsKey version |> not) then
let dic = Generic.Dictionary<string, List<StorageSnapshot>>()
dic.Add(name, snapshots)
snapshots_dic.Add(version, dic)
else
let dic = snapshots_dic.[version]
if (dic.ContainsKey name |> not) then
dic.Add(name, snapshots)
else
dic.[name] <- snapshots
[<MethodImpl(MethodImplOptions.Synchronized)>]
let addAggregateSnapshots version name aggregateId snapshot =
logger.Value.LogDebug (sprintf "AddAggregateSnapshots %s %s" version name)
if (aggregate_snapshots_dic.ContainsKey version |> not) then
let dic = Generic.Dictionary<Name, Generic.Dictionary<Guid, List<StorageAggregateSnapshot>>>()
let aggregateDic = Generic.Dictionary<Guid, List<StorageAggregateSnapshot>>()
aggregateDic.Add(aggregateId, [snapshot])
dic.Add(name, aggregateDic)
aggregate_snapshots_dic.Add(version, dic)
else
let dic = aggregate_snapshots_dic.[version]
if (dic.ContainsKey name |> not) then
let aggregateDic = Generic.Dictionary<Guid, List<StorageAggregateSnapshot>>()
aggregateDic.Add(aggregateId, [snapshot])
dic.Add(name, aggregateDic)
else
if (aggregate_snapshots_dic.[version].[name].ContainsKey aggregateId |> not) then
aggregate_snapshots_dic.[version].[name].Add(aggregateId, [snapshot])
else
aggregate_snapshots_dic.[version].[name].[aggregateId] <- [snapshot]
let getExistingSnapshots version name =
logger.Value.LogDebug (sprintf "getExistingSnapshots %s %s" version name)
if (snapshots_dic.ContainsKey version |> not) || (snapshots_dic.[version].ContainsKey name |> not) then
[]
else
snapshots_dic.[version].[name]
let getExistingEvents version name =
logger.Value.LogDebug (sprintf "getExistingEvents %s %s" version name)
if (events_dic.ContainsKey version |> not) || (events_dic.[version].ContainsKey name |> not) then
[]
else
events_dic.[version].[name]
let getExistingAggregateEvents version name aggregateId =
logger.Value.LogDebug (sprintf "getExistingAggregateEvents %s %s %A" version name aggregateId)
if (aggregate_events_dic.ContainsKey version |> not) || (aggregate_events_dic.[version].ContainsKey name |> not) || (aggregate_events_dic.[version].[name].ContainsKey aggregateId |> not) then
[]
else
aggregate_events_dic.[version].[name].[aggregateId]
[<MethodImpl(MethodImplOptions.Synchronized)>]
let storeSnapshots version name snapshots =
logger.Value.LogDebug (sprintf "storeSnapshots %s %s" version name)
if (snapshots_dic.ContainsKey version |> not) then
let dic = Generic.Dictionary<string, List<StorageSnapshot>>()
dic.Add(name, snapshots)
snapshots_dic.Add(version, dic)
else
let dic = snapshots_dic.[version]
if (dic.ContainsKey name |> not) then
dic.Add(name, snapshots)
else
dic.[name] <- snapshots
let getExistingSnapshots version name =
logger.Value.LogDebug (sprintf "getExistingSnapshots %s %s" version name)
if (snapshots_dic.ContainsKey version |> not) || (snapshots_dic.[version].ContainsKey name |> not) then
[]
else
snapshots_dic.[version].[name]
let getExistingEvents version name =
logger.Value.LogDebug (sprintf "getExistingEvents %s %s" version name)
if (events_dic.ContainsKey version |> not) || (events_dic.[version].ContainsKey name |> not) then
[]
else
events_dic.[version].[name]
member this.Reset version name =
logger.Value.LogDebug (sprintf "Reset %s %s" version name)
events_dic.Clear()
snapshots_dic.Clear()
event_id_seq_dic.Clear()
snapshot_id_seq_dic.Clear()
aggregate_events_dic.Clear()
aggregate_snapshots_dic.Clear()
interface IEventStore<string> with
[<MethodImpl(MethodImplOptions.Synchronized)>]
member this.Reset version name =
this.Reset version name
member this.ResetAggregateStream version name =
this.Reset version name
[<MethodImpl(MethodImplOptions.Synchronized)>]
member this.AddEvents _ version name xs: Result<List<int>, string> =
logger.Value.LogDebug (sprintf "AddEvents %s %s" version name)
let newEvents =
[ for e in xs do
yield {
Id = next_event_id version name
JsonEvent = e
Timestamp = DateTime.UtcNow
}
]
let events = getExistingEvents version name @ newEvents
storeEvents version name events
let ids = newEvents |>> (fun x -> x.Id)
ids |> Ok
// not handling metadata in memory storage
member this.AddEventsMd _ version name md xs: Result<List<int>, string> =
logger.Value.LogDebug (sprintf "AddEvents %s %s" version name)
let newEvents =
[ for e in xs do
yield {
Id = next_event_id version name
JsonEvent = e
Timestamp = DateTime.UtcNow
}
]
let events = getExistingEvents version name @ newEvents
storeEvents version name events
let ids = newEvents |>> (fun x -> x.Id)
ids |> Ok
member this.SetInitialAggregateStateAndAddEvents _ aggregateId aggregateVersion aggregatename json contextVersion contextName events =
let initialState =
{
Id = next_snapshot_id aggregateVersion aggregatename
AggregateId = aggregateId
Snapshot = json
TimeStamp = DateTime.UtcNow
EventId = None
}
let snapshots = Generic.Dictionary<AggregateId, List<StorageAggregateSnapshot>>()
snapshots.Add (aggregateId, [initialState])
addAggregateSnapshots aggregateVersion aggregatename aggregateId initialState
(this:> IEventStore<string>).AddEvents 0 contextVersion contextName events
member this.SetInitialAggregateStateAndAddEventsMd _ aggregateId aggregateVersion aggregatename json contextVersion contextName _ events =
let initialState =
{
Id = next_snapshot_id aggregateVersion aggregatename
AggregateId = aggregateId
Snapshot = json
TimeStamp = DateTime.UtcNow
EventId = None
}
let snapshots = Generic.Dictionary<AggregateId, List<StorageAggregateSnapshot>>()
snapshots.Add (aggregateId, [initialState])
addAggregateSnapshots aggregateVersion aggregatename aggregateId initialState
(this:> IEventStore<string>).AddEvents 0 contextVersion contextName events
member this.SetInitialAggregateStateAndAddAggregateEvents _ aggregateId aggregateVersion aggregatename secondAggregateId json contextVersion contextName events =
let initialState =
{
Id = next_snapshot_id aggregateVersion aggregatename
AggregateId = aggregateId
Snapshot = json
TimeStamp = DateTime.UtcNow
EventId = None
}
let snapshots = Generic.Dictionary<AggregateId, List<StorageAggregateSnapshot>>()
snapshots.Add (aggregateId, [initialState])
addAggregateSnapshots aggregateVersion aggregatename aggregateId initialState
(this:> IEventStore<string>).AddAggregateEvents 0 contextVersion contextName secondAggregateId events
member this.SetInitialAggregateStateAndAddAggregateEventsMd _ aggregateId aggregateVersion aggregatename secondAggregateId json contextVersion contextName _ events =
let initialState =
{
Id = next_snapshot_id aggregateVersion aggregatename
AggregateId = aggregateId
Snapshot = json
TimeStamp = DateTime.UtcNow
EventId = None
}
let snapshots = Generic.Dictionary<AggregateId, List<StorageAggregateSnapshot>>()
snapshots.Add (aggregateId, [initialState])
addAggregateSnapshots aggregateVersion aggregatename aggregateId initialState
(this:> IEventStore<string>).AddAggregateEvents 0 contextVersion contextName secondAggregateId events
member this.SetInitialAggregateStateAndMultiAddAggregateEvents aggregateId Version Name jsonSnapshot events =
let initialState =
{
Id = next_snapshot_id Version Name
AggregateId = aggregateId
Snapshot = jsonSnapshot
TimeStamp = DateTime.UtcNow
EventId = None
}
let snapshots = Generic.Dictionary<AggregateId, List<StorageAggregateSnapshot>>()
snapshots.Add (aggregateId, [initialState])
addAggregateSnapshots Version Name aggregateId initialState
(this:> IEventStore<string>).MultiAddAggregateEvents events
member this.SetInitialAggregateStateAndMultiAddAggregateEventsMd aggregateId Version Name jsonSnapshot md events =
let initialState =
{
Id = next_snapshot_id Version Name
AggregateId = aggregateId
Snapshot = jsonSnapshot
TimeStamp = DateTime.UtcNow
EventId = None
}
let snapshots = Generic.Dictionary<AggregateId, List<StorageAggregateSnapshot>>()
snapshots.Add (aggregateId, [initialState])
addAggregateSnapshots Version Name aggregateId initialState
(this:> IEventStore<string>).MultiAddAggregateEvents events
member this.GetEventsAfterId version id name =
logger.Value.LogDebug (sprintf "GetEventsAfterId %s %A %s" version id name)
if (events_dic.ContainsKey version |> not) || (events_dic.[version].ContainsKey name |> not) then
[] |> Ok
else
events_dic.[version].[name]
|> List.filter (fun x -> x.Id > id)
|>> (fun x -> x.Id, x.JsonEvent)
|> Ok
member this.MultiAddEvents (arg: List< _ * List<Json> * Version * Name>) =
logger.Value.LogDebug (sprintf "MultiAddEvents %A" arg)
let cmds =
arg
|> List.map
(fun (_, xs, version, name) ->
(this :> IEventStore<string>).AddEvents 0 version name xs |> Result.get
)
cmds |> Ok
member this.MultiAddEventsMd md (arg: List< _ * List<Json> * Version * Name>) =
logger.Value.LogDebug (sprintf "MultiAddEvents %A" arg)
let cmds =
arg
|> List.map
(fun (_, xs, version, name) ->
(this :> IEventStore<string>).AddEvents 0 version name xs |> Result.get
)
cmds |> Ok
member this.SetSnapshot version (id, snapshot) name =
logger.Value.LogDebug (sprintf "SetSnapshot %s %A %s" version id name)
let newSnapshot =
{
Id = next_snapshot_id version name
Snapshot = snapshot
TimeStamp = DateTime.UtcNow
EventId = id
}
let snapshots = getExistingSnapshots version name @ [newSnapshot]
storeSnapshots version name snapshots
() |> Ok
member this.TryGetEvent version id name =
logger.Value.LogDebug (sprintf "TryGetEvent %s %A %s" version id name)
if (events_dic.ContainsKey version |> not) || (events_dic.[version].ContainsKey name |> not) then
None
else
let res =
events_dic.[version].[name]
|> List.tryFind (fun x -> x.Id = id)
res
member this.SetInitialAggregateState aggregateId version name snapshot =
let initialState =
{
Id = next_snapshot_id version name
AggregateId = aggregateId
Snapshot = snapshot
TimeStamp = DateTime.UtcNow
EventId = None
}
let snapshots = Generic.Dictionary<AggregateId, List<StorageAggregateSnapshot>>()
snapshots.Add(aggregateId, [initialState])
addAggregateSnapshots version name aggregateId initialState
() |> Ok
member this.TryGetLastEventId version name =
logger.Value.LogDebug (sprintf "TryGetLastEventId %s %s" version name)
if (events_dic.ContainsKey version |> not) || (events_dic.[version].ContainsKey name |> not) then
None
else
events_dic.[version].[name]
|> List.tryLast
|>> (fun x -> x.Id)
member this.TryGetLastSnapshot version name =
logger.Value.LogDebug (sprintf "TryGetLastSnapshot %s %s" version name)
if (snapshots_dic.ContainsKey version |> not)|| (snapshots_dic.[version].ContainsKey name |> not) then
None
else
let res =
snapshots_dic.[version].[name]
|> List.tryLast
match res with
| None -> None
| Some x ->
Some (x.Id, x.EventId, x.Snapshot)
member this.TryGetLastSnapshotIdByAggregateId version name aggregateId =
logger.Value.LogDebug (sprintf "TryGetLastSnapshotIdByAggregateId %s %s %A" version name aggregateId)
if (aggregate_snapshots_dic.ContainsKey version |> not) ||
(aggregate_snapshots_dic.[version].ContainsKey name |> not) ||
(aggregate_snapshots_dic.[version].[name].ContainsKey aggregateId |> not) then
None
else
aggregate_snapshots_dic.[version].[name].[aggregateId]
|> List.tryLast
|>> (fun x -> (x.EventId, x.Id))
member this.TryGetLastSnapshotEventId version name =
logger.Value.LogDebug (sprintf "TryGetLastSnapshotEventId %s %s" version name)
if (snapshots_dic.ContainsKey version |> not) || (snapshots_dic.[version].ContainsKey name |> not) then
None
else
snapshots_dic.[version].[name]
|> List.tryLast
|>> (fun x -> x.EventId)
member this.TryGetLastAggregateSnapshotEventId version name aggregateId =
if (aggregate_snapshots_dic.ContainsKey version |> not) || (aggregate_snapshots_dic.[version].ContainsKey name |> not) ||
(aggregate_snapshots_dic.[version].[name].ContainsKey aggregateId |> not) then
None
else
aggregate_snapshots_dic.[version].[name].[aggregateId]
|> List.tryLast
|> tryLast
|>> (fun x -> x.EventId)
|> Option.bind (fun x -> x)
member this.TryGetLastSnapshotId version name =
logger.Value.LogDebug (sprintf "TryGetLastSnapshotId %s %s" version name)
if (snapshots_dic.ContainsKey version |> not) || (snapshots_dic.[version].ContainsKey name |> not) then
None
else
snapshots_dic.[version].[name]
|> List.tryLast
|>> (fun x -> x.EventId, x.Id)
member this.TryGetFirstSnapshot version name aggregateId =
logger.Value.LogDebug (sprintf "TryGetFirstSnapshot %s %s %A" version name aggregateId)
if (aggregate_snapshots_dic.ContainsKey version |> not) || (aggregate_snapshots_dic.[version].ContainsKey name |> not) || (aggregate_snapshots_dic.[version].[name].ContainsKey aggregateId |> not) then
Error "not found"
else
aggregate_snapshots_dic.[version].[name].[aggregateId]
|> List.tryHead
|> Result.ofOption "not found"
>>= (fun x -> (x.Id, x.Snapshot) |> Ok)
member this.TryGetSnapshotById version name id =
logger.Value.LogDebug (sprintf "TryGetSnapshotById %s %s %A" version name id)
if (snapshots_dic.ContainsKey version |> not) || (snapshots_dic.[version].ContainsKey name |> not) then
None
else
snapshots_dic.[version].[name]
|> List.tryFind (fun x -> x.Id = id)
|>> (fun x -> (x.EventId, x.Snapshot))
member this.TryGetAggregateSnapshotById version name aggregateId id =
if (aggregate_snapshots_dic.ContainsKey version |> not
|| aggregate_snapshots_dic.[version].ContainsKey name |> not
|| aggregate_snapshots_dic.[version].[name].ContainsKey aggregateId |> not)
then
None
else
aggregate_snapshots_dic.[version].[name].[aggregateId]
|> List.tryFind (fun x -> x.Id = id)
|>> (fun x -> (x.EventId, x.Snapshot))
// Issue: it will not survive after a version migration because the timestamps will be different
member this.GetEventsInATimeInterval (version: Version) (name: Name) (dateFrom: DateTime) (dateTo: DateTime) =
logger.Value.LogDebug (sprintf "GetEventsInATimeInterval %s %s %A %A" version name dateFrom dateTo)
if (events_dic.ContainsKey version |> not) || (events_dic.[version].ContainsKey name |> not) then
[] |> Ok
else
events_dic.[version].[name]
|> List.filter (fun x -> x.Timestamp >= dateFrom && x.Timestamp <= dateTo)
|>> (fun x -> x.Id, x.JsonEvent)
|>> (fun (id, event) -> id, event)
|> Ok
member this.GetAggregateSnapshotsInATimeInterval version name dateFrom dateTo =
logger.Value.LogDebug (sprintf "GetAggregateSnapshotsInATimeInterval %s %s %A %A" version name dateFrom dateTo)
if (aggregate_snapshots_dic.ContainsKey version |> not) || (aggregate_snapshots_dic.[version].ContainsKey name |> not) then
[] |> Ok
else
aggregate_snapshots_dic.[version].[name]
|> Dictionary.keys
|> Seq.toList
|> List.map (fun x -> aggregate_snapshots_dic.[version].[name].[x])
|> List.collect (fun x -> x)
|> List.filter (fun x -> x.TimeStamp >= dateFrom && x.TimeStamp <= dateTo)
|>> (fun x -> x.Id, x.AggregateId, x.TimeStamp, x.Snapshot)
|> Ok
member this.GetAggregateEventsInATimeInterval (version: Version) (name: Name) (aggregateId: AggregateId) (dateFrom: DateTime) (dateTo: DateTime) =
logger.Value.LogDebug (sprintf "GetAggregateEventsInATimeInterval %s %s %A %A %A" version name aggregateId dateFrom dateTo)
if
( aggregate_events_dic.ContainsKey version |> not)
|| (aggregate_events_dic.[version].ContainsKey name |> not)
|| (aggregate_events_dic.[version].[name].ContainsKey aggregateId |> not )
then
[] |> Ok
else
aggregate_events_dic.[version].[name].[aggregateId]
|> List.filter (fun x -> x.Timestamp >= dateFrom && x.Timestamp <= dateTo)
|>> (fun x -> x.Id, x.JsonEvent)
|>> (fun (id, event) -> id, event)
|> Ok
member this.GetAllAggregateEventsInATimeInterval version name dateFrom dateTo =
logger.Value.LogDebug (sprintf "GetAllaggregateEventsInAtimeInterval %s %s %A %A" version name dateFrom dateTo)
if (aggregate_events_dic.ContainsKey version |> not) || (aggregate_events_dic.[version].ContainsKey name |> not) then
[] |> Ok
else
aggregate_events_dic.[version].[name]
|> Dictionary.keys
|> Seq.toList
|> List.map (fun x -> aggregate_events_dic.[version].[name].[x])
|> List.collect (fun x -> x)
|> List.filter (fun x -> x.Timestamp >= dateFrom && x.Timestamp <= dateTo)
|>> (fun x -> x.Id, x.JsonEvent)
|> Ok
member this.GetMultipleAggregateEventsInATimeInterval version name aggregateIds dateFrom dateTo =
logger.Value.LogDebug (sprintf "GetMultipleAggregateEventsInATimeInterval %s %s %A %A %A" version name aggregateIds dateFrom dateTo)
if (aggregate_events_dic.ContainsKey version |> not) || (aggregate_events_dic.[version].ContainsKey name |> not) then
[] |> Ok
else
aggregate_events_dic.[version].[name]
|> Dictionary.keys
|> Seq.toList
|> List.filter (fun x -> aggregateIds |> List.contains x)
|> List.map (fun x -> aggregate_events_dic.[version].[name].[x])
|> List.collect (fun x -> x)
|> List.filter (fun x -> x.Timestamp >= dateFrom && x.Timestamp <= dateTo)
|>> (fun x -> x.Id, x.AggregateId, x.JsonEvent)
|> Ok
member this.MultiAddAggregateEvents (arg: List< _* List<Json> * Version * Name * AggregateId>) =
logger.Value.LogDebug (sprintf "MultiAddAggregateEvents %A" arg)
let cmds =
arg
|> List.map
(fun (_, xs, version, name, aggregateId) ->
(this :> IEventStore<string>).AddAggregateEvents 0 version name aggregateId xs |> Result.get
)
cmds |> Ok
member this.MultiAddAggregateEventsMd md (arg: List< _* List<Json> * Version * Name * AggregateId>) =
logger.Value.LogDebug (sprintf "MultiAddAggregateEvents %A" arg)
let cmds =
arg
|> List.map
(fun (_, xs, version, name, aggregateId) ->
(this :> IEventStore<string>).AddAggregateEvents 0 version name aggregateId xs |> Result.get
)
cmds |> Ok
[<MethodImpl(MethodImplOptions.Synchronized)>]
member this.AddAggregateEvents _ version name aggregateId events =
logger.Value.LogDebug (sprintf "AddAggregateEvents %s %s %A" version name aggregateId)
let newEvents =
[
for e in events do
yield {
AggregateId = aggregateId
Id = next_aggregate_event_id version name aggregateId
JsonEvent = e
KafkaOffset = None
KafkaPartition = None
Timestamp = DateTime.UtcNow
}
]
let events' = getExistingAggregateEvents version name aggregateId @ newEvents
storeAggregateEvents version name aggregateId events'
let ids = newEvents |>> (fun x -> x.Id)
ids |> Ok
[<MethodImpl(MethodImplOptions.Synchronized)>]
member this.AddAggregateEventsMd _ version name aggregateId _ events =
logger.Value.LogDebug (sprintf "AddAggregateEvents %s %s %A" version name aggregateId)
let newEvents =
[
for e in events do
yield {
AggregateId = aggregateId
Id = next_aggregate_event_id version name aggregateId
JsonEvent = e
KafkaOffset = None
KafkaPartition = None
Timestamp = DateTime.UtcNow
}
]
let events' = getExistingAggregateEvents version name aggregateId @ newEvents
storeAggregateEvents version name aggregateId events'
let ids = newEvents |>> (fun x -> x.Id)
ids |> Ok
member this.TryGetLastAggregateEventId(version: Version) (name: Name) (aggregateId: AggregateId): Option<EventId> =
logger.Value.LogDebug (sprintf "TryGetLastAggregateEventId %s %s %A" version name aggregateId)
if (aggregate_events_dic.ContainsKey version |> not) then
None
else
if (aggregate_events_dic.[version].ContainsKey name |> not) then
None
else
if (aggregate_events_dic.[version].[name].ContainsKey aggregateId) then
aggregate_events_dic.[version].[name].[aggregateId]
|> List.tryLast
|>> (fun x -> x.Id) //, x.KafkaOffset, x.KafkaPartition ))
else
None
member this.GetAggregateEventsAfterId version name aggregateId id =
if (aggregate_events_dic.ContainsKey version |> not) then
[] |> Ok
else
if (aggregate_events_dic.[version].ContainsKey name |> not) then
[] |> Ok
else
if (aggregate_events_dic.[version].[name].ContainsKey aggregateId |> not) then
[] |> Ok
else
aggregate_events_dic.[version].[name].[aggregateId]
|> List.filter (fun x -> x.Id > id)
|>> (fun x -> x.Id, x.JsonEvent)
|> Ok
member this.GetAggregateEvents version name aggregateId: Result<List<EventId * Json>,string> =
logger.Value.LogDebug (sprintf "GetAggregateEvents %s %s %A" version name aggregateId)
if (aggregate_events_dic.ContainsKey version |> not) then
[] |> Ok
else
if (aggregate_events_dic.[version].ContainsKey name |> not) then
[] |> Ok
else
if (aggregate_events_dic.[version].[name].ContainsKey aggregateId |> not) then
[] |> Ok
else
aggregate_events_dic.[version].[name].[aggregateId]
|>> (fun x -> (x.Id, x.JsonEvent))
|> Ok
member this.SetAggregateSnapshot version (aggregateId, eventId, snapshot) name =
let state =
{
Id = next_snapshot_id version name
AggregateId = aggregateId
Snapshot = snapshot
TimeStamp = DateTime.UtcNow
EventId = eventId |> Some
}
addAggregateSnapshots version name aggregateId state
() |> Ok
member this.GDPRReplaceSnapshotsAndEventsOfAnAggregate version name aggregateId snapshot event =
// in memory storage does not need to be compliant to GDPR as it is used only for tests
logger.Value.LogDebug (sprintf "GDPRReplaceSnapshotsAndEventsOfAnAggregate %s %s %A %A %A" version name aggregateId snapshot event)
() |> Ok