storage

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2021 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrShortenedNotFound indicates the given shortened URL was not found in the underlying storage.
	ErrShortenedNotFound = errors.New("the shortened URL was not found")

	// ErrShortenedExists indicates that an attempt was made to add a shortened URL that already existed.
	ErrShortenedExists = errors.New("the shortened URL already exists")
)
View Source
var (

	// ErrNotWriteOperation indicates that the given input string was not a known write operation.
	ErrNotWriteOperation = errors.New("the input string was not a known write operation")
)

Functions

This section is empty.

Types

type AuthorizationStore added in v0.1.0

type AuthorizationStore interface {

	// AuthorizedShortened creates a map of users to the shortened URLs they are authorized for.
	AuthorizedShortened(ctx context.Context, users []*models.Principal) (map[*models.Principal][]string, error)

	// Close closes the connection to the underlying storage.
	Close(ctx context.Context) (err error)

	// Delete deletes the authorization information for the given shortened URLs. If shortenedURLs is nil or empty, all
	// Authorization data are deleted. No error should be returned if a shortened URL is not found.
	Delete(ctx context.Context, shortenedURLs []string) (err error)
}

AuthorizationStore is the Authorization data storage interface. It allows for Authorization data storage operations without needing to know of the Authorization data are stored.

type BboltTerse

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

BboltTerse is a TerseStore implementation that relies on a bbolt file for the backend storage.

func (BboltTerse) BucketName added in v0.0.3

func (b BboltTerse) BucketName() (bucketName []byte)

BucketName returns the name of the bbolt bucket.

func (BboltTerse) Close

func (b BboltTerse) Close(_ context.Context) (err error)

Close closes the connection to the underlying storage.

func (BboltTerse) DB added in v0.0.3

func (b BboltTerse) DB() (db *bbolt.DB)

DB returns the bbolt database.

func (BboltTerse) Delete

func (b BboltTerse) Delete(_ context.Context, shortenedURLs []string) (err error)

Delete deletes the Terse data for the given shortened URLs. If shortenedURLs is nil or empty, all shortened URL Terse data are deleted. There should be no error if a shortened URL is not found.

func (BboltTerse) Read

func (b BboltTerse) Read(_ context.Context, shortenedURLs []string) (terseData map[string]*models.Terse, err error)

Read returns a map of shortened URLs to Terse data. If shortenedURLs is nil or empty, all shortened URL Terse data are expected. The error must be storage.ErrShortenedNotFound if a shortened URL is not found.

func (BboltTerse) Summary added in v0.0.3

func (b BboltTerse) Summary(_ context.Context, shortenedURLs []string) (summaries map[string]*models.TerseSummary, err error)

Summary summarizes the Terse data for the given shortened URLs. If shortenedURLs is nil or empty, then all shortened URL Summary data are expected.

func (BboltTerse) Write added in v0.0.3

func (b BboltTerse) Write(_ context.Context, terseData map[string]*models.Terse, operation WriteOperation) (err error)

Write writes the given Terse data according to the given operation. The error must be storage.ErrShortenedExists if an Insert operation cannot be performed due to the Terse data already existing. The error must be storage.ErrShortenedNotFound if an Update operation cannot be performed due to the Terse data not existing.

type BboltVisits

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

BboltVisits if a VisitsStore implementation that relies on a bbolt file for the backend storage.

func (BboltVisits) BucketName added in v0.0.3

func (b BboltVisits) BucketName() (bucketName []byte)

BucketName returns the name of the bbolt bucket.

func (BboltVisits) Close

func (b BboltVisits) Close(_ context.Context) (err error)

Close closes the connection to the underlying storage.

func (BboltVisits) DB added in v0.0.3

func (b BboltVisits) DB() (db *bbolt.DB)

DB returns the bbolt database.

func (BboltVisits) Delete

func (b BboltVisits) Delete(_ context.Context, shortenedURLs []string) (err error)

Delete deletes Visits data for the given shortened URLs. If shortenedURLs is nil or empty, then all Visits data are deleted. No error should be given if a shortened URL is not found.

