fix SAdd logic

This commit is contained in:
holys 2014-08-14 09:15:46 +08:00
parent 54a686277d
commit 572a8f2c9a
2 changed files with 19 additions and 16 deletions

View File

@ -139,10 +139,6 @@ func (db *DB) sIncrSize(key []byte, delta int64) (int64, error) {
db.rmExpire(t, SetType, key) db.rmExpire(t, SetType, key)
} else { } else {
t.Put(sk, PutInt64(size)) t.Put(sk, PutInt64(size))
//FIXME
if err := t.Commit(); err != nil {
return 0, err
}
} }
} }
@ -189,22 +185,32 @@ func (db *DB) SAdd(key []byte, args ...[]byte) (int64, error) {
t.Lock() t.Lock()
defer t.Unlock() defer t.Unlock()
var err error
var ek []byte
var num int64 = 0 var num int64 = 0
for i := 0; i < len(args); i++ { for i := 0; i < len(args); i++ {
member := args[i] if err := checkSetKMSize(key, args[i]); err != nil {
if err := checkSetKMSize(key, member); err != nil {
return 0, err return 0, err
} }
if n, err := db.sSetItem(key, member); err != nil { ek = db.sEncodeSetKey(key, args[i])
if v, err := db.db.Get(ek); err != nil {
return 0, err return 0, err
} else if n == 1 { } else if v == nil {
num++ num++
} }
t.Put(ek, nil)
} }
err := t.Commit() if _, err = db.sIncrSize(key, num); err != nil {
return 0, err
}
err = t.Commit()
return num, err return num, err
} }
func (db *DB) SCard(key []byte) (int64, error) { func (db *DB) SCard(key []byte) (int64, error) {

View File

@ -82,21 +82,18 @@ func TestDBSet(t *testing.T) {
db.SAdd(key1, member1, member2) db.SAdd(key1, member1, member2)
// tx := db.setTx
// if n := db.sDelete(tx, key1); n != 2 {
// t.Fatal(n)
// }
if n, err := db.SClear(key1); err != nil { if n, err := db.SClear(key1); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 2 { } else if n != 2 {
t.Fatal(n) t.Fatal(n)
} }
db.SAdd(key2, member1, member2)
db.SAdd(key1, member1, member2) db.SAdd(key1, member1, member2)
db.SAdd(key2, member1, member2, []byte("xxx"))
if n, _ := db.SCard(key2); n != 3 {
t.Fatal(n)
}
if n, err := db.SMclear(key1, key2); err != nil { if n, err := db.SMclear(key1, key2); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 2 { } else if n != 2 {