Skip to content

Commit cb95af5

Browse files
committed
feat(IteratorOptions): Allow IteratorOptions/DefaultIteratorOptions to use builder methods just like Options/DefaultOptions
1 parent 773a835 commit cb95af5

3 files changed

Lines changed: 63 additions & 6 deletions

File tree

docs/quickstart.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,7 @@ method. Iteration happens in byte-wise lexicographical sorting order.
384384

385385
```go
386386
err := db.View(func(txn *badger.Txn) error {
387-
opts := badger.DefaultIteratorOptions
388-
opts.PrefetchSize = 10
389-
it := txn.NewIterator(opts)
387+
it := txn.NewIterator(badger.DefaultIteratorOptions.WithPrefetchSize(10))
390388
defer it.Close()
391389
for it.Rewind(); it.Valid(); it.Next() {
392390
item := it.Item()
@@ -526,9 +524,7 @@ reads for selected keys during an iteration, by calling `item.Value()` only when
526524

527525
```go
528526
err := db.View(func(txn *badger.Txn) error {
529-
opts := badger.DefaultIteratorOptions
530-
opts.PrefetchValues = false
531-
it := txn.NewIterator(opts)
527+
it := txn.NewIterator(badger.DefaultIteratorOptions.WithPrefetchValues(false))
532528
defer it.Close()
533529
for it.Rewind(); it.Valid(); it.Next() {
534530
item := it.Item()

iterator.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,48 @@ type IteratorOptions struct {
320320
SinceTs uint64 // Only read data that has version > SinceTs.
321321
}
322322

323+
// WithPrefetchSize returns a new IteratorOptions value with PrefetchSize set to the given value.
324+
func (opt IteratorOptions) WithPrefetchSize(size int) IteratorOptions {
325+
opt.PrefetchSize = size
326+
return opt
327+
}
328+
329+
// WithPrefetchValues returns a new IteratorOptions value with PrefetchValues set to the given value.
330+
func (opt IteratorOptions) WithPrefetchValues(prefetch bool) IteratorOptions {
331+
opt.PrefetchValues = prefetch
332+
return opt
333+
}
334+
335+
// WithReverse returns a new IteratorOptions value with Reverse set to the given value.
336+
func (opt IteratorOptions) WithReverse(reverse bool) IteratorOptions {
337+
opt.Reverse = reverse
338+
return opt
339+
}
340+
341+
// WithAllVersions returns a new IteratorOptions value with AllVersions set to the given value.
342+
func (opt IteratorOptions) WithAllVersions(all bool) IteratorOptions {
343+
opt.AllVersions = all
344+
return opt
345+
}
346+
347+
// WithInternalAccess returns a new IteratorOptions value with InternalAccess set to the given value.
348+
func (opt IteratorOptions) WithInternalAccess(access bool) IteratorOptions {
349+
opt.InternalAccess = access
350+
return opt
351+
}
352+
353+
// WithPrefix returns a new IteratorOptions value with Prefix set to the given prefix.
354+
func (opt IteratorOptions) WithPrefix(prefix []byte) IteratorOptions {
355+
opt.Prefix = prefix
356+
return opt
357+
}
358+
359+
// WithSinceTs returns a new IteratorOptions value with SinceTs set to the given value.
360+
func (opt IteratorOptions) WithSinceTs(ts uint64) IteratorOptions {
361+
opt.SinceTs = ts
362+
return opt
363+
}
364+
323365
func (opt *IteratorOptions) compareToPrefix(key []byte) int {
324366
// We should compare key without timestamp. For example key - a[TS] might be > "aa" prefix.
325367
key = y.ParseKey(key)

iterator_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,25 @@ func TestPickSortTables(t *testing.T) {
114114
require.Equal(t, y.ParseKey(filtered[0].Biggest()), []byte("abc"))
115115
}
116116

117+
func TestIteratorOptionsBuilderMethods(t *testing.T) {
118+
opt := DefaultIteratorOptions.
119+
WithPrefetchSize(0).
120+
WithPrefetchValues(false).
121+
WithReverse(true).
122+
WithAllVersions(true).
123+
WithInternalAccess(true).
124+
WithPrefix([]byte("prefix")).
125+
WithSinceTs(123)
126+
127+
require.Equal(t, 0, opt.PrefetchSize)
128+
require.False(t, opt.PrefetchValues)
129+
require.True(t, opt.Reverse)
130+
require.True(t, opt.AllVersions)
131+
require.True(t, opt.InternalAccess)
132+
require.Equal(t, []byte("prefix"), opt.Prefix)
133+
require.Equal(t, uint64(123), opt.SinceTs)
134+
}
135+
117136
func TestIterateSinceTs(t *testing.T) {
118137
bkey := func(i int) []byte {
119138
return []byte(fmt.Sprintf("%04d", i))

0 commit comments

Comments
 (0)