This repository has been archived by the owner on Dec 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator_combined.go
81 lines (68 loc) · 1.64 KB
/
iterator_combined.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
70
71
72
73
74
75
76
77
78
79
80
81
package ftdc
import (
"context"
"github.com/deciduosity/birch"
"github.com/deciduosity/ftdc/util"
"github.com/pkg/errors"
)
type combinedIterator struct {
closer context.CancelFunc
chunks *ChunkIterator
sample *sampleIterator
metadata *birch.Document
document *birch.Document
pipe chan *birch.Document
catcher util.Catcher
flatten bool
}
func (iter *combinedIterator) Close() {
iter.closer()
if iter.sample != nil {
iter.sample.Close()
}
if iter.chunks != nil {
iter.chunks.Close()
}
}
func (iter *combinedIterator) Err() error { return iter.catcher.Resolve() }
func (iter *combinedIterator) Metadata() *birch.Document { return iter.metadata }
func (iter *combinedIterator) Document() *birch.Document { return iter.document }
func (iter *combinedIterator) Next() bool {
doc, ok := <-iter.pipe
if !ok {
return false
}
iter.document = doc
return true
}
func (iter *combinedIterator) worker(ctx context.Context) {
defer close(iter.pipe)
var ok bool
for iter.chunks.Next() {
chunk := iter.chunks.Chunk()
if iter.flatten {
iter.sample, ok = chunk.Iterator(ctx).(*sampleIterator)
} else {
iter.sample, ok = chunk.StructuredIterator(ctx).(*sampleIterator)
}
if !ok {
iter.catcher.Add(errors.New("programmer error"))
return
}
if iter.metadata != nil {
iter.metadata = chunk.GetMetadata()
}
for iter.sample.Next() {
select {
case iter.pipe <- iter.sample.Document():
continue
case <-ctx.Done():
iter.catcher.Add(errors.New("operation aborted"))
return
}
}
iter.catcher.Add(iter.sample.Err())
iter.sample.Close()
}
iter.catcher.Add(iter.chunks.Err())
}