forked from mirror/ledisdb
remove scan cmd, we implement a little diff
This commit is contained in:
parent
581a492f78
commit
f404e35142
|
@ -16,7 +16,7 @@ const (
|
|||
)
|
||||
|
||||
const (
|
||||
defaultScanCount int = 20
|
||||
defaultScanCount int = 10
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue