forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta.go
107 lines (91 loc) · 2.19 KB
/
meta.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
package tsdb
//go:generate protoc --gogo_out=. internal/meta.proto
import (
"sort"
"github.com/influxdata/influxdb/models"
"github.com/influxdata/influxdb/pkg/escape"
)
// MarshalTags converts a tag set to bytes for use as a lookup key.
func MarshalTags(tags map[string]string) []byte {
// Empty maps marshal to empty bytes.
if len(tags) == 0 {
return nil
}
// Extract keys and determine final size.
sz := (len(tags) * 2) - 1 // separators
keys := make([]string, 0, len(tags))
for k, v := range tags {
keys = append(keys, k)
sz += len(k) + len(v)
}
sort.Strings(keys)
// Generate marshaled bytes.
b := make([]byte, sz)
buf := b
for _, k := range keys {
copy(buf, k)
buf[len(k)] = '|'
buf = buf[len(k)+1:]
}
for i, k := range keys {
v := tags[k]
copy(buf, v)
if i < len(keys)-1 {
buf[len(v)] = '|'
buf = buf[len(v)+1:]
}
}
return b
}
// MakeTagsKey converts a tag set to bytes for use as a lookup key.
func MakeTagsKey(keys []string, tags models.Tags) []byte {
// precondition: keys is sorted
// precondition: models.Tags is sorted
// Empty maps marshal to empty bytes.
if len(keys) == 0 || len(tags) == 0 {
return nil
}
sel := make([]int, 0, len(keys))
sz := 0
i, j := 0, 0
for i < len(keys) && j < len(tags) {
if keys[i] < string(tags[j].Key) {
i++
} else if keys[i] > string(tags[j].Key) {
j++
} else {
sel = append(sel, j)
sz += len(keys[i]) + len(tags[j].Value)
i++
j++
}
}
if len(sel) == 0 {
// no tags matched the requested keys
return nil
}
sz += (len(sel) * 2) - 1 // selected tags, add separators
// Generate marshaled bytes.
b := make([]byte, sz)
buf := b
for _, k := range sel {
copy(buf, tags[k].Key)
buf[len(tags[k].Key)] = '|'
buf = buf[len(tags[k].Key)+1:]
}
for i, k := range sel {
copy(buf, tags[k].Value)
if i < len(sel)-1 {
buf[len(tags[k].Value)] = '|'
buf = buf[len(tags[k].Value)+1:]
}
}
return b
}
// MeasurementFromSeriesKey returns the name of the measurement from a key that
// contains a measurement name.
func MeasurementFromSeriesKey(key []byte) []byte {
// Ignoring the error because the func returns "missing fields"
k, _ := models.ParseName(key)
return escape.Unescape(k)
}