pgclient

package
v0.0.0-...-7d323fe Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

pgclient.go - postgres backend client

TODO: make the client delete expired stuff... but when? 1. on demand through a DeleteExpired call 2. On a ticker calling the same every N

tricky part seems to be wait for execution when at end of program, want to always wait on the single exec

easy to prove?

Index

Constants

This section is empty.

Variables

View Source
var DatabaseUrlEnvVar = "DATABASE_URL"

Override if using multiple databases in your process.

View Source
var DefaultTable = "obj_store"
View Source
var DeleteTestingHackKeyWord = "DELETE"

This is a really annoying hack, but it saves us the trouble of making a pgxpool mock just for this package:

View Source
var ErrNotFound = pgx.ErrNoRows

Functions

This section is empty.

Types

type PgClient

type PgClient struct {
	Pool            *pgxpool.Pool
	Table           string
	PurgeOnShutdown bool
}

PgClient is a BackendClient for PostgreSQL databases.

func New

func New() (*PgClient, error)

New returns a new PgClient using DefaultTable and a pool from DatabaseUrlEnvVar, with PurgeOnShutdown true.

func NewForPool

func NewForPool(pool *pgxpool.Pool) *PgClient

NewForPool returns a new PgClient with the provided Pool, with defaults as in New.

func (*PgClient) Count

func (c *PgClient) Count(prefix string) (int, error)

Count returns the number of non-expired objects beginning with prefix. If none are found, zero is returned.

func (*PgClient) CountAll

func (c *PgClient) CountAll() (int, error)

CountAll returns the total number of non-expired objects in the database.

func (*PgClient) CreateTable

func (c *PgClient) CreateTable() error

CreateTable executes the SQL returned from Schema on the current database. If the table exists an error is returned. For obvious reasons there is no corresponding DropTable function.

func (*PgClient) Delete

func (c *PgClient) Delete(path string) error

Delete deletes the object at path.

For security reasons, if the object is already expired it will still be deleted. (It would be possible to DELETE RETURNING and thus check the expiry and return a different error if the caller deletes an expired object, but that use-case seems silly. You want it gone, we make it gone.)

If the object does not exist, the error returned will be ErrNotFound.

func (*PgClient) List

func (c *PgClient) List(prefix string) ([]string, error)

List returns an array of all objects beginning with prefix. An empty array is not considered an error.

func (*PgClient) ListDetail

func (c *PgClient) ListDetail(prefix string) ([]backend.Detailer, error)

ListDetail returns an array of all Detailers describing all objects beginning with prefix. An empty array is not considered an error.

For S3, this operation may be slow if paged results are returned!

func (*PgClient) LoadDetail

func (c *PgClient) LoadDetail(path string) (backend.Detailer, error)

LoadDetail retrieves the details of the object at path and returns its a jsobs.Detailer. If the object does not exist, the error returned will be ErrNotFound.

func (*PgClient) LoadRaw

func (c *PgClient) LoadRaw(path string) ([]byte, error)

LoadRaw retrieves the object at path and returns its raw value. If the object does not exist, the error returned will be ErrNotFound.

func (*PgClient) Purge

func (c *PgClient) Purge() (int, error)

Purge deletes expired items from the database. Returns the number of rows deleted.

func (*PgClient) SaveRaw

func (c *PgClient) SaveRaw(path string, raw_obj []byte) error

SaveRaw saves the raw_obj to the database with no expiry.

func (*PgClient) SaveRawExpiry

func (c *PgClient) SaveRawExpiry(path string, raw_obj []byte, expiry time.Time) error

SaveRawExpiry saves the raw bytes to the database for availability until expiry.

func (*PgClient) Schema

func (c *PgClient) Schema() string

Schema returns the SQL required to create this client's Table.

func (*PgClient) Shutdown

func (c *PgClient) Shutdown() error

Shutdown calls Purge if PurgeOnShutdown is true.

Note that it *should* be safe to kill the client in mid-purge, but you presumably want to purge at least once per run.

func (*PgClient) String

func (c *PgClient) String() string

String returns an identifying string.

type PgDetailer

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

PgDetailer implements backend.Detailer to describe an object.

func (*PgDetailer) Expires

func (d *PgDetailer) Expires() bool

Expires implements backend.Detailer.

func (*PgDetailer) Expiry

func (d *PgDetailer) Expiry() time.Time

Expiry implements backend.Detailer. If Expires returns false then Expiry must be ignored.

func (*PgDetailer) Modified

func (d *PgDetailer) Modified() time.Time

Modified implements backend.Detailer.

func (*PgDetailer) Path

func (d *PgDetailer) Path() string

Path implements backend.Detailer.

func (*PgDetailer) Scan

func (d *PgDetailer) Scan(row pgx.Row) error

Scan scans a database row. TODO: figure out why this works inversely from the documentation, is it a bug here, or there, or what?

func (*PgDetailer) Size

func (d *PgDetailer) Size() int

Size implements backend.Detailer.

Jump to

Keyboard shortcuts

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