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
package plc
import (
"context"
"encoding/json"
"errors"
"os"
"path/filepath"
"sort"
"sync"
"time"
)
type Store interface {
GetCurrentOps(ctx context.Context, did string) ([]LogEntry, error)
GetAuditLog(ctx context.Context, did string) ([]LogEntry, error)
GetOpByCID(ctx context.Context, did string, cid string) (*LogEntry, error)
InsertOp(ctx context.Context, entry LogEntry) error
NullifyOps(ctx context.Context, did string, cids []string) error
ExportLogs(ctx context.Context, after *time.Time, count int) ([]LogEntry, error)
}
type MemoryStore struct {
mu sync.RWMutex
logs map[string][]LogEntry
}
func NewMemoryStore() *MemoryStore {
return &MemoryStore{logs: map[string][]LogEntry{}}
}
var (
_ Store = (*MemoryStore)(nil)
_ Store = (*FileStore)(nil)
)
func OpenStore(path string) (Store, error) {
if path == "" {
return NewMemoryStore(), nil
}
fs, err := NewFileStore(path)
if err != nil {
return nil, err
}
return fs, nil
}
func (s *MemoryStore) GetCurrentOps(ctx context.Context, did string) ([]LogEntry, error) {
s.mu.RLock()
defer s.mu.RUnlock()
out := []LogEntry{}
for _, e := range s.logs[did] {
if !e.Nullified {
out = append(out, e)
}
}
return out, nil
}
func (s *MemoryStore) GetAuditLog(ctx context.Context, did string) ([]LogEntry, error) {
s.mu.RLock()
defer s.mu.RUnlock()
out := append([]LogEntry{}, s.logs[did]...)
return out, nil
}
func (s *MemoryStore) GetOpByCID(ctx context.Context, did string, cid string) (*LogEntry, error) {
s.mu.RLock()
defer s.mu.RUnlock()
for i := range s.logs[did] {
if s.logs[did][i].CID == cid {
e := s.logs[did][i]
return &e, nil
}
}
return nil, nil
}
func (s *MemoryStore) InsertOp(ctx context.Context, entry LogEntry) error {
s.mu.Lock()
defer s.mu.Unlock()
s.logs[entry.DID] = append(s.logs[entry.DID], entry)
return nil
}
func (s *MemoryStore) NullifyOps(ctx context.Context, did string, cids []string) error {
s.mu.Lock()
defer s.mu.Unlock()
set := make(map[string]struct{}, len(cids))
for _, c := range cids {
set[c] = struct{}{}
}
for i := range s.logs[did] {
if _, ok := set[s.logs[did][i].CID]; ok {
s.logs[did][i].Nullified = true
}
}
return nil
}
func (s *MemoryStore) ExportLogs(ctx context.Context, after *time.Time, count int) ([]LogEntry, error) {
s.mu.RLock()
all := []LogEntry{}
for _, entries := range s.logs {
all = append(all, entries...)
}
s.mu.RUnlock()
if after != nil {
filtered := all[:0]
for _, e := range all {
t, err := time.Parse(time.RFC3339Nano, e.CreatedAt)
if err != nil || t.After(*after) {
filtered = append(filtered, e)
}
}
all = filtered
}
sort.SliceStable(all, func(i, j int) bool { return all[i].CreatedAt < all[j].CreatedAt })
if count >= 0 && len(all) > count {
all = all[:count]
}
return all, nil
}
func (s *MemoryStore) snapshot() map[string][]LogEntry {
s.mu.RLock()
defer s.mu.RUnlock()
out := make(map[string][]LogEntry, len(s.logs))
for did, entries := range s.logs {
out[did] = append([]LogEntry{}, entries...)
}
return out
}
func (s *MemoryStore) replace(logs map[string][]LogEntry) {
s.mu.Lock()
defer s.mu.Unlock()
s.logs = logs
}
type FileStore struct {
*MemoryStore
path string
mu sync.Mutex
}
func NewFileStore(path string) (*FileStore, error) {
fs := &FileStore{MemoryStore: NewMemoryStore(), path: path}
b, err := os.ReadFile(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return fs, nil
}
return nil, err
}
if len(b) == 0 {
return fs, nil
}
var logs map[string][]LogEntry
if err := json.Unmarshal(b, &logs); err != nil {
return nil, err
}
if logs == nil {
logs = map[string][]LogEntry{}
}
fs.MemoryStore.replace(logs)
return fs, nil
}
func (s *FileStore) InsertOp(ctx context.Context, entry LogEntry) error {
if err := s.MemoryStore.InsertOp(ctx, entry); err != nil {
return err
}
return s.save()
}
func (s *FileStore) NullifyOps(ctx context.Context, did string, cids []string) error {
if err := s.MemoryStore.NullifyOps(ctx, did, cids); err != nil {
return err
}
return s.save()
}
func (s *FileStore) save() error {
s.mu.Lock()
defer s.mu.Unlock()
b, err := json.Marshal(s.MemoryStore.snapshot())
if err != nil {
return err
}
dir := filepath.Dir(s.path)
if dir != "" && dir != "." {
if err := os.MkdirAll(dir, 0o700); err != nil {
return err
}
}
tmp, err := os.CreateTemp(dir, filepath.Base(s.path)+".tmp-*")
if err != nil {
return err
}
tmpName := tmp.Name()
defer os.Remove(tmpName)
if err := tmp.Chmod(0o600); err != nil {
tmp.Close()
return err
}
if _, err := tmp.Write(b); err != nil {
tmp.Close()
return err
}
if err := tmp.Close(); err != nil {
return err
}
return os.Rename(tmpName, s.path)
}