diff --git a/command/init.go b/command/init.go index 46a27ab..3796d0c 100644 --- a/command/init.go +++ b/command/init.go @@ -124,16 +124,16 @@ func init() { "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}}, - "zrangebylex": Desc{Proc: AutoCommit(ZRangeByLex), Txn: ZRangeByScore, Cons: Constraint{-4, flags("rF"), 1, 1, 1}}, + "zrangebylex": Desc{Proc: AutoCommit(ZRangeByLex), Txn: ZRangeByScore, Cons: Constraint{-4, flags("rF"), 1, 1, 1}}, "zrevrangebylex": Desc{Proc: AutoCommit(ZRevRangeByLex), 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}}, - "zremrangebylex": Desc{Proc: AutoCommit(ZRemRangeByLex), Txn: ZRemRangeByLex, Cons: Constraint{-4, 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}}, - "zscan": Desc{Proc: AutoCommit(ZScan), Txn: ZScan, Cons: Constraint{-3, flags("rF"), 1, 1, 1}}, + "zrem": Desc{Proc: AutoCommit(ZRem), Txn: ZRem, Cons: Constraint{-3, flags("wF"), 1, 1, 1}}, + "zremrangebylex": Desc{Proc: AutoCommit(ZRemRangeByLex), Txn: ZRemRangeByLex, Cons: Constraint{-4, 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}}, + "zlexcount": Desc{Proc: AutoCommit(ZLexCount), Txn: ZLexCount, Cons: Constraint{-4, flags("rF"), 1, 1, 1}}, + "zscore": Desc{Proc: AutoCommit(ZScore), Txn: ZScore, Cons: Constraint{3, flags("rF"), 1, 1, 1}}, + "zscan": Desc{Proc: AutoCommit(ZScan), Txn: ZScan, 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}}, diff --git a/command/zsets.go b/command/zsets.go index d766da6..94ef3bb 100644 --- a/command/zsets.go +++ b/command/zsets.go @@ -117,6 +117,29 @@ func ZRevRangeByLex(ctx *Context, txn *db.Transaction) (OnCommit, error) { return zAnyOrderRangeByLex(ctx, txn, false) } +func ZLexCount(ctx *Context, txn *db.Transaction) (OnCommit, error) { + key := []byte(ctx.Args[0]) + startKey, startInclude := getLexKeyAndInclude([]byte(ctx.Args[1])) + stopKey, stopInclude := getLexKeyAndInclude([]byte(ctx.Args[2])) + zset, err := txn.ZSet(key) + if err != nil { + if err == db.ErrTypeMismatch { + return nil, ErrTypeMismatch + } + return nil, errors.New("ERR " + err.Error()) + } + if !zset.Exist() { + return Integer(ctx.Out, 0), nil + } + + items, err := zset.ZOrderRangeByLex(startKey, stopKey, startInclude, stopInclude, 0, math.MaxInt64, true) + if err != nil { + return nil, errors.New("ERR " + err.Error()) + } + + return Integer(ctx.Out, int64(len(items))), nil +} + func ZCount(ctx *Context, txn *db.Transaction) (OnCommit, error) { key := []byte(ctx.Args[0]) startScore, startInclude, err := getFloatAndInclude(ctx.Args[1]) diff --git a/db/zset.go b/db/zset.go index c37bee2..3665cc7 100644 --- a/db/zset.go +++ b/db/zset.go @@ -191,10 +191,8 @@ func (zset *ZSet) ZOrderRangeByLex(start, stop []byte, startInclude, stopInclude ) dkey := DataKey(zset.txn.db, zset.meta.ID) memPrefix := zsetMemberKey(dkey, []byte{}) - zap.L().Info("zorderrange", zap.String("sr", string(start)), zap.String("so", string(stop)), zap.Bool("starin", startInclude), zap.Bool("stopin", stopInclude), zap.Bool("position", positiveOrder)) f := func(key, val []byte) bool { member := key[len(memPrefix):] - zap.L().Info("iterm", zap.String("K", string(key)), zap.String("v", string(val))) startCmp := kv.Key(member).Cmp(start) stopCmp := kv.Key(member).Cmp(stop) if len(stop) == 0 { diff --git a/docs/command_list.md b/docs/command_list.md index 437a4eb..3e2f391 100644 --- a/docs/command_list.md +++ b/docs/command_list.md @@ -142,7 +142,7 @@ - [x] zcount - [ ] zincrby - [ ] zinterstore -- [ ] zlexcount +- [x] zlexcount - [ ] zpopmax - [ ] zpopmin - [x] zrange