postgres

package
v0.0.0-...-4a05471 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2024 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Overview

Package postgres is an implementation of a routes and neighbors store using the postgres database.

Routes and neighbors are stored as jsonb information with indexes for query performance.

Index

Constants

View Source
const CurrentSchemaVersion = 1

CurrentSchemaVersion is the current version of the schema

Variables

View Source
var (
	// ErrMaxConnsUnconfigured will be returned, if the
	// the maximum connections are zero.
	ErrMaxConnsUnconfigured = errors.New("max connections not configured")
)
View Source
var (
	// ReMatchNonChar will match on all characters not
	// within a to Z or 0 to 9
	ReMatchNonChar = regexp.MustCompile(`[^a-zA-Z0-9]`)
)

Functions

func Connect

func Connect(ctx context.Context, opts *config.PostgresConfig) (*pgxpool.Pool, error)

Connect creates and configures a pgx pool

Types

type Manager

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

The Manager supervises the database. It can migrate the schema and retrieve a status.

func NewManager

func NewManager(pool *pgxpool.Pool) *Manager

NewManager creates a new database manager

func (*Manager) Initialize

func (m *Manager) Initialize(ctx context.Context) error

Initialize will apply the database schema. This will clear the database. However for now we treat the state as disposable.

func (*Manager) Migrate

func (m *Manager) Migrate(ctx context.Context) error

Migrate applies the database intialisation script if required.

func (*Manager) Start

func (m *Manager) Start(ctx context.Context)

Start the background jobs for database management

func (*Manager) Status

func (m *Manager) Status(ctx context.Context) *Status

Status retrieves the current schema version and checks if migrated. In case an error occures, it will be included in the result.

type NeighborsBackend

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

NeighborsBackend implements a neighbors store using a postgres database

func NewNeighborsBackend

func NewNeighborsBackend(pool *pgxpool.Pool) *NeighborsBackend

NewNeighborsBackend initializes the backend with a pool.

func (*NeighborsBackend) CountNeighborsAt

func (b *NeighborsBackend) CountNeighborsAt(
	ctx context.Context,
	sourceID string,
) (int, error)

CountNeighborsAt retrieves the current number of stored neighbors.

func (*NeighborsBackend) GetNeighborsAt

func (b *NeighborsBackend) GetNeighborsAt(
	ctx context.Context,
	sourceID string,
) (api.Neighbors, error)

GetNeighborsAt retrieves all neighbors associated with a route server (source).

func (*NeighborsBackend) GetNeighborsMapAt

func (b *NeighborsBackend) GetNeighborsMapAt(
	ctx context.Context,
	sourceID string,
) (map[string]*api.Neighbor, error)

GetNeighborsMapAt retrieve a neighbor map for a route server. The Neighbor is identified by ID.

func (*NeighborsBackend) SetNeighbors

func (b *NeighborsBackend) SetNeighbors(
	ctx context.Context,
	sourceID string,
	neighbors api.Neighbors,
) error

SetNeighbors updates the current neighbors of a route server identified by sourceID

type RoutesBackend

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

RoutesBackend implements a postgres store for routes.

func NewRoutesBackend

func NewRoutesBackend(
	pool *pgxpool.Pool,
	sources []*config.SourceConfig,
) *RoutesBackend

NewRoutesBackend creates a new instance with a postgres connection pool.

func (*RoutesBackend) CountRoutesAt

func (b *RoutesBackend) CountRoutesAt(
	ctx context.Context,
	sourceID string,
) (uint, uint, error)

CountRoutesAt returns the number of filtered and imported routes and implements the RoutesStoreBackend interface.

func (*RoutesBackend) FindByNeighbors

func (b *RoutesBackend) FindByNeighbors(
	ctx context.Context,
	neighbors []*api.NeighborQuery,
	filters *api.SearchFilters,
) (api.LookupRoutes, error)

FindByNeighbors will return the prefixes for a list of neighbors identified by ID.

func (*RoutesBackend) FindByPrefix

func (b *RoutesBackend) FindByPrefix(
	ctx context.Context,
	prefix string,
	filters *api.SearchFilters,
	limit uint,
) (api.LookupRoutes, error)

FindByPrefix will return the prefixes matching a pattern

func (*RoutesBackend) Init

func (b *RoutesBackend) Init(ctx context.Context) error

Init will initialize all the route tables

func (*RoutesBackend) SetRoutes

func (b *RoutesBackend) SetRoutes(
	ctx context.Context,
	sourceID string,
	routes api.LookupRoutes,
) error

SetRoutes implements the RoutesStoreBackend interface function for setting all routes of a source identified by ID.

type Status

type Status struct {
	Migrated        bool      `json:"migrated"`
	SchemaVersion   int       `json:"schema_version"`
	SchemaAppliedAt time.Time `json:"schema_applied_at"`
	Error           error     `json:"error"`
}

Status is the database / store status

func (*Status) Log

func (s *Status) Log()

Log writes the status into the log

Jump to

Keyboard shortcuts

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