forked from influxdata/influxdb-comparisons
/
serializer_influxdb.go
70 lines (56 loc) · 1.54 KB
/
serializer_influxdb.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
package common
import (
"io"
)
type serializerInflux struct {
}
func NewSerializerInflux() *serializerInflux {
return &serializerInflux{}
}
// SerializeInfluxBulk writes Point data to the given writer, conforming to the
// InfluxDB wire protocol.
//
// This function writes output that looks like:
// <measurement>,<tag key>=<tag value> <field name>=<field value> <timestamp>\n
//
// For example:
// foo,tag0=bar baz=-1.0 100\n
//
// TODO(rw): Speed up this function. The bulk of time is spent in strconv.
func (s *serializerInflux) SerializePoint(w io.Writer, p *Point) (err error) {
buf := scratchBufPool.Get().([]byte)
buf = append(buf, p.MeasurementName...)
for i := 0; i < len(p.TagKeys); i++ {
buf = append(buf, ',')
buf = append(buf, p.TagKeys[i]...)
buf = append(buf, '=')
buf = append(buf, p.TagValues[i]...)
}
if len(p.FieldKeys) > 0 {
buf = append(buf, ' ')
}
for i := 0; i < len(p.FieldKeys); i++ {
buf = append(buf, p.FieldKeys[i]...)
buf = append(buf, '=')
v := p.FieldValues[i]
buf = fastFormatAppend(v, buf, false)
// Influx uses 'i' to indicate integers:
switch v.(type) {
case int, int64:
buf = append(buf, 'i')
}
if i+1 < len(p.FieldKeys) {
buf = append(buf, ',')
}
}
buf = append(buf, ' ')
buf = fastFormatAppend(p.Timestamp.UTC().UnixNano(), buf, true)
buf = append(buf, '\n')
_, err = w.Write(buf)
buf = buf[:0]
scratchBufPool.Put(buf)
return err
}
func (s *serializerInflux) SerializeSize(w io.Writer, points int64, values int64) error {
return serializeSizeInText(w, points, values)
}