Merge branch 'develop'

This commit is contained in:
siddontang 2014-07-01 09:28:04 +08:00
commit e3e2229f79
4 changed files with 50 additions and 33 deletions

View File

@ -322,46 +322,41 @@ func (db *DB) LRange(key []byte, start int32, stop int32) ([][]byte, error) {
return nil, err
}
var startSeq int32
var stopSeq int32
if start > stop {
return [][]byte{}, nil
}
v := make([][]byte, 0, 16)
var headSeq int32
var tailSeq int32
var llen int32
var err error
metaKey := db.lEncodeMetaKey(key)
if headSeq, tailSeq, _, err = db.lGetMeta(metaKey); err != nil {
if headSeq, _, llen, err = db.lGetMeta(metaKey); err != nil {
return nil, err
}
if start >= 0 && stop >= 0 {
startSeq = headSeq + start
stopSeq = headSeq + stop
} else if start < 0 && stop < 0 {
startSeq = tailSeq + start + 1
stopSeq = tailSeq + stop + 1
} else {
//start < 0 && stop > 0
startSeq = tailSeq + start + 1
stopSeq = headSeq + stop
if start < 0 {
start = llen + start
}
if stop < 0 {
stop = llen + stop
}
if start < 0 {
start = 0
}
if startSeq < listMinSeq {
startSeq = listMinSeq
} else if stopSeq > listMaxSeq {
stopSeq = listMaxSeq
if start > stop || start >= llen {
return [][]byte{}, nil
}
startKey := db.lEncodeListKey(key, startSeq)
stopKey := db.lEncodeListKey(key, stopSeq)
it := db.db.RangeLimitIterator(startKey, stopKey, leveldb.RangeClose, 0, -1)
if stop >= llen {
stop = llen - 1
}
limit := (stop - start) + 1
headSeq += start
v := make([][]byte, 0, limit)
startKey := db.lEncodeListKey(key, headSeq)
it := db.db.RangeLimitIterator(startKey, nil, leveldb.RangeClose, 0, int(limit))
for ; it.Valid(); it.Next() {
v = append(v, it.Value())
}

View File

@ -37,6 +37,18 @@ func TestDBList(t *testing.T) {
t.Fatal(n)
}
if ay, err := db.LRange(key, 0, -1); err != nil {
t.Fatal(err)
} else if len(ay) != 3 {
t.Fatal(len(ay))
} else {
for i := range ay {
if ay[i][0] != '1'+byte(i) {
t.Fatal(string(ay[i]))
}
}
}
if k, err := db.RPop(key); err != nil {
t.Fatal(err)
} else if string(k) != "3" {

View File

@ -335,7 +335,17 @@ func zrangebyscoreGeneric(c *client, reverse bool) error {
}
key := args[0]
min, max, err := zparseScoreRange(args[1], args[2])
var minScore, maxScore []byte
if !reverse {
minScore, maxScore = args[1], args[2]
} else {
minScore, maxScore = args[2], args[1]
}
min, max, err := zparseScoreRange(minScore, maxScore)
if err != nil {
return err
}

View File

@ -264,7 +264,7 @@ func TestZSetRangeScore(t *testing.T) {
}
}
if v, err := ledis.MultiBulk(c.Do("zrevrangebyscore", key, 1, 4, "withscores")); err != nil {
if v, err := ledis.MultiBulk(c.Do("zrevrangebyscore", key, 4, 1, "withscores")); err != nil {
t.Fatal(err)
} else {
if err := testZSetRange(v, "d", 4, "c", 3, "b", 2, "a", 1); err != nil {
@ -272,7 +272,7 @@ func TestZSetRangeScore(t *testing.T) {
}
}
if v, err := ledis.MultiBulk(c.Do("zrevrangebyscore", key, 1, 4, "withscores", "limit", 1, 2)); err != nil {
if v, err := ledis.MultiBulk(c.Do("zrevrangebyscore", key, 4, 1, "withscores", "limit", 1, 2)); err != nil {
t.Fatal(err)
} else {
if err := testZSetRange(v, "c", 3, "b", 2); err != nil {
@ -280,7 +280,7 @@ func TestZSetRangeScore(t *testing.T) {
}
}
if v, err := ledis.MultiBulk(c.Do("zrevrangebyscore", key, "-inf", "+inf", "withscores")); err != nil {
if v, err := ledis.MultiBulk(c.Do("zrevrangebyscore", key, "+inf", "-inf", "withscores")); err != nil {
t.Fatal(err)
} else {
if err := testZSetRange(v, "d", 4, "c", 3, "b", 2, "a", 1); err != nil {
@ -288,7 +288,7 @@ func TestZSetRangeScore(t *testing.T) {
}
}
if v, err := ledis.MultiBulk(c.Do("zrevrangebyscore", key, "(1", "(4")); err != nil {
if v, err := ledis.MultiBulk(c.Do("zrevrangebyscore", key, "(4", "(1")); err != nil {
t.Fatal(err)
} else {
if err := testZSetRange(v, "c", "b"); err != nil {