@@ -28,7 +28,6 @@ import (
28
28
"bytes"
29
29
"context"
30
30
"encoding/json"
31
- "fmt"
32
31
33
32
"github.com/influxdata/influxdb/v2"
34
33
"github.com/influxdata/influxdb/v2/kv"
@@ -160,38 +159,39 @@ func (s *Service) unsetDefault(tx kv.Tx, compKey []byte) error {
160
159
// getFirstBut returns the first element in the db/rp index (not accounting for the `skipID`).
161
160
// If the length of the returned ID is 0, it means no element was found.
162
161
// The skip value is useful, for instance, if one wants to delete an element based on the result of this operation.
163
- func (s * Service ) getFirstBut (tx kv.Tx , compKey []byte , skipID []byte ) ([]byte , error ) {
164
- stop := fmt .Errorf ("stop" )
165
- var next []byte
166
- if err := s .byOrgAndDatabase .Walk (context .Background (), tx , compKey , func (k , v []byte ) error {
162
+ func (s * Service ) getFirstBut (tx kv.Tx , compKey []byte , skipID []byte ) (next []byte , err error ) {
163
+ err = s .byOrgAndDatabase .Walk (context .Background (), tx , compKey , func (k , v []byte ) (bool , error ) {
167
164
if bytes .Equal (skipID , k ) {
168
- return nil
165
+ return true , nil
169
166
}
167
+
170
168
next = k
171
- return stop
172
- }); err != nil && err != stop {
173
- return nil , ErrInternalService (err )
174
- }
175
- return next , nil
169
+
170
+ return false , nil
171
+ })
172
+ return
176
173
}
177
174
178
175
// isDBRPUnique verifies if the triple orgID-database-retention-policy is unique.
179
176
func (s * Service ) isDBRPUnique (ctx context.Context , m influxdb.DBRPMappingV2 ) error {
180
177
return s .store .View (ctx , func (tx kv.Tx ) error {
181
- return s .byOrgAndDatabase .Walk (ctx , tx , composeForeignKey (m .OrganizationID , m .Database ), func (k , v []byte ) error {
178
+ return s .byOrgAndDatabase .Walk (ctx , tx , composeForeignKey (m .OrganizationID , m .Database ), func (k , v []byte ) ( bool , error ) {
182
179
dbrp := & influxdb.DBRPMappingV2 {}
183
180
if err := json .Unmarshal (v , dbrp ); err != nil {
184
- return ErrInternalService (err )
181
+ return false , ErrInternalService (err )
185
182
}
183
+
186
184
if dbrp .ID == m .ID {
187
185
// Corner case.
188
186
// This is the very same DBRP, just skip it!
189
- return nil
187
+ return true , nil
190
188
}
189
+
191
190
if dbrp .RetentionPolicy == m .RetentionPolicy {
192
- return ErrDBRPAlreadyExists ("another DBRP mapping with same orgID, db, and rp exists" )
191
+ return false , ErrDBRPAlreadyExists ("another DBRP mapping with same orgID, db, and rp exists" )
193
192
}
194
- return nil
193
+
194
+ return true , nil
195
195
})
196
196
})
197
197
}
@@ -254,22 +254,23 @@ func (s *Service) FindMany(ctx context.Context, filter influxdb.DBRPMappingFilte
254
254
}
255
255
256
256
ms := []* influxdb.DBRPMappingV2 {}
257
- add := func (tx kv.Tx ) func (k , v []byte ) error {
258
- return func (k , v []byte ) error {
257
+ add := func (tx kv.Tx ) func (k , v []byte ) ( bool , error ) {
258
+ return func (k , v []byte ) ( bool , error ) {
259
259
m := influxdb.DBRPMappingV2 {}
260
260
if err := json .Unmarshal (v , & m ); err != nil {
261
- return ErrInternalService (err )
261
+ return false , ErrInternalService (err )
262
262
}
263
263
// Updating the Default field must be done before filtering.
264
264
defID , err := get (tx , m .OrganizationID , m .Database )
265
265
if err != nil {
266
- return ErrInternalService (err )
266
+ return false , ErrInternalService (err )
267
267
}
268
+
268
269
m .Default = m .ID == * defID
269
270
if filterFunc (& m , filter ) {
270
271
ms = append (ms , & m )
271
272
}
272
- return nil
273
+ return true , nil
273
274
}
274
275
}
275
276
@@ -303,7 +304,8 @@ func (s *Service) FindMany(ctx context.Context, filter influxdb.DBRPMappingFilte
303
304
if err != nil {
304
305
return ErrInternalService (err )
305
306
}
306
- return add (tx )(defID , v )
307
+ _ , err = add (tx )(defID , v )
308
+ return err
307
309
}
308
310
}
309
311
return s .byOrgAndDatabase .Walk (ctx , tx , compKey , add (tx ))
@@ -318,7 +320,7 @@ func (s *Service) FindMany(ctx context.Context, filter influxdb.DBRPMappingFilte
318
320
}
319
321
320
322
for k , v := cur .First (); k != nil ; k , v = cur .Next () {
321
- if err := add (tx )(k , v ); err != nil {
323
+ if _ , err := add (tx )(k , v ); err != nil {
322
324
return err
323
325
}
324
326
}
0 commit comments