forked from itimofeev/go-saga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.go
42 lines (36 loc) · 766 Bytes
/
memory.go
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
package saga
import (
"errors"
)
func New() Store {
return &store{
m: make(map[string][]*Log),
}
}
type store struct {
m map[string][]*Log
}
func (s *store) GetAllLogsByExecutionID(executionID string) ([]*Log, error) {
res, ok := s.m[executionID]
if ok {
return res, nil
}
return nil, errors.New("no logs found")
}
func (s *store) GetStepLogsToCompensate(executionID string) ([]*Log, error) {
logs, ok := s.m[executionID]
if !ok {
return nil, errors.New("no logs found")
}
var res []*Log
for i := len(logs) - 1; i >= 0; i-- {
if logs[i].Type == LogTypeSagaStepExec {
res = append(res, logs[i])
}
}
return res, nil
}
func (s *store) AppendLog(log *Log) error {
s.m[log.ExecutionID] = append(s.m[log.ExecutionID], log)
return nil
}