-
Notifications
You must be signed in to change notification settings - Fork 30
/
command_get.go
65 lines (53 loc) · 1.25 KB
/
command_get.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
package shell
import (
"fmt"
"io"
"github.com/chrislusf/vasto/goclient/vs"
"github.com/chrislusf/vasto/pb"
"github.com/chrislusf/vasto/util"
)
func init() {
commands = append(commands, &commandGet{})
}
type commandGet struct {
}
func (c *commandGet) Name() string {
return "get"
}
func (c *commandGet) Help() string {
return "<key>"
}
func (c *commandGet) Do(vastoClient *vs.VastoClient, args []string, commandEnv *commandEnv, writer io.Writer) error {
if commandEnv.clusterClient == nil {
return errNoKeyspaceSelected
}
// fmt.Printf("env: %+v\n", env)
if len(args) == 1 {
key := vs.Key([]byte(args[0]))
value, dt, err := commandEnv.clusterClient.Get(key)
if err == nil {
switch dt {
case pb.OpAndDataType_FLOAT64:
fmt.Fprintf(writer, "%f\n", util.BytesToFloat64(value))
default:
fmt.Fprintf(writer, "%s\n", string(value))
}
}
return err
}
var keys []*vs.KeyObject
for _, arg := range args {
keys = append(keys, vs.Key([]byte(arg)))
}
keyValues, err := commandEnv.clusterClient.BatchGet(keys)
if err != nil {
return err
}
for _, keyValue := range keyValues {
if keyValue == nil {
continue
}
fmt.Fprintf(writer, "%s : %s\n", string(keyValue.GetKey()), string(keyValue.GetValue()))
}
return nil
}