mock

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

Documentation

Overview

The mock database package allows for testing that are decoupled from a particular database implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Database

type Database struct {
	// a map that associates each public key with a unique nodeID
	KeyIndex map[string]uint32

	// a map that associates each nodeID with its node data
	NodeIndex map[uint32]*models.Node

	// maps that associate each nodeID with the slice of its follows
	Follow   map[uint32]NodeSet
	Follower map[uint32]NodeSet

	// the next nodeID to be used. When a new node is added, this fiels is incremented by one
	LastNodeID int
}

simulates a simple graph database for testing.

func GenerateDB

func GenerateDB(nodesNum, successorsPerNode int, rng *rand.Rand) *Database

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() *Database

NewDatabase() creates and returns a new Database instance.

func SetupDB

func SetupDB(DBType string) *Database

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. In case of errors, it returns MaxUint32 as the nodeID.

func (*Database) AllNodes

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

All returns a slice with the IDs of all the nodes

func (*Database) ContainsNode

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

ContainsNode() returns whether nodeID is found in the DB

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 the slice of followers of each nodeID

func (*Database) Follows

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

Follows() returns the slice of follows of each nodeID

func (*Database) NodeByID

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

NodeByID() retrieves a node (Node) by its nodeID.

func (*Database) NodeByKey

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

NodeByKey() retrieves a node (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 pubkey 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 all of the nodeIDs, ignoring the limit.

func (*Database) Size

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

Size() returns the number of nodes in the DB (ignores errors).

func (*Database) Update

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

Update() applies the delta to nodeID.

func (*Database) Validate

func (DB *Database) Validate() error

Validate() returns an error if the DB is nil or has no nodes

type NodeSet

type NodeSet mapset.Set[uint32]

Jump to

Keyboard shortcuts

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