func HandleConnection(conn net.Conn) { for { var cmd *command.Command = command.DecodeRedisProtocol(conn) if cmd == nil { break } //fmt.Println("recv: "+ command.EncodeText(*cmd)) //通过key的第一部分来确定分区 if cmd.Argc > 1 { index := strings.Index(cmd.Args[1], ".") if index == -1 { cmd.PartitionKey = cmd.Args[1] } else { cmd.PartitionKey = cmd.Args[1][:index] } server, _ := c.Get(cmd.PartitionKey) //fmt.Println("lookup node for key:"+cmd.PartitionKey+"->"+server) if server != hostname { //fmt.Println("forward to another node!") Send(server, command.EncodeRedisProtocol(*cmd)) } else { DoCommand(conn, *cmd) } } else { DoCommand(conn, *cmd) } } }
func Iterate() string { cmd := command.Command{} cmd.Argc = 1 cmd.Args = []string{"iterate"} value := SendCommand(conn, cmd) return value }
func Incr(key string) string { cmd := command.Command{} cmd.Argc = 2 cmd.Args = []string{"incr", key} value := SendCommand(conn, cmd) return value }
func Set(key string, value string) string { cmd := command.Command{} cmd.Argc = 3 cmd.Args = []string{"set", key, value} response := SendCommand(conn, cmd) return response }