forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplode.go
81 lines (68 loc) · 2.48 KB
/
explode.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
package tsdb
import (
"encoding/binary"
platform "github.com/influxdata/influxdb"
"github.com/influxdata/influxdb/models"
)
// DecodeName converts tsdb internal serialization back to organization and bucket IDs.
func DecodeName(name [16]byte) (org, bucket platform.ID) {
org = platform.ID(binary.BigEndian.Uint64(name[0:8]))
bucket = platform.ID(binary.BigEndian.Uint64(name[8:16]))
return
}
// EncodeName converts org/bucket pairs to the tsdb internal serialization
func EncodeName(org, bucket platform.ID) [16]byte {
var nameBytes [16]byte
binary.BigEndian.PutUint64(nameBytes[0:8], uint64(org))
binary.BigEndian.PutUint64(nameBytes[8:16], uint64(bucket))
return nameBytes
}
// ExplodePoints creates a list of points that only contains one field per point. It also
// moves the measurement to a tag, and changes the measurement to be the provided argument.
func ExplodePoints(org, bucket platform.ID, points []models.Point) ([]models.Point, error) {
out := make([]models.Point, 0, len(points))
// TODO(jeff): We should add a RawEncode() method or something to the platform.ID type
// or we should use hex encoded measurement names. Either way, we shouldn't be doing a
// decode of the encode here, and we don't want to depend on details of how the ID type
// is represented.
ob := EncodeName(org, bucket)
name := string(ob[:])
tags := make(models.Tags, 1)
for _, pt := range points {
tags = tags[:1] // reset buffer for next point.
tags[0] = models.NewTag(models.MeasurementTagKeyBytes, pt.Name())
pt.ForEachTag(func(k, v []byte) bool {
tags = append(tags, models.NewTag(k, v))
return true
})
t := pt.Time()
itr := pt.FieldIterator()
tags = append(tags, models.Tag{}) // Make room for field key and value.
for itr.Next() {
tags[len(tags)-1] = models.NewTag(models.FieldKeyTagKeyBytes, itr.FieldKey())
var err error
field := make(models.Fields, 1)
switch itr.Type() {
case models.Float:
field[string(itr.FieldKey())], err = itr.FloatValue()
case models.Integer:
field[string(itr.FieldKey())], err = itr.IntegerValue()
case models.Boolean:
field[string(itr.FieldKey())], err = itr.BooleanValue()
case models.String:
field[string(itr.FieldKey())] = itr.StringValue()
case models.Unsigned:
field[string(itr.FieldKey())], err = itr.UnsignedValue()
}
if err != nil {
return nil, err
}
pt, err := models.NewPoint(name, tags, field, t)
if err != nil {
return nil, err
}
out = append(out, pt)
}
}
return out, nil
}