From 1540f81e681830f7c2c80f5fcbdb6faa4175ad58 Mon Sep 17 00:00:00 2001 From: siddontang Date: Thu, 5 Feb 2015 09:50:40 +0800 Subject: [PATCH] add some kv command setrange, getrange, strlen, append --- server/cmd_kv.go | 80 +++++++++++++++++++++++++++++++++++++++++++ server/cmd_kv_test.go | 26 ++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/server/cmd_kv.go b/server/cmd_kv.go index 4e7d601..11d670d 100644 --- a/server/cmd_kv.go +++ b/server/cmd_kv.go @@ -391,12 +391,90 @@ func xrevscanCommand(c *client) error { 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() { + register("append", appendCommand) register("decr", decrCommand) register("decrby", decrbyCommand) register("del", delCommand) register("exists", existsCommand) register("get", getCommand) + register("getrange", getrangeCommand) register("getset", getsetCommand) register("incr", incrCommand) register("incrby", incrbyCommand) @@ -405,6 +483,8 @@ func init() { register("set", setCommand) register("setnx", setnxCommand) register("setex", setexCommand) + register("setrange", setrangeCommand) + register("strlen", strlenCommand) register("expire", expireCommand) register("expireat", expireAtCommand) register("ttl", ttlCommand) diff --git a/server/cmd_kv_test.go b/server/cmd_kv_test.go index d3b0fe4..a736760 100644 --- a/server/cmd_kv_test.go +++ b/server/cmd_kv_test.go @@ -78,6 +78,32 @@ func TestKV(t *testing.T) { } else if n != 0 { 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) {