forked from mongodb/ftdc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator.go
108 lines (97 loc) · 2.95 KB
/
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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package ftdc
import (
"context"
"io"
"github.com/evergreen-ci/birch"
"github.com/mongodb/ftdc/util"
)
type Iterator interface {
Next() bool
Document() *birch.Document
Metadata() *birch.Document
Err() error
Close()
}
// ReadMetrics returns a standard document iterator that reads FTDC
// chunks. The Documents returned by the iterator are flattened.
func ReadMetrics(ctx context.Context, r io.Reader) Iterator {
iterctx, cancel := context.WithCancel(ctx)
iter := &combinedIterator{
closer: cancel,
chunks: ReadChunks(iterctx, r),
flatten: true,
pipe: make(chan *birch.Document, 100),
catcher: util.NewCatcher(),
}
go iter.worker(iterctx)
return iter
}
// ReadStructuredMetrics returns a standard document iterator that reads FTDC
// chunks. The Documents returned by the iterator retain the structure
// of the input documents.
func ReadStructuredMetrics(ctx context.Context, r io.Reader) Iterator {
iterctx, cancel := context.WithCancel(ctx)
iter := &combinedIterator{
closer: cancel,
chunks: ReadChunks(iterctx, r),
flatten: false,
pipe: make(chan *birch.Document, 100),
catcher: util.NewCatcher(),
}
go iter.worker(iterctx)
return iter
}
// ReadMatrix returns a "matrix format" for the data in a chunk. The
// ducments returned by the iterator represent the entire chunk, in
// flattened form, with each field representing a single metric as an
// array of all values for the event.
//
// The matrix documents have full type fidelity, but are not
// substantially less expensive to produce than full iteration.
func ReadMatrix(ctx context.Context, r io.Reader) Iterator {
iterctx, cancel := context.WithCancel(ctx)
iter := &matrixIterator{
closer: cancel,
chunks: ReadChunks(iterctx, r),
pipe: make(chan *birch.Document, 25),
catcher: util.NewCatcher(),
}
go iter.worker(iterctx)
return iter
}
// ReadSeries is similar to the ReadMatrix format, and produces a
// single document per chunk, that contains the flattented keys for
// that chunk, mapped to arrays of all the values of the chunk.
//
// The matrix documents have better type fidelity than raw chunks but
// do not properly collapse the bson timestamp type. To use these
// values produced by the iterator, consider marshaling them directly
// to map[string]interface{} and use a case statement, on the values
// in the map, such as:
//
// switch v.(type) {
// case []int32:
// // ...
// case []int64:
// // ...
// case []bool:
// // ...
// case []time.Time:
// // ...
// case []float64:
// // ...
// }
//
// Although the *birch.Document type does support iteration directly.
func ReadSeries(ctx context.Context, r io.Reader) Iterator {
iterctx, cancel := context.WithCancel(ctx)
iter := &matrixIterator{
closer: cancel,
chunks: ReadChunks(iterctx, r),
pipe: make(chan *birch.Document, 25),
catcher: util.NewCatcher(),
reflect: true,
}
go iter.worker(iterctx)
return iter
}