rlist

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: 5 Imported by: 0

Documentation

Overview

Package rlist is a database-backed list repository. It provides methods to interact with lists 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 list repository. A list is a sequence of strings ordered by insertion order. Use the list repository to work with lists and their elements.

func New

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

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

func (*DB) Delete

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

Delete deletes all occurrences of an element from a list. Returns the number of elements deleted. Does nothing if the key does not exist or is not a list.

func (*DB) DeleteBack

func (d *DB) DeleteBack(key string, elem any, count int) (int, error)

DeleteBack deletes the first count occurrences of an element from a list, starting from the back. Count must be positive. Returns the number of elements deleted. Does nothing if the key does not exist or is not a list.

func (*DB) DeleteFront

func (d *DB) DeleteFront(key string, elem any, count int) (int, error)

DeleteFront deletes the first count occurrences of an element from a list, starting from the front. Count must be positive. Returns the number of elements deleted. Does nothing if the key does not exist or is not a list.

func (*DB) Get

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

Get returns an element from a list by index (0-based). Negative index count from the end of the list (-1 is the last element, -2 is the second last, etc.) If the index is out of bounds, returns ErrNotFound. If the key does not exist or is not a list, returns ErrNotFound.

func (*DB) InsertAfter

func (d *DB) InsertAfter(key string, pivot, elem any) (int, error)

InsertAfter inserts an element after another element (pivot). Returns the length of the list after the operation. If the pivot does not exist, returns (-1, ErrNotFound). If the key does not exist or is not a list, returns (0, ErrNotFound).

func (*DB) InsertBefore

func (d *DB) InsertBefore(key string, pivot, elem any) (int, error)

InsertBefore inserts an element before another element (pivot). Returns the length of the list after the operation. If the pivot does not exist, returns (-1, ErrNotFound). If the key does not exist or is not a list, returns (0, ErrNotFound).

func (*DB) Len

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

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

func (*DB) PopBack

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

PopBack removes and returns the last element of a list. If the key does not exist or is not a list, returns ErrNotFound.

func (*DB) PopBackPushFront

func (d *DB) PopBackPushFront(src, dest string) (core.Value, error)

PopBackPushFront removes the last element of a list and prepends it to another list (or the same list). If the source key does not exist or is not a list, returns ErrNotFound.

func (*DB) PopFront

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

PopFront removes and returns the first element of a list. If the key does not exist or is not a list, returns ErrNotFound.

func (*DB) PushBack

func (d *DB) PushBack(key string, elem any) (int, error)

PushBack appends an element to a list. Returns the length of the list after the operation. If the key does not exist, creates it. If the key exists but is not a list, returns ErrKeyType.

func (*DB) PushFront

func (d *DB) PushFront(key string, elem any) (int, error)

PushFront prepends an element to a list. Returns the length of the list after the operation. If the key does not exist, creates it. If the key exists but is not a list, returns ErrKeyType.

func (*DB) Range

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

Range returns a range of elements from a list. Both start and stop are zero-based, inclusive. Negative indexes count from the end of the list (-1 is the last element, -2 is the second last, etc.) If the key does not exist or is not a list, returns an empty slice.

func (*DB) Set

func (d *DB) Set(key string, idx int, elem any) error

Set sets an element in a list by index (0-based). Negative index count from the end of the list (-1 is the last element, -2 is the second last, etc.) If the index is out of bounds, returns ErrNotFound. If the key does not exist or is not a list, returns ErrNotFound.

func (*DB) Trim

func (d *DB) Trim(key string, start, stop int) (int, error)

Trim removes elements from both ends of a list so that only the elements between start and stop indexes remain. Returns the number of elements removed.

Both start and stop are zero-based, inclusive. Negative indexes count from the end of the list (-1 is the last element, -2 is the second last, etc.)

Does nothing if the key does not exist or is not a list.

type Tx

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

Tx is a list repository transaction.

func NewTx

func NewTx(tx sqlx.Tx) *Tx

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

func (*Tx) Delete

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

Delete deletes all occurrences of an element from a list. Returns the number of elements deleted. Does nothing if the key does not exist or is not a list.

func (*Tx) DeleteBack

func (tx *Tx) DeleteBack(key string, elem any, count int) (int, error)

DeleteBack deletes the first count occurrences of an element from a list, starting from the back. Count must be positive. Returns the number of elements deleted. Does nothing if the key does not exist or is not a list.

func (*Tx) DeleteFront

func (tx *Tx) DeleteFront(key string, elem any, count int) (int, error)

DeleteFront deletes the first count occurrences of an element from a list, starting from the front. Count must be positive. Returns the number of elements deleted. Does nothing if the key does not exist or is not a list.

func (*Tx) Get

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

Get returns an element from a list by index (0-based). Negative index count from the end of the list (-1 is the last element, -2 is the second last, etc.) If the index is out of bounds, returns ErrNotFound. If the key does not exist or is not a list, returns ErrNotFound.

func (*Tx) InsertAfter

func (tx *Tx) InsertAfter(key string, pivot, elem any) (int, error)

InsertAfter inserts an element after another element (pivot). Returns the length of the list after the operation. If the pivot does not exist, returns (-1, ErrNotFound). If the key does not exist or is not a list, returns (0, ErrNotFound).

func (*Tx) InsertBefore

func (tx *Tx) InsertBefore(key string, pivot, elem any) (int, error)

InsertBefore inserts an element before another element (pivot). Returns the length of the list after the operation. If the pivot does not exist, returns (-1, ErrNotFound). If the key does not exist or is not a list, returns (0, ErrNotFound).

func (*Tx) Len

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

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

func (*Tx) PopBack

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

PopBack removes and returns the last element of a list. If the key does not exist or is not a list, returns ErrNotFound.

func (*Tx) PopBackPushFront

func (tx *Tx) PopBackPushFront(src, dest string) (core.Value, error)

PopBackPushFront removes the last element of a list and prepends it to another list (or the same list). If the source key does not exist or is not a list, returns ErrNotFound.

func (*Tx) PopFront

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

PopFront removes and returns the first element of a list. If the key does not exist or is not a list, returns ErrNotFound.

func (*Tx) PushBack

func (tx *Tx) PushBack(key string, elem any) (int, error)

PushBack appends an element to a list. Returns the length of the list after the operation. If the key does not exist, creates it. If the key exists but is not a list, returns ErrKeyType.

func (*Tx) PushFront

func (tx *Tx) PushFront(key string, elem any) (int, error)

PushFront prepends an element to a list. Returns the length of the list after the operation. If the key does not exist, creates it. If the key exists but is not a list, returns ErrKeyType.

func (*Tx) Range

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

Range returns a range of elements from a list. Both start and stop are zero-based, inclusive. Negative indexes count from the end of the list (-1 is the last element, -2 is the second last, etc.) If the key does not exist or is not a list, returns an empty slice.

func (*Tx) Set

func (tx *Tx) Set(key string, idx int, elem any) error

Set sets an element in a list by index (0-based). Negative index count from the end of the list (-1 is the last element, -2 is the second last, etc.) If the index is out of bounds, returns ErrNotFound. If the key does not exist or is not a list, returns ErrNotFound.

func (*Tx) Trim

func (tx *Tx) Trim(key string, start, stop int) (int, error)

Trim removes elements from both ends of a list so that only the elements between start and stop indexes remain. Returns the number of elements removed.

Both start and stop are zero-based, inclusive. Negative indexes count from the end of the list (-1 is the last element, -2 is the second last, etc.)

Does nothing if the key does not exist or is not a list.

Jump to

Keyboard shortcuts

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