forked from influxdata/line-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
130 lines (112 loc) · 2.85 KB
/
writer.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
package protocol
import (
"fmt"
"time"
)
// Write writes out data to a line protocol encoder. Note: it does no sorting. It assumes you have done your own sorting for tagValues
func (e *Encoder) Write(name []byte, ts time.Time, tagKeys, tagVals, fieldKeys [][]byte, fieldVals []interface{}) (int, error) {
e.header = e.header[:0]
if len(name) == 0 || name[len(name)-1] == byte('\\') {
return 0, ErrInvalidName
}
nameEscapeBytes(&e.header, name)
for i := range tagKeys {
// Some keys and values are not encodeable as line protocol, such as
// those with a trailing '\' or empty strings.
if len(tagKeys[i]) == 0 || len(tagVals[i]) == 0 || tagKeys[i][len(tagKeys[i])-1] == byte('\\') {
if e.failOnFieldError {
return 0, fmt.Errorf("invalid field: key \"%s\", val \"%s\"", tagKeys[i], tagVals[i])
}
continue
}
e.header = append(e.header, byte(','))
escapeBytes(&e.header, tagKeys[i])
e.header = append(e.header, byte('='))
escapeBytes(&e.header, tagVals[i])
}
e.header = append(e.header, byte(' '))
e.buildFooter(ts)
i := 0
totalWritten := 0
pairsLen := 0
firstField := true
for i := range fieldKeys {
e.pair = e.pair[:0]
key := fieldKeys[i]
if len(key) == 0 || key[len(key)-1] == byte('\\') {
if e.failOnFieldError {
return 0, &FieldError{"invalid field key"}
}
continue
}
escapeBytes(&e.pair, key)
// Some keys are not encodeable as line protocol, such as those with a
// trailing '\' or empty strings.
e.pair = append(e.pair, byte('='))
err := e.buildFieldVal(fieldVals[i])
if err != nil {
if e.failOnFieldError {
return 0, err
}
continue
}
bytesNeeded := len(e.header) + pairsLen + len(e.pair) + len(e.footer)
// Additional length needed for field separator `,`
if !firstField {
bytesNeeded++
}
if e.maxLineBytes > 0 && bytesNeeded > e.maxLineBytes {
// Need at least one field per line
if firstField {
return 0, ErrNeedMoreSpace
}
i, err = e.w.Write(e.footer)
if err != nil {
return 0, err
}
totalWritten += i
bytesNeeded = len(e.header) + len(e.pair) + len(e.footer)
if e.maxLineBytes > 0 && bytesNeeded > e.maxLineBytes {
return 0, ErrNeedMoreSpace
}
i, err = e.w.Write(e.header)
if err != nil {
return 0, err
}
totalWritten += i
i, err = e.w.Write(e.pair)
if err != nil {
return 0, err
}
totalWritten += i
pairsLen += len(e.pair)
firstField = false
continue
}
if firstField {
i, err = e.w.Write(e.header)
if err != nil {
return 0, err
}
totalWritten += i
} else {
i, err = e.w.Write(comma)
if err != nil {
return 0, err
}
totalWritten += i
}
e.w.Write(e.pair)
pairsLen += len(e.pair)
firstField = false
}
if firstField {
return 0, ErrNoFields
}
i, err := e.w.Write(e.footer)
if err != nil {
return 0, err
}
totalWritten += i
return totalWritten, nil
}