-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate.go
85 lines (73 loc) · 1.78 KB
/
create.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
package redists
import (
"context"
)
type cmdCreate struct {
key string
retention Duration
encoding *Encoding
chunkSize *int
duplicatePolicy *DuplicatePolicy
labels map[string]string
}
func newCmdCreate(key string) *cmdCreate {
return &cmdCreate{key: key}
}
func (c *cmdCreate) Name() string {
return "TS.CREATE"
}
func (c *cmdCreate) Args() []interface{} {
args := []interface{}{c.key}
if c.retention != nil {
args = append(args, optionNameRetention, c.retention.Milliseconds())
}
if c.encoding != nil {
args = append(args, optionNameEncoding, string(*c.encoding))
}
if c.chunkSize != nil {
args = append(args, optionNameChunkSize, *c.chunkSize)
}
if c.duplicatePolicy != nil {
args = append(args, optionNameDuplicatePolicy, string(*c.duplicatePolicy))
}
if c.labels != nil {
args = append(args, optionNameLabels)
args = append(args, encodeLabels(c.labels)...)
}
return args
}
type OptionCreate func(cmd *cmdCreate)
// Create creates a new time-series.
func (c *Client) Create(ctx context.Context, key string, options ...OptionCreate) error {
cmd := newCmdCreate(key)
for i := range options {
options[i](cmd)
}
_, err := c.d.Do(ctx, cmd.Name(), cmd.Args()...)
return err
}
func CreateWithRetention(r Duration) OptionCreate {
return func(cmd *cmdCreate) {
cmd.retention = r
}
}
func CreateWithEncoding(e Encoding) OptionCreate {
return func(cmd *cmdCreate) {
cmd.encoding = &e
}
}
func CreateWithChunkSize(cs int) OptionCreate {
return func(cmd *cmdCreate) {
cmd.chunkSize = &cs
}
}
func CreateWithDuplicatePolicy(dp DuplicatePolicy) OptionCreate {
return func(cmd *cmdCreate) {
cmd.duplicatePolicy = &dp
}
}
func CreateWithLabels(ls Labels) OptionCreate {
return func(cmd *cmdCreate) {
cmd.labels = ls
}
}