github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/sets/zset/README.md (about)

     1  # zset
     2  
     3  ## Introduction
     4  
     5  zset provides a concurrent-safety sorted set, can be used as a local replacement
     6  of [Redis' zset](https://redis.com/ebook/part-2-core-concepts/chapter-3-commands-in-redis/3-5-sorted-sets/).
     7  
     8  The main difference to other sets is, every Value of set is associated with a score, that is used to take the sorted set
     9  ordered, from the smallest to the greatest score.
    10  
    11  The zset has `O(log(N))` time complexity when doing Add(ZADD) and Remove(ZREM) operations and `O(1)` time complexity
    12  when doing Contains operations.
    13  
    14  ## Features
    15  
    16  - Concurrent safe API
    17  - Values are sorted with score
    18  - Implementation equivalent to redis
    19  - Fast skiplist level randomization
    20  
    21  ## Comparison
    22  
    23  | Redis command         | Go function         |
    24  |-----------------------|---------------------|
    25  | ZADD                  | Add                 |
    26  | ZINCRBY               | IncrBy              |
    27  | ZREM                  | Remove              |
    28  | ZREMRANGEBYSCORE      | RemoveRangeByScore  |
    29  | ZREMRANGEBYRANK       | RemoveRangeByRank   |
    30  | ZUNION                | Union               |
    31  | ZINTER                | Inter               |
    32  | ZINTERCARD            | *TODO*              |
    33  | ZDIFF                 | *TODO*              |
    34  | ZRANGE                | Range               |
    35  | ZRANGEBYSCORE         | IncrBy              |
    36  | ZREVRANGEBYSCORE      | RevRangeByScore     |
    37  | ZCOUNT                | Count               |
    38  | ZREVRANGE             | RevRange            |
    39  | ZCARD                 | Len                 |
    40  | ZSCORE                | Score               |
    41  | ZRANK                 | Rank                |
    42  | ZREVRANK              | RevRank             |
    43  | ZPOPMIN               | *TODO*              |
    44  | ZPOPMAX               | *TODO*              |
    45  | ZRANDMEMBER           | *TODO*              |
    46  
    47  List of redis commands are generated from the following command:
    48  
    49  ```bash
    50  cat redis/src/server.c | grep -o '"z.*",z.*Command' | grep -o '".*"' | cut -d '"' -f2
    51  ```
    52  
    53  You may find that not all redis commands have corresponding go implementations,
    54  the reason is as follows:
    55  
    56  ### Unsupported Commands
    57  
    58  Redis' zset can operates elements in lexicographic order, which is not commonly
    59  used function, so zset does not support commands like ZREMRANGEBYLEX, ZLEXCOUNT
    60  and so on.
    61  
    62  | Redis command         |
    63  |-----------------------|
    64  | ZREMRANGEBYLEX        |
    65  | ZRANGEBYLEX           |
    66  | ZREVRANGEBYLEX        |
    67  | ZLEXCOUNT             |
    68  
    69  In redis, user accesses zset via a string key. We do not need such string key
    70  because we have variable. so the following commands are not implemented:
    71  
    72  | Redis command         |
    73  |-----------------------|
    74  | ZUNIONSTORE           |
    75  | ZINTERSTORE           |
    76  | ZDIFFSTORE            |
    77  | ZRANGESTORE           |
    78  | ZMSCORE               |
    79  | ZSCAN                 |
    80  
    81