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
/
Copy pathcollector_batch.go
79 lines (64 loc) · 1.66 KB
/
collector_batch.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
package ftdc
import (
"bytes"
"github.com/pkg/errors"
)
type batchCollector struct {
maxSamples int
chunks []*betterCollector
}
// NewBatchCollector constructs a collector implementation that
// builds data chunks with payloads of the specified number of samples.
// This implementation allows you break data into smaller components
// for more efficient read operations.
func NewBatchCollector(maxSamples int) Collector {
return newBatchCollector(maxSamples)
}
func newBatchCollector(size int) *batchCollector {
return &batchCollector{
maxSamples: size,
chunks: []*betterCollector{
{
maxDeltas: size,
},
},
}
}
func (c *batchCollector) Info() CollectorInfo {
out := CollectorInfo{}
for _, c := range c.chunks {
info := c.Info()
out.MetricsCount += info.MetricsCount
out.SampleCount += info.SampleCount
}
return out
}
func (c *batchCollector) Reset() {
c.chunks = []*betterCollector{&betterCollector{maxDeltas: c.maxSamples}}
}
func (c *batchCollector) SetMetadata(in interface{}) error {
return errors.WithStack(c.chunks[0].SetMetadata(in))
}
func (c *batchCollector) Add(in interface{}) error {
doc, err := readDocument(in)
if err != nil {
return errors.WithStack(err)
}
last := c.chunks[len(c.chunks)-1]
if last.Info().SampleCount >= c.maxSamples {
last = &betterCollector{maxDeltas: c.maxSamples}
c.chunks = append(c.chunks, last)
}
return errors.WithStack(last.Add(doc))
}
func (c *batchCollector) Resolve() ([]byte, error) {
buf := &bytes.Buffer{}
for _, chunk := range c.chunks {
out, err := chunk.Resolve()
if err != nil {
return nil, errors.WithStack(err)
}
_, _ = buf.Write(out)
}
return buf.Bytes(), nil
}