func (BboltVisits) Insert added in v0.0.3

func (b BboltVisits) Insert(_ context.Context, visitsData map[string][]models.Visit) (err error)

Insert inserts the given Visits data. The visits do not need to be unique, so the Visits data should be appended to the data structure in storage.

func (BboltVisits) Read added in v0.0.3

func (b BboltVisits) Read(_ context.Context, shortenedURLs []string) (visitsData map[string][]models.Visit, err error)

Read exports the Visits data for the given shortened URLs. If shortenedURLs is nil or empty, then all shortened URL Visits data are expected. The error must be storage.ErrShortenedNotFound if a shortened URL is not found.

func (BboltVisits) Summary added in v0.0.3

func (b BboltVisits) Summary(_ context.Context, shortenedURLs []string) (summaries map[string]*models.VisitsSummary, err error)

Summary summarizes the Visits data for the given shortened URLs. If shortenedURLs is nil or empty, then all shortened URL Summary data are expected. The error must be storage.ErrShortenedNotFound if a shortened URL is not found.

type CtxCreator added in v0.1.0

type CtxCreator func() (ctx context.Context, cancel context.CancelFunc)

CtxCreator is a function signature that creates a context and its cancel function.

type MemSummary added in v0.0.3

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

MemSummary is a SummaryStore implementation that stores all data in a Go map in memory.

func (*MemSummary) Close added in v0.0.3

func (m *MemSummary) Close(_ context.Context) (err error)

Close closes the connection to the underlying storage.

func (*MemSummary) Delete added in v0.0.3

func (m *MemSummary) Delete(_ context.Context, shortenedURLs []string) (err error)

Delete deletes the summary information for the given shortened URLs. If shortenedURLs is nil or empty, all Summary data are deleted. No error should be returned if a shortened URL is not found.

func (*MemSummary) IncrementVisitCount added in v0.0.3

func (m *MemSummary) IncrementVisitCount(_ context.Context, shortened string) (err error)

IncrementVisitCount increments the visit count for the given shortened URL. It is called in separate goroutine. The error must be storage.ErrShortenedNotFound if the shortened URL is not found.

func (*MemSummary) Read added in v0.0.3

func (m *MemSummary) Read(_ context.Context, shortenedURLs []string) (summaries map[string]*models.Summary, err error)

Read provides the summary information for the given shortened URLs. If shortenedURLs is nil or empty, all summaries are returned. The error must be storage.ErrShortenedNotFound if a shortened URL is not found.

func (*MemSummary) Upsert added in v0.0.3

func (m *MemSummary) Upsert(_ context.Context, summaries map[string]*models.Summary) (err error)

Upsert upserts the Summary data for the given shortened URL.

type MemTerse

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

MemTerse is a TerseStore implementation that stores all data in a Go map in memory.

func (*MemTerse) Close

func (m *MemTerse) Close(_ context.Context) (err error)

Close closes the connection to the underlying storage.

func (*MemTerse) Delete

func (m *MemTerse) Delete(_ context.Context, shortenedURLs []string) (err error)

Delete deletes the Terse data for the given shortened URLs. If shortenedURLs is nil or empty, all shortened URL Terse data are deleted. There should be no error if a shortened URL is not found.

func (*MemTerse) Read

func (m *MemTerse) Read(_ context.Context, shortenedURLs []string) (terseData map[string]*models.Terse, err error)

Read returns a map of shortened URLs to Terse data. If shortenedURLs is nil or empty, all shortened URL Terse data are expected. The error must be storage.ErrShortenedNotFound if a shortened URL is not found.

func (*MemTerse) Summary added in v0.0.3

func (m *MemTerse) Summary(_ context.Context, shortenedURLs []string) (summaries map[string]*models.TerseSummary, err error)

Summary summarizes the Terse data for the given shortened URLs. If shortenedURLs is nil or empty, then all shortened URL Summary data are expected.

func (*MemTerse) Write added in v0.0.3

func (m *MemTerse) Write(_ context.Context, terseData map[string]*models.Terse, operation WriteOperation) (err error)

