mirror of
https://github.com/grafana/loki.git
synced 2026-03-13 09:33:58 +08:00
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>
93 lines
1.8 KiB
Go
93 lines
1.8 KiB
Go
package iter
|
|
|
|
import (
|
|
iter "github.com/grafana/loki/v3/pkg/iter/v2"
|
|
"github.com/grafana/loki/v3/pkg/logproto"
|
|
)
|
|
|
|
type Iterator interface {
|
|
iter.CloseIterator[logproto.PatternSample]
|
|
|
|
Pattern() string
|
|
}
|
|
|
|
func NewSlice(pattern string, s []logproto.PatternSample) *PatternIter {
|
|
return &PatternIter{
|
|
CloseIterator: iter.WithClose(iter.NewSliceIter(s), nil),
|
|
pattern: pattern,
|
|
}
|
|
}
|
|
|
|
func NewEmpty(pattern string) *PatternIter {
|
|
return &PatternIter{
|
|
CloseIterator: iter.WithClose(iter.NewEmptyIter[logproto.PatternSample](), nil),
|
|
pattern: pattern,
|
|
}
|
|
}
|
|
|
|
type PatternIter struct {
|
|
iter.CloseIterator[logproto.PatternSample]
|
|
pattern string
|
|
}
|
|
|
|
func (s *PatternIter) Pattern() string {
|
|
return s.pattern
|
|
}
|
|
|
|
type nonOverlappingIterator struct {
|
|
iterators []Iterator
|
|
curr Iterator
|
|
pattern string
|
|
}
|
|
|
|
// NewNonOverlappingIterator gives a chained iterator over a list of iterators.
|
|
func NewNonOverlappingIterator(pattern string, iterators []Iterator) Iterator {
|
|
return &nonOverlappingIterator{
|
|
iterators: iterators,
|
|
pattern: pattern,
|
|
}
|
|
}
|
|
|
|
func (i *nonOverlappingIterator) Next() bool {
|
|
for i.curr == nil || !i.curr.Next() {
|
|
if len(i.iterators) == 0 {
|
|
if i.curr != nil {
|
|
i.curr.Close()
|
|
}
|
|
return false
|
|
}
|
|
if i.curr != nil {
|
|
i.curr.Close()
|
|
}
|
|
i.curr, i.iterators = i.iterators[0], i.iterators[1:]
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func (i *nonOverlappingIterator) At() logproto.PatternSample {
|
|
return i.curr.At()
|
|
}
|
|
|
|
func (i *nonOverlappingIterator) Pattern() string {
|
|
return i.pattern
|
|
}
|
|
|
|
func (i *nonOverlappingIterator) Err() error {
|
|
if i.curr != nil {
|
|
return i.curr.Err()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (i *nonOverlappingIterator) Close() error {
|
|
if i.curr != nil {
|
|
i.curr.Close()
|
|
}
|
|
for _, iter := range i.iterators {
|
|
iter.Close()
|
|
}
|
|
i.iterators = nil
|
|
return nil
|
|
}
|