-
Notifications
You must be signed in to change notification settings - Fork 492
/
iterator_record.go
69 lines (56 loc) · 1.48 KB
/
iterator_record.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package v2
import (
"bytes"
"context"
"errors"
"io"
"github.com/grafana/tempo/tempodb/encoding/common"
)
type recordIterator struct {
records []common.Record
objectRW common.ObjectReaderWriter
dataR common.DataReader
currentIterator common.Iterator
buffer []byte
}
// NewRecordIterator returns a recordIterator. This iterator is used for iterating through
// a series of objects by reading them one at a time from Records.
func NewRecordIterator(r []common.Record, dataR common.DataReader, objectRW common.ObjectReaderWriter) common.Iterator {
return &recordIterator{
records: r,
objectRW: objectRW,
dataR: dataR,
}
}
func (i *recordIterator) Next(ctx context.Context) (common.ID, []byte, error) {
if i.currentIterator != nil {
id, object, err := i.currentIterator.Next(ctx)
if err != nil && err != io.EOF {
return nil, nil, err
}
if id != nil {
return id, object, nil
}
}
// read the next record and create an iterator
if len(i.records) > 0 {
var pages [][]byte
var err error
pages, i.buffer, err = i.dataR.Read(ctx, i.records[:1], pages, i.buffer)
if err != nil {
return nil, nil, err
}
if len(pages) == 0 {
return nil, nil, errors.New("unexpected 0 length pages from dataReader")
}
buff := pages[0]
i.currentIterator = NewIterator(bytes.NewReader(buff), i.objectRW)
i.records = i.records[1:]
return i.currentIterator.Next(ctx)
}
// done
return nil, nil, io.EOF
}
func (i *recordIterator) Close() {
i.dataR.Close()
}