rhash

package
v0.5.5 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2024 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package rhash is a database-backed hash repository. It provides methods to interact with hashmaps 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 hash repository. A hash (hashmap) is a field-value map associated with a key. Use the hash repository to work with individual hashmaps and their fields.

func New

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

New connects to the hash repository. Does not create the database schema.

func (*DB) Delete

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

Delete deletes one or more items from a hash. Returns the number of fields deleted. Ignores non-existing fields. Does nothing if the key does not exist or is not a hash.

func (*DB) Exists

func (d *DB) Exists(key, field string) (bool, error)

Exists checks if a field exists in a hash. If the key does not exist or is not a hash, returns false.

func (*DB) Fields

func (d *DB) Fields(key string) ([]string, error)

Fields returns all fields in a hash. If the key does not exist or is not a hash, returns an empty slice.

func (*DB) Get

func (d *DB) Get(key, field string) (core.Value, error)

Get returns the value of a field in a hash. If the element does not exist, returns ErrNotFound. If the key does not exist or is not a hash, returns ErrNotFound.

func (*DB) GetMany

func (d *DB) GetMany(key string, fields ...string) (map[string]core.Value, error)

GetMany returns a map of values for given fields. Ignores fields that do not exist and do not return them in the map. If the key does not exist or is not a hash, returns an empty map.

func (*DB) Incr

func (d *DB) Incr(key, field string, delta int) (int, error)

Incr increments the integer value of a field in a hash. Returns the value after the increment. If the field does not exist, sets it to 0 before the increment. If the field value is not an integer, returns ErrValueType. If the key does not exist, creates it. If the key exists but is not a hash, returns ErrKeyType.

func (*DB) IncrFloat

func (d *DB) IncrFloat(key, field string, delta float64) (float64, error)

IncrFloat increments the float value of a field in a hash. Returns the value after the increment. If the field does not exist, sets it to 0 before the increment. If the field value is not a float, returns ErrValueType. If the key does not exist, creates it. If the key exists but is not a hash, returns ErrKeyType.

func (*DB) Items

func (d *DB) Items(key string) (map[string]core.Value, error)

Items returns a map of all fields and values in a hash. If the key does not exist or is not a hash, returns an empty map.

func (*DB) Len

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

Len returns the number of fields in a hash. If the key does not exist or is not a hash, returns 0.

func (*DB) Scan

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

Scan iterates over hash items with fields matching pattern. Returns a slice of field-value pairs (see HashItem) of size count based on the current state of the cursor. Returns an empty HashItem slice when there are no more items. If the key does not exist or is not a hash, 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 hash items with fields 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 hash, stops immediately. Supports glob-style patterns. Set pageSize = 0 for default page size.

func (*DB) Set

func (d *DB) Set(key, field string, value any) (bool, error)

Set creates or updates the value of a field in a hash. Returns true if the field was created, false if it was updated. If the key does not exist, creates it. If the key exists but is not a hash, returns ErrKeyType.

func (*DB) SetMany

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

SetMany creates or updates the values of multiple fields in a hash. Returns the number of fields created (as opposed to updated). If the key does not exist, creates it. If the key exists but is not a hash, returns ErrKeyType.

func (*DB) SetNotExists

func (d *DB) SetNotExists(key, field string, value any) (bool, error)

SetNotExists creates the value of a field in a hash if it does not exist. Returns true if the field was created, false if it already exists. If the key does not exist, creates it. If the key exists but is not a hash, returns ErrKeyType.

func (*DB) Values

func (d *DB) Values(key string) ([]core.Value, error)

Values returns all values in a hash. If the key does not exist or is not a hash, returns an empty slice.

type HashItem

type HashItem struct {
	Field string
	Value core.Value
	// contains filtered or unexported fields
}

HashItem represents an item in a hash.

type ScanResult

type ScanResult struct {
	Cursor int
	Items  []HashItem
}

ScanResult represents a result of the scan command.

type Scanner

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

Scanner is the iterator for hash 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() HashItem

Item returns the current hash 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 hash.

type Tx

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

Tx is a hash repository transaction.

func NewTx

func NewTx(tx sqlx.Tx) *Tx

NewTx creates a hash repository transaction from a generic database transaction.

func (*Tx) Delete

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

Delete deletes one or more items from a hash. Returns the number of fields deleted. Ignores non-existing fields. Does nothing if the key does not exist or is not a hash.

func (*Tx) Exists

func (tx *Tx) Exists(key, field string) (bool, error)

Exists checks if a field exists in a hash. If the key does not exist or is not a hash, returns false.

func (*Tx) Fields

func (tx *Tx) Fields(key string) ([]string, error)

Fields returns all fields in a hash. If the key does not exist or is not a hash, returns an empty slice.

func (*Tx) Get

func (tx *Tx) Get(key, field string) (core.Value, error)

Get returns the value of a field in a hash. If the element does not exist, returns ErrNotFound. If the key does not exist or is not a hash, returns ErrNotFound.

func (*Tx) GetMany

func (tx *Tx) GetMany(key string, fields ...string) (map[string]core.Value, error)

GetMany returns a map of values for given fields. Ignores fields that do not exist and do not return them in the map. If the key does not exist or is not a hash, returns an empty map.

func (*Tx) Incr

func (tx *Tx) Incr(key, field string, delta int) (int, error)

Incr increments the integer value of a field in a hash. Returns the value after the increment. If the field does not exist, sets it to 0 before the increment. If the field value is not an integer, returns ErrValueType. If the key does not exist, creates it. If the key exists but is not a hash, returns ErrKeyType.

func (*Tx) IncrFloat

func (tx *Tx) IncrFloat(key, field string, delta float64) (float64, error)

IncrFloat increments the float value of a field in a hash. Returns the value after the increment. If the field does not exist, sets it to 0 before the increment. If the field value is not a float, returns ErrValueType. If the key does not exist, creates it. If the key exists but is not a hash, returns ErrKeyType.

func (*Tx) Items

func (tx *Tx) Items(key string) (map[string]core.Value, error)

Items returns a map of all fields and values in a hash. If the key does not exist or is not a hash, returns an empty map.

func (*Tx) Len

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

Len returns the number of fields in a hash. If the key does not exist or is not a hash, returns 0.

func (*Tx) Scan

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

Scan iterates over hash items with fields matching pattern. Returns a slice of field-value pairs (see HashItem) of size count based on the current state of the cursor. Returns an empty HashItem slice when there are no more items. If the key does not exist or is not a hash, 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 hash items with fields 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 hash, stops immediately. Supports glob-style patterns. Set pageSize = 0 for default page size.

func (*Tx) Set

func (tx *Tx) Set(key string, field string, value any) (bool, error)

Set creates or updates the value of a field in a hash. Returns true if the field was created, false if it was updated. If the key does not exist, creates it. If the key exists but is not a hash, returns ErrKeyType.

func (*Tx) SetMany

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

SetMany creates or updates the values of multiple fields in a hash. Returns the number of fields created (as opposed to updated). If the key does not exist, creates it. If the key exists but is not a hash, returns ErrKeyType.

func (*Tx) SetNotExists

func (tx *Tx) SetNotExists(key, field string, value any) (bool, error)

SetNotExists creates the value of a field in a hash if it does not exist. Returns true if the field was created, false if it already exists. If the key does not exist, creates it. If the key exists but is not a hash, returns ErrKeyType.

func (*Tx) Values

func (tx *Tx) Values(key string) ([]core.Value, error)

Values returns all values in a hash. If the key does not exist or is not a hash, returns an empty slice.

Jump to

Keyboard shortcuts

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