Write writes the given Terse data according to the given operation. The error must be storage.ErrShortenedExists if an Insert operation cannot be performed due to the Terse data already existing. The error must be storage.ErrShortenedNotFound if an Update operation cannot be performed due to the Terse data not existing.

type MemVisits

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

MemVisits is a VisitsStore implementation that stores all data in a Go map in memory.

func (*MemVisits) Close

func (m *MemVisits) Close(_ context.Context) (err error)

Close closes the connection to the underlying storage.

func (*MemVisits) Delete

func (m *MemVisits) Delete(_ context.Context, shortenedURLs []string) (err error)

Delete deletes Visits data for the given shortened URLs. If shortenedURLs is nil or empty, then all Visits data are deleted. No error should be given if a shortened URL is not found.

func (*MemVisits) Insert added in v0.0.3

func (m *MemVisits) Insert(_ context.Context, visitsData map[string][]models.Visit) (err error)

Insert inserts the given Visits data. The visits do not need to be unique, so the Visits data should be appended to the data structure in storage.

func (*MemVisits) Read added in v0.0.3

func (m *MemVisits) Read(_ context.Context, shortenedURLs []string) (visitsData map[string][]models.Visit, err error)

Read exports the Visits data for the given shortened URLs. If shortenedURLs is nil or empty, then all shortened URL Visits data are expected. The error must be storage.ErrShortenedNotFound if a shortened URL is not found.

func (*MemVisits) Summary added in v0.0.3

func (m *MemVisits) Summary(_ context.Context, shortenedURLs []string) (summaries map[string]*models.VisitsSummary, err error)

Summary summarizes the Visits data for the given shortened URLs. If shortenedURLs is nil or empty, then all shortened URL Summary data are expected. The error must be storage.ErrShortenedNotFound if a shortened URL is not found.

type StoreManager added in v0.0.3

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

StoreManager holds all data stores and coordinates operations on them.

func NewStoreManager added in v0.0.3

func NewStoreManager(createCtx CtxCreator, group ctxerrgroup.Group, summaryStore SummaryStore, terseStore TerseStore, visitsStore VisitsStore) (manager StoreManager)

NewStoreManager creates a new manager for the data stores.

func (StoreManager) Close added in v0.0.3

func (s StoreManager) Close(ctx context.Context) (err error)

Close closes the ctxerrgroup and all the underlying data stores.

func (StoreManager) DeleteShortened added in v0.0.3

func (s StoreManager) DeleteShortened(ctx context.Context, shortenedURLs []string) (err error)

DeleteShortened deletes the all data for the given shortened URLs. If shortenedURLs is nil, all shortened URL data are deleted. There should be no error if a shortened URL is not found.

func (StoreManager) DeleteVisits added in v0.0.3

func (s StoreManager) DeleteVisits(ctx context.Context, shortenedURLs []string) (err error)

DeleteVisits deletes Visits data for the given shortened URLs. If shortenedURLs is nil, then all Visits data are deleted. No error should be given if a shortened URL is not found.

func (StoreManager) Export added in v0.0.3

func (s StoreManager) Export(ctx context.Context, shortenedURLs []string) (export map[string]*models.Export, err error)

Export exports the Terse data and Visits data for the given shortened URLs. If shortenedURLs is nil, then all shortened URLs are exported.

func (StoreManager) Import added in v0.0.3

func (s StoreManager) Import(ctx context.Context, data map[string]*models.Export) (err error)

Import imports the given Terse data and Visits data to the TerseStore and VisitsStore respectively. Terse data will be overwritten, Visits data will be appended.

func (StoreManager) InitializeSummaryStore added in v0.0.3

func (s StoreManager) InitializeSummaryStore(ctx context.Context) (err error)

InitializeSummaryStore initializes the SummaryStore with SummaryData gathered from the TerseStore and VisitsStore.

func (StoreManager) Redirect added in v0.0.3

func (s StoreManager) Redirect(ctx context.Context, shortened string, visit models.Visit) (terse *models.Terse, err error)

