github.com/hdt3213/godis@v1.2.9/cluster/utils.go (about) 1 package cluster 2 3 import ( 4 "github.com/hdt3213/godis/config" 5 "github.com/hdt3213/godis/interface/redis" 6 "github.com/hdt3213/godis/redis/protocol" 7 "strconv" 8 ) 9 10 func ping(cluster *Cluster, c redis.Connection, cmdLine CmdLine) redis.Reply { 11 return cluster.db.Exec(c, cmdLine) 12 } 13 14 func info(cluster *Cluster, c redis.Connection, cmdLine CmdLine) redis.Reply { 15 return cluster.db.Exec(c, cmdLine) 16 } 17 18 func randomkey(cluster *Cluster, c redis.Connection, cmdLine CmdLine) redis.Reply { 19 return cluster.db.Exec(c, cmdLine) 20 } 21 22 /*----- utils -------*/ 23 24 func makeArgs(cmd string, args ...string) [][]byte { 25 result := make([][]byte, len(args)+1) 26 result[0] = []byte(cmd) 27 for i, arg := range args { 28 result[i+1] = []byte(arg) 29 } 30 return result 31 } 32 33 // return node -> writeKeys 34 func (cluster *Cluster) groupBy(keys []string) map[string][]string { 35 result := make(map[string][]string) 36 for _, key := range keys { 37 peer := cluster.peerPicker.PickNode(key) 38 group, ok := result[peer] 39 if !ok { 40 group = make([]string, 0) 41 } 42 group = append(group, key) 43 result[peer] = group 44 } 45 return result 46 } 47 48 func execSelect(c redis.Connection, args [][]byte) redis.Reply { 49 dbIndex, err := strconv.Atoi(string(args[1])) 50 if err != nil { 51 return protocol.MakeErrReply("ERR invalid DB index") 52 } 53 if dbIndex >= config.Properties.Databases || dbIndex < 0 { 54 return protocol.MakeErrReply("ERR DB index is out of range") 55 } 56 c.SelectDB(dbIndex) 57 return protocol.MakeOkReply() 58 }