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_matrix.go
147 lines (129 loc) · 2.89 KB
/
iterator_matrix.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package ftdc
import (
"context"
"io"
"time"
"github.com/deciduosity/birch"
"github.com/deciduosity/birch/bsontype"
"github.com/deciduosity/ftdc/util"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
)
func (c *Chunk) exportMatrix() map[string]interface{} {
out := make(map[string]interface{})
for _, m := range c.Metrics {
out[m.Key()] = m.getSeries()
}
return out
}
func (c *Chunk) export() (*birch.Document, error) {
doc := birch.DC.Make(len(c.Metrics))
sample := 0
var elem *birch.Element
var err error
for i := 0; i < len(c.Metrics); i++ {
elem, sample, err = rehydrateMatrix(c.Metrics, sample)
if err == io.EOF {
break
}
if err != nil {
return nil, errors.WithStack(err)
}
doc.Append(elem)
}
return doc, nil
}
func (m *Metric) getSeries() interface{} {
switch m.originalType {
case bsontype.Int64, bsontype.Timestamp:
out := make([]int64, len(m.Values))
copy(out, m.Values)
return out
case bsontype.Int32:
out := make([]int32, len(m.Values))
for idx, p := range m.Values {
out[idx] = int32(p)
}
return out
case bsontype.Boolean:
out := make([]bool, len(m.Values))
for idx, p := range m.Values {
out[idx] = p != 0
}
return out
case bsontype.Double:
out := make([]float64, len(m.Values))
for idx, p := range m.Values {
out[idx] = restoreFloat(p)
}
return out
case bsontype.DateTime:
out := make([]time.Time, len(m.Values))
for idx, p := range m.Values {
out[idx] = timeEpocMs(p)
}
return out
default:
return nil
}
}
type matrixIterator struct {
chunks *ChunkIterator
closer context.CancelFunc
metadata *birch.Document
document *birch.Document
pipe chan *birch.Document
catcher util.Catcher
reflect bool
}
func (iter *matrixIterator) Close() {
if iter.chunks != nil {
iter.chunks.Close()
}
}
func (iter *matrixIterator) Err() error { return iter.catcher.Resolve() }
func (iter *matrixIterator) Metadata() *birch.Document { return iter.metadata }
func (iter *matrixIterator) Document() *birch.Document { return iter.document }
func (iter *matrixIterator) Next() bool {
doc, ok := <-iter.pipe
if !ok {
return false
}
iter.document = doc
return true
}
func (iter *matrixIterator) worker(ctx context.Context) {
defer func() { iter.catcher.Add(iter.chunks.Err()) }()
defer close(iter.pipe)
var payload []byte
var doc *birch.Document
var err error
for iter.chunks.Next() {
chunk := iter.chunks.Chunk()
if iter.reflect {
payload, err = bson.Marshal(chunk.exportMatrix())
if err != nil {
iter.catcher.Add(err)
return
}
doc, err = birch.ReadDocument(payload)
if err != nil {
iter.catcher.Add(err)
return
}
} else {
doc, err = chunk.export()
if err != nil {
iter.catcher.Add(err)
return
}
}
select {
case iter.pipe <- doc:
continue
case <-ctx.Done():
iter.catcher.Add(errors.New("operation aborted"))
return
}
}
}