rzset

package
v0.0.0-...-85961ca Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2025 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package rzset is a database-backed sorted set repository. It provides methods to interact with sorted sets in the database.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB struct {
	*sqlx.DB[*Tx]
}

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 New

func New(rw *sql.DB, ro *sql.DB) *DB

New connects to the sorted set repository. Does not create the database schema.

func NewWithDriver

func NewWithDriver(rw *sql.DB, ro *sql.DB, driver string) *DB

NewWithDriver connects to the sorted set repository with a specific driver. Does not create the database schema.

func (*DB) Add

func (d *DB) Add(key string, elem any, score float64) (bool, error)

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

func (d *DB) AddMany(key string, items map[any]float64) (int, error)

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

func (d *DB) Count(key string, min, max float64) (int, error)

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

func (d *DB) Delete(key string, elems ...any) (int, error)

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

func (d *DB) DeleteWith(key string) DeleteCmd

DeleteWith removes elements from a set with additional options.

func (*DB) GetRank

func (d *DB) GetRank(key string, elem any) (rank int, score float64, err error)

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

func (d *DB) GetRankRev(key string, elem any) (rank int, score float64, err error)

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

func (d *DB) GetScore(key string, elem any) (float64, error)

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

func (d *DB) Incr(key string, elem any, delta float64) (float64, error)

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

func (d *DB) Inter(keys ...string) ([]SetItem, error)

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) InterWith

func (d *DB) InterWith(keys ...string) InterCmd

InterWith intersects multiple sets with additional options.

func (*DB) Len

func (d *DB) Len(key string) (int, error)

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

func (d *DB) Range(key string, start, stop int) ([]SetItem, error)

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) RangeWith

func (d *DB) RangeWith(key string) RangeCmd

RangeWith ranges elements from a set with additional options.

func (*DB) Scan

func (d *DB) Scan(key string, cursor int, pattern string, count int) (ScanResult, error)

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

func (d *DB) Scanner(key, pattern string, pageSize int) *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.

func (*DB) Union

func (d *DB) Union(keys ...string) ([]SetItem, error)

Union 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 sum 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 (*DB) UnionWith

func (d *DB) UnionWith(keys ...string) UnionCmd

UnionWith unions multiple sets with additional options.

type DeleteCmd

type DeleteCmd struct {
	// contains filtered or unexported fields
}

DeleteCmd removes elements from a set.

func (DeleteCmd) ByRank

func (c DeleteCmd) ByRank(start, stop int) DeleteCmd

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.

func (DeleteCmd) ByScore

func (c DeleteCmd) ByScore(start, stop float64) DeleteCmd

ByScore sets filtering by score. Start and stop are inclusive.

func (DeleteCmd) Run

func (c DeleteCmd) Run() (int, error)

Run removes elements from a set according to the specified criteria (rank or score range). Returns the number of elements removed. Does nothing if the key does not exist or is not a set.

type InterCmd

type InterCmd struct {
	// contains filtered or unexported fields
}

InterCmd intersects multiple sets.

func (InterCmd) Dest

func (c InterCmd) Dest(dest string) InterCmd

Dest sets the key to store the result of the intersection.

func (InterCmd) Max

func (c InterCmd) Max() InterCmd

Max changes the aggregation function to take the maximum score.

func (InterCmd) Min

func (c InterCmd) Min() InterCmd

Min changes the aggregation function to take the minimum score.

func (InterCmd) Run

func (c InterCmd) Run() ([]SetItem, error)

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

func (c InterCmd) Store() (int, error)

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.

func (InterCmd) Sum

func (c InterCmd) Sum() InterCmd

Sum changes the aggregation function to take the sum of scores.

type RangeCmd

type RangeCmd struct {
	// contains filtered or unexported fields
}

RangeCmd retrieves a range of elements from a sorted set.

func (RangeCmd) Asc

func (c RangeCmd) Asc() RangeCmd

Asc sets the sorting direction to ascending.

func (RangeCmd) ByRank

func (c RangeCmd) ByRank(start, stop int) RangeCmd

ByRank sets filtering and sorting by rank. Start and stop are 0-based, inclusive. Negative values are not supported.

func (RangeCmd) ByScore

