-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.go
219 lines (176 loc) · 5.38 KB
/
storage.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
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
//go:generate go run github.com/vektra/mockery/v2@latest --name=ObjectStore --filename object_store_mock.go
//go:generate go run github.com/vektra/mockery/v2@latest --name=DocumentStore --filename document_store_mock.go
package sakuin
import (
"context"
"sync"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
)
type ObjectDoesNotExistErr struct {
ID string
}
func (e ObjectDoesNotExistErr) Error() string {
return e.ID
}
type DocumentDoesNotExistErr struct {
ID string
}
func (e DocumentDoesNotExistErr) Error() string {
return e.ID
}
type StatInfo struct {
Exists bool
Size int
}
type ObjectStore interface {
Stat(ctx context.Context, id string) (*StatInfo, error)
Get(ctx context.Context, id string) ([]byte, error)
Put(ctx context.Context, id string, b []byte) error
Update(ctx context.Context, id string, b []byte) error
}
type TestingT interface {
assert.TestingT
Run(name string, f func(TestingT))
}
func RunObjectStorageTests(t TestingT, objStore ObjectStore) {
t.Run("get object should fail with ObjectDoesNotExistErr if object doesn't exist", func(subT TestingT) {
var objErr ObjectDoesNotExistErr
_, err := objStore.Get(context.Background(), "")
assert.ErrorAs(subT, err, &objErr, "expected an ObjectDoesNotExistErr")
})
t.Run("update object should fail with ObjectDoesNotExistErr if object doesn't exist", func(subT TestingT) {
var objErr ObjectDoesNotExistErr
err := objStore.Update(context.Background(), "", []byte{})
assert.ErrorAs(subT, err, &objErr, "expected an ObjectDoesNotExistErr")
})
}
type InMemoryObjectStore struct {
mu sync.Mutex
objects map[string][]byte
}
func NewInMemoryObjectStore() *InMemoryObjectStore {
return &InMemoryObjectStore{
objects: make(map[string][]byte),
}
}
func (s *InMemoryObjectStore) Stat(ctx context.Context, id string) (*StatInfo, error) {
s.mu.Lock()
obj, exists := s.objects[id]
s.mu.Unlock()
return &StatInfo{Exists: exists, Size: len(obj)}, nil
}
func (s *InMemoryObjectStore) Get(ctx context.Context, id string) ([]byte, error) {
s.mu.Lock()
obj, exists := s.objects[id]
s.mu.Unlock()
if !exists {
zap.L().Warn("unable to find object in memory", zap.String("id", id))
return nil, ObjectDoesNotExistErr{ID: id}
}
zap.L().Debug("successfully retrieved object from memory", zap.String("id", id))
return obj, nil
}
func (s *InMemoryObjectStore) Put(ctx context.Context, id string, b []byte) error {
s.mu.Lock()
s.objects[id] = b
s.mu.Unlock()
zap.L().Debug("successfully stored object in memory", zap.String("id", id))
return nil
}
func (s *InMemoryObjectStore) Update(ctx context.Context, id string, b []byte) error {
s.mu.Lock()
if _, exists := s.objects[id]; !exists {
return ObjectDoesNotExistErr{ID: id}
}
s.objects[id] = b
s.mu.Unlock()
zap.L().Debug("successfully updated object in memory", zap.String("id", id))
return nil
}
func (s *InMemoryObjectStore) WithObject(id string, obj []byte) *InMemoryObjectStore {
s.objects[id] = obj
return s
}
func (s *InMemoryObjectStore) NumOfObects() int {
s.mu.Lock()
defer s.mu.Unlock()
return len(s.objects)
}
type DocumentStore interface {
Stat(ctx context.Context, id string) (*StatInfo, error)
Get(ctx context.Context, id string) (map[string]interface{}, error)
Upsert(ctx context.Context, id string, b map[string]interface{}) error
}
func RunDocumentStorageTests(t TestingT, docStore DocumentStore) {
t.Run("should fail with DocumentDoesNotExistErr if document doesn't exist", func(subT TestingT) {
var docErr DocumentDoesNotExistErr
_, err := docStore.Get(context.Background(), "")
assert.ErrorAs(subT, err, &docErr, "expected and DocumentDoesNotExistErr")
})
}
type InMemoryDocumentStore struct {
mu sync.Mutex
docs map[string]map[string]interface{}
}
func NewInMemoryDocumentStore() *InMemoryDocumentStore {
return &InMemoryDocumentStore{
docs: make(map[string]map[string]interface{}),
}
}
func (s *InMemoryDocumentStore) Stat(ctx context.Context, id string) (*StatInfo, error) {
s.mu.Lock()
doc, exists := s.docs[id]
s.mu.Unlock()
return &StatInfo{Exists: exists, Size: len(doc)}, nil
}
func (s *InMemoryDocumentStore) Get(ctx context.Context, id string) (map[string]interface{}, error) {
s.mu.Lock()
doc, exists := s.docs[id]
s.mu.Unlock()
if !exists {
zap.L().Warn("unable to retrieve document from memory", zap.String("id", id))
return nil, DocumentDoesNotExistErr{ID: id}
}
zap.L().Debug("successfully retrieved document from memory", zap.String("id", id))
return doc, nil
}
func (s *InMemoryDocumentStore) Upsert(ctx context.Context, id string, doc map[string]interface{}) error {
s.mu.Lock()
d, ok := s.docs[id]
if ok {
doc = mergeDocs(doc, d)
}
s.docs[id] = doc
s.mu.Unlock()
zap.L().Debug("successfully stored document in memory", zap.String("id", id))
return nil
}
func (s *InMemoryDocumentStore) WithDocument(id string, doc map[string]interface{}) *InMemoryDocumentStore {
s.docs[id] = doc
return s
}
func (s *InMemoryDocumentStore) NumOfDocs() int {
s.mu.Lock()
defer s.mu.Unlock()
return len(s.docs)
}
func mergeDocs(dst, src map[string]interface{}) map[string]interface{} {
for k, sv := range src {
dv, exists := dst[k]
if !exists {
dst[k] = sv
continue
}
svMap, ok := sv.(map[string]interface{})
if !ok {
continue
}
dvMap, ok := dv.(map[string]interface{})
if !ok {
panic("expected documents to have consistent type for given field")
}
mergeDocs(dvMap, svMap)
}
return dst
}