@@ -17,6 +17,7 @@ import (
17
17
"github.com/prometheus/client_golang/prometheus/promauto"
18
18
"github.com/quay/zlog"
19
19
20
+ "github.com/quay/claircore/datastore"
20
21
"github.com/quay/claircore/libvuln/driver"
21
22
"github.com/quay/claircore/pkg/microbatch"
22
23
)
@@ -60,10 +61,27 @@ var (
60
61
)
61
62
)
62
63
64
+ func (s * MatcherStore ) UpdateEnrichmentsIter (ctx context.Context , updater string , fp driver.Fingerprint , it datastore.EnrichmentIter ) (uuid.UUID , error ) {
65
+ ctx = zlog .ContextWithValues (ctx , "component" , "datastore/postgres/MatcherStore.UpdateEnrichmentsIter" )
66
+ return s .updateEnrichments (ctx , updater , fp , it )
67
+ }
68
+
63
69
// UpdateEnrichments creates a new UpdateOperation, inserts the provided
64
70
// EnrichmentRecord(s), and ensures enrichments from previous updates are not
65
71
// queried by clients.
66
- func (s * MatcherStore ) UpdateEnrichments (ctx context.Context , name string , fp driver.Fingerprint , es []driver.EnrichmentRecord ) (uuid.UUID , error ) {
72
+ func (s * MatcherStore ) UpdateEnrichments (ctx context.Context , updater string , fp driver.Fingerprint , es []driver.EnrichmentRecord ) (uuid.UUID , error ) {
73
+ ctx = zlog .ContextWithValues (ctx , "component" , "datastore/postgres/MatcherStore.UpdateEnrichments" )
74
+ enIter := func (yield func (record * driver.EnrichmentRecord , err error ) bool ) {
75
+ for i := range es {
76
+ if ! yield (& es [i ], nil ) {
77
+ break
78
+ }
79
+ }
80
+ }
81
+ return s .updateEnrichments (ctx , updater , fp , enIter )
82
+ }
83
+
84
+ func (s * MatcherStore ) updateEnrichments (ctx context.Context , name string , fp driver.Fingerprint , it datastore.EnrichmentIter ) (uuid.UUID , error ) {
67
85
const (
68
86
create = `
69
87
INSERT
134
152
135
153
batch := microbatch .NewInsert (tx , 2000 , time .Minute )
136
154
start = time .Now ()
137
- for i := range es {
138
- hashKind , hash := hashEnrichment (& es [i ])
139
- err := batch .Queue (ctx , insert ,
140
- hashKind , hash , name , es [i ].Tags , es [i ].Enrichment ,
155
+ enCt := 0
156
+ it (func (en * driver.EnrichmentRecord , iterErr error ) bool {
157
+ if iterErr != nil {
158
+ err = iterErr
159
+ return false
160
+ }
161
+ enCt ++
162
+ hashKind , hash := hashEnrichment (en )
163
+ err = batch .Queue (ctx , insert ,
164
+ hashKind , hash , name , en .Tags , en .Enrichment ,
141
165
)
142
166
if err != nil {
143
- return uuid .Nil , fmt .Errorf ("failed to queue enrichment: %w" , err )
167
+ err = fmt .Errorf ("failed to queue enrichment: %w" , err )
168
+ return false
144
169
}
145
170
if err := batch .Queue (ctx , assoc , hashKind , hash , name , id ); err != nil {
146
- return uuid .Nil , fmt .Errorf ("failed to queue association: %w" , err )
171
+ err = fmt .Errorf ("failed to queue association: %w" , err )
172
+ return false
147
173
}
174
+ return true
175
+ })
176
+ if err != nil {
177
+ return uuid .Nil , fmt .Errorf ("iterating on enrichments: %w" , err )
148
178
}
149
179
if err := batch .Done (ctx ); err != nil {
150
180
return uuid .Nil , fmt .Errorf ("failed to finish batch enrichment insert: %w" , err )
160
190
}
161
191
zlog .Debug (ctx ).
162
192
Stringer ("ref" , ref ).
163
- Int ("inserted" , len ( es ) ).
193
+ Int ("inserted" , enCt ).
164
194
Msg ("update_operation committed" )
165
195
return ref , nil
166
196
}
0 commit comments