-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.go
43 lines (37 loc) · 879 Bytes
/
tools.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
package mtsdb
import (
"bytes"
"hash/fnv"
"strconv"
"strings"
"unsafe"
)
func hashLabels(labels []string) (uint32, error) {
h := fnv.New32a()
_, err := h.Write([]byte(strings.Join(labels, "")))
if err != nil {
return 0, err
}
return h.Sum32(), nil
}
func (m *mtsdb) generateSql(tableName string, labels []string) string {
bb := bytes.NewBuffer(nil)
bb.WriteString("INSERT ")
bb.WriteString("INTO ")
bb.WriteString(tableName)
bb.WriteString(" (")
for _, label := range labels {
bb.WriteString(label)
bb.WriteRune(',')
}
bb.WriteString("cnt) VALUES (")
for i := 0; i < len(labels); i++ {
bb.WriteRune('$')
bb.WriteString(strconv.FormatInt(int64(i+1), 10))
bb.WriteRune(',')
}
bb.WriteString("$")
bb.WriteString(strconv.FormatInt(int64(len(labels)+1), 10))
bb.WriteString(")")
return unsafe.String(unsafe.SliceData(bb.Bytes()), bb.Len())
}