Redirect is called when a visit to a shortened URL has occurred. It will keep track of the visit and return the required information for a redirect.

func (StoreManager) Summary added in v0.0.3

func (s StoreManager) Summary(ctx context.Context, shortenedURLs []string) (summaries map[string]*models.Summary, err error)

Summary retrieves the Summary data for the given shortened URLs. If shortenedURLs is nil, then all shortened URL summary data will be returned.

func (StoreManager) SummaryStore added in v0.0.3

func (s StoreManager) SummaryStore(doThis func(store SummaryStore))

SummaryStore accepts a function to do if the SummaryStore is not nil

func (StoreManager) Terse added in v0.0.3

func (s StoreManager) Terse(ctx context.Context, shortenedURLs []string) (terse map[string]*models.Terse, err error)

Terse returns a map of shortened URLs to Terse data. If shortenedURLs is nil, all shortened URL Terse data are expected. The error must be storage.ErrShortenedNotFound if a shortened URL is not found.

func (StoreManager) Visits added in v0.0.3

func (s StoreManager) Visits(ctx context.Context, shortenedURLs []string) (visits map[string][]models.Visit, err error)

Visits exports the Visits data for the given shortened URLs. If shortenedURLs is nil, then all shortened URL Visits data are expected. The error must be storage.ErrShortenedNotFound if a shortened URL is not found.

func (StoreManager) VisitsStore added in v0.0.3

func (s StoreManager) VisitsStore(doThis func(store VisitsStore))

VisitsStore accepts a function to do if the VisitsStore is not nil.

func (StoreManager) WriteTerse added in v0.0.3

func (s StoreManager) WriteTerse(ctx context.Context, terse map[string]*models.Terse, operation WriteOperation) (err error)

WriteTerse Write writes the given Terse data according to the given operation. The error must be storage.ErrShortenedExists if an Insert operation cannot be performed due to the Terse data already existing. The error must be storage.ErrShortenedNotFound if an Update operation cannot be performed due to the Terse data not existing.

type SummaryStore added in v0.0.3

type SummaryStore interface {

	// Close closes the connection to the underlying storage.
	Close(ctx context.Context) (err error)

	// Delete deletes the summary information for the given shortened URLs. If shortenedURLs is nil or empty, all
	// Summary data are deleted. No error should be returned if a shortened URL is not found.
	Delete(ctx context.Context, shortenedURLs []string) (err error)

	// IncrementVisitCount increments the visit count for the given shortened URL. It is called in separate goroutine.
	// The error must be storage.ErrShortenedNotFound if the shortened URL is not found.
	IncrementVisitCount(ctx context.Context, shortened string) (err error)

	// Read provides the summary information for the given shortened URLs. If shortenedURLs is nil or empty, all
	// summaries are returned. The error must be storage.ErrShortenedNotFound if a shortened URL is not found.
	Read(ctx context.Context, shortenedURLs []string) (summaries map[string]*models.Summary, err error)

	// Upsert upserts the summary information for the given shortened URL.
	Upsert(ctx context.Context, summaries map[string]*models.Summary) (err error)
}

SummaryStore is the Summary data storage interface. It allows for Summary data storage operations without needing to know how the Summary data are stored. Summary data should not persist through a service restart.

func NewMemSummary added in v0.0.3

func NewMemSummary() (summaryStore SummaryStore)

NewMemSummary creates a new MemSummary.

func NewSummaryStore added in v0.0.3

func NewSummaryStore(configJSON json.RawMessage) (summaryStore SummaryStore, storeType string, err error)

NewSummaryStore creates a new SummaryStore from the given configJSON. The storeType return value is used for logging.

type TerseStore

