Skip to content

Commit

Permalink
Merge pull request #223 from distributedio/Feat/rever_zset
Browse files Browse the repository at this point in the history
Feat/rever zset
  • Loading branch information
shafreeck authored Jun 17, 2021
2 parents c2a4271 + 7da9319 commit 545791b
Show file tree
Hide file tree
Showing 3 changed files with 482 additions and 26 deletions.
16 changes: 9 additions & 7 deletions command/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,15 @@ func init() {
"smove": Desc{Proc: AutoCommit(SMove), Txn: SMove, Cons: Constraint{4, flags("wF"), 1, 2, 1}},

// zsets
"zadd": Desc{Proc: AutoCommit(ZAdd), Txn: ZAdd, Cons: Constraint{-4, flags("wmF"), 1, 1, 1}},
"zrange": Desc{Proc: AutoCommit(ZRange), Txn: ZRange, Cons: Constraint{-4, flags("rF"), 1, 1, 1}},
"zrevrange": Desc{Proc: AutoCommit(ZRevRange), Txn: ZRevRange, Cons: Constraint{-4, flags("rF"), 1, 1, 1}},
"zrangebyscore": Desc{Proc: AutoCommit(ZRangeByScore), Txn: ZRangeByScore, Cons: Constraint{-4, flags("rF"), 1, 1, 1}},
"zrem": Desc{Proc: AutoCommit(ZRem), Txn: ZRem, Cons: Constraint{-3, flags("wF"), 1, 1, 1}},
"zcard": Desc{Proc: AutoCommit(ZCard), Txn: ZCard, Cons: Constraint{2, flags("rF"), 1, 1, 1}},
"zscore": Desc{Proc: AutoCommit(ZScore), Txn: ZScore, Cons: Constraint{3, flags("rF"), 1, 1, 1}},
"zadd": Desc{Proc: AutoCommit(ZAdd), Txn: ZAdd, Cons: Constraint{-4, flags("wmF"), 1, 1, 1}},
"zrange": Desc{Proc: AutoCommit(ZRange), Txn: ZRange, Cons: Constraint{-4, flags("rF"), 1, 1, 1}},
"zrevrange": Desc{Proc: AutoCommit(ZRevRange), Txn: ZRevRange, Cons: Constraint{-4, flags("rF"), 1, 1, 1}},
"zrangebyscore": Desc{Proc: AutoCommit(ZRangeByScore), Txn: ZRangeByScore, Cons: Constraint{-4, flags("rF"), 1, 1, 1}},
"zrevrangebyscore": Desc{Proc: AutoCommit(ZRevRangeByScore), Txn: ZRevRangeByScore, Cons: Constraint{-4, flags("rF"), 1, 1, 1}},
"zrem": Desc{Proc: AutoCommit(ZRem), Txn: ZRem, Cons: Constraint{-3, flags("wF"), 1, 1, 1}},
"zcard": Desc{Proc: AutoCommit(ZCard), Txn: ZCard, Cons: Constraint{2, flags("rF"), 1, 1, 1}},
//"zcount": Desc{Proc: AutoCommit(ZCount), Txn: ZCount, Cons: Constraint{4, flags("rF"), 1, 1, 1}},
"zscore": Desc{Proc: AutoCommit(ZScore), Txn: ZScore, Cons: Constraint{3, flags("rF"), 1, 1, 1}},

// extension commands
"escan": Desc{Proc: AutoCommit(Escan), Txn: Escan, Cons: Constraint{-1, flags("rR"), 0, 0, 0}},
Expand Down
26 changes: 7 additions & 19 deletions db/zset.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,12 @@ func (zset *ZSet) ZAnyOrderRange(start int64, stop int64, withScore bool, positi
var iter Iterator
var err error
startTime := time.Now()

upperBoundKey := kv.Key(scorePrefix).PrefixNext()
if positiveOrder {
iter, err = zset.txn.t.Iter(scorePrefix, nil)
iter, err = zset.txn.t.Iter(scorePrefix, upperBoundKey)
} else {
//tikv sdk didn't implement SeekReverse(), so we just use seek() to implement zrevrange now
//because tikv sdk scan 256 keys in next(), for the zset which have <=256 members,
// its performance should be similar with seekReverse, for >256 zset, it should be bad
//iter, err = zset.txn.t.SeekReverse(scorePrefix)
iter, err = zset.txn.t.Iter(scorePrefix, nil)
tmp := start
start = zset.meta.Len - 1 - stop
stop = zset.meta.Len - 1 - tmp
iter, err = zset.txn.t.IterReverse(upperBoundKey)
}
zap.L().Debug("zset seek", zap.Int64("cost(us)", time.Since(startTime).Nanoseconds()/1000))

Expand Down Expand Up @@ -243,9 +238,6 @@ func (zset *ZSet) ZAnyOrderRange(start int64, stop int64, withScore bool, positi
if withScore {
val := []byte(strconv.FormatFloat(DecodeFloat64(score), 'f', -1, 64))
items = append(items, val)
if !positiveOrder {
items[len(items)-1], items[len(items)-2] = items[len(items)-2], items[len(items)-1]
}
}
}

Expand All @@ -255,12 +247,6 @@ func (zset *ZSet) ZAnyOrderRange(start int64, stop int64, withScore bool, positi
}
zap.L().Debug("zset all next", zap.Int64("cost(us)", cost/1000))

if !positiveOrder {
for i, j := 0, len(items)-1; i < j; i, j = i+1, j-1 {
items[i], items[j] = items[j], items[i]
}
}

return items, nil
}

Expand Down Expand Up @@ -303,11 +289,13 @@ func (zset *ZSet) ZAnyOrderRangeByScore(startScore float64, startInclude bool,
copy(stopPrefix[len(scorePrefix):], byteStopScore)

var iter Iterator

if positiveOrder {
upperBoundKey := kv.Key(stopPrefix).PrefixNext()
iter, err = zset.txn.t.Iter(startPrefix, upperBoundKey)
} else {
iter, err = zset.txn.t.IterReverse(startPrefix)
upperBoundKey := kv.Key(startPrefix).PrefixNext()
iter, err = zset.txn.t.IterReverse(upperBoundKey)
}
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 545791b

Please sign in to comment.