redisdb

package
v0.0.0-...-744dbeb Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2025 License: MIT Imports: 12 Imported by: 1

Documentation

Overview

The redisdb package defines a Redis database that fulfills the Database interface in models.

Index

Constants

View Source
const (
	// redis variable names
	KeyDatabase        string = "database"
	KeyLastNodeID      string = "lastNodeID"
	KeyKeyIndex        string = "keyIndex"
	KeyNodePrefix      string = "node:"
	KeyFollowsPrefix   string = "follows:"
	KeyFollowersPrefix string = "followers:"

	// redis node HASH fields
	NodeID          string = "id"
	NodePubkey      string = "pubkey"
	NodeStatus      string = "status"
	NodePromotionTS string = "promotion_TS"
	NodeDemotionTS  string = "demotion_TS"
	NodeAddedTS     string = "added_TS"
)

Variables

View Source
var ErrNilClient = errors.New("nil redis client pointer")

Functions

func KeyFollowers

func KeyFollowers[ID uint32 | int64 | int](nodeID ID) string

KeyFollowers() returns the Redis key for the followers of the specified nodeID

func KeyFollows

func KeyFollows[ID uint32 | int64 | int](nodeID ID) string

KeyFollows() returns the Redis key for the follows of the specified nodeID

func KeyNode

func KeyNode[ID uint32 | int64 | int](nodeID ID) string

KeyNode() returns the Redis key for the node with specified nodeID.

func ParseNode

func ParseNode(nodeMap map[string]string) (*models.Node, error)

ParseNode() parses the map into a node structure, with only one Record of type Follow.

Types

type Database

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

Database fulfills the Database interface defined in models

func GenerateDB

func GenerateDB(cl *redis.Client, nodesNum, successorsPerNode int, rng *rand.Rand) (*Database, error)

generates a random mock database of a specified number of nodes and successors per node the successor of a node won't include itself, and won't have repetitions

func NewDatabase

func NewDatabase(ctx context.Context, cl *redis.Client) (*Database, error)

NewDatabase() creates and returns a new Database instance.

func NewDatabaseConnection

func NewDatabaseConnection(ctx context.Context, cl *redis.Client) (*Database, error)

NewDatabaseConnection() returns an initialized Database

func NewDatabaseFromPubkeys

func NewDatabaseFromPubkeys(ctx context.Context, cl *redis.Client, pubkeys []string) (*Database, error)

NewDatabaseFromPubkeys() returns an initialized database storing the specified pubkeys.

func SetupDB

func SetupDB(cl *redis.Client, DBType string) (*Database, error)

function that returns a DB setup based on the DBType

func (*Database) AddNode

func (DB *Database) AddNode(ctx context.Context, pubkey string) (uint32, error)

AddNode() adds a node to the database and returns its assigned nodeID.

func (*Database) AllNodes

func (DB *Database) AllNodes(ctx context.Context) ([]uint32, error)

AllNodes() returns a slice with the IDs of all nodes in the DB

func (*Database) ContainsNode

func (DB *Database) ContainsNode(ctx context.Context, nodeID uint32) bool

ContainsNode() returns wheter the DB contains nodeID. In case of errors returns false.

func (*Database) FollowCounts

func (DB *Database) FollowCounts(ctx context.Context, nodeIDs ...uint32) ([]int, error)

func (*Database) FollowerCounts

func (DB *Database) FollowerCounts(ctx context.Context, nodeIDs ...uint32) ([]int, error)

func (*Database) Followers

func (DB *Database) Followers(ctx context.Context, nodeIDs ...uint32) ([][]uint32, error)

Followers() returns a slice containing the follows of each of the specified nodeIDs.

func (*Database) Follows

func (DB *Database) Follows(ctx context.Context, nodeIDs ...uint32) ([][]uint32, error)

Follows() returns a slice containing the follows of each of the specified nodeIDs.

func (*Database) NodeByID

func (DB *Database) NodeByID(ctx context.Context, nodeID uint32) (*models.Node, error)

NodeByID() retrieves a node by its nodeID.

func (*Database) NodeByKey

func (DB *Database) NodeByKey(ctx context.Context, pubkey string) (*models.Node, error)

NodeByKey() retrieves a node by its pubkey.

func (*Database) NodeIDs

func (DB *Database) NodeIDs(ctx context.Context, pubkeys ...string) ([]*uint32, error)

NodeIDs() returns a slice of nodeIDs that correspond with the given slice of pubkeys. If a pubkey is not found, nil is returned

func (*Database) Pubkeys

func (DB *Database) Pubkeys(ctx context.Context, nodeIDs ...uint32) ([]*string, error)

Pubkeys() returns a slice of pubkeys that correspond with the given slice of nodeIDs. If a nodeID is not found, nil is returned

func (*Database) ScanNodes

func (DB *Database) ScanNodes(ctx context.Context, cursor uint64, limit int) ([]uint32, uint64, error)

ScanNodes() scans over the nodes and returns a batch of nodeIDs of size roughly equal to limit. Limit controls how much "work" is invested in fetching the batch, hence it's not precise in determining the number of nodes returned.

func (*Database) Size

func (DB *Database) Size(ctx context.Context) int

Size() returns the number of nodes in the DB. In case of errors, it returns 0.

func (*Database) Update

func (DB *Database) Update(ctx context.Context, delta *models.Delta) error

UpdateNode() updates the nodeID using the new values inside the nodeDiff.

func (*Database) Validate

func (DB *Database) Validate() error

Validate() check if DB and client are nil and returns the appropriare error

type DatabaseFields

type DatabaseFields struct {
	LastNodeID int `redis:"lastNodeID"`
}

DatabaseFields are the fields of the Database in Redis. This struct is used for serialize and deserialize.

Jump to

Keyboard shortcuts

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