mirror of https://github.com/ledisdb/ledisdb.git
bug fix: zinterstore
This commit is contained in:
parent
4d6b42671b
commit
94b3dac3e7
|
@ -939,7 +939,6 @@ func (db *DB) ZUnionStore(destKey []byte, srcKeys [][]byte, weights []int64, agg
|
||||||
|
|
||||||
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) {
|
||||||
|
|
||||||
var destMap = map[string]int64{}
|
|
||||||
aggregateFunc := getAggregateFunc(aggregate)
|
aggregateFunc := getAggregateFunc(aggregate)
|
||||||
if aggregateFunc == nil {
|
if aggregateFunc == nil {
|
||||||
return 0, errInvalidAggregate
|
return 0, errInvalidAggregate
|
||||||
|
@ -958,20 +957,27 @@ func (db *DB) ZInterStore(destKey []byte, srcKeys [][]byte, weights []int64, agg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var keptMembers [][]byte
|
var destMap = map[string]int64{}
|
||||||
for i, key := range srcKeys {
|
scorePairs, err := db.ZRange(srcKeys[0], 0, -1)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
for _, pair := range scorePairs {
|
||||||
|
destMap[String(pair.Member)] = pair.Score * weights[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, key := range srcKeys[1:] {
|
||||||
scorePairs, err := db.ZRange(key, 0, -1)
|
scorePairs, err := db.ZRange(key, 0, -1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
tmpMap := map[string]int64{}
|
||||||
for _, pair := range scorePairs {
|
for _, pair := range scorePairs {
|
||||||
if score, ok := destMap[String(pair.Member)]; !ok {
|
if score, ok := destMap[String(pair.Member)]; ok {
|
||||||
destMap[String(pair.Member)] = pair.Score * weights[i]
|
tmpMap[String(pair.Member)] = aggregateFunc(score, pair.Score*weights[i+1])
|
||||||
} else {
|
|
||||||
keptMembers = append(keptMembers, pair.Member)
|
|
||||||
destMap[String(pair.Member)] = aggregateFunc(score, pair.Score*weights[i])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
destMap = tmpMap
|
||||||
}
|
}
|
||||||
|
|
||||||
t := db.zsetTx
|
t := db.zsetTx
|
||||||
|
@ -981,13 +987,12 @@ func (db *DB) ZInterStore(destKey []byte, srcKeys [][]byte, weights []int64, agg
|
||||||
db.zDelete(t, destKey)
|
db.zDelete(t, destKey)
|
||||||
|
|
||||||
var num int64 = 0
|
var num int64 = 0
|
||||||
for _, member := range keptMembers {
|
for member, score := range destMap {
|
||||||
score := destMap[String(member)]
|
if err := checkZSetKMSize(destKey, []byte(member)); err != nil {
|
||||||
if err := checkZSetKMSize(destKey, member); err != nil {
|
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if n, err := db.zSetItem(t, destKey, score, 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 {
|
} else if n == 0 {
|
||||||
//add new
|
//add new
|
||||||
|
@ -1002,5 +1007,5 @@ func (db *DB) ZInterStore(destKey []byte, srcKeys [][]byte, weights []int64, agg
|
||||||
if err := t.Commit(); err != nil {
|
if err := t.Commit(); err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
return int64(len(keptMembers)), nil
|
return int64(len(destMap)), nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue