-
Notifications
You must be signed in to change notification settings - Fork 3
/
kv.go
216 lines (208 loc) · 5.79 KB
/
kv.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package cobra
import (
"context"
"fmt"
"time"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/vx-labs/mqtt-broker/kv/pb"
)
func KV(ctx context.Context, config *viper.Viper) *cobra.Command {
c := &cobra.Command{
Use: "kv",
}
c.AddCommand(Get(ctx, config))
c.AddCommand(List(ctx, config))
c.AddCommand(GetMetadata(ctx, config))
c.AddCommand(GetWithMetadata(ctx, config))
c.AddCommand(Set(ctx, config))
c.AddCommand(Delete(ctx, config))
c.AddCommand(Benchmark(ctx, config))
return c
}
func Get(ctx context.Context, config *viper.Viper) *cobra.Command {
c := &cobra.Command{
Use: "get",
PreRun: func(c *cobra.Command, _ []string) {
},
Run: func(cmd *cobra.Command, argv []string) {
client := getClient(config)
for _, key := range argv {
value, err := client.Get(ctx, []byte(key))
if err != nil {
logrus.Errorf("failed to get %q: %v", key, err)
} else {
fmt.Println(string(value))
}
}
},
}
return c
}
func Benchmark(ctx context.Context, config *viper.Viper) *cobra.Command {
c := &cobra.Command{
Use: "benchmark",
PreRun: func(c *cobra.Command, _ []string) {
},
Run: func(cmd *cobra.Command, argv []string) {
client := getClient(config)
count := 0
total := 1000
start := time.Now()
for count < total {
key := []byte{byte(count)}
err := client.Set(ctx, key, []byte("benchmark"))
if err != nil {
logrus.Errorf("failed to write key %q: %v", string(key), err)
return
}
count++
if count%(total/3) == 0 {
logrus.Infof("written %d key in %s", count, time.Since(start).String())
}
}
timer := time.Since(start)
logrus.Infof("written %d key in %s", count, timer.String())
count = 0
start = time.Now()
for count < total {
key := []byte{byte(count)}
_, err := client.Get(ctx, key)
if err != nil {
logrus.Errorf("failed to read key %q: %v", string(key), err)
break
}
count++
if count%(total/3) == 0 {
logrus.Infof("read %d key in %s", count, time.Since(start).String())
}
}
timer = time.Since(start)
logrus.Infof("read %d key in %s", count, timer.String())
},
}
return c
}
func GetMetadata(ctx context.Context, config *viper.Viper) *cobra.Command {
c := &cobra.Command{
Use: "get-metadata",
Aliases: []string{"md"},
PreRun: func(c *cobra.Command, _ []string) {
},
Run: func(cmd *cobra.Command, argv []string) {
client := getClient(config)
for _, key := range argv {
value, err := client.GetMetadata(ctx, []byte(key))
if err != nil {
logrus.Errorf("failed to get %q: %v", key, err)
} else {
logrus.Infof("%s: version=%v, deadline=%d", key, value.Version, value.Deadline)
}
}
},
}
return c
}
func GetWithMetadata(ctx context.Context, config *viper.Viper) *cobra.Command {
c := &cobra.Command{
Use: "get-with-metadata",
Aliases: []string{"gmd"},
PreRun: func(c *cobra.Command, _ []string) {
},
Run: func(cmd *cobra.Command, argv []string) {
client := getClient(config)
for _, key := range argv {
value, md, err := client.GetWithMetadata(ctx, []byte(key))
if err != nil {
logrus.Errorf("failed to get %q: %v", key, err)
} else {
fmt.Println(string(value))
logrus.Infof("%s: version=%v, deadline=%d", key, md.Version, md.Deadline)
}
}
},
}
return c
}
func Delete(ctx context.Context, config *viper.Viper) *cobra.Command {
c := &cobra.Command{
Use: "delete",
PreRun: func(c *cobra.Command, _ []string) {
config.BindPFlag("version", c.Flags().Lookup("version"))
},
Run: func(cmd *cobra.Command, argv []string) {
client := getClient(config)
for _, key := range argv {
var err error
if cmd.Flag("version").Changed {
err = client.DeleteWithVersion(ctx, []byte(key), config.GetUint64("version"))
} else {
err = client.Delete(ctx, []byte(key))
}
if err != nil {
logrus.Errorf("failed to delete %q: %v", key, err)
} else {
logrus.Infof("%s: deleted", key)
}
}
},
}
c.Flags().Uint64("version", 0, "Key version to delete")
return c
}
func Set(ctx context.Context, config *viper.Viper) *cobra.Command {
c := &cobra.Command{
Use: "set",
PreRun: func(c *cobra.Command, _ []string) {
config.BindPFlag("key", c.Flags().Lookup("key"))
config.BindPFlag("value", c.Flags().Lookup("value"))
config.BindPFlag("ttl", c.Flags().Lookup("ttl"))
config.BindPFlag("version", c.Flags().Lookup("version"))
},
Run: func(cmd *cobra.Command, _ []string) {
client := getClient(config)
key := config.GetString("key")
ttl := config.GetDuration("ttl")
version := config.GetUint64("version")
var err error
if cmd.Flag("version").Changed {
err = client.SetWithVersion(ctx, []byte(key), []byte(config.GetString("value")), version, pb.WithTimeToLive(ttl))
} else {
err = client.Set(ctx, []byte(key), []byte(config.GetString("value")), pb.WithTimeToLive(ttl))
}
if err != nil {
logrus.Errorf("failed to set %s: %v", key, err)
} else {
logrus.Infof("%s: set", key)
}
},
}
c.Flags().StringP("key", "k", "", "Key's name")
c.Flags().StringP("value", "v", "", "Key's value")
c.Flags().Duration("ttl", 0, "Key's time to live")
c.Flags().Uint64("version", 0, "Key version to update")
c.MarkFlagRequired("key")
c.MarkFlagRequired("value")
return c
}
func List(ctx context.Context, config *viper.Viper) *cobra.Command {
c := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
PreRun: func(c *cobra.Command, _ []string) {
},
Run: func(cmd *cobra.Command, argv []string) {
client := getClient(config)
mds, err := client.List(ctx)
if err != nil {
logrus.Errorf("failed to list keys: %v", err)
return
}
for _, md := range mds {
logrus.Infof("%s: version=%v, deadline=%d", md.Key, md.Version, md.Deadline)
}
},
}
return c
}