type TerseStore interface {

	// Close closes the connection to the underlying storage.
	Close(ctx context.Context) (err error)

	// Delete deletes the Terse data for the given shortened URLs. If shortenedURLs is nil or empty, all shortened URL
	// Terse data are deleted. There should be no error if a shortened URL is not found.
	Delete(ctx context.Context, shortenedURLs []string) (err error)

	// Read returns a map of shortened URLs to Terse data. If shortenedURLs is nil or empty, all shortened URL Terse
	// data are expected. The error must be storage.ErrShortenedNotFound if a shortened URL is not found.
	Read(ctx context.Context, shortenedURLs []string) (terseData map[string]*models.Terse, err error)

	// Summary summarizes the Terse data for the given shortened URLs. If shortenedURLs is nil or empty, then all
	// shortened URL Summary data are expected.
	Summary(ctx context.Context, shortenedURLs []string) (summaries map[string]*models.TerseSummary, err error)

	// Write writes the given Terse data according to the given operation. The error must be storage.ErrShortenedExists
	// if an Insert operation cannot be performed due to the Terse data already existing. The error must be
	// storage.ErrShortenedNotFound if an Update operation cannot be performed due to the Terse data not existing.
	Write(ctx context.Context, terseData map[string]*models.Terse, operation WriteOperation) (err error)
}

TerseStore is the Terse storage interface. It allows for Terse storage operations without needing to know how the Terse data are stored.

func NewBboltTerse

func NewBboltTerse(db *bbolt.DB, terseBucket []byte) (terseStore TerseStore)

NewBboltTerse creates a new BboltTerse given the required assets.

func NewMemTerse

func NewMemTerse() (terseStore TerseStore)

NewMemTerse creates a new MemTerse given the required assets.

func NewTerseStore

func NewTerseStore(configJSON json.RawMessage) (terseStore TerseStore, storeType string, err error)

NewTerseStore creates a new TerseStore from the given configJSON. The storeType return value is used for logging.

type VisitsStore

type VisitsStore interface {

	// Close closes the connection to the underlying storage.
	Close(ctx context.Context) (err error)

	// Delete deletes Visits data for the given shortened URLs. If shortenedURLs is nil or empty, then all Visits data
	// are deleted. No error should be given if a shortened URL is not found.
	Delete(ctx context.Context, shortenedURLs []string) (err error)

	// Insert inserts the given Visits data. The visits do not need to be unique, so the Visits data should be appended
	// to the data structure in storage.
	Insert(ctx context.Context, visitsData map[string][]models.Visit) (err error)

	// Read exports the Visits data for the given shortened URLs. If shortenedURLs is nil or empty, then all shortened
	// URL Visits data are expected. The error must be storage.ErrShortenedNotFound if a shortened URL is not found.
	Read(ctx context.Context, shortenedURLs []string) (visitsData map[string][]models.Visit, err error)

	// Summary summarizes the Visits data for the given shortened URLs. If shortenedURLs is nil or empty, then all
	// shortened URL Summary data are expected. The error must be storage.ErrShortenedNotFound if a shortened URL is not
	// found.
	Summary(ctx context.Context, shortenedURLs []string) (summaries map[string]*models.VisitsSummary, err error)
}

VisitsStore is the Visits storage interface. It allows for Visits storage operations without needing to know how the Visits data are stored.

func NewBboltVisits

func NewBboltVisits(db *bbolt.DB, visitsBucket []byte) (visitsStore VisitsStore)

NewBboltVisits creates a new NewBboltVisits given the required assets.

func NewMemVisits

func NewMemVisits() (visitsStore VisitsStore)

NewMemVisits creates a new MemVisits.

func NewVisitsStore

func NewVisitsStore(configJSON json.RawMessage) (visitsStore VisitsStore, storeType string, err error)

NewVisitsStore creates a new VisitsStore from the given configJSON. The storeType return value is used for logging.

type WriteOperation added in v0.0.3

type WriteOperation uint

WriteOperation indicates the type of write operation.

const (

	// Insert indicates an insertion write operation. It will fail if the target already exists.
	Insert WriteOperation = iota

	// Update indicates an update write operation. It will fail if the target does not exist.
	Update

	// Upsert indicates an upsert write operation. It will be performed if the target exists or does not exist.
	Upsert
)

func (WriteOperation) FromString added in v0.0.3

func (w WriteOperation) FromString(s string) (operation WriteOperation, err error)

FromString will determine what write operation should take place based on the input string.

Jump to

Keyboard shortcuts

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