remove scan cmd, we implement a little diff

This commit is contained in:
siddontang 2014-05-23 12:39:56 +08:00
parent 581a492f78
commit f404e35142
3 changed files with 15 additions and 54 deletions

View File

@ -16,7 +16,7 @@ const (
) )
const ( const (
defaultScanCount int = 20 defaultScanCount int = 10
) )
const ( const (

View File

@ -289,36 +289,33 @@ func (db *DB) KvFlush() (drop int64, err error) {
return return
} }
func (db *DB) Scan(cursor int, count int) ([]interface{}, error) { func (db *DB) Scan(key []byte, count int) ([]KVPair, error) {
minKey := db.encodeKVMinKey() var minKey []byte
if key != nil {
if err := checkKeySize(key); err != nil {
return nil, err
}
minKey = key
} else {
minKey = db.encodeKVMinKey()
}
maxKey := db.encodeKVMaxKey() maxKey := db.encodeKVMaxKey()
if count <= 0 { if count <= 0 {
count = defaultScanCount count = defaultScanCount
} }
v := make([]interface{}, 2) v := make([]KVPair, 0, 2*count)
r := make([]interface{}, 0, count)
var num int = 0 it := db.db.Iterator(minKey, maxKey, leveldb.RangeROpen, 0, count)
it := db.db.Iterator(minKey, maxKey, leveldb.RangeROpen, cursor, count)
for ; it.Valid(); it.Next() { for ; it.Valid(); it.Next() {
num++
if key, err := db.decodeKVKey(it.Key()); err != nil { if key, err := db.decodeKVKey(it.Key()); err != nil {
continue continue
} else { } 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 return v, nil
} }

View File

@ -2,7 +2,6 @@ package server
import ( import (
"github.com/siddontang/ledisdb/ledis" "github.com/siddontang/ledisdb/ledis"
"strings"
) )
func getCommand(c *client) error { func getCommand(c *client) error {
@ -204,40 +203,6 @@ func mgetCommand(c *client) error {
return nil 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() { func init() {
register("decr", decrCommand) register("decr", decrCommand)
register("decrby", decrbyCommand) register("decrby", decrbyCommand)
@ -251,5 +216,4 @@ func init() {
register("mset", msetCommand) register("mset", msetCommand)
register("set", setCommand) register("set", setCommand)
register("setnx", setnxCommand) register("setnx", setnxCommand)
register("scan", scanCommand)
} }