-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathstorage.go
More file actions
359 lines (299 loc) · 10.3 KB
/
Copy pathstorage.go
File metadata and controls
359 lines (299 loc) · 10.3 KB
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
package storage
import (
"fmt"
"sync"
messages "github.com/cucumber/messages/go/v21"
"github.com/hashicorp/go-memdb"
"github.com/cucumber/godog/internal/models"
)
const (
writeMode bool = true
readMode bool = false
tableFeature string = "feature"
tableFeatureIndexURI string = "id"
tablePickle string = "pickle"
tablePickleIndexID string = "id"
tablePickleIndexURI string = "uri"
tablePickleStep string = "pickle_step"
tablePickleStepIndexID string = "id"
tablePickleResult string = "pickle_result"
tablePickleResultIndexPickleID string = "id"
tablePickleStepResult string = "pickle_step_result"
tablePickleStepResultIndexPickleStepID string = "id"
tablePickleStepResultIndexPickleID string = "pickle_id"
tablePickleStepResultIndexStatus string = "status"
tableStepDefintionMatch string = "step_defintion_match"
tableStepDefintionMatchIndexStepID string = "id"
)
// Storage is a thread safe in-mem storage
type Storage struct {
db *memdb.MemDB
testRunStarted models.TestRunStarted
testRunStartedLock *sync.Mutex
}
// NewStorage will create an in-mem storage that
// is used across concurrent runners and formatters
func NewStorage() *Storage {
schema := memdb.DBSchema{
Tables: map[string]*memdb.TableSchema{
tableFeature: {
Name: tableFeature,
Indexes: map[string]*memdb.IndexSchema{
tableFeatureIndexURI: {
Name: tableFeatureIndexURI,
Unique: true,
Indexer: &memdb.StringFieldIndex{Field: "Uri"},
},
},
},
tablePickle: {
Name: tablePickle,
Indexes: map[string]*memdb.IndexSchema{
tablePickleIndexID: {
Name: tablePickleIndexID,
Unique: true,
Indexer: &memdb.StringFieldIndex{Field: "Id"},
},
tablePickleIndexURI: {
Name: tablePickleIndexURI,
Unique: false,
Indexer: &memdb.StringFieldIndex{Field: "Uri"},
},
},
},
tablePickleStep: {
Name: tablePickleStep,
Indexes: map[string]*memdb.IndexSchema{
tablePickleStepIndexID: {
Name: tablePickleStepIndexID,
Unique: true,
Indexer: &memdb.StringFieldIndex{Field: "Id"},
},
},
},
tablePickleResult: {
Name: tablePickleResult,
Indexes: map[string]*memdb.IndexSchema{
tablePickleResultIndexPickleID: {
Name: tablePickleResultIndexPickleID,
Unique: true,
Indexer: &memdb.StringFieldIndex{Field: "PickleID"},
},
},
},
tablePickleStepResult: {
Name: tablePickleStepResult,
Indexes: map[string]*memdb.IndexSchema{
tablePickleStepResultIndexPickleStepID: {
Name: tablePickleStepResultIndexPickleStepID,
Unique: true,
Indexer: &memdb.StringFieldIndex{Field: "PickleStepID"},
},
tablePickleStepResultIndexPickleID: {
Name: tablePickleStepResultIndexPickleID,
Unique: false,
Indexer: &memdb.StringFieldIndex{Field: "PickleID"},
},
tablePickleStepResultIndexStatus: {
Name: tablePickleStepResultIndexStatus,
Unique: false,
Indexer: &memdb.IntFieldIndex{Field: "Status"},
},
},
},
tableStepDefintionMatch: {
Name: tableStepDefintionMatch,
Indexes: map[string]*memdb.IndexSchema{
tableStepDefintionMatchIndexStepID: {
Name: tableStepDefintionMatchIndexStepID,
Unique: true,
Indexer: &memdb.StringFieldIndex{Field: "StepID"},
},
},
},
},
}
db, err := memdb.NewMemDB(&schema)
if err != nil {
panic(err)
}
return &Storage{db: db, testRunStartedLock: new(sync.Mutex)}
}
// MustInsertPickle will insert a pickle and it's steps,
// will panic on error.
func (s *Storage) MustInsertPickle(p *messages.Pickle) {
txn := s.db.Txn(writeMode)
if err := txn.Insert(tablePickle, p); err != nil {
panic(err)
}
for _, step := range p.Steps {
if err := txn.Insert(tablePickleStep, step); err != nil {
panic(err)
}
}
txn.Commit()
}
// MustGetPickle will retrieve a pickle by id and panic on error.
func (s *Storage) MustGetPickle(id string) *messages.Pickle {
v := s.mustFirst(tablePickle, tablePickleIndexID, id)
return v.(*messages.Pickle)
}
// MustGetPickles will retrieve pickles by URI and panic on error.
func (s *Storage) MustGetPickles(uri string) (ps []*messages.Pickle) {
it := s.mustGet(tablePickle, tablePickleIndexURI, uri)
for v := it.Next(); v != nil; v = it.Next() {
ps = append(ps, v.(*messages.Pickle))
}
return
}
// MustGetPickleStep will retrieve a pickle step and panic on error.
func (s *Storage) MustGetPickleStep(id string) *messages.PickleStep {
v := s.mustFirst(tablePickleStep, tablePickleStepIndexID, id)
return v.(*messages.PickleStep)
}
// MustInsertTestRunStarted will set the test run started event and panic on error.
func (s *Storage) MustInsertTestRunStarted(trs models.TestRunStarted) {
s.testRunStartedLock.Lock()
defer s.testRunStartedLock.Unlock()
s.testRunStarted = trs
}
// MustGetTestRunStarted will retrieve the test run started event and panic on error.
func (s *Storage) MustGetTestRunStarted() models.TestRunStarted {
s.testRunStartedLock.Lock()
defer s.testRunStartedLock.Unlock()
return s.testRunStarted
}
// MustInsertPickleResult will instert a pickle result and panic on error.
func (s *Storage) MustInsertPickleResult(pr models.PickleResult) {
s.mustInsert(tablePickleResult, pr)
}
// MustInsertPickleStepResult will insert a pickle step result and panic on error.
func (s *Storage) MustInsertPickleStepResult(psr models.PickleStepResult) {
s.mustInsert(tablePickleStepResult, psr)
}
// MustGetPickleResult will retrieve a pickle result by id and panic on error.
func (s *Storage) MustGetPickleResult(id string) models.PickleResult {
v := s.mustFirst(tablePickleResult, tablePickleResultIndexPickleID, id)
return v.(models.PickleResult)
}
// MustGetPickleResults will retrieve all pickle results and panic on error.
func (s *Storage) MustGetPickleResults() (prs []models.PickleResult) {
it := s.mustGet(tablePickleResult, tablePickleResultIndexPickleID)
for v := it.Next(); v != nil; v = it.Next() {
prs = append(prs, v.(models.PickleResult))
}
return prs
}
// MustGetPickleStepResult will retrieve a pickle step result by id and panic on error.
func (s *Storage) MustGetPickleStepResult(id string) models.PickleStepResult {
v := s.mustFirst(tablePickleStepResult, tablePickleStepResultIndexPickleStepID, id)
return v.(models.PickleStepResult)
}
// MustGetPickleStepResultsByPickleID will retrieve pickle step results by pickle id and panic on error.
func (s *Storage) MustGetPickleStepResultsByPickleID(pickleID string) (psrs []models.PickleStepResult) {
it := s.mustGet(tablePickleStepResult, tablePickleStepResultIndexPickleID, pickleID)
for v := it.Next(); v != nil; v = it.Next() {
psrs = append(psrs, v.(models.PickleStepResult))
}
return psrs
}
// MustGetPickleStepResultsByPickleIDUntilStep will retrieve pickle step results by pickle id
// from 0..stepID for that pickle.
func (s *Storage) MustGetPickleStepResultsByPickleIDUntilStep(pickleID string, untilStepID string) (psrs []models.PickleStepResult) {
it := s.mustGet(tablePickleStepResult, tablePickleStepResultIndexPickleID, pickleID)
for v := it.Next(); v != nil; v = it.Next() {
psr := v.(models.PickleStepResult)
psrs = append(psrs, psr)
if psr.PickleStepID == untilStepID {
break
}
}
return psrs
}
// MustGetPickleStepResultsByStatus will retrieve pickle strep results by status and panic on error.
func (s *Storage) MustGetPickleStepResultsByStatus(status models.StepResultStatus) (psrs []models.PickleStepResult) {
it := s.mustGet(tablePickleStepResult, tablePickleStepResultIndexStatus, status)
for v := it.Next(); v != nil; v = it.Next() {
psrs = append(psrs, v.(models.PickleStepResult))
}
return psrs
}
// MustInsertFeature will insert a feature and panic on error.
func (s *Storage) MustInsertFeature(f *models.Feature) {
s.mustInsert(tableFeature, f)
}
// MustGetFeature will retrieve a feature by URI and panic on error.
func (s *Storage) MustGetFeature(uri string) *models.Feature {
v := s.mustFirst(tableFeature, tableFeatureIndexURI, uri)
return v.(*models.Feature)
}
// MustGetFeatures will retrieve all features by and panic on error.
func (s *Storage) MustGetFeatures() (fs []*models.Feature) {
it := s.mustGet(tableFeature, tableFeatureIndexURI)
for v := it.Next(); v != nil; v = it.Next() {
fs = append(fs, v.(*models.Feature))
}
return
}
// GetPickleStepResultByStepID will retrieve a pickle step result by id and panic on error.
func (s *Storage) GetPickleStepResultByStepID(id string) models.PickleStepResult {
v := s.first(tablePickleStepResult, tablePickleStepResultIndexPickleStepID, id)
if v != nil {
return v.(models.PickleStepResult)
}
return models.PickleStepResult{}
}
type stepDefinitionMatch struct {
StepID string
StepDefinition *models.StepDefinition
}
// MustInsertStepDefintionMatch will insert the matched StepDefintion for the step ID and panic on error.
func (s *Storage) MustInsertStepDefintionMatch(stepID string, match *models.StepDefinition) {
d := stepDefinitionMatch{
StepID: stepID,
StepDefinition: match,
}
s.mustInsert(tableStepDefintionMatch, d)
}
// MustGetStepDefintionMatch will retrieve the matched StepDefintion for the step ID and panic on error.
func (s *Storage) MustGetStepDefintionMatch(stepID string) *models.StepDefinition {
v := s.mustFirst(tableStepDefintionMatch, tableStepDefintionMatchIndexStepID, stepID)
return v.(stepDefinitionMatch).StepDefinition
}
func (s *Storage) mustInsert(table string, obj interface{}) {
txn := s.db.Txn(writeMode)
if err := txn.Insert(table, obj); err != nil {
panic(err)
}
txn.Commit()
}
func (s *Storage) mustFirst(table, index string, args ...interface{}) interface{} {
txn := s.db.Txn(readMode)
defer txn.Abort()
v, err := txn.First(table, index, args...)
if err != nil {
panic(err)
} else if v == nil {
err = fmt.Errorf("couldn't find index: %q in table: %q with args: %+v", index, table, args)
panic(err)
}
return v
}
func (s *Storage) mustGet(table, index string, args ...interface{}) memdb.ResultIterator {
txn := s.db.Txn(readMode)
defer txn.Abort()
it, err := txn.Get(table, index, args...)
if err != nil {
panic(err)
}
return it
}
func (s *Storage) first(table, index string, args ...interface{}) interface{} {
txn := s.db.Txn(readMode)
defer txn.Abort()
v, err := txn.First(table, index, args...)
if err != nil {
panic(err)
}
return v
}