Documentation
¶
Overview ¶
Package rzset is a database-backed sorted set repository. It provides methods to interact with sorted sets in the database.
Index ¶
- type DB
- func (d *DB) Add(key string, elem any, score float64) (bool, error)
- func (d *DB) AddMany(key string, items map[any]float64) (int, error)
- func (d *DB) Count(key string, min, max float64) (int, error)
- func (d *DB) Delete(key string, elems ...any) (int, error)
- func (d *DB) DeleteWith(key string) DeleteCmd
- func (d *DB) GetRank(key string, elem any) (rank int, score float64, err error)
- func (d *DB) GetRankRev(key string, elem any) (rank int, score float64, err error)
- func (d *DB) GetScore(key string, elem any) (float64, error)
- func (d *DB) Incr(key string, elem any, delta float64) (float64, error)
- func (d *DB) Inter(keys ...string) ([]SetItem, error)
- func (d *DB) InterWith(keys ...string) InterCmd
- func (d *DB) Len(key string) (int, error)
- func (d *DB) Range(key string, start, stop int) ([]SetItem, error)
- func (d *DB) RangeWith(key string) RangeCmd
- func (d *DB) Scan(key string, cursor int, pattern string, count int) (ScanResult, error)
- func (d *DB) Scanner(key, pattern string, pageSize int) *Scanner
- func (d *DB) Union(keys ...string) ([]SetItem, error)
- func (d *DB) UnionWith(keys ...string) UnionCmd
- type DeleteCmd
- type InterCmd
- type RangeCmd
- func (c RangeCmd) Asc() RangeCmd
- func (c RangeCmd) ByRank(start, stop int) RangeCmd
- func (c RangeCmd) ByScore(start, stop float64) RangeCmd
- func (c RangeCmd) Count(count int) RangeCmd
- func (c RangeCmd) Desc() RangeCmd
- func (c RangeCmd) Offset(offset int) RangeCmd
- func (c RangeCmd) Run() ([]SetItem, error)
- type ScanResult
- type Scanner
- type SetItem
- type Tx
- func (tx *Tx) Add(key string, elem any, score float64) (bool, error)
- func (tx *Tx) AddMany(key string, items map[any]float64) (int, error)
- func (tx *Tx) Count(key string, min, max float64) (int, error)
- func (tx *Tx) Delete(key string, elems ...any) (int, error)
- func (tx *Tx) DeleteWith(key string) DeleteCmd
- func (tx *Tx) GetRank(key string, elem any) (rank int, score float64, err error)
- func (tx *Tx) GetRankRev(key string, elem any) (rank int, score float64, err error)
- func (tx *Tx) GetScore(key string, elem any) (float64, error)
- func (tx *Tx) Incr(key string, elem any, delta float64) (float64, error)
- func (tx *Tx) Inter(keys ...string) ([]SetItem, error)
- func (tx *Tx) InterWith(keys ...string) InterCmd
- func (tx *Tx) Len(key string) (int, error)
- func (tx *Tx) Range(key string, start, stop int) ([]SetItem, error)
- func (tx *Tx) RangeWith(key string) RangeCmd
- func (tx *Tx) Scan(key string, cursor int, pattern string, count int) (ScanResult, error)
- func (tx *Tx) Scanner(key, pattern string, pageSize int) *Scanner
- func (tx *Tx) Union(keys ...string) ([]SetItem, error)
- func (tx *Tx) UnionWith(keys ...string) UnionCmd
- type UnionCmd
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DB ¶
DB is a database-backed sorted set repository. A sorted set (zset) is a like a set, but each element has a score. While elements are unique, scores can be repeated.
Elements in the set are ordered by score (from low to high), and then by lexicographical order (ascending). Adding, updating or removing elements maintains the order of the set.
Use the sorted set repository to work with sets and their elements, and to perform set operations like union or intersection.
func NewWithDriver ¶
NewWithDriver connects to the sorted set repository with a specific driver. Does not create the database schema.
func (*DB) Add ¶
Add adds or updates an element in a set. Returns true if the element was created, false if it was updated. If the key does not exist, creates it. If the key exists but is not a set, returns ErrKeyType.
func (*DB) AddMany ¶
AddMany adds or updates multiple elements in a set. Returns the number of elements created (as opposed to updated). If the key does not exist, creates it. If the key exists but is not a set, returns ErrKeyType.
func (*DB) Count ¶
Count returns the number of elements in a set with a score between min and max (inclusive). Exclusive ranges are not supported. Returns 0 if the key does not exist or is not a set.
func (*DB) Delete ¶
Delete removes elements from a set. Returns the number of elements removed. Ignores the elements that do not exist. Does nothing if the key does not exist or is not a set.
func (*DB) DeleteWith ¶
DeleteWith removes elements from a set with additional options.
func (*DB) GetRank ¶
GetRank returns the rank and score of an element in a set. The rank is the 0-based position of the element in the set, ordered by score (from low to high), and then by lexicographical order (ascending). If the element does not exist, returns ErrNotFound. If the key does not exist or is not a set, returns ErrNotFound.
func (*DB) GetRankRev ¶
GetRankRev returns the rank and score of an element in a set. The rank is the 0-based position of the element in the set, ordered by score (from high to low), and then by lexicographical order (descending). If the element does not exist, returns ErrNotFound. If the key does not exist or is not a set, returns ErrNotFound.
func (*DB) GetScore ¶
GetScore returns the score of an element in a set. If the element does not exist, returns ErrNotFound. If the key does not exist or is not a set, returns ErrNotFound.
func (*DB) Incr ¶
Incr increments the score of an element in a set. Returns the score after the increment. If the element does not exist, adds it and sets the score to 0.0 before the increment. If the key does not exist, creates it. If the key exists but is not a set, returns ErrKeyType.
func (*DB) Inter ¶
Inter returns the intersection of multiple sets. The intersection consists of elements that exist in all given sets. The score of each element is the sum of its scores in the given sets. If any of the source keys do not exist or are not sets, returns an empty slice.
func (*DB) Len ¶
Len returns the number of elements in a set. Returns 0 if the key does not exist or is not a set.
func (*DB) Range ¶
Range returns a range of elements from a set with ranks between start and stop. The rank is the 0-based position of the element in the set, ordered by score (from low to high), and then by lexicographical order (ascending). Start and stop are 0-based, inclusive. Negative values are not supported. If the key does not exist or is not a set, returns a nil slice.
func (*DB) Scan ¶
Scan iterates over set items with elements matching pattern. Returns a slice of element-score pairs (see SetItem) of size count based on the current state of the cursor. Returns an empty SetItem slice when there are no more items. If the key does not exist or is not a set, returns a nil slice. Supports glob-style patterns. Set count = 0 for default page size.
func (*DB) Scanner ¶
Scanner returns an iterator for set items with elements matching pattern. The scanner returns items one by one, fetching them from the database in pageSize batches when necessary. Stops when there are no more items or an error occurs. If the key does not exist or is not a set, stops immediately. Supports glob-style patterns. Set pageSize = 0 for default page size.
type DeleteCmd ¶
type DeleteCmd struct {
// contains filtered or unexported fields
}
DeleteCmd removes elements from a set.
func (DeleteCmd) ByRank ¶
ByRank sets filtering by rank. The rank is the 0-based position of the element in the set, ordered by score (from high to low), and then by lexicographical order (descending). Start and stop are 0-based, inclusive. Negative values are not supported.
type InterCmd ¶
type InterCmd struct {
// contains filtered or unexported fields
}
InterCmd intersects multiple sets.
func (InterCmd) Run ¶
Run returns the intersection of multiple sets. The intersection consists of elements that exist in all given sets. The score of each element is the aggregate of its scores in the given sets. If any of the source keys do not exist or are not sets, returns an empty slice.
func (InterCmd) Store ¶
Store intersects multiple sets and stores the result in a new set. Returns the number of elements in the resulting set. If the destination key already exists, it is fully overwritten (all old elements are removed and the new ones are inserted). If the destination key already exists and is not a set, returns ErrKeyType. If any of the source keys do not exist or are not sets, does nothing, except deleting the destination key if it exists.
type RangeCmd ¶
type RangeCmd struct {
// contains filtered or unexported fields
}
RangeCmd retrieves a range of elements from a sorted set.
func (RangeCmd) ByRank ¶
ByRank sets filtering and sorting by rank. Start and stop are 0-based, inclusive. Negative values are not supported.
func (RangeCmd) ByScore ¶
ByScore sets filtering and sorting by score. Start and stop are inclusive.
func (RangeCmd) Count ¶
Count sets the maximum number of elements to return. Only takes effect when filtering by score.
func (RangeCmd) Offset ¶
Offset sets the offset of the range. Only takes effect when filtering by score.
func (RangeCmd) Run ¶
Run returns a range of elements from a sorted set. Uses either by-rank or by-score filtering. The elements are sorted by score/rank and then by element according to the sorting direction.
Offset and count are optional, and only take effect when filtering by score.
If the key does not exist or is not a sorted set, returns a nil slice.
type ScanResult ¶
ScanResult is a result of the scan operation.
type Scanner ¶
type Scanner struct {
// contains filtered or unexported fields
}
Scanner is the iterator for set items. Stops when there are no more items or an error occurs.
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
Tx is a sorted set repository transaction.
func (*Tx) Add ¶
Add adds or updates an element in a set. Returns true if the element was created, false if it was updated. If the key does not exist, creates it. If the key exists but is not a set, returns ErrKeyType.
func (*Tx) AddMany ¶
AddMany adds or updates multiple elements in a set. Returns the number of elements created (as opposed to updated). If the key does not exist, creates it. If the key exists but is not a set, returns ErrKeyType.
func (*Tx) Count ¶
Count returns the number of elements in a set with a score between min and max (inclusive). Exclusive ranges are not supported. Returns 0 if the key does not exist or is not a set.
func (*Tx) Delete ¶
Delete removes elements from a set. Returns the number of elements removed. Ignores the elements that do not exist. Does nothing if the key does not exist or is not a set.
func (*Tx) DeleteWith ¶
DeleteWith removes elements from a set with additional options.
func (*Tx) GetRank ¶
GetRank returns the rank and score of an element in a set. The rank is the 0-based position of the element in the set, ordered by score (from low to high), and then by lexicographical order (ascending). If the element does not exist, returns ErrNotFound. If the key does not exist or is not a set, returns ErrNotFound.
func (*Tx) GetRankRev ¶
GetRankRev returns the rank and score of an element in a set. The rank is the 0-based position of the element in the set, ordered by score (from high to low), and then by lexicographical order (descending). If the element does not exist, returns ErrNotFound. If the key does not exist or is not a set, returns ErrNotFound.
func (*Tx) GetScore ¶
GetScore returns the score of an element in a set. If the element does not exist, returns ErrNotFound. If the key does not exist or is not a set, returns ErrNotFound.
func (*Tx) Incr ¶
Incr increments the score of an element in a set. Returns the score after the increment. If the element does not exist, adds it and sets the score to 0.0 before the increment. If the key does not exist, creates it. If the key exists but is not a set, returns ErrKeyType.
func (*Tx) Inter ¶
Inter returns the intersection of multiple sets. The intersection consists of elements that exist in all given sets. The score of each element is the sum of its scores in the given sets. If any of the source keys do not exist or are not sets, returns an empty slice.
func (*Tx) Len ¶
Len returns the number of elements in a set. Returns 0 if the key does not exist or is not a set.
func (*Tx) Range ¶
Range returns a range of elements from a set with ranks between start and stop. The rank is the 0-based position of the element in the set, ordered by score (from low to high), and then by lexicographical order (ascending). Start and stop are 0-based, inclusive. Negative values are not supported. If the key does not exist or is not a set, returns a nil slice.
func (*Tx) Scan ¶
Scan iterates over set items with elements matching pattern. Returns a slice of element-score pairs (see SetItem) of size count based on the current state of the cursor. Returns an empty SetItem slice when there are no more items. If the key does not exist or is not a set, returns a nil slice. Supports glob-style patterns. Set count = 0 for default page size.
func (*Tx) Scanner ¶
Scanner returns an iterator for set items with elements matching pattern. The scanner returns items one by one, fetching them from the database in pageSize batches when necessary. Stops when there are no more items or an error occurs. If the key does not exist or is not a set, stops immediately. Supports glob-style patterns. Set pageSize = 0 for default page size.
type UnionCmd ¶
type UnionCmd struct {
// contains filtered or unexported fields
}
UnionCmd unions multiple sets.
func (UnionCmd) Run ¶
Run returns the union of multiple sets. The union consists of elements that exist in any of the given sets. The score of each element is the aggregate of its scores in the given sets. Ignores the keys that do not exist or are not sets. If no keys exist, returns a nil slice.
func (UnionCmd) Store ¶
Store unions multiple sets and stores the result in a new set. Returns the number of elements in the resulting set. If the destination key already exists, it is fully overwritten (all old elements are removed and the new ones are inserted). If the destination key already exists and is not a set, returns ErrKeyType. Ignores the source keys that do not exist or are not sets. If all of the source keys do not exist or are not sets, does nothing, except deleting the destination key if it exists.