-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
stream_tags.go
298 lines (252 loc) · 6.39 KB
/
stream_tags.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package logstorage
import (
"bytes"
"fmt"
"sort"
"strconv"
"sync"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/encoding"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
)
// GetStreamTags returns a StreamTags from pool.
func GetStreamTags() *StreamTags {
v := streamTagsPool.Get()
if v == nil {
return &StreamTags{}
}
return v.(*StreamTags)
}
// PutStreamTags returns st to the pool.
func PutStreamTags(st *StreamTags) {
st.Reset()
streamTagsPool.Put(st)
}
var streamTagsPool sync.Pool
// StreamTags contains stream tags.
type StreamTags struct {
// buf holds all the data backed by tags
buf []byte
// tags contains added tags.
tags []streamTag
}
// Reset resets st for re-use
func (st *StreamTags) Reset() {
st.buf = st.buf[:0]
tags := st.tags
for i := range tags {
t := &tags[i]
t.Name = nil
t.Value = nil
}
st.tags = tags[:0]
}
// String returns string representation of st.
func (st *StreamTags) String() string {
b := st.marshalString(nil)
return string(b)
}
func (st *StreamTags) marshalString(dst []byte) []byte {
dst = append(dst, '{')
tags := st.tags
if len(tags) > 0 {
dst = tags[0].marshalString(dst)
tags = tags[1:]
for i := range tags {
dst = append(dst, ',')
dst = tags[i].marshalString(dst)
}
}
dst = append(dst, '}')
return dst
}
// Add adds (name:value) tag to st.
func (st *StreamTags) Add(name, value string) {
if len(name) == 0 || len(value) == 0 {
return
}
buf := st.buf
bufLen := len(buf)
buf = append(buf, name...)
bName := buf[bufLen:]
bufLen = len(buf)
buf = append(buf, value...)
bValue := buf[bufLen:]
st.buf = buf
st.tags = append(st.tags, streamTag{
Name: bName,
Value: bValue,
})
}
// MarshalCanonical marshal st in a canonical way
func (st *StreamTags) MarshalCanonical(dst []byte) []byte {
sort.Sort(st)
tags := st.tags
dst = encoding.MarshalVarUint64(dst, uint64(len(tags)))
for i := range tags {
tag := &tags[i]
dst = encoding.MarshalBytes(dst, tag.Name)
dst = encoding.MarshalBytes(dst, tag.Value)
}
return dst
}
// UnmarshalCanonical unmarshals st from src marshaled with MarshalCanonical.
func (st *StreamTags) UnmarshalCanonical(src []byte) ([]byte, error) {
st.Reset()
srcOrig := src
n, nSize := encoding.UnmarshalVarUint64(src)
if nSize <= 0 {
return srcOrig, fmt.Errorf("cannot unmarshal tags len")
}
src = src[nSize:]
for i := uint64(0); i < n; i++ {
name, nSize := encoding.UnmarshalBytes(src)
if nSize <= 0 {
return srcOrig, fmt.Errorf("cannot unmarshal tag name")
}
src = src[nSize:]
value, nSize := encoding.UnmarshalBytes(src)
if nSize <= 0 {
return srcOrig, fmt.Errorf("cannot unmarshal tag value")
}
src = src[nSize:]
sName := bytesutil.ToUnsafeString(name)
sValue := bytesutil.ToUnsafeString(value)
st.Add(sName, sValue)
}
return src, nil
}
func getStreamTagsString(streamTagsCanonical []byte) string {
st := GetStreamTags()
mustUnmarshalStreamTags(st, streamTagsCanonical)
s := st.String()
PutStreamTags(st)
return s
}
func mustUnmarshalStreamTags(dst *StreamTags, src []byte) {
tail, err := dst.UnmarshalCanonical(src)
if err != nil {
logger.Panicf("FATAL: cannot unmarshal StreamTags from value obtained from cache: %s", err)
}
if len(tail) > 0 {
logger.Panicf("FATAL: unexpected tail left after unmarshaling StreamTags; len(tail)=%d; tail=%q", len(tail), tail)
}
}
// Len returns the number of tags in st.
func (st *StreamTags) Len() int {
return len(st.tags)
}
// Less returns true if tag i is smaller than the tag j.
func (st *StreamTags) Less(i, j int) bool {
tags := st.tags
return tags[i].less(&tags[j])
}
// Swap swaps i and j tags
func (st *StreamTags) Swap(i, j int) {
tags := st.tags
tags[i], tags[j] = tags[j], tags[i]
}
// streamTag represents a (name:value) tag for stream.
type streamTag struct {
Name []byte
Value []byte
}
func (tag *streamTag) marshalString(dst []byte) []byte {
dst = append(dst, tag.Name...)
dst = append(dst, '=')
dst = strconv.AppendQuote(dst, bytesutil.ToUnsafeString(tag.Value))
return dst
}
// reset resets the tag.
func (tag *streamTag) reset() {
tag.Name = tag.Name[:0]
tag.Value = tag.Value[:0]
}
func (tag *streamTag) equal(t *streamTag) bool {
return string(tag.Name) == string(t.Name) && string(tag.Value) == string(t.Value)
}
func (tag *streamTag) less(t *streamTag) bool {
if string(tag.Name) != string(t.Name) {
return string(tag.Name) < string(t.Name)
}
return string(tag.Value) < string(t.Value)
}
func (tag *streamTag) indexdbMarshal(dst []byte) []byte {
dst = marshalTagValue(dst, tag.Name)
dst = marshalTagValue(dst, tag.Value)
return dst
}
func (tag *streamTag) indexdbUnmarshal(src []byte) ([]byte, error) {
var err error
src, tag.Name, err = unmarshalTagValue(tag.Name[:0], src)
if err != nil {
return src, fmt.Errorf("cannot unmarshal key: %w", err)
}
src, tag.Value, err = unmarshalTagValue(tag.Value[:0], src)
if err != nil {
return src, fmt.Errorf("cannot unmarshal value: %w", err)
}
return src, nil
}
const (
escapeChar = 0
tagSeparatorChar = 1
kvSeparatorChar = 2
)
func marshalTagValue(dst, src []byte) []byte {
n1 := bytes.IndexByte(src, escapeChar)
n2 := bytes.IndexByte(src, tagSeparatorChar)
n3 := bytes.IndexByte(src, kvSeparatorChar)
if n1 < 0 && n2 < 0 && n3 < 0 {
// Fast path.
dst = append(dst, src...)
dst = append(dst, tagSeparatorChar)
return dst
}
// Slow path.
for _, ch := range src {
switch ch {
case escapeChar:
dst = append(dst, escapeChar, '0')
case tagSeparatorChar:
dst = append(dst, escapeChar, '1')
case kvSeparatorChar:
dst = append(dst, escapeChar, '2')
default:
dst = append(dst, ch)
}
}
dst = append(dst, tagSeparatorChar)
return dst
}
func unmarshalTagValue(dst, src []byte) ([]byte, []byte, error) {
n := bytes.IndexByte(src, tagSeparatorChar)
if n < 0 {
return src, dst, fmt.Errorf("cannot find the end of tag value")
}
b := src[:n]
src = src[n+1:]
for {
n := bytes.IndexByte(b, escapeChar)
if n < 0 {
dst = append(dst, b...)
return src, dst, nil
}
dst = append(dst, b[:n]...)
b = b[n+1:]
if len(b) == 0 {
return src, dst, fmt.Errorf("missing escaped char")
}
switch b[0] {
case '0':
dst = append(dst, escapeChar)
case '1':
dst = append(dst, tagSeparatorChar)
case '2':
dst = append(dst, kvSeparatorChar)
default:
return src, dst, fmt.Errorf("unsupported escaped char: %c", b[0])
}
b = b[1:]
}
}