This commit is contained in:
siddontang 2014-05-03 14:55:12 +08:00
parent 5cf0d47012
commit 687805549e
7 changed files with 261 additions and 22 deletions

View File

@ -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 len(req) == 0 {
err = ErrEmptyCommand
} else {
c.cmd = strings.ToLower(hack.String(req[0]))
c.args = req[1:]
f, ok := regCmds[c.cmd]
if !ok {
err = ErrNotFound
} else {
r, err = f(c, req[1:])
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()

59
ssdb/cmd_hash.go Normal file
View File

@ -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)
}

69
ssdb/cmd_kv.go Normal file
View File

@ -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)
}

39
ssdb/cmd_list.go Normal file
View File

@ -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)
}

42
ssdb/cmd_zset.go Normal file
View File

@ -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 }

View File

@ -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)
}

View File

@ -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"
)