-
Notifications
You must be signed in to change notification settings - Fork 9
/
record_iterator.go
57 lines (45 loc) · 1.07 KB
/
record_iterator.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
package rangedb
import (
"context"
)
type recordIterator struct {
resultRecords <-chan ResultRecord
current ResultRecord
}
// NewRecordIterator constructs a new rangedb.Record iterator
func NewRecordIterator(recordResult <-chan ResultRecord) *recordIterator {
return &recordIterator{resultRecords: recordResult}
}
func (i *recordIterator) Next() bool {
if i.current.Err != nil {
return false
}
i.current = <-i.resultRecords
return i.current.Record != nil
}
func (i *recordIterator) NextContext(ctx context.Context) bool {
if i.current.Err != nil {
return false
}
select {
case i.current = <-i.resultRecords:
case <-ctx.Done():
i.current = ResultRecord{
Record: nil,
Err: ctx.Err(),
}
}
return i.current.Record != nil
}
func (i *recordIterator) Record() *Record {
return i.current.Record
}
func (i *recordIterator) Err() error {
return i.current.Err
}
func NewRecordIteratorWithError(err error) *recordIterator {
records := make(chan ResultRecord, 1)
records <- ResultRecord{Err: err}
close(records)
return NewRecordIterator(records)
}