mirrored from https://skia.googlesource.com/buildbot
/
issuestore.go
280 lines (238 loc) · 8.23 KB
/
issuestore.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
package issuestore
import (
"path"
"github.com/boltdb/bolt"
"go.skia.org/infra/go/boltutil"
"go.skia.org/infra/go/fileutil"
"go.skia.org/infra/go/util"
)
// IssueStore captures the functions necessary to persist the connection between
// Monorail issues and digests, traces, tests and ignores.
type IssueStore interface {
// ByDigest returns the ids of all issue associated with the given digest.
ByDigest(digest string) ([]string, error) // list of issues
// ByDigest returns the ids of all issue associated with the given digest.
ByIgnore(ignoreID string) ([]string, error) // list of issues
// ByDigest returns the ids of all issue associated with the given digest.
ByTrace(traceID string) ([]string, error) // list of issues
// ByDigest returns the ids of all issue associated with the given digest.
ByTest(testName string) ([]string, error) // list of issues
// Add allows to create an issue annotation or add to an existing annotation.
// If the issue identified by delta.IssueID exists, delta will be merged into
// the existing annotation.
Add(delta *Annotation) error
// Subtract removes from an existing issue annotation. The values in
// delta are subtracted from an existing annotation.
Subtract(delta *Annotation) error
// Get returns the annotations for the given list of issue ids.
Get(issueIDs []string) ([]*Annotation, error)
// List returns a list of all issues that are currently annotated with
// support of paging. The first 'offset' annotations will be skipped and
// the returned array has at most 'size'. If 'size' <= 0 there is no limit
// on the number of annotations returned.
List(offset int, size int) ([]*Annotation, int, error)
// Delete the given issue annotations.
Delete(issueIDs []string) error
}
var annotationIndices = []string{DIGEST_INDEX, TRACE_INDEX, IGNORE_INDEX, TEST_INDEX}
// Annotation captures annotations for the issue identified by IssueID.
type Annotation struct {
IssueID string // id of the issue in Monorail
Digests []string // Image digests connected to this issue
Traces []string // Trace ids connected to this issues.
Ignores []string // Ignore ids connected to this issue.
TestNames []string // Test names connected to this issue.
}
// Key see boltutil.Record interface.
func (a *Annotation) Key() string {
return a.IssueID
}
// IndexValues see boltutil.Record interface.
func (a *Annotation) IndexValues() map[string][]string {
ret := make(map[string][]string, len(annotationIndices))
for _, idx := range annotationIndices {
switch idx {
case DIGEST_INDEX:
ret[idx] = append(ret[idx], a.Digests...)
case TRACE_INDEX:
ret[idx] = append(ret[idx], a.Traces...)
case IGNORE_INDEX:
ret[idx] = append(ret[idx], a.Ignores...)
case TEST_INDEX:
ret[idx] = append(ret[idx], a.TestNames...)
}
}
return ret
}
// Adds the digests, traces, ignores and tests in delta to the current annotation.
// and deduplicates in the process.
func (r *Annotation) Add(deltaRec *Annotation) bool {
updated := mergeStrings(&r.Digests, deltaRec.Digests)
updated = mergeStrings(&r.Traces, deltaRec.Traces) || updated
updated = mergeStrings(&r.Ignores, deltaRec.Ignores) || updated
return mergeStrings(&r.TestNames, deltaRec.TestNames) || updated
}
// Subtract removes the digests, traces, ignores and tests in delta from the current annotation.
func (r *Annotation) Subtract(deltaRec *Annotation) bool {
updated := removeStrings(&r.Digests, deltaRec.Digests)
updated = removeStrings(&r.Traces, deltaRec.Traces) || updated
updated = removeStrings(&r.Ignores, deltaRec.Ignores) || updated
return removeStrings(&r.TestNames, deltaRec.TestNames) || updated
}
// IsEmpty returns true if all all annotations are empty.
func (r *Annotation) IsEmpty() bool {
return (len(r.Digests) + len(r.Traces) + len(r.Ignores) + len(r.TestNames)) == 0
}
const (
// Bucket names in boltdb. 'INDEX' in the name indicates an index.
ISSUES_DB = "issues"
DIGEST_INDEX = "digest"
TRACE_INDEX = "trace"
IGNORE_INDEX = "ignore"
TEST_INDEX = "test"
)
// Separator used to separate child and parent id in indices.
const IDX_SEPARATOR = "|"
// boltIssueStore implements the IssueStore interface.
type boltIssueStore struct {
store *boltutil.IndexedBucket
}
// New returns a new instance of IssueStore that is stored in the given directory.
func New(baseDir string) (IssueStore, error) {
baseDir, err := fileutil.EnsureDirExists(baseDir)
if err != nil {
return nil, err
}
db, err := bolt.Open(path.Join(baseDir, "issuestore.db"), 0600, nil)
if err != nil {
return nil, err
}
config := &boltutil.Config{
DB: db,
Name: "issues",
Indices: annotationIndices,
Codec: util.JSONCodec(&Annotation{}),
}
store, err := boltutil.NewIndexedBucket(config)
if err != nil {
return nil, err
}
return &boltIssueStore{
store: store,
}, nil
}
// ByDigest, see IgnoreStore interface.
func (b *boltIssueStore) ByDigest(digest string) ([]string, error) {
return b.readFromIndex(DIGEST_INDEX, digest)
}
// ByIgnore, see IgnoreStore interface.
func (b *boltIssueStore) ByIgnore(ignoreID string) ([]string, error) {
return b.readFromIndex(IGNORE_INDEX, ignoreID)
}
// ByTrace, see IgnoreStore interface.
func (b *boltIssueStore) ByTrace(traceID string) ([]string, error) {
return b.readFromIndex(TRACE_INDEX, traceID)
}
// ByTest, see IgnoreStore interface.
func (b *boltIssueStore) ByTest(testName string) ([]string, error) {
return b.readFromIndex(TEST_INDEX, testName)
}
// Get, see IgnoreStore interface.
func (b *boltIssueStore) Get(issueIDs []string) ([]*Annotation, error) {
if len(issueIDs) == 0 {
return []*Annotation{}, nil
}
result, err := b.store.Read(issueIDs)
if err != nil {
return nil, err
}
ret := make([]*Annotation, len(result))
for i, val := range result {
ret[i] = val.(*Annotation)
}
return ret, nil
}
// Add, see IgnoreStore interface.
func (b *boltIssueStore) Add(delta *Annotation) error {
if delta.IsEmpty() {
return nil
}
writeFn := func(tx *bolt.Tx, result []boltutil.Record) error {
if result[0] != nil {
// If there a no change then don't write any records.
if !result[0].(*Annotation).Add(delta) {
result[0] = nil
}
} else {
result[0] = delta
}
return nil
}
return b.store.Update([]boltutil.Record{delta}, writeFn)
}
// List, see IgnoreStore interface.
func (b *boltIssueStore) List(offset int, size int) ([]*Annotation, int, error) {
result, total, err := b.store.List(offset, size)
if err != nil {
return nil, 0, err
}
ret := make([]*Annotation, len(result))
for i, rec := range result {
ret[i] = rec.(*Annotation)
}
return ret, total, nil
}
// Subtract, see IgnoreStore interface.
func (b *boltIssueStore) Subtract(delta *Annotation) error {
writeFn := func(tx *bolt.Tx, result []boltutil.Record) error {
found := result[0]
if found != nil {
rec := found.(*Annotation)
// Subtract the delta and only take action if there was a change.
if rec.Subtract(delta) {
// If the resulting record is not empty, then we write it to disk.
if !rec.IsEmpty() {
return nil
}
// Delete the empty record.
if err := b.store.DeleteTx(tx, []string{rec.IssueID}); err != nil {
return err
}
}
}
result[0] = nil
return nil
}
return b.store.Update([]boltutil.Record{delta}, writeFn)
}
// Delete, see IgnoreStore interface.
func (b *boltIssueStore) Delete(issueIDs []string) error {
return b.store.Delete(issueIDs)
}
// readFromIndex does a lookup in the given index for the given value and
// returns all primary keys that match.
func (b *boltIssueStore) readFromIndex(index, value string) ([]string, error) {
ret, err := b.store.ReadIndex(index, []string{value})
if err != nil {
return nil, err
}
return ret[value], nil
}
// mergeStrings merges the strings of src into tgt. true is returned if the
// strings in tgt changed as a result of the merge.
func mergeStrings(tgt *[]string, src []string) bool {
if t := util.NewStringSet(*tgt, src); len(t) != len(*tgt) {
*tgt = t.Keys()
return true
}
return false
}
// removeStrings removes all strings from tgt that also appear in src. true is returned
// if tgt changed as part of the removal.
func removeStrings(tgt *[]string, src []string) bool {
if t := util.NewStringSet(*tgt).Complement(util.NewStringSet(src)); len(t) != len(*tgt) {
*tgt = t.Keys()
return true
}
return false
}