func (c RangeCmd) ByScore(start, stop float64) RangeCmd

ByScore sets filtering and sorting by score. Start and stop are inclusive.

func (RangeCmd) Count

func (c RangeCmd) Count(count int) RangeCmd

Count sets the maximum number of elements to return. Only takes effect when filtering by score.

func (RangeCmd) Desc

func (c RangeCmd) Desc() RangeCmd

Desc sets the sorting direction to descending.

func (RangeCmd) Offset

func (c RangeCmd) Offset(offset int) RangeCmd

Offset sets the offset of the range. Only takes effect when filtering by score.

func (RangeCmd) Run

func (c RangeCmd) Run() ([]SetItem, error)

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

type ScanResult struct {
	Cursor int
	Items  []SetItem
}

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.

func (*Scanner) Err

func (sc *Scanner) Err() error

Err returns the first error encountered during iteration.

func (*Scanner) Item

func (sc *Scanner) Item() SetItem

Item returns the current set item.

func (*Scanner) Scan

func (sc *Scanner) Scan() bool

Scan advances to the next item, fetching items from db as necessary. Returns false when there are no more items or an error occurs. Returns false if the key does not exist or is not a set.

type SetItem

type SetItem struct {
	Elem  core.Value
	Score float64
	// contains filtered or unexported fields
}

SetItem represents an element-score pair in a sorted set.

type Tx

type Tx struct {
	// contains filtered or unexported fields
}

Tx is a sorted set repository transaction.

func NewTx

func NewTx(tx sqlx.Tx) *Tx

NewTx creates a sorted set repository transaction from a generic database transaction.

func (*Tx) Add

func (tx *Tx) Add(key string, elem any, score float64) (bool, error)

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

func (tx *Tx) AddMany(key string, items map[any]float64) (int, error)

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

func (tx *Tx) Count(key string, min, max float64) (int, error)

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

func (tx *Tx) Delete(key string, elems ...any) (int, error)

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

func (tx *Tx) DeleteWith(key string) DeleteCmd

DeleteWith removes elements from a set with additional options.

func (*Tx) GetRank

func (tx *Tx) GetRank(key string, elem any) (rank int, score float64, err error)

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

func (tx *Tx) GetRankRev(key string, elem any) (rank int, score float64, err error)

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

func (tx *Tx) GetScore(key string, elem any) (float64, error)

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

func (tx *Tx) Incr(key string, elem any, delta float64) (float64, error)

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

func (tx *Tx) Inter(keys ...string) ([]SetItem, error)

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) InterWith

func (tx *Tx) InterWith(keys ...string) InterCmd

InterWith intersects multiple sets with additional options.

func (*Tx) Len

func (tx *Tx) Len(key string) (int, error)

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

func (tx *Tx) Range(key string, start, stop int) ([]SetItem, error)

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) RangeWith

func (tx *Tx) RangeWith(key string) RangeCmd

RangeWith ranges elements from a set with additional options.

func (*Tx) Scan

func (tx *Tx) Scan(key string, cursor int, pattern string, count int) (ScanResult, error)

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

func (tx *Tx) Scanner(key, pattern string, pageSize int) *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.

func (*Tx) Union

func (tx *Tx) Union(keys ...string) ([]SetItem, error)

Union 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 sum 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 (*Tx) UnionWith

func (tx *Tx) UnionWith(keys ...string) UnionCmd

UnionWith unions multiple sets with additional options.

type UnionCmd

type UnionCmd struct {
	// contains filtered or unexported fields
}

UnionCmd unions multiple sets.

func (UnionCmd) Dest

func (c UnionCmd) Dest(dest string) UnionCmd

Dest sets the key to store the result of the union.

func (UnionCmd) Max

func (c UnionCmd) Max() UnionCmd

Max changes the aggregation function to take the maximum score.

func (UnionCmd) Min

func (c UnionCmd) Min() UnionCmd

Min changes the aggregation function to take the minimum score.

func (UnionCmd) Run

func (c UnionCmd) Run() ([]SetItem, error)

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

func (c UnionCmd) Store() (int, error)

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.

func (UnionCmd) Sum

func (c UnionCmd) Sum() UnionCmd

Sum changes the aggregation function to take the sum of scores.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL