ledisdb/server/cmd_server.go

161 lines
2.6 KiB
Go
Raw Normal View History

2014-09-24 05:44:42 +04:00
package server
import (
2014-09-24 08:34:21 +04:00
"github.com/siddontang/go/hack"
2014-10-08 09:55:46 +04:00
"github.com/siddontang/go/num"
2014-09-24 05:44:42 +04:00
"strconv"
"strings"
2014-10-08 09:55:46 +04:00
"time"
2014-09-24 05:44:42 +04:00
)
func pingCommand(c *client) error {
c.resp.writeStatus(PONG)
return nil
}
func echoCommand(c *client) error {
if len(c.args) != 1 {
return ErrCmdParams
}
c.resp.writeBulk(c.args[0])
return nil
}
func selectCommand(c *client) error {
if len(c.args) != 1 {
return ErrCmdParams
}
2014-09-24 08:34:21 +04:00
if index, err := strconv.Atoi(hack.String(c.args[0])); err != nil {
2014-09-24 05:44:42 +04:00
return err
} else {
if c.db.IsInMulti() {
if err := c.script.Select(index); err != nil {
return err
} else {
c.db = c.script.DB
}
} else {
if db, err := c.ldb.Select(index); err != nil {
return err
} else {
c.db = db
}
}
c.resp.writeStatus(OK)
}
return nil
}
func infoCommand(c *client) error {
if len(c.args) > 1 {
return ErrCmdParams
}
var section string
if len(c.args) == 1 {
2014-09-24 08:34:21 +04:00
section = strings.ToLower(hack.String(c.args[0]))
2014-09-24 05:44:42 +04:00
}
buf := c.app.info.Dump(section)
c.resp.writeBulk(buf)
return nil
}
func flushallCommand(c *client) error {
err := c.ldb.FlushAll()
if err != nil {
return err
}
//we will restart the replication from master if possible
c.app.tryReSlaveof()
2014-09-24 05:44:42 +04:00
c.resp.writeStatus(OK)
return nil
}
func flushdbCommand(c *client) error {
_, err := c.db.FlushAll()
if err != nil {
return err
}
c.resp.writeStatus(OK)
return nil
}
2014-10-08 09:55:46 +04:00
func timeCommand(c *client) error {
if len(c.args) != 0 {
return ErrCmdParams
}
t := time.Now()
//seconds
s := t.Unix()
n := t.UnixNano()
//micro seconds
m := (n - s*1e9) / 1e3
ay := []interface{}{
num.FormatInt64ToSlice(s),
num.FormatInt64ToSlice(m),
}
c.resp.writeArray(ay)
return nil
}
2015-03-04 04:15:28 +03:00
func configGetCommand(c *client) error {
args := c.args
if len(args) != 2 {
return ErrCmdParams
}
ay := make([][]byte, 0, 2)
key := hack.String(args[1])
switch key {
case "databases":
ay = append(ay, []byte("databases"), num.FormatUint8ToSlice(c.app.cfg.Databases))
}
c.resp.writeSliceArray(ay)
return nil
}
2014-10-08 12:39:14 +04:00
func configCommand(c *client) error {
if len(c.args) < 1 {
return ErrCmdParams
}
switch strings.ToLower(hack.String(c.args[0])) {
case "rewrite":
if err := c.app.cfg.Rewrite(); err != nil {
return err
} else {
c.resp.writeStatus(OK)
return nil
}
2015-03-04 04:15:28 +03:00
case "get":
return configGetCommand(c)
2014-10-08 12:39:14 +04:00
default:
return ErrCmdParams
}
}
2014-09-24 05:44:42 +04:00
func init() {
register("ping", pingCommand)
register("echo", echoCommand)
register("select", selectCommand)
register("info", infoCommand)
register("flushall", flushallCommand)
register("flushdb", flushdbCommand)
2014-10-08 09:55:46 +04:00
register("time", timeCommand)
2014-10-08 12:39:14 +04:00
register("config", configCommand)
2014-09-24 05:44:42 +04:00
}