Skip to content

Commit d8a4268

Browse files
committed
rename iterator method
1 parent e72f5a9 commit d8a4268

File tree

3 files changed

+43
-44
lines changed

3 files changed

+43
-44
lines changed

lib/backend/backend.go

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"context"
2424
"fmt"
2525
"io"
26+
"iter"
2627
"sort"
2728
"time"
2829

@@ -113,6 +114,29 @@ type Backend interface {
113114
CloseWatchers()
114115
}
115116

117+
// IterateParams are parameters that are provided to
118+
// [Itemser.Items] to alter the iteration behavior.
119+
type IterateParams struct {
120+
// StartKey is the key at which to begin iteration. This key
121+
// will be included in the results if it exists.
122+
StartKey Key
123+
// EndKey is the key at which to end iteration. This key
124+
// will be included in the results if it exists.
125+
EndKey Key
126+
// Descending indicates which direction the items should be produced,
127+
// by default from [StartKey] to [EndKey].
128+
Descending bool
129+
}
130+
131+
// Itemser is a temporary interface that will be added to [backend.Backend]
132+
// once all concrete backend implementations satisfy the new interface.
133+
// TODO(tross): REMEMBER TO DELETE THIS
134+
type Itemser interface {
135+
// Items produce an iterator of backend items in the range, and order
136+
// described in the provided [IterateParams].
137+
Items(ctx context.Context, params IterateParams) iter.Seq2[Item, error]
138+
}
139+
116140
// New initializes a new [Backend] implementation based on the service config.
117141
func New(ctx context.Context, backend string, params Params) (Backend, error) {
118142
registryMu.RLock()
@@ -248,19 +272,6 @@ type Event struct {
248272
Item Item
249273
}
250274

251-
// IterationOrder dictates the order in which iteration should
252-
// return items.
253-
type IterationOrder int
254-
255-
const (
256-
// IterateAscending indicates to [backend.Iterate] that items should be
257-
// returned in ascending lexicographical order of the queried key range.
258-
IterateAscending IterationOrder = iota
259-
// IterateDescending indicates to [backend.Iterate] that items should be
260-
// returned in descending lexicographical order of the queried key range.
261-
IterateDescending
262-
)
263-
264275
// Item is a key value item
265276
type Item struct {
266277
// Key is a key of the key value item

lib/backend/etcdbk/etcd.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -649,30 +649,32 @@ func (b *EtcdBackend) NewWatcher(ctx context.Context, watch backend.Watch) (back
649649
return b.buf.NewWatcher(ctx, watch)
650650
}
651651

652-
func (b *EtcdBackend) Iterate(ctx context.Context, startKey, endKey backend.Key, order backend.IterationOrder) (iter.Seq2[backend.Item, error], error) {
653-
if startKey.IsZero() {
654-
return nil, trace.BadParameter("missing parameter startKey")
652+
func (b *EtcdBackend) Items(ctx context.Context, params backend.IterateParams) iter.Seq2[backend.Item, error] {
653+
if params.StartKey.IsZero() {
654+
err := trace.BadParameter("missing parameter startKey")
655+
return func(yield func(backend.Item, error) bool) { yield(backend.Item{}, err) }
655656
}
656-
if endKey.IsZero() {
657-
return nil, trace.BadParameter("missing parameter endKey")
657+
if params.EndKey.IsZero() {
658+
err := trace.BadParameter("missing parameter endKey")
659+
return func(yield func(backend.Item, error) bool) { yield(backend.Item{}, err) }
658660
}
659661

660662
sort := clientv3.SortAscend
661-
if order == backend.IterateDescending {
663+
if params.Descending {
662664
sort = clientv3.SortDescend
663665
}
664666

665667
const defaultPageSize = int64(1000)
666668
return func(yield func(backend.Item, error) bool) {
667-
inclusiveStartKey := b.prependPrefix(startKey)
669+
inclusiveStartKey := b.prependPrefix(params.StartKey)
668670
for {
669671
start := b.clock.Now()
670672
re, err := b.clients.Next().Get(ctx, inclusiveStartKey,
671673
// etcd's range query includes the start point and excludes the end point,
672674
// but Backend.GetRange is supposed to be inclusive at both ends, so we
673675
// query until the very next key in lexicographic order (i.e., the same key
674676
// followed by a 0 byte)
675-
clientv3.WithRange(b.prependPrefix(endKey)+"\x00"),
677+
clientv3.WithRange(b.prependPrefix(params.EndKey)+"\x00"),
676678
clientv3.WithSort(clientv3.SortByKey, sort),
677679
clientv3.WithLimit(defaultPageSize),
678680
)
@@ -705,13 +707,13 @@ func (b *EtcdBackend) Iterate(ctx context.Context, startKey, endKey backend.Key,
705707
}
706708
}
707709

708-
if order == backend.IterateDescending {
710+
if params.Descending {
709711
inclusiveStartKey = backend.RangeEnd(backend.KeyFromString(string(re.Kvs[0].Key))).String()
710712
} else {
711713
inclusiveStartKey = backend.RangeEnd(backend.KeyFromString(string(re.Kvs[len(re.Kvs)-1].Key))).String()
712714
}
713715
}
714-
}, nil
716+
}
715717
}
716718

717719
// GetRange returns query range
@@ -723,13 +725,8 @@ func (b *EtcdBackend) GetRange(ctx context.Context, startKey, endKey backend.Key
723725
return nil, trace.BadParameter("missing parameter endKey")
724726
}
725727

726-
iter, err := b.Iterate(ctx, startKey, endKey, backend.IterateAscending)
727-
if err != nil {
728-
return nil, trace.Wrap(err)
729-
}
730-
731728
var result backend.GetResult
732-
for item, err := range iter {
729+
for item, err := range b.Items(ctx, backend.IterateParams{StartKey: startKey, EndKey: endKey}) {
733730
if err != nil {
734731
return nil, trace.Wrap(err)
735732
}

lib/backend/test/suite.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"encoding/hex"
2727
"errors"
2828
"fmt"
29-
"iter"
3029
"os"
3130
"strconv"
3231
"sync"
@@ -144,8 +143,8 @@ func RunBackendComplianceSuite(t *testing.T, newBackend Constructor) {
144143
testQueryRange(t, newBackend)
145144
})
146145

147-
t.Run("Iterate", func(t *testing.T) {
148-
testIterate(t, newBackend)
146+
t.Run("Items", func(t *testing.T) {
147+
testItems(t, newBackend)
149148
})
150149

151150
t.Run("DeleteRange", func(t *testing.T) {
@@ -363,14 +362,12 @@ func testQueryRange(t *testing.T, newBackend Constructor) {
363362
require.Empty(t, result.Items)
364363
}
365364

366-
func testIterate(t *testing.T, newBackend Constructor) {
365+
func testItems(t *testing.T, newBackend Constructor) {
367366
uut, _, err := newBackend()
368367
require.NoError(t, err)
369368
defer func() { require.NoError(t, uut.Close()) }()
370369

371-
it, ok := uut.(interface {
372-
Iterate(ctx context.Context, startKey, endKey backend.Key, order backend.IterationOrder) (iter.Seq2[backend.Item, error], error)
373-
})
370+
it, ok := uut.(backend.Itemser)
374371
if !ok {
375372
t.Skip("Backend does not support iteration")
376373
}
@@ -443,11 +440,8 @@ func testIterate(t *testing.T, newBackend Constructor) {
443440

444441
for _, test := range cases {
445442
t.Run(test.name, func(t *testing.T) {
446-
iterator, err := it.Iterate(ctx, test.startKey, test.endKey, backend.IterateAscending)
447-
require.NoError(t, err)
448-
449443
i := 0
450-
for item, err := range iterator {
444+
for item, err := range it.Items(ctx, backend.IterateParams{StartKey: test.startKey, EndKey: test.endKey}) {
451445
require.NoError(t, err)
452446

453447
if len(test.expected) == 0 {
@@ -520,11 +514,8 @@ func testIterate(t *testing.T, newBackend Constructor) {
520514

521515
for _, test := range cases {
522516
t.Run(test.name, func(t *testing.T) {
523-
iterator, err := it.Iterate(ctx, test.startKey, test.endKey, backend.IterateDescending)
524-
require.NoError(t, err)
525-
526517
i := 0
527-
for item, err := range iterator {
518+
for item, err := range it.Items(ctx, backend.IterateParams{StartKey: test.startKey, EndKey: test.endKey, Descending: true}) {
528519
require.NoError(t, err)
529520

530521
if len(test.expected) == 0 {

0 commit comments

Comments
 (0)