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_sample.go
88 lines (70 loc) · 1.89 KB
/
iterator_sample.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
82
83
84
85
86
87
88
package ftdc
import (
"context"
"github.com/deciduosity/birch"
)
// sampleIterator provides an iterator for iterating through the
// results of a FTDC data chunk as BSON documents.
type sampleIterator struct {
closer context.CancelFunc
stream <-chan *birch.Document
sample *birch.Document
metadata *birch.Document
}
func (c *Chunk) streamFlattenedDocuments(ctx context.Context) <-chan *birch.Document {
out := make(chan *birch.Document, 100)
go func() {
defer close(out)
for i := 0; i < c.nPoints; i++ {
doc := birch.DC.Make(len(c.Metrics))
for _, m := range c.Metrics {
elem, ok := restoreFlat(m.originalType, m.Key(), m.Values[i])
if !ok {
continue
}
doc.Append(elem)
}
select {
case out <- doc:
continue
case <-ctx.Done():
return
}
}
}()
return out
}
func (c *Chunk) streamDocuments(ctx context.Context) <-chan *birch.Document {
out := make(chan *birch.Document, 100)
go func() {
defer close(out)
for i := 0; i < c.nPoints; i++ {
doc, _ := restoreDocument(c.reference, i, c.Metrics, 0)
select {
case <-ctx.Done():
return
case out <- doc:
continue
}
}
}()
return out
}
// Close releases all resources associated with the iterator.
func (iter *sampleIterator) Close() { iter.closer() }
func (iter *sampleIterator) Err() error { return nil }
func (iter *sampleIterator) Metadata() *birch.Document { return iter.metadata }
// Document returns the current document in the iterator. It is safe
// to call this method more than once, and the result will only be nil
// before the iterator is advanced.
func (iter *sampleIterator) Document() *birch.Document { return iter.sample }
// Next advances the iterator one document. Returns true when there is
// a document, and false otherwise.
func (iter *sampleIterator) Next() bool {
doc, ok := <-iter.stream
if !ok {
return false
}
iter.sample = doc
return true
}