db

package
v0.0.0-...-c490ea4 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2022 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package db contains the storage layer for the application.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrShortConflict means that a conflicting short-name was chosen.
	ErrShortConflict = errors.New("the requested short link name already exists")
)

Functions

This section is empty.

Types

type Click

type Click struct {
	Link    *Link
	Request *http.Request
}

A Click is generated whenever we redirect a caller.

type ClickReport

type ClickReport struct {
	Destination string
	Short       string
	Time        time.Time
	UUID        uuid.UUID
}

A ClickReport can be downloaded.

type ETag

type ETag string

ETag is an opaque value used to track changes in the links table.

const EmptyETag ETag = "<<empty>>"

EmptyETag will be returned when there is no link data.

type GlobalStats

type GlobalStats struct {
	Clicks int
	Links  int
}

GlobalStats summarizes the total usage.

type Link struct {
	Author    string    // Only the owner can update
	CreatedAt time.Time // Creation time
	Comment   string    // A user-defined comment
	Count     int       // Number of times clicked
	Listed    bool      // Appears on the front page
	Public    bool      // Accessible from outside
	Short     string    // The unique short-link value
	UpdatedAt time.Time // Last-updated time
	URL       string    // A well-formed URL
}

Link is a shortened link.

func (*Link) Validate

func (l *Link) Validate() error

Validate returns nil if the link is well-formed for storage.

type PublishOpt

type PublishOpt int

PublishOpt can be passed to Store.Publish to alter the default behaviors.

const (
	// AllowNewAuthor instructs Store.Publish to allow a link to be
	// taken over by an author other than the one that created it.
	AllowNewAuthor PublishOpt = iota
)

type Store

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

Store provides access to the short-link data.

func New

func New(ctx context.Context, conn string) (*Store, error)

New creates a new Store.

func (*Store) Click

func (s *Store) Click(ctx context.Context, c *Click) error

Click records a click on a short link.

func (*Store) ClickCount

func (s *Store) ClickCount(ctx context.Context, short string) (int, error)

ClickCount returns the number of times that the link has been clicked.

func (*Store) ClickReport

func (s *Store) ClickReport(ctx context.Context, short string) (<-chan *ClickReport, error)

ClickReport returns the recorded click information for a specific link.

func (*Store) ClickReportListed

func (s *Store) ClickReportListed(ctx context.Context) (<-chan *ClickReport, error)

ClickReportListed returns click information for all listed links.

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, short, author string) error

Delete removes the given short link if it is owned by author.

func (*Store) ETag

func (s *Store) ETag(ctx context.Context) (ETag, error)

ETag returns a last-modified value for the collection of links.

func (*Store) Get

func (s *Store) Get(ctx context.Context, short string) (*Link, error)

Get locates a short link in the database, or returns nil if one does not exist.

func (*Store) List

func (s *Store) List(ctx context.Context, author string) (<-chan *Link, error)

List returns the Links that were created by the given author.

func (*Store) ListAll

func (s *Store) ListAll(ctx context.Context) (<-chan *Link, error)

ListAll returns all Links in the database.

func (*Store) Listed

func (s *Store) Listed(ctx context.Context, limit int) (<-chan *Link, error)

Listed returns CRL-visible links.

func (*Store) Ping

func (s *Store) Ping(ctx context.Context) error

Ping checks the database connection.

func (*Store) Publish

func (s *Store) Publish(ctx context.Context, l *Link, opts ...PublishOpt) (_ *Link, err error)

Publish stores or updates the link in the database. This function returns the latest value in the database.

func (*Store) Served

func (s *Store) Served(ctx context.Context) (*GlobalStats, error)

Served returns global statistics.

func (*Store) WithTransaction

func (s *Store) WithTransaction(parent context.Context) (context.Context, *Tx, error)

WithTransaction returns a new context that represents a database transaction. If the given context already contains a transaction, this method will return it. The transaction will be automatically rolled back if the

type Tx

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

Tx is a handle to an underlying database transaction.

func (*Tx) Commit

func (t *Tx) Commit() error

Commit will commit the underlying database transaction. This method will return an error if the transaction has already been closed.

func (*Tx) Rollback

func (t *Tx) Rollback()

Rollback will abort the underlying database transaction. This method is a no-op if the transaction has already been committed.

type ValidationError

type ValidationError string

ValidationError is returned from Link.Validate().

func (ValidationError) Error

func (v ValidationError) Error() string

Jump to

Keyboard shortcuts

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