From 687805549edb574dc787d4c8e87bdaf977fdfe97 Mon Sep 17 00:00:00 2001 From: siddontang Date: Sat, 3 May 2014 14:55:12 +0800 Subject: [PATCH] update --- ssdb/client.go | 36 +++++++++++-------------- ssdb/cmd_hash.go | 59 +++++++++++++++++++++++++++++++++++++++++ ssdb/cmd_kv.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ ssdb/cmd_list.go | 39 +++++++++++++++++++++++++++ ssdb/cmd_zset.go | 42 +++++++++++++++++++++++++++++ ssdb/command.go | 34 ++++++++++++++++++++++-- ssdb/const.go | 4 +++ 7 files changed, 261 insertions(+), 22 deletions(-) create mode 100644 ssdb/cmd_hash.go create mode 100644 ssdb/cmd_kv.go create mode 100644 ssdb/cmd_list.go create mode 100644 ssdb/cmd_zset.go diff --git a/ssdb/client.go b/ssdb/client.go index a5fa96b..36e549c 100644 --- a/ssdb/client.go +++ b/ssdb/client.go @@ -9,6 +9,7 @@ import ( "net" "runtime" "strconv" + "strings" ) var errReadRequest = errors.New("read request error, invalid format") @@ -19,6 +20,9 @@ type client struct { rb *bufio.Reader wb *bufio.Writer + + cmd string + args [][]byte } func newClient(c net.Conn, app *App) { @@ -126,31 +130,23 @@ func (c *client) readRequest() ([][]byte, error) { func (c *client) handleRequest(req [][]byte) { var err error - var r interface{} - f, ok := regCmds[hack.String(req[0])] - if !ok { - err = ErrNotFound + + if len(req) == 0 { + err = ErrEmptyCommand } else { - r, err = f(c, req[1:]) + c.cmd = strings.ToLower(hack.String(req[0])) + c.args = req[1:] + + f, ok := regCmds[c.cmd] + if !ok { + err = ErrNotFound + } else { + err = f(c) + } } if err != nil { c.writeError(err) - } else { - switch v := r.(type) { - case string: - c.writeStatus(v) - case []byte: - c.writeBulk(v) - case int64: - c.writeInteger(v) - case []interface{}: - c.writeArray(v) - case nil: - c.writeBulk(nil) - default: - panic("invalid type") - } } c.wb.Flush() diff --git a/ssdb/cmd_hash.go b/ssdb/cmd_hash.go new file mode 100644 index 0000000..c4ff7dc --- /dev/null +++ b/ssdb/cmd_hash.go @@ -0,0 +1,59 @@ +package ssdb + +func hsetCommand(c *client) error { + return nil +} + +func hgetCommand(c *client) error { + return nil +} + +func hexistsCommand(c *client) error { + return nil +} + +func hmsetCommand(c *client) error { + return nil +} + +func hdelCommand(c *client) error { + return nil +} + +func hlenCommand(c *client) error { + return nil +} + +func hincrbyCommand(c *client) error { + return nil +} + +func hmgetCommand(c *client) error { + return nil +} + +func hgetallCommand(c *client) error { + return nil +} + +func hkeysCommand(c *client) error { + return nil +} + +func hvalsCommand(c *client) error { + return nil +} + +func init() { + register("hdel", hdelCommand) + register("hexists", hexistsCommand) + register("hget", hgetCommand) + register("hgetall", hgetallCommand) + register("hincrby", hincrbyCommand) + register("hkeys", hkeysCommand) + register("hlen", hlenCommand) + register("hmget", hmgetCommand) + register("hmset", hmsetCommand) + register("hset", hsetCommand) + register("hvals", hvalsCommand) +} diff --git a/ssdb/cmd_kv.go b/ssdb/cmd_kv.go new file mode 100644 index 0000000..10d9348 --- /dev/null +++ b/ssdb/cmd_kv.go @@ -0,0 +1,69 @@ +package ssdb + +func getCommand(c *client) error { + return nil +} + +func setCommand(c *client) error { + return nil +} + +func getsetCommand(c *client) error { + return nil +} + +func setnxCommand(c *client) error { + return nil +} + +func existsCommand(c *client) error { + return nil +} + +func incrCommand(c *client) error { + return nil +} + +func decrCommand(c *client) error { + return nil +} + +func incrbyCommand(c *client) error { + return nil +} + +func decrbyCommand(c *client) error { + return nil +} + +func delCommand(c *client) error { + return nil +} + +func msetCommand(c *client) error { + return nil +} + +func setexCommand(c *client) error { + return nil +} + +func mgetCommand(c *client) error { + return nil +} + +func init() { + register("decr", decrCommand) + register("decrby", decrbyCommand) + register("del", delCommand) + register("exists", existsCommand) + register("get", getCommand) + register("getset", getsetCommand) + register("incr", incrCommand) + register("incrby", incrbyCommand) + register("mget", mgetCommand) + register("mset", msetCommand) + register("set", setCommand) + register("setex", setexCommand) + register("setnx", setnxCommand) +} diff --git a/ssdb/cmd_list.go b/ssdb/cmd_list.go new file mode 100644 index 0000000..a10cb55 --- /dev/null +++ b/ssdb/cmd_list.go @@ -0,0 +1,39 @@ +package ssdb + +func lpushCommand(c *client) error { + return nil +} + +func rpushCommand(c *client) error { + return nil +} + +func lpopCommand(c *client) error { + return nil +} + +func rpopCommand(c *client) error { + return nil +} + +func llenCommand(c *client) error { + return nil +} + +func lindexCommand(c *client) error { + return nil +} + +func lrangeCommand(c *client) error { + return nil +} + +func init() { + register("lindex", lindexCommand) + register("llen", llenCommand) + register("lpop", lpopCommand) + register("lrange", lrangeCommand) + register("lpush", lpushCommand) + register("rpop", rpopCommand) + register("rpush", rpushCommand) +} diff --git a/ssdb/cmd_zset.go b/ssdb/cmd_zset.go new file mode 100644 index 0000000..ae238fb --- /dev/null +++ b/ssdb/cmd_zset.go @@ -0,0 +1,42 @@ +package ssdb + +func init() { + register("zadd", zaddCommand) + register("zcard", zcardCommand) + register("zcount", zcountCommand) + register("zincrby", zincrbyCommand) + register("zrange", zrangeCommand) + register("zrangebyscore", zrangebyscoreCommand) + register("zrank", zrankCommand) + register("zrem", zremCommand) + register("zremrangebyrank", zremrangebyrankCommand) + register("zremrangebyscore", zremrangebyscoreCommand) + register("zrevrange", zrevrangeCommand) + register("zrevrank", zrevrankCommand) + register("zrevrangebyscore", zrevrangebyscoreCommand) + register("zscore", zscoreCommand) +} + +func zcardCommand(c *client) error { + return nil +} + +func zscoreCommand(c *client) error { + return nil +} + +func zremCommand(c *client) error { + return nil +} + +func zrankCommand(c *client) error { return nil } +func zrevrankCommand(c *client) error { return nil } +func zcountCommand(c *client) error { return nil } +func zremrangebyrankCommand(c *client) error { return nil } +func zremrangebyscoreCommand(c *client) error { return nil } +func zrangeCommand(c *client) error { return nil } +func zrevrangeCommand(c *client) error { return nil } +func zaddCommand(c *client) error { return nil } +func zincrbyCommand(c *client) error { return nil } +func zrangebyscoreCommand(c *client) error { return nil } +func zrevrangebyscoreCommand(c *client) error { return nil } diff --git a/ssdb/command.go b/ssdb/command.go index 621fc3a..56ac7e9 100644 --- a/ssdb/command.go +++ b/ssdb/command.go @@ -1,7 +1,37 @@ package ssdb -import () +import ( + "fmt" + "strings" +) -type CommandFunc func(c *client, args [][]byte) (interface{}, error) +type CommandFunc func(c *client) error var regCmds = map[string]CommandFunc{} + +func register(name string, f CommandFunc) { + if _, ok := regCmds[strings.ToLower(name)]; ok { + panic(fmt.Sprintf("%s has been registered", name)) + } + + regCmds[name] = f +} + +func pingCommand(c *client) error { + c.writeStatus(PONG) + return nil +} + +func echoCommand(c *client) error { + if len(c.args) != 1 { + return ErrCmdParams + } + + c.writeBulk(c.args[0]) + return nil +} + +func init() { + register("ping", pingCommand) + register("echo", echoCommand) +} diff --git a/ssdb/const.go b/ssdb/const.go index f7e8e34..541d8bb 100644 --- a/ssdb/const.go +++ b/ssdb/const.go @@ -7,6 +7,7 @@ import ( var ( ErrEmptyCommand = errors.New("empty command") ErrNotFound = errors.New("command not found") + ErrCmdParams = errors.New("invalid command param") ) var ( @@ -14,4 +15,7 @@ var ( NullBulk = []byte("-1") NullArray = []byte("-1") + + PONG = "PONG" + OK = "OK" )