github.com/songzhibin97/gkit@v1.2.13/structure/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  ## QuickStart
    82  
    83  ```go
    84  package main
    85  
    86  import (
    87  	"fmt"
    88  
    89  	"github.com/songzhibin97/gkit/structure/zset"
    90  )
    91  
    92  func main() {
    93  	z := zset.NewFloat64()
    94  
    95  	values := []string{"Alice", "Bob", "Zhang"}
    96  	scores := []float64{90, 89, 59.9}
    97  	for i := range values {
    98  		z.Add(scores[i], values[i])
    99  	}
   100  
   101  	s, ok := z.Score("Alice")
   102  	if ok {
   103  		fmt.Println("Alice's score is", s)
   104  	}
   105  
   106  	n := z.Count(0, 60)
   107  	fmt.Println("There are", n, "people below 60 points")
   108  
   109  	for _, n := range z.Range(0, -1) {
   110  		fmt.Println("zset range found", n.Value, n.Score)
   111  	}
   112  }
   113  ```