add some kv command

setrange, getrange, strlen, append
This commit is contained in:
siddontang 2015-02-05 09:50:40 +08:00
parent f77438741d
commit 1540f81e68
2 changed files with 106 additions and 0 deletions

View File

@ -391,12 +391,90 @@ func xrevscanCommand(c *client) error {
return xscanGeneric(c, c.db.RevScan) return xscanGeneric(c, c.db.RevScan)
} }
func appendCommand(c *client) error {
args := c.args
if len(args) != 2 {
return ErrCmdParams
}
if n, err := c.db.Append(args[0], args[1]); err != nil {
return err
} else {
c.resp.writeInteger(n)
}
return nil
}
func getrangeCommand(c *client) error {
args := c.args
if len(args) != 3 {
return ErrCmdParams
}
key := args[0]
start, err := strconv.Atoi(string(args[1]))
if err != nil {
return err
}
end, err := strconv.Atoi(string(args[2]))
if err != nil {
return err
}
if v, err := c.db.GetRange(key, start, end); err != nil {
return err
} else {
c.resp.writeBulk(v)
}
return nil
}
func setrangeCommand(c *client) error {
args := c.args
if len(args) != 3 {
return ErrCmdParams
}
key := args[0]
offset, err := strconv.Atoi(string(args[1]))
if err != nil {
return err
}
value := args[2]
if n, err := c.db.SetRange(key, offset, value); err != nil {
return err
} else {
c.resp.writeInteger(n)
}
return nil
}
func strlenCommand(c *client) error {
if len(c.args) != 1 {
return ErrCmdParams
}
if n, err := c.db.StrLen(c.args[0]); err != nil {
return err
} else {
c.resp.writeInteger(n)
}
return nil
}
func init() { func init() {
register("append", appendCommand)
register("decr", decrCommand) register("decr", decrCommand)
register("decrby", decrbyCommand) register("decrby", decrbyCommand)
register("del", delCommand) register("del", delCommand)
register("exists", existsCommand) register("exists", existsCommand)
register("get", getCommand) register("get", getCommand)
register("getrange", getrangeCommand)
register("getset", getsetCommand) register("getset", getsetCommand)
register("incr", incrCommand) register("incr", incrCommand)
register("incrby", incrbyCommand) register("incrby", incrbyCommand)
@ -405,6 +483,8 @@ func init() {
register("set", setCommand) register("set", setCommand)
register("setnx", setnxCommand) register("setnx", setnxCommand)
register("setex", setexCommand) register("setex", setexCommand)
register("setrange", setrangeCommand)
register("strlen", strlenCommand)
register("expire", expireCommand) register("expire", expireCommand)
register("expireat", expireAtCommand) register("expireat", expireAtCommand)
register("ttl", ttlCommand) register("ttl", ttlCommand)

View File

@ -78,6 +78,32 @@ func TestKV(t *testing.T) {
} else if n != 0 { } else if n != 0 {
t.Fatal(n) t.Fatal(n)
} }
rangeKey := "range_key"
if n, err := ledis.Int(c.Do("append", rangeKey, "Hello ")); err != nil {
t.Fatal(err)
} else if n != 6 {
t.Fatal(n)
}
if n, err := ledis.Int(c.Do("setrange", rangeKey, 6, "Redis")); err != nil {
t.Fatal(err)
} else if n != 11 {
t.Fatal(n)
}
if n, err := ledis.Int(c.Do("strlen", rangeKey)); err != nil {
t.Fatal(err)
} else if n != 11 {
t.Fatal(n)
}
if v, err := ledis.String(c.Do("getrange", rangeKey, 0, -1)); err != nil {
t.Fatal(err)
} else if v != "Hello Redis" {
t.Fatal(v)
}
} }
func TestKVM(t *testing.T) { func TestKVM(t *testing.T) {