store

package
v0.0.0-...-b16a518 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2024 License: BSD-3-Clause Imports: 19 Imported by: 0

Documentation

Overview

Package store implements a data store for memes.

Structure

A DB manages a directory in the filesystem. At the top level of the directory is a SQLite database (index.db) that keeps track of metadata about templates, macros, and votes. There are also subdirectories to store the image data, "templates" and "macros".

The "macros" subdirectory is a cache, and the DB maintains a background polling thread that cleans up files that have not been accessed for a while. It is safe to manually delete files inside the macros directory; the server will re-create them on demand. Templates images are persistent, and should not be modified or deleted.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

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

A DB is a meme database. It consists of a directory containing files and subdirectories holding images and metadata. A DB is safe for concurrent use by multiple goroutines.

func New

func New(dirPath string, opts *Options) (*DB, error)

New creates or opens a data store. A store is a directory that is created if necessary. The DB assumes ownership of the directory contents. A nil *Options provides default settings (see Options).

The caller should Close the DB when it is no longer in use, to ensure the cache maintenance routine is stopped and cleaned up.

func (*DB) AddMacro

func (db *DB) AddMacro(m *tmemes.Macro) error

AddMacro adds m to the database. It reports an error if m.ID != 0, or updates m.ID on success.

func (*DB) AddTemplate

func (db *DB) AddTemplate(t *tmemes.Template, fileExt string, data io.Reader) error

AddTemplate adds t to the database. The ID must be 0 and the Path must be empty, these are populated by a successful add. The other fields of t should be initialized by the caller.

If set, fileExt is used as the filename extension for the image file. The contents of the template image are fully read from r.

func (*DB) AnyTemplate

func (db *DB) AnyTemplate(id int) (*tmemes.Template, error)

AnyTemplate returns the template data for the specified ID. Hidden templates are included.

func (*DB) CachePath

func (db *DB) CachePath(m *tmemes.Macro) (string, error)

CachePath returns a cache file path for the specified macro. The path is returned even if the file is not cached.

func (*DB) Close

func (db *DB) Close() error

Close stops background tasks and closes the index database.

func (*DB) DeleteMacro

func (db *DB) DeleteMacro(id int) error

DeleteMacro deletes the specified macro ID from the database.

func (*DB) GetVote

func (db *DB) GetVote(userID tailcfg.UserID, macroID int) (vote int, err error)

GetVote returns the given user's vote on a single macro. If vote < 0, the user downvoted this macro. If vote == 0, the user did not vote on this macro. If vote > 0, the user upvoted this macro.

func (*DB) Macro

func (db *DB) Macro(id int) (*tmemes.Macro, error)

Macro returns the macro data for the specified ID.

func (*DB) Macros

func (db *DB) Macros() []*tmemes.Macro

Macros returns all the macros in the store.

func (*DB) MacrosByCreator

func (db *DB) MacrosByCreator(creator tailcfg.UserID) []*tmemes.Macro

MacrosByCreator returns all the macros created by the specified user.

func (*DB) SetCacheSeed

func (db *DB) SetCacheSeed(s string) error

SetCacheSeed sets the base string used when generating cache keys for generated macros. If not set, the value persisted in the index is used. Changing the cache seed invalidates cached entries.

func (*DB) SetTemplateHidden

func (db *DB) SetTemplateHidden(id int, hidden bool) error

SetTemplateHidden sets (or clears) the "hidden" flag of a template. Hidden templates are not available for use in creating macros.

func (*DB) SetVote

func (db *DB) SetVote(userID tailcfg.UserID, macroID, vote int) (*tmemes.Macro, error)

SetVote records a user vote on the specified macro. If vote < 0, a downvote is recorded; if vote > 0 an upvote is recorded. If vote == 0 the user's vote is removed. Each user can vote at most once for a given macro.

func (*DB) Template

func (db *DB) Template(id int) (*tmemes.Template, error)

Template returns the template data for the specified ID. Hidden templates are treated as not found.

func (*DB) TemplateByName

func (db *DB) TemplateByName(name string) (*tmemes.Template, error)

TemplateByName returns the template data matching the given name. Comparison is done without regard to case, leading and trailing whitespace are removed, and interior whitespace, "-", and "_" are normalized to "-". HIdden templates are excluded.

func (*DB) TemplatePath

func (db *DB) TemplatePath(id int) (string, error)

TemplatePath returns the path of the file containing a template image. Hidden templates are included.

func (*DB) Templates

func (db *DB) Templates() []*tmemes.Template

Templates returns all the non-hidden templates in the store. Templates are ordered non-decreasing by ID.

func (*DB) TemplatesByCreator

func (db *DB) TemplatesByCreator(creator tailcfg.UserID) []*tmemes.Template

TemplatesByCreator returns all the non-hidden templates in the store created by the specified user. The results are ordered non-decreasing by ID.

func (*DB) UpdateMacro

func (db *DB) UpdateMacro(m *tmemes.Macro) error

UpdateMacro updates macro m. It reports an error if m is not already in the store; otherwise it updates the stored data to the current state of m.

func (*DB) UserMacroVote

func (db *DB) UserMacroVote(userID tailcfg.UserID, macroID int) (int, error)

UserMacroVote reports the vote status of the given user for a single macro. The result is -1 for a downvote, 1 for an upvote, 0 for no vote.

func (*DB) UserVotes

func (db *DB) UserVotes(userID tailcfg.UserID) (map[int]int, error)

UserVotes all the votes for the given user, as a map from macroID to vote. The votes are -1 for a downvote, 1 for an upvote. Macros on which the user has not voted are not included.

type Options

type Options struct {
	// Do not prune the macro cache until it is at least this big.
	// Default: 50MB.
	MinPruneBytes int64

	// When pruning the cache, discard entries that have not been accessed in at
	// least this long. Default: 30m.
	MaxAccessAge time.Duration
}

Options are optional settings for a DB. A nil *Options is ready for use with default values.

Jump to

Keyboard shortcuts

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