This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 105
/
schema.go
110 lines (103 loc) · 2.91 KB
/
schema.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
package bigtable
import (
"bytes"
"encoding/binary"
"fmt"
"strconv"
"strings"
"cloud.google.com/go/bigtable"
"github.com/grafana/metrictank/idx"
"github.com/grafana/metrictank/schema"
)
// FormatRowKey formats an MKey and partition into a rowKey
func FormatRowKey(mkey schema.MKey, partition int32) string {
return strconv.Itoa(int(partition)) + "_" + mkey.String()
}
// SchemaToRow takes a metricDefintion and returns a rowKey and column data.
func SchemaToRow(def *schema.MetricDefinition) (string, map[string][]byte) {
row := map[string][]byte{
//"Id" omitted as it is part of the rowKey
"OrgId": make([]byte, 8),
"Name": []byte(def.Name),
"Interval": make([]byte, 8),
"Unit": []byte(def.Unit),
"Mtype": []byte(def.Mtype),
"Tags": []byte(strings.Join(def.Tags, ";")),
"LastUpdate": make([]byte, 8),
//"Partition" omitted as it is part of te rowKey
}
binary.PutVarint(row["OrgId"], int64(def.OrgId))
binary.PutVarint(row["Interval"], int64(def.Interval))
binary.PutVarint(row["LastUpdate"], def.LastUpdate)
return FormatRowKey(def.Id, def.Partition), row
}
// DecodeRowKey takes a rowKey string and returns the corresponding MKey and partition
func DecodeRowKey(key string) (schema.MKey, int32, error) {
parts := strings.SplitN(key, "_", 2)
partition, err := strconv.Atoi(parts[0])
if err != nil {
return schema.MKey{}, 0, err
}
mkey, err := schema.MKeyFromString(parts[1])
if err != nil {
return schema.MKey{}, 0, err
}
return mkey, int32(partition), nil
}
// RowToSchema takes a row and unmarshals the data into the provided MetricDefinition.
func RowToSchema(row bigtable.Row, def *schema.MetricDefinition) error {
if def == nil {
return fmt.Errorf("cant write row to nil MetricDefinition")
}
columns, ok := row[COLUMN_FAMILY]
if !ok {
return fmt.Errorf("no columns in columnFamly %s", COLUMN_FAMILY)
}
*def = schema.MetricDefinition{}
var err error
var val int64
def.Id, def.Partition, err = DecodeRowKey(row.Key())
if err != nil {
return err
}
for _, col := range columns {
switch strings.SplitN(col.Column, ":", 2)[1] {
case "OrgId":
val, err = binary.ReadVarint(bytes.NewReader(col.Value))
if err != nil {
return err
}
if val < 0 {
def.OrgId = idx.OrgIdPublic
} else {
def.OrgId = uint32(val)
}
case "Name":
def.Name = string(col.Value)
case "Interval":
val, err = binary.ReadVarint(bytes.NewReader(col.Value))
if err != nil {
return err
}
def.Interval = int(val)
case "Unit":
def.Unit = string(col.Value)
case "Mtype":
def.Mtype = string(col.Value)
case "Tags":
if len(col.Value) == 0 {
def.Tags = nil
} else {
def.Tags = strings.Split(string(col.Value), ";")
}
case "LastUpdate":
def.LastUpdate, err = binary.ReadVarint(bytes.NewReader(col.Value))
if err != nil {
return err
}
default:
return fmt.Errorf("unknown column: %s", col.Column)
}
}
return nil
}