This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
/
dummystore.go
382 lines (302 loc) · 10.2 KB
/
dummystore.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
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
// Copyright 2016-2018 Stratumn SAS. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package dummystore implements a store that saves all the segments in memory.
//
// It can be used for testing, but it's unoptimized and not designed for
// production.
package dummystore
import (
"context"
"fmt"
"sort"
"sync"
"github.com/stratumn/go-chainscript"
"github.com/stratumn/go-core/bufferedbatch"
"github.com/stratumn/go-core/monitoring/errorcode"
"github.com/stratumn/go-core/store"
"github.com/stratumn/go-core/types"
)
const (
// Name is the name set in the store's information.
Name = "dummystore"
// Description is the description set in the store's information.
Description = "Stratumn's Dummy Store"
)
// Config contains configuration options for the store.
type Config struct {
// A version string that will be set in the store's information.
Version string
// A git commit hash that will be set in the store's information.
Commit string
}
// Info is the info returned by GetInfo.
type Info struct {
Name string `json:"name"`
Description string `json:"description"`
Version string `json:"version"`
Commit string `json:"commit"`
}
// DummyStore is the type that implements github.com/stratumn/go-core/store.Adapter.
type DummyStore struct {
config *Config
eventChans []chan *store.Event
links linkMap // maps link hashes to segments
linksChildCount linkChildCountMap // maps link hashes to their children count
evidences evidenceMap // maps link hashes to evidences
values valueMap // maps keys to values
maps hashSetMap // maps chains IDs to sets of link hashes
mutex sync.RWMutex // simple global mutex
}
type linkMap map[string]*chainscript.Link
type linkChildCountMap map[string]int
type evidenceMap map[string]types.EvidenceSlice
type hashSet map[string]struct{}
type hashSetMap map[string]hashSet
type valueMap map[string][]byte
// New creates an instance of a DummyStore.
func New(config *Config) *DummyStore {
return &DummyStore{
config: config,
eventChans: nil,
links: linkMap{},
linksChildCount: linkChildCountMap{},
evidences: evidenceMap{},
values: valueMap{},
maps: hashSetMap{},
mutex: sync.RWMutex{},
}
}
// GetInfo implements github.com/stratumn/go-core/store.Adapter.GetInfo.
func (a *DummyStore) GetInfo(ctx context.Context) (interface{}, error) {
return &Info{
Name: Name,
Description: Description,
Version: a.config.Version,
Commit: a.config.Commit,
}, nil
}
// AddStoreEventChannel implements github.com/stratumn/go-core/store.Adapter.AddStoreEventChannel
func (a *DummyStore) AddStoreEventChannel(eventChan chan *store.Event) {
a.eventChans = append(a.eventChans, eventChan)
}
/********** Store writer implementation **********/
// CreateLink implements github.com/stratumn/go-core/store.LinkWriter.CreateLink.
func (a *DummyStore) CreateLink(ctx context.Context, link *chainscript.Link) (chainscript.LinkHash, error) {
a.mutex.Lock()
defer a.mutex.Unlock()
return a.createLink(link)
}
func (a *DummyStore) createLink(link *chainscript.Link) (chainscript.LinkHash, error) {
linkHash, err := link.Hash()
if err != nil {
return nil, types.WrapError(err, errorcode.InvalidArgument, store.Component, "could not hash link")
}
linkHashStr := linkHash.String()
_, ok := a.links[linkHashStr]
if ok {
return linkHash, nil
}
parentOk := a.canHaveNewChild(link.PrevLinkHash())
if !parentOk {
return linkHash, types.WrapError(chainscript.ErrOutDegree, errorcode.FailedPrecondition, store.Component, "could not create link")
}
a.links[linkHashStr] = link
a.incrementChildCount(link.PrevLinkHash())
mapID := link.Meta.MapId
_, exists := a.maps[mapID]
if !exists {
a.maps[mapID] = hashSet{}
}
a.maps[mapID][linkHashStr] = struct{}{}
linkEvent := store.NewSavedLinks(link)
for _, c := range a.eventChans {
c <- linkEvent
}
return linkHash, nil
}
func (a *DummyStore) canHaveNewChild(linkHash chainscript.LinkHash) bool {
if len(linkHash) == 0 {
return true
}
linkHashStr := linkHash.String()
link := a.links[linkHashStr]
if link.Meta.OutDegree < 0 {
return true
}
if link.Meta.OutDegree == 0 {
return false
}
childCount, ok := a.linksChildCount[linkHashStr]
if !ok {
childCount = 0
}
return childCount < int(link.Meta.OutDegree)
}
func (a *DummyStore) incrementChildCount(linkHash chainscript.LinkHash) {
linkHashStr := linkHash.String()
_, ok := a.linksChildCount[linkHashStr]
if !ok {
a.linksChildCount[linkHashStr] = 0
}
a.linksChildCount[linkHashStr]++
}
// AddEvidence implements github.com/stratumn/go-core/store.EvidenceWriter.AddEvidence.
func (a *DummyStore) AddEvidence(ctx context.Context, linkHash chainscript.LinkHash, evidence *chainscript.Evidence) error {
a.mutex.Lock()
defer a.mutex.Unlock()
if err := a.addEvidence(linkHash.String(), evidence); err != nil {
return err
}
evidenceEvent := store.NewSavedEvidences()
evidenceEvent.AddSavedEvidence(linkHash, evidence)
for _, c := range a.eventChans {
c <- evidenceEvent
}
return nil
}
func (a *DummyStore) addEvidence(linkHash string, evidence *chainscript.Evidence) error {
currentEvidences := a.evidences[linkHash]
if currentEvidences == nil {
currentEvidences = types.EvidenceSlice{}
}
if err := currentEvidences.AddEvidence(evidence); err != nil {
return err
}
a.evidences[linkHash] = currentEvidences
return nil
}
/********** Store reader implementation **********/
// GetSegment implements github.com/stratumn/go-core/store.Adapter.GetSegment.
func (a *DummyStore) GetSegment(ctx context.Context, linkHash chainscript.LinkHash) (*chainscript.Segment, error) {
a.mutex.RLock()
defer a.mutex.RUnlock()
return a.getSegment(linkHash.String())
}
// GetSegment implements github.com/stratumn/go-core/store.Adapter.GetSegment.
func (a *DummyStore) getSegment(linkHash string) (*chainscript.Segment, error) {
link, exists := a.links[linkHash]
if !exists {
return nil, nil
}
segment, err := link.Segmentify()
if err != nil {
return nil, types.WrapError(err, errorcode.InvalidArgument, store.Component, "could not segmentify")
}
evidences, exists := a.evidences[linkHash]
if exists {
segment.Meta.Evidences = evidences
}
return segment, nil
}
// FindSegments implements github.com/stratumn/go-core/store.Adapter.FindSegments.
func (a *DummyStore) FindSegments(ctx context.Context, filter *store.SegmentFilter) (*types.PaginatedSegments, error) {
a.mutex.RLock()
defer a.mutex.RUnlock()
var linkHashes = hashSet{}
if len(filter.MapIDs) == 0 {
for linkHash := range a.links {
linkHashes[linkHash] = struct{}{}
}
} else {
for _, mapID := range filter.MapIDs {
l, e := a.maps[mapID]
if e {
for k, v := range l {
linkHashes[k] = v
}
}
}
}
return a.findHashesSegments(linkHashes, filter)
}
// GetMapIDs implements github.com/stratumn/go-core/store.Adapter.GetMapIDs.
func (a *DummyStore) GetMapIDs(ctx context.Context, filter *store.MapFilter) ([]string, error) {
a.mutex.RLock()
defer a.mutex.RUnlock()
mapIDs := make([]string, 0, len(a.maps))
for mapID, linkHashes := range a.maps {
for linkHash := range linkHashes {
if link, exists := a.links[linkHash]; exists && filter.MatchLink(link) {
mapIDs = append(mapIDs, mapID)
break
}
}
}
sort.Strings(mapIDs)
return filter.Pagination.PaginateStrings(mapIDs), nil
}
// GetEvidences implements github.com/stratumn/go-core/store.EvidenceReader.GetEvidences.
func (a *DummyStore) GetEvidences(ctx context.Context, linkHash chainscript.LinkHash) (types.EvidenceSlice, error) {
a.mutex.RLock()
defer a.mutex.RUnlock()
evidences := a.evidences[linkHash.String()]
return evidences, nil
}
/********** github.com/stratumn/go-core/store.KeyValueStore implementation **********/
// GetValue implements github.com/stratumn/go-core/store.KeyValueStore.GetValue.
func (a *DummyStore) GetValue(ctx context.Context, key []byte) ([]byte, error) {
a.mutex.RLock()
defer a.mutex.RUnlock()
return a.values[createKey(key)], nil
}
// SetValue implements github.com/stratumn/go-core/store.KeyValueStore.SetValue.
func (a *DummyStore) SetValue(ctx context.Context, key, value []byte) error {
a.mutex.Lock()
defer a.mutex.Unlock()
return a.setValue(key, value)
}
func (a *DummyStore) setValue(key, value []byte) error {
k := createKey(key)
a.values[k] = value
return nil
}
// DeleteValue implements github.com/stratumn/go-core/store.KeyValueStore.DeleteValue.
func (a *DummyStore) DeleteValue(ctx context.Context, key []byte) ([]byte, error) {
a.mutex.Lock()
defer a.mutex.Unlock()
return a.deleteValue(key)
}
func (a *DummyStore) deleteValue(key []byte) ([]byte, error) {
k := createKey(key)
value, exists := a.values[k]
if !exists {
return nil, nil
}
delete(a.values, k)
return value, nil
}
/********** github.com/stratumn/go-core/store.Batch implementation **********/
// NewBatch implements github.com/stratumn/go-core/store.Adapter.NewBatch.
func (a *DummyStore) NewBatch(ctx context.Context) (store.Batch, error) {
return bufferedbatch.NewBatch(ctx, a), nil
}
/********** Utilities **********/
func (a *DummyStore) findHashesSegments(linkHashes hashSet, filter *store.SegmentFilter) (*types.PaginatedSegments, error) {
segments := &types.PaginatedSegments{}
for linkHash := range linkHashes {
segment, err := a.getSegment(linkHash)
if err != nil {
return nil, err
}
if filter.Match(segment) {
segments.Segments = append(segments.Segments, segment)
}
}
segments.TotalCount = len(segments.Segments)
segments.Segments.Sort(filter.Reverse)
return filter.Pagination.PaginateSegments(segments), nil
}
func createKey(k []byte) string {
return fmt.Sprintf("%x", k)
}