forked from cockroachdb/cockroach
/
kv.go
448 lines (396 loc) · 10.8 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Copyright 2015 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Peter Mattis (peter@cockroachlabs.com)
package cli
import (
"bytes"
"fmt"
"net"
"strconv"
"strings"
"golang.org/x/net/context"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/cockroachdb/cockroach/pkg/internal/client"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util/stop"
)
func addrWithDefaultHost(addr string) (string, error) {
host, port, err := net.SplitHostPort(baseCfg.Addr)
if err != nil {
return "", err
}
if host == "" {
host = "localhost"
}
return net.JoinHostPort(host, port), nil
}
// MakeDBClient creates a kv client for use in cli tools.
func MakeDBClient() (*client.DB, *stop.Stopper, error) {
conn, stopper, err := getGRPCConn()
if err != nil {
return nil, nil, err
}
return client.NewDB(client.NewSender(conn)), stopper, nil
}
// unquoteArg unquotes the provided argument using Go double-quoted
// string literal rules.
func unquoteArg(arg string, disallowSystem bool) (string, error) {
s, err := strconv.Unquote(`"` + arg + `"`)
if err != nil {
return "", errors.Wrapf(err, "invalid argument %q", arg)
}
if disallowSystem && strings.HasPrefix(s, "\x00") {
return "", errors.Errorf("cannot specify system key %q", s)
}
return s, nil
}
// A getCmd gets the value for the specified key.
var getCmd = &cobra.Command{
Use: "get [options] <key>",
Short: "gets the value for a key",
Long: `
Fetches and displays the value for <key>.
`,
RunE: MaybeDecorateGRPCError(runGet),
}
func runGet(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return usageAndError(cmd)
}
kvDB, stopper, err := MakeDBClient()
if err != nil {
return err
}
defer stopper.Stop()
unquoted, err := unquoteArg(args[0], false)
if err != nil {
return err
}
key := roachpb.Key(unquoted)
r, err := kvDB.Get(context.Background(), key)
if err != nil {
return err
}
if !r.Exists() {
return errors.Errorf("%s not found", key)
}
fmt.Printf("%s\n", r.PrettyValue())
return nil
}
// A putCmd sets the value for one or more keys.
var putCmd = &cobra.Command{
Use: "put [options] <key> <value> [<key2> <value2>...]",
Short: "sets the value for one or more keys",
Long: `
Sets the value for one or more keys. Keys and values must be provided
in pairs on the command line.
WARNING: Modifying system or table keys can corrupt your cluster.
`,
RunE: MaybeDecorateGRPCError(runPut),
}
func runPut(cmd *cobra.Command, args []string) error {
if len(args) == 0 || len(args)%2 == 1 {
return usageAndError(cmd)
}
b := &client.Batch{}
for i := 0; i < len(args); i += 2 {
k, err := unquoteArg(args[i], true /* disallow system keys */)
if err != nil {
return err
}
v, err := unquoteArg(args[i+1], false)
if err != nil {
return err
}
b.Put(k, v)
}
kvDB, stopper, err := MakeDBClient()
if err != nil {
return err
}
defer stopper.Stop()
return kvDB.Run(context.Background(), b)
}
// A cPutCmd conditionally sets a value for a key.
var cPutCmd = &cobra.Command{
Use: "cput [options] <key> <value> [<expValue>]",
Short: "conditionally sets a value for a key",
Long: `
Conditionally sets a value for a key if the existing value is equal
to expValue. To conditionally set a value only if there is no existing entry
pass nil for expValue. The expValue defaults to 1 if not specified.
WARNING: Modifying system or table keys can corrupt your cluster.
`,
RunE: MaybeDecorateGRPCError(runCPut),
}
func runCPut(cmd *cobra.Command, args []string) error {
if len(args) != 2 && len(args) != 3 {
return usageAndError(cmd)
}
kvDB, stopper, err := MakeDBClient()
if err != nil {
return err
}
defer stopper.Stop()
key, err := unquoteArg(args[0], true /* disallow system keys */)
if err != nil {
return err
}
value, err := unquoteArg(args[1], false)
if err != nil {
return err
}
if len(args) == 3 {
existing, err := unquoteArg(args[2], false)
if err != nil {
return err
}
return kvDB.CPut(context.Background(), key, value, existing)
}
return kvDB.CPut(context.Background(), key, value, nil)
}
// A incCmd command increments the value for a key.
var incCmd = &cobra.Command{
Use: "inc [options] <key> [<amount>]",
Short: "increments the value for a key",
Long: `
Increments the value for a key. The increment amount defaults to 1 if
not specified. Displays the incremented value upon success.
Negative values need to be prefixed with -- to not get interpreted as
flags.
WARNING: Modifying system or table keys can corrupt your cluster.
`,
RunE: MaybeDecorateGRPCError(runInc),
}
func runInc(cmd *cobra.Command, args []string) error {
if len(args) < 1 || len(args) > 2 {
return usageAndError(cmd)
}
kvDB, stopper, err := MakeDBClient()
if err != nil {
return err
}
defer stopper.Stop()
amount := int64(1)
if len(args) == 2 {
var err error
if amount, err = strconv.ParseInt(args[1], 10, 0); err != nil {
return errors.Wrap(err, "invalid increment")
}
}
unquoted, err := unquoteArg(args[0], true /* disallow system keys */)
if err != nil {
return err
}
key := roachpb.Key(unquoted)
r, err := kvDB.Inc(context.TODO(), key, amount)
if err != nil {
return errors.Wrap(err, "increment failed")
}
fmt.Printf("%d\n", r.ValueInt())
return nil
}
// A delCmd deletes the values of one or more keys.
var delCmd = &cobra.Command{
Use: "del [options] <key> [<key2>...]",
Short: "deletes the values of one or more keys",
Long: `
Deletes the values of one or more keys.
WARNING: Modifying system or table keys can corrupt your cluster.
`,
RunE: MaybeDecorateGRPCError(runDel),
}
func runDel(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return usageAndError(cmd)
}
b := &client.Batch{}
for _, arg := range args {
key, err := unquoteArg(arg, true /* disallow system keys */)
if err != nil {
return err
}
b.Del(key)
}
kvDB, stopper, err := MakeDBClient()
if err != nil {
return err
}
defer stopper.Stop()
return kvDB.Run(context.Background(), b)
}
// A delRangeCmd deletes the values for a range of keys.
// [startKey, endKey).
var delRangeCmd = &cobra.Command{
Use: "delrange [options] <startKey> <endKey>",
Short: "deletes the values for a range of keys",
Long: `
Deletes the values for the range of keys [startKey, endKey).
WARNING: Modifying system or table keys can corrupt your cluster.
`,
RunE: MaybeDecorateGRPCError(runDelRange),
}
func runDelRange(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return usageAndError(cmd)
}
kvDB, stopper, err := MakeDBClient()
if err != nil {
return err
}
defer stopper.Stop()
uqFrom, err := unquoteArg(args[0], true /* disallow system keys */)
if err != nil {
return err
}
uqTo, err := unquoteArg(args[1], true /* disallow system keys */)
if err != nil {
return err
}
return kvDB.DelRange(
context.TODO(), roachpb.Key(uqFrom), roachpb.Key(uqTo),
)
}
// A scanCmd fetches the key/value pairs for a specified
// range.
var scanCmd = &cobra.Command{
Use: "scan [options] [<start-key> [<end-key>]]",
Short: "scans a range of keys",
Long: `
Fetches and display the key/value pairs for a range. If no <start-key>
is specified then all (non-system) key/value pairs are retrieved. If no
<end-key> is specified then all keys greater than or equal to <start-key>
are retrieved.
`,
RunE: MaybeDecorateGRPCError(runScan),
}
func runScan(cmd *cobra.Command, args []string) error {
if len(args) > 2 {
return usageAndError(cmd)
}
startKey, endKey, err := initScanArgs(args)
if err != nil {
return err
}
kvDB, stopper, err := MakeDBClient()
if err != nil {
return err
}
defer stopper.Stop()
rows, err := kvDB.Scan(context.Background(), startKey, endKey, maxResults)
if err != nil {
return err
}
showResult(rows)
return nil
}
// A reverseScanCmd fetches the key/value pairs for a specified
// range.
var reverseScanCmd = &cobra.Command{
Use: "revscan [options] [<start-key> [<end-key>]]",
Short: "scans a range of keys in reverse order",
Long: `
Fetches and display the key/value pairs for a range. If no <start-key>
is specified then all (non-system) key/value pairs are retrieved. If no
<end-key> is specified then all keys greater than or equal to <start-key>
are retrieved.
`,
RunE: MaybeDecorateGRPCError(runReverseScan),
}
func runReverseScan(cmd *cobra.Command, args []string) error {
if len(args) > 2 {
return usageAndError(cmd)
}
startKey, endKey, err := initScanArgs(args)
if err != nil {
return err
}
kvDB, stopper, err := MakeDBClient()
if err != nil {
return err
}
defer stopper.Stop()
rows, err := kvDB.ReverseScan(context.TODO(), startKey, endKey, maxResults)
if err != nil {
return err
}
showResult(rows)
return nil
}
func initScanArgs(args []string) (startKey, endKey roachpb.Key, _ error) {
if len(args) >= 1 {
unquoted, err := unquoteArg(args[0], false)
if err != nil {
return nil, nil, errors.Wrap(err, "invalid start key")
}
startKey = roachpb.Key(unquoted)
} else {
// Start with the first key after the system key range.
startKey = keys.UserDataSpan.Key
}
if len(args) >= 2 {
unquoted, err := unquoteArg(args[1], false)
if err != nil {
return nil, nil, errors.Wrap(err, "invalid end key")
}
endKey = roachpb.Key(unquoted)
} else {
// Exclude table data keys by default. The user can explicitly request them
// by passing \xff\xff for the end key.
endKey = keys.UserDataSpan.EndKey
}
if bytes.Compare(startKey, endKey) >= 0 {
return nil, nil, errors.New("start key must be smaller than end key")
}
return startKey, endKey, nil
}
func showResult(rows []client.KeyValue) {
for _, row := range rows {
if bytes.HasPrefix(row.Key, []byte{0}) {
// TODO(pmattis): Pretty-print system keys.
fmt.Printf("%s\n", row.Key)
continue
}
fmt.Printf("%s\t%s\n", row.Key, row.PrettyValue())
}
fmt.Printf("%d result(s)\n", len(rows))
}
var kvCmds = []*cobra.Command{
getCmd,
putCmd,
cPutCmd,
incCmd,
delCmd,
delRangeCmd,
scanCmd,
reverseScanCmd,
}
var kvCmd = &cobra.Command{
Use: "kv",
Short: "get, put, delete, and scan key/value pairs",
Long: `
Special characters in keys or values should be specified according to
the double-quoted Go string literal rules (see
https://golang.org/ref/spec#String_literals).
`,
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Usage()
},
}
func init() {
kvCmd.AddCommand(kvCmds...)
}