Skip to content

Commit

Permalink
chore: Convert iterators in pattern module to v2.Iterator interface (
Browse files Browse the repository at this point in the history
…#13407)

This is first follow up to #13273 

It changes the iterator from the pattern module to use the `v2.CloseIterator` to avoid re-implementing the iterator logic. With this change, the function `Error()` on the pattern iterator is renamed to `Err()` to conform with the `v2.Iterator` interface.

Signed-off-by: Christian Haudum <christian.haudum@gmail.com>
  • Loading branch information
chaudum authored Jul 8, 2024
1 parent f27100f commit 5ebd24d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 75 deletions.
8 changes: 4 additions & 4 deletions pkg/iter/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"github.com/grafana/loki/v3/pkg/logproto"
)

type Value interface {
type logprotoType interface {
logproto.Entry | logproto.Sample
}

type StreamIterator[T Value] interface {
type StreamIterator[T logprotoType] interface {
v2.CloseIterator[T]
// Labels returns the labels for the current entry.
// The labels can be mutated by the query engine and not reflect the original stream.
Expand All @@ -24,7 +24,7 @@ type EntryIterator StreamIterator[logproto.Entry]
type SampleIterator StreamIterator[logproto.Sample]

// noOpIterator implements StreamIterator
type noOpIterator[T Value] struct{}
type noOpIterator[T logprotoType] struct{}

func (noOpIterator[T]) Next() bool { return false }
func (noOpIterator[T]) Err() error { return nil }
Expand All @@ -37,7 +37,7 @@ var NoopEntryIterator = noOpIterator[logproto.Entry]{}
var NoopSampleIterator = noOpIterator[logproto.Sample]{}

// errorIterator implements StreamIterator
type errorIterator[T Value] struct{}
type errorIterator[T logprotoType] struct{}

func (errorIterator[T]) Next() bool { return false }
func (errorIterator[T]) Err() error { return errors.New("error") }
Expand Down
27 changes: 23 additions & 4 deletions pkg/iter/v2/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,15 @@ func NewCancelableIter[T any](ctx context.Context, itr Iterator[T]) *Cancellable
return &CancellableIter[T]{ctx: ctx, Iterator: itr}
}

func NewCloseableIterator[T io.Closer](itr Iterator[T]) *CloseIter[T] {
return &CloseIter[T]{itr}
func NewCloserIter[T io.Closer](itr Iterator[T]) *CloserIter[T] {
return &CloserIter[T]{itr}
}

type CloseIter[T io.Closer] struct {
type CloserIter[T io.Closer] struct {
Iterator[T]
}

func (i *CloseIter[T]) Close() error {
func (i *CloserIter[T]) Close() error {
return i.At().Close()
}

Expand Down Expand Up @@ -226,3 +226,22 @@ func (it *CounterIter[T]) Next() bool {
func (it *CounterIter[T]) Count() int {
return it.count
}

func WithClose[T any](itr Iterator[T], close func() bool) *CloseIter[T] {
return &CloseIter[T]{
Iterator: itr,
close: close,
}
}

type CloseIter[T any] struct {
Iterator[T]
close func() bool
}

func (i *CloseIter[T]) Close() error {
if i.close != nil {
return i.Close()
}
return nil
}
2 changes: 1 addition & 1 deletion pkg/pattern/iter/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func ReadBatch(it Iterator, batchSize int) (*logproto.QueryPatternsResponse, err
Samples: samples,
})
}
return &result, it.Error()
return &result, it.Err()
}

func ReadAll(it Iterator) (*logproto.QueryPatternsResponse, error) {
Expand Down
81 changes: 19 additions & 62 deletions pkg/pattern/iter/iterator.go
Original file line number Diff line number Diff line change
@@ -1,80 +1,37 @@
package iter

import (
iter "github.com/grafana/loki/v3/pkg/iter/v2"
"github.com/grafana/loki/v3/pkg/logproto"
)

var Empty Iterator = &emptyIterator{}

// TODO(chaudum): inline v2.Iteratpr[logproto.PatternSample]
type Iterator interface {
Next() bool
iter.CloseIterator[logproto.PatternSample]

Pattern() string
At() logproto.PatternSample

Error() error
Close() error
}

func NewSlice(pattern string, s []logproto.PatternSample) Iterator {
// TODO(chaudum): replace with v2.NewSliceIter()
return &sliceIterator{
values: s,
pattern: pattern,
i: -1,
func NewSlice(pattern string, s []logproto.PatternSample) *PatternIter {
return &PatternIter{
CloseIterator: iter.WithClose(iter.NewSliceIter(s), nil),
pattern: pattern,
}
}

type sliceIterator struct {
i int
pattern string
values []logproto.PatternSample
}

func (s *sliceIterator) Next() bool {
s.i++
return s.i < len(s.values)
}

func (s *sliceIterator) Pattern() string {
return s.pattern
}

func (s *sliceIterator) At() logproto.PatternSample {
return s.values[s.i]
}

func (s *sliceIterator) Error() error {
return nil
}

func (s *sliceIterator) Close() error {
return nil
func NewEmpty(pattern string) *PatternIter {
return &PatternIter{
CloseIterator: iter.WithClose(iter.NewEmptyIter[logproto.PatternSample](), nil),
pattern: pattern,
}
}

type emptyIterator struct {
type PatternIter struct {
iter.CloseIterator[logproto.PatternSample]
pattern string
}

func (e *emptyIterator) Next() bool {
return false
}

func (e *emptyIterator) Pattern() string {
return e.pattern
}

func (e *emptyIterator) At() logproto.PatternSample {
return logproto.PatternSample{}
}

func (e *emptyIterator) Error() error {
return nil
}

func (e *emptyIterator) Close() error {
return nil
func (s *PatternIter) Pattern() string {
return s.pattern
}

type nonOverlappingIterator struct {
Expand Down Expand Up @@ -116,11 +73,11 @@ func (i *nonOverlappingIterator) Pattern() string {
return i.pattern
}

func (i *nonOverlappingIterator) Error() error {
if i.curr == nil {
return nil
func (i *nonOverlappingIterator) Err() error {
if i.curr != nil {
return i.curr.Err()
}
return i.curr.Error()
return nil
}

func (i *nonOverlappingIterator) Close() error {
Expand Down
4 changes: 2 additions & 2 deletions pkg/pattern/iter/iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ func slice(it Iterator) []patternSample {
sample: it.At(),
})
}
if it.Error() != nil {
panic(it.Error())
if it.Err() != nil {
panic(it.Err())
}
return samples
}
2 changes: 1 addition & 1 deletion pkg/pattern/iter/merge_pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (m *mergeIterator) At() logproto.PatternSample {
return m.current.sample
}

func (m *mergeIterator) Error() error {
func (m *mergeIterator) Err() error {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/pattern/iter/query_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (i *queryClientIterator) At() logproto.PatternSample {
return i.curr.At()
}

func (i *queryClientIterator) Error() error {
func (i *queryClientIterator) Err() error {
return i.err
}

Expand Down

0 comments on commit 5ebd24d

Please sign in to comment.