bug fix: set zset size

This commit is contained in:
wenyekui 2014-08-16 13:43:21 +08:00
parent 8eb9109e91
commit 14dd858b04
3 changed files with 32 additions and 42 deletions

View File

@ -1,8 +1,6 @@
package ledis package ledis
import ( import (
"fmt"
"github.com/siddontang/ledisdb/store"
"testing" "testing"
) )
@ -198,26 +196,6 @@ func TestDBBScan(t *testing.T) {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
ek1 := db.bEncodeMetaKey(k1)
fmt.Printf("%x\n", ek1)
ek2 := db.bEncodeMetaKey(k2)
fmt.Printf("%x\n", ek2)
ek3 := db.bEncodeMetaKey(k3)
fmt.Printf("%x\n", ek3)
start := db.bEncodeMetaKey(nil)
fmt.Printf("start: %x\n", start)
end := db.bEncodeMetaKey(nil)
end[len(end)-1] = BitMetaType + 1
fmt.Printf("end: %x\n", end)
it := db.db.RangeLimitIterator(start, end, store.RangeClose, 0, 4)
for ; it.Valid(); it.Next() {
fmt.Printf("%x\n", it.RawKey())
}
it.Close()
if v, err := db.BScan(nil, 1, true); err != nil { if v, err := db.BScan(nil, 1, true); err != nil {
t.Fatal(err) t.Fatal(err)
} else if len(v) != 1 { } else if len(v) != 1 {

View File

@ -873,29 +873,25 @@ func (db *DB) ZUnionStore(destKey []byte, srcKeys [][]byte, weights []int64, agg
db.zDelete(t, destKey) db.zDelete(t, destKey)
var num int64 = 0
for member, score := range destMap { for member, score := range destMap {
if err := checkZSetKMSize(destKey, []byte(member)); err != nil { if err := checkZSetKMSize(destKey, []byte(member)); err != nil {
return 0, err return 0, err
} }
if n, err := db.zSetItem(t, destKey, score, []byte(member)); err != nil { if _, err := db.zSetItem(t, destKey, score, []byte(member)); err != nil {
return 0, err return 0, err
} else if n == 0 {
//add new
num++
} }
} }
if _, err := db.zIncrSize(t, destKey, num); err != nil { var num = int64(len(destMap))
return 0, err sk := db.zEncodeSizeKey(destKey)
} t.Put(sk, PutInt64(num))
//todo add binlog //todo add binlog
if err := t.Commit(); err != nil { if err := t.Commit(); err != nil {
return 0, err return 0, err
} }
return int64(len(destMap)), nil return num, nil
} }
func (db *DB) ZInterStore(destKey []byte, srcKeys [][]byte, weights []int64, aggregate byte) (int64, error) { func (db *DB) ZInterStore(destKey []byte, srcKeys [][]byte, weights []int64, aggregate byte) (int64, error) {
@ -947,28 +943,23 @@ func (db *DB) ZInterStore(destKey []byte, srcKeys [][]byte, weights []int64, agg
db.zDelete(t, destKey) db.zDelete(t, destKey)
var num int64 = 0
for member, score := range destMap { for member, score := range destMap {
if err := checkZSetKMSize(destKey, []byte(member)); err != nil { if err := checkZSetKMSize(destKey, []byte(member)); err != nil {
return 0, err return 0, err
} }
if _, err := db.zSetItem(t, destKey, score, []byte(member)); err != nil {
if n, err := db.zSetItem(t, destKey, score, []byte(member)); err != nil {
return 0, err return 0, err
} else if n == 0 {
//add new
num++
} }
} }
if _, err := db.zIncrSize(t, destKey, num); err != nil { var num int64 = int64(len(destMap))
return 0, err sk := db.zEncodeSizeKey(destKey)
} t.Put(sk, PutInt64(num))
//todo add binlog //todo add binlog
if err := t.Commit(); err != nil { if err := t.Commit(); err != nil {
return 0, err return 0, err
} }
return int64(len(destMap)), nil return num, nil
} }
func (db *DB) ZScan(key []byte, count int, inclusive bool) ([][]byte, error) { func (db *DB) ZScan(key []byte, count int, inclusive bool) ([][]byte, error) {

View File

@ -253,6 +253,9 @@ func TestZUnionStore(t *testing.T) {
weights := []int64{1, 2} weights := []int64{1, 2}
out := []byte("out") out := []byte("out")
db.ZAdd(out, ScorePair{3, []byte("out")})
n, err := db.ZUnionStore(out, keys, weights, AggregateSum) n, err := db.ZUnionStore(out, keys, weights, AggregateSum)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
@ -296,6 +299,15 @@ func TestZUnionStore(t *testing.T) {
if n != 3 { if n != 3 {
t.Fatal("invalid value ", v) t.Fatal("invalid value ", v)
} }
n, err = db.ZCard(out)
if err != nil {
t.Fatal(err.Error())
}
if n != 3 {
t.Fatal("invalid value ", n)
}
} }
func TestZInterStore(t *testing.T) { func TestZInterStore(t *testing.T) {
@ -314,6 +326,8 @@ func TestZInterStore(t *testing.T) {
weights := []int64{2, 3} weights := []int64{2, 3}
out := []byte("out") out := []byte("out")
db.ZAdd(out, ScorePair{3, []byte("out")})
n, err := db.ZInterStore(out, keys, weights, AggregateSum) n, err := db.ZInterStore(out, keys, weights, AggregateSum)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
@ -329,7 +343,6 @@ func TestZInterStore(t *testing.T) {
t.Fatal("invalid value ", v) t.Fatal("invalid value ", v)
} }
out = []byte("out")
n, err = db.ZInterStore(out, keys, weights, AggregateMin) n, err = db.ZInterStore(out, keys, weights, AggregateMin)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
@ -355,4 +368,12 @@ func TestZInterStore(t *testing.T) {
t.Fatal("invalid value ", n) t.Fatal("invalid value ", n)
} }
n, err = db.ZCard(out)
if err != nil {
t.Fatal(err.Error())
}
if n != 1 {
t.Fatal("invalid value ", n)
}
} }