@@ -18,29 +18,29 @@ type Aggregate[R Root] struct {
18
18
// It is kept in memory
19
19
OnCreate func (string ) (R , error )
20
20
21
- // Events
22
- Events Schemas
23
-
24
21
// OnSession called on command dispatch when Session not exists
25
22
//OnSession func(R) (Session, error)
26
23
27
- // OnRead when all events are committed to R and state is rebuild from all previously persisted Event
28
- OnRead RootFunc [R ]
24
+ // OnLoad when all events are committed to R and state is rebuild from all previously persisted Event
25
+ OnLoad RootFunc [R ]
29
26
30
27
// OnRecall
31
28
//OnRecall func(Session, R) error
32
29
33
- // OnWrite called just before events are persisted to database
34
- OnWrite RootFunc [R ]
30
+ // OnSave called just after events are persisted to database
31
+ OnSave RootFunc [R ]
35
32
36
- // OnCommit
33
+ // OnCommit when new events are committed to a Root
37
34
OnCommit func (R , Events ) error // todo not able to deny it (error)
38
35
36
+ // OnRecall
37
+ OnRecall func (R ) time.Time
38
+
39
39
// OnCacheCleanup when aggregate is removed from memory
40
40
OnCacheCleanup RootFunc [R ]
41
41
42
- // RecallAfter default
43
- //RecallAfter time.Duration
42
+ // Events
43
+ Events Schemas
44
44
45
45
CleanCacheAfter time.Duration
46
46
@@ -56,7 +56,7 @@ type Aggregate[R Root] struct {
56
56
Writer Writer
57
57
58
58
// Logger
59
- // Log Printer
59
+ Log Printer
60
60
61
61
// memory keeps created Changelog of Aggregate in order to avoid rebuilding
62
62
// state of each Aggregate everytime when Thread is called
@@ -89,24 +89,30 @@ func (a *Aggregate[R]) Execute(id string, command RootFunc[R]) error {
89
89
}
90
90
91
91
func (a * Aggregate [R ]) Get (id string ) (R , error ) {
92
- d , r , err := a .read (id )
93
- if err != nil {
92
+ var found bool
93
+ var d RootID
94
+ var r R
95
+ var err error
96
+
97
+ if err = a .init (); err != nil {
94
98
return r , err
95
99
}
96
100
97
- if a .Store == nil {
98
- a .Store = NewEventStore ()
101
+ if r , found = a .memory .Get (id ); ! found {
102
+ if r , err = a .OnCreate (id ); err != nil {
103
+ return r , err
104
+ }
99
105
}
100
106
101
- rw , evs , m := a .Store .ReadWriter (d ), make ([]Event [any ], a .LoadEventsInChunks ), 0
107
+ if d , err = NewRootID (r ); err != nil {
108
+ return r , err
109
+ }
110
+
111
+ rw , evs , m := a .Store .ReadWriter (d ), make (Events , a .LoadEventsInChunks ), 0
102
112
for {
103
113
switch m , err = rw .ReadAt (evs , r .Version ()); {
104
114
105
115
case err == ErrEndOfStream || err == nil :
106
- if m == 0 {
107
- return r , nil
108
- }
109
-
110
116
if failed := a .commit (r , evs [:m ]); failed != nil {
111
117
return r , failed
112
118
}
@@ -115,13 +121,13 @@ func (a *Aggregate[R]) Get(id string) (R, error) {
115
121
continue
116
122
}
117
123
118
- if a .OnRead != nil {
119
- if err = a .OnRead (r ); err != nil {
124
+ if a .OnLoad != nil {
125
+ if err = a .OnLoad (r ); err != nil {
120
126
return r , err
121
127
}
122
128
}
123
129
124
- return r , nil
130
+ return r , a . memory . Set ( r . ID (), r )
125
131
126
132
default :
127
133
return r , Err ("%s root read failed due %w" , r , err )
@@ -135,7 +141,7 @@ func (a *Aggregate[R]) Set(r R) error {
135
141
}
136
142
137
143
events , err := NewEvents (r )
138
- if err != nil || len (events ) == 0 {
144
+ if err = events . Extend ( r ); err != nil || len (events ) == 0 {
139
145
return err
140
146
}
141
147
@@ -158,67 +164,44 @@ func (a *Aggregate[R]) Set(r R) error {
158
164
return err
159
165
}
160
166
161
- if a .OnWrite != nil {
162
- if err = a .OnWrite (r ); err != nil {
163
- return err
164
- }
165
- }
166
-
167
167
if a .Writer != nil {
168
168
if _ , err = a .Writer .Write (events ); err != nil {
169
169
return err
170
170
}
171
171
}
172
172
173
- return nil
174
- }
175
-
176
- func (a * Aggregate [R ]) read (id string ) (RootID , R , error ) {
177
- var r , ok = a .memory .Get (id )
178
- var d RootID
179
- var err error
180
-
181
- if err = a .init (); err != nil {
182
- return d , r , err
183
- }
184
-
185
- if ! ok {
186
- if r , err = a .OnCreate (id ); err != nil {
187
- return d , r , err
173
+ if a .OnSave != nil {
174
+ if err = a .OnSave (r ); err != nil {
175
+ return err
188
176
}
189
177
}
190
178
191
- if d , err = NewRootID (r ); err != nil {
192
- return d , r , err
193
- }
194
-
195
- return d , r , nil
179
+ return a .memory .Set (r .ID (), r )
196
180
}
197
181
198
- func (a * Aggregate [R ]) commit (r R , e []Event [ any ] ) error {
182
+ func (a * Aggregate [R ]) commit (r R , e []Event ) error {
199
183
if len (e ) == 0 {
200
184
return nil
201
185
}
202
186
203
- if a .OnCommit != nil {
204
- if err := a .OnCommit (r , e ); err != nil {
205
- return err
206
- }
207
- }
208
-
209
187
// todo check if given slice of events has correct iteration of sequences, match it with current
210
188
// version of R
211
189
for i := range e {
212
- if err := r .Commit (e [i ].Body , e [i ].CreatedAt ); err != nil {
190
+ if err := r .Commit (e [i ].body , e [i ].createdAt ); err != nil {
213
191
return err
214
192
}
215
193
216
194
//s += int64(len(e.Body))
217
195
}
218
196
197
+ if a .OnCommit != nil {
198
+ if err := a .OnCommit (r , e ); err != nil {
199
+ return err
200
+ }
201
+ }
219
202
//a.version = events[len(events)-1].Sequence
220
203
//a.size += s
221
- return a . memory . Set ( r . ID (), r )
204
+ return nil
222
205
}
223
206
224
207
func (a * Aggregate [R ]) init () (err error ) {
@@ -229,6 +212,10 @@ func (a *Aggregate[R]) init() (err error) {
229
212
}
230
213
}
231
214
215
+ if a .Store == nil {
216
+ a .Store = NewEventStore ()
217
+ }
218
+
232
219
if a .memory == nil {
233
220
a .memory = NewCache [string , R ](a .CleanCacheAfter )
234
221
}
@@ -258,14 +245,18 @@ func (a *Aggregate[R]) Register(in *Domain) (err error) {
258
245
return err
259
246
}
260
247
261
- if a .Store == nil {
248
+ if _ , ok := a .Store .( * store ); ok {
262
249
a .Store = in .store
263
250
}
264
251
265
- if err = in . schemas . Merge (a .Events ); err != nil {
252
+ if err = registry . merge (a .Events , a . Type ); err != nil {
266
253
return err
267
254
}
268
255
256
+ if a .Log == nil {
257
+ a .Log = in .logger (a .Type )
258
+ }
259
+
269
260
if a .Writer != nil {
270
261
in .writers .list = append (in .writers .list , a .Writer )
271
262
}
0 commit comments