Documentation
¶
Overview ¶
Package zset provides a concurrent-safety sorted set, can be used as a local replacement of Redis' zset (https://redis.com/ebook/part-2-core-concepts/chapter-3-commands-in-redis/3-5-sorted-sets/).
The main different to other sets is, every value of set is associated with a score, that is used in order to take the sorted set ordered, from the smallest to the greatest score.
The sorted set has O(log(N)) time complexity when doing Add(ZADD) and Remove(ZREM) operations and O(1) time complexity when doing Contains operations.
Index ¶
- type Node
- type RangeOpt
- type Set
- func (z *Set[K]) Add(elements ...K)
- func (z *Set[K]) AddB(score float64, value K) bool
- func (z *Set[K]) Clear()
- func (z *Set[K]) Contains(elements ...K) bool
- func (z *Set[K]) ContainsB(value K) bool
- func (z *Set[K]) Count(min, max float64) int
- func (z *Set[K]) CountWithOpt(min, max float64, opt RangeOpt) int
- func (z *Set[K]) Empty() bool
- func (z *Set[K]) IncrBy(incr float64, value K) (float64, bool)
- func (z *Set[K]) Len() int
- func (z *Set[K]) Range(start, stop int) []Node[K]
- func (z *Set[K]) RangeByScore(min, max float64) []Node[K]
- func (z *Set[K]) RangeByScoreWithOpt(min, max float64, opt RangeOpt) []Node[K]
- func (z *Set[K]) Rank(value K) int
- func (z *Set[K]) Remove(elements ...K)
- func (z *Set[K]) RemoveB(value K) (float64, bool)
- func (z *Set[K]) RemoveRangeByRank(start, stop int) []Node[K]
- func (z *Set[K]) RemoveRangeByScore(min, max float64) []Node[K]
- func (z *Set[K]) RemoveRangeByScoreWithOpt(min, max float64, opt RangeOpt) []Node[K]
- func (z *Set[K]) RevRange(start, stop int) []Node[K]
- func (z *Set[K]) RevRangeByScore(max, min float64) []Node[K]
- func (z *Set[K]) RevRangeByScoreWithOpt(max, min float64, opt RangeOpt) []Node[K]
- func (z *Set[K]) RevRank(value K) int
- func (z *Set[K]) Score(value K) (float64, bool)
- func (z *Set[K]) Size() int
- func (z *Set[K]) String() string
- func (z *Set[K]) Values() []K
- type SetSafe
- func (s *SetSafe[K]) Add(elements ...K)
- func (s *SetSafe[K]) AddB(score float64, value K) bool
- func (s *SetSafe[K]) Clear()
- func (s *SetSafe[K]) Contains(elements ...K) bool
- func (s *SetSafe[K]) ContainsB(value K) bool
- func (s *SetSafe[K]) Count(min, max float64) int
- func (s *SetSafe[K]) CountWithOpt(min, max float64, opt RangeOpt) int
- func (s *SetSafe[K]) Empty() bool
- func (s *SetSafe[K]) IncrBy(incr float64, value K) (float64, bool)
- func (s *SetSafe[K]) Len() int
- func (s *SetSafe[K]) Range(start, stop int) []Node[K]
- func (s *SetSafe[K]) RangeByScore(min, max float64) []Node[K]
- func (s *SetSafe[K]) RangeByScoreWithOpt(min, max float64, opt RangeOpt) []Node[K]
- func (s *SetSafe[K]) Rank(value K) int
- func (s *SetSafe[K]) Remove(elements ...K)
- func (s *SetSafe[K]) RemoveB(value K) (float64, bool)
- func (s *SetSafe[K]) RemoveRangeByRank(start, stop int) []Node[K]
- func (s *SetSafe[K]) RemoveRangeByScore(min, max float64) []Node[K]
- func (s *SetSafe[K]) RemoveRangeByScoreWithOpt(min, max float64, opt RangeOpt) []Node[K]
- func (s *SetSafe[K]) RevRange(start, stop int) []Node[K]
- func (s *SetSafe[K]) RevRangeByScore(max, min float64) []Node[K]
- func (s *SetSafe[K]) RevRangeByScoreWithOpt(max, min float64, opt RangeOpt) []Node[K]
- func (s *SetSafe[K]) RevRank(value K) int
- func (s *SetSafe[K]) Score(value K) (float64, bool)
- func (s *SetSafe[K]) Size() int
- func (s *SetSafe[K]) String() string
- func (s *SetSafe[K]) Values() []K
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Node ¶
type Node[K comparable] struct { Value K Score float64 }
Node represents an element of Set.
type Set ¶
type Set[K comparable] struct { // contains filtered or unexported fields }
Set is a sorted set implementation with string value and float64 score.
func Inter ¶
func Inter[K comparable](comparator bcomparator.Comparator[K], zs ...*Set[K]) *Set[K]
Inter returns the intersection of given sorted sets, the resulting score of a value is the sum of its scores in the sorted sets where it exists.
Inter is the replacement of INTERSTORE command of redis.
func New ¶
func New[K comparable](comparator bcomparator.Comparator[K]) *Set[K]
New returns an empty string sorted set with int score. strings are sorted in ascending order.
func Union ¶
func Union[K comparable](comparator bcomparator.Comparator[K], zs ...*Set[K]) *Set[K]
Union returns the union of given sorted sets, the resulting score of a value is the sum of its scores in the sorted sets where it exists.
Union is the replacement of UNIONSTORE command of redis.
func (*Set[K]) AddB ¶
Add adds a new value or update the score of an existing value. Returns true if the value is newly created.
Add is the replacement of ZADD command of redis.
func (*Set[K]) Count ¶
Count returns the number of elements in the sorted set at element with a score between min and max (including elements with score equal to min or max).
Count is the replacement of ZCOUNT command of redis.
func (*Set[K]) IncrBy ¶
IncrBy increments the score of value in the sorted set by incr. If value does not exist in the sorted set, it is added with incr as its score (as if its previous score was zero).
IncrBy is the replacement of ZINCRBY command of redis.
func (*Set[K]) Len ¶
Len returns the length of Set.
Len is the replacement of ZCARD command of redis.
func (*Set[K]) Range ¶
Range returns the specified inclusive range of elements in the sorted set by rank(index). Both start and stop are 0-based, they can also be negative numbers indicating offsets from the end of the sorted set, with -1 being the last element of the sorted set, and so on.
The returned elements are ordered by score, from lowest to highest. Elements with the same score are ordered lexicographically.
This function won't panic even when the given rank out of range.
NOTE: Please always use z.Range(0, -1) for iterating the whole sorted set. z.Range(0, z.Len()-1) has 2 method calls, the sorted set may changes during the gap of calls.
Range is the replacement of ZRANGE command of redis.
func (*Set[K]) RangeByScore ¶
RangeByScore returns all the elements in the sorted set with a score between min and max (including elements with score equal to min or max). The elements are considered to be ordered from low to high scores.
RangeByScore is the replacement of ZRANGEBYSCORE command of redis.
func (*Set[K]) RangeByScoreWithOpt ¶
func (*Set[K]) Rank ¶
Rank returns the rank of element in the sorted set, with the scores ordered from low to high. The rank (or index) is 0-based, which means that the member with the lowest score has rank 0. -1 is returned when value is not found.
Rank is the replacement of ZRANK command of redis.
func (*Set[K]) RemoveB ¶
Remove removes a value from the sorted set. Returns score of the removed value and true if the node was found and deleted, otherwise returns (0.0, false).
Remove is the replacement of ZREM command of redis.
func (*Set[K]) RemoveRangeByRank ¶
RemoveRangeByRank removes all elements in the sorted set stored with rank between start and stop. Both start and stop are 0-based, they can also be negative numbers indicating offsets from the end of the sorted set, with -1 being the last element of the sorted set, and so on.
RemoveRangeByRank is the replacement of ZREMRANGEBYRANK command of redis.
func (*Set[K]) RemoveRangeByScore ¶
RemoveRangeByScore removes all elements in the sorted set stored with a score between min and max (including elements with score equal to min or max).
RemoveRangeByScore is the replacement of ZREMRANGEBYSCORE command of redis.
func (*Set[K]) RemoveRangeByScoreWithOpt ¶
func (*Set[K]) RevRange ¶
RevRange returns the specified inclusive range of elements in the sorted set by rank(index). Both start and stop are 0-based, they can also be negative numbers indicating offsets from the end of the sorted set, with -1 being the first element of the sorted set, and so on.
The returned elements are ordered by score, from highest to lowest. Elements with the same score are ordered in reverse lexicographical ordering.
This function won't panic even when the given rank out of range.
NOTE: Please always use z.RevRange(0, -1) for iterating the whole sorted set. z.RevRange(0, z.Len()-1) has 2 method calls, the sorted set may changes during the gap of calls.
RevRange is the replacement of ZREVRANGE command of redis.
func (*Set[K]) RevRangeByScore ¶
RevRangeByScore returns all the elements in the sorted set with a score between max and min (including elements with score equal to max or min). The elements are considered to be ordered from high to low scores.
RevRangeByScore is the replacement of ZREVRANGEBYSCORE command of redis.
func (*Set[K]) RevRangeByScoreWithOpt ¶
func (*Set[K]) RevRank ¶
RevRank returns the rank of element in the sorted set, with the scores ordered from high to low. The rank (or index) is 0-based, which means that the member with the highest score has rank 0. -1 is returned when value is not found.
RevRank is the replacement of ZREVRANK command of redis.
type SetSafe ¶
type SetSafe[K comparable] struct { // contains filtered or unexported fields }
func NewSafe ¶
func NewSafe[K comparable](comparator bcomparator.Comparator[K]) *SetSafe[K]