From f404e351429366af852b8bb0390d13eae3eab25d Mon Sep 17 00:00:00 2001 From: siddontang Date: Fri, 23 May 2014 12:39:56 +0800 Subject: [PATCH] remove scan cmd, we implement a little diff --- ledis/const.go | 2 +- ledis/t_kv.go | 31 ++++++++++++++----------------- server/cmd_kv.go | 36 ------------------------------------ 3 files changed, 15 insertions(+), 54 deletions(-) diff --git a/ledis/const.go b/ledis/const.go index 64473ad..9e4d0cf 100644 --- a/ledis/const.go +++ b/ledis/const.go @@ -16,7 +16,7 @@ const ( ) const ( - defaultScanCount int = 20 + defaultScanCount int = 10 ) const ( diff --git a/ledis/t_kv.go b/ledis/t_kv.go index 8c683be..16a92c5 100644 --- a/ledis/t_kv.go +++ b/ledis/t_kv.go @@ -289,36 +289,33 @@ func (db *DB) KvFlush() (drop int64, err error) { return } -func (db *DB) Scan(cursor int, count int) ([]interface{}, error) { - minKey := db.encodeKVMinKey() +func (db *DB) Scan(key []byte, count int) ([]KVPair, error) { + var minKey []byte + if key != nil { + if err := checkKeySize(key); err != nil { + return nil, err + } + minKey = key + } else { + minKey = db.encodeKVMinKey() + } + maxKey := db.encodeKVMaxKey() if count <= 0 { count = defaultScanCount } - v := make([]interface{}, 2) - r := make([]interface{}, 0, count) + v := make([]KVPair, 0, 2*count) - var num int = 0 - it := db.db.Iterator(minKey, maxKey, leveldb.RangeROpen, cursor, count) + it := db.db.Iterator(minKey, maxKey, leveldb.RangeROpen, 0, count) for ; it.Valid(); it.Next() { - num++ - if key, err := db.decodeKVKey(it.Key()); err != nil { continue } else { - r = append(r, key) + v = append(v, KVPair{Key: key, Value: it.Value()}) } } - if num < count { - v[0] = int64(0) - } else { - v[0] = int64(cursor + count) - } - - v[1] = r - return v, nil } diff --git a/server/cmd_kv.go b/server/cmd_kv.go index 11ec4c6..a6bdcbd 100644 --- a/server/cmd_kv.go +++ b/server/cmd_kv.go @@ -2,7 +2,6 @@ package server import ( "github.com/siddontang/ledisdb/ledis" - "strings" ) func getCommand(c *client) error { @@ -204,40 +203,6 @@ func mgetCommand(c *client) error { return nil } -func scanCommand(c *client) error { - args := c.args - - if len(args) != 1 && len(args) != 3 { - return ErrCmdParams - } - var offset int64 - var count int64 - var err error - - if offset, err = ledis.StrInt64(args[0], nil); err != nil { - return err - } - - //now we only support count - if len(args) == 3 { - if strings.ToLower(ledis.String(args[1])) != "count" { - return ErrCmdParams - } - - if count, err = ledis.StrInt64(args[2], nil); err != nil { - return err - } - } - - if v, err := c.db.Scan(int(offset), int(count)); err != nil { - return err - } else { - c.writeArray(v) - } - - return nil -} - func init() { register("decr", decrCommand) register("decrby", decrbyCommand) @@ -251,5 +216,4 @@ func init() { register("mset", msetCommand) register("set", setCommand) register("setnx", setnxCommand) - register("scan", scanCommand) }