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 ¶
- type DB
- func (db *DB) AddMacro(m *tmemes.Macro) error
- func (db *DB) AddTemplate(t *tmemes.Template, fileExt string, data io.Reader) error
- func (db *DB) AnyTemplate(id int) (*tmemes.Template, error)
- func (db *DB) CachePath(m *tmemes.Macro) (string, error)
- func (db *DB) Close() error
- func (db *DB) DeleteMacro(id int) error
- func (db *DB) GetVote(userID tailcfg.UserID, macroID int) (vote int, err error)
- func (db *DB) Macro(id int) (*tmemes.Macro, error)
- func (db *DB) Macros() []*tmemes.Macro
- func (db *DB) MacrosByCreator(creator tailcfg.UserID) []*tmemes.Macro
- func (db *DB) SetCacheSeed(s string) error
- func (db *DB) SetTemplateHidden(id int, hidden bool) error
- func (db *DB) SetVote(userID tailcfg.UserID, macroID, vote int) (*tmemes.Macro, error)
- func (db *DB) Template(id int) (*tmemes.Template, error)
- func (db *DB) TemplateByName(name string) (*tmemes.Template, error)
- func (db *DB) TemplatePath(id int) (string, error)
- func (db *DB) Templates() []*tmemes.Template
- func (db *DB) TemplatesByCreator(creator tailcfg.UserID) []*tmemes.Template
- func (db *DB) UpdateMacro(m *tmemes.Macro) error
- func (db *DB) UserMacroVote(userID tailcfg.UserID, macroID int) (int, error)
- func (db *DB) UserVotes(userID tailcfg.UserID) (map[int]int, error)
- type Options
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 ¶
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 ¶
AddMacro adds m to the database. It reports an error if m.ID != 0, or updates m.ID on success.
func (*DB) AddTemplate ¶
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 ¶
AnyTemplate returns the template data for the specified ID. Hidden templates are included.
func (*DB) CachePath ¶
CachePath returns a cache file path for the specified macro. The path is returned even if the file is not cached.
func (*DB) DeleteMacro ¶
DeleteMacro deletes the specified macro ID from the database.
func (*DB) GetVote ¶
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) MacrosByCreator ¶
MacrosByCreator returns all the macros created by the specified user.
func (*DB) SetCacheSeed ¶
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 ¶
SetTemplateHidden sets (or clears) the "hidden" flag of a template. Hidden templates are not available for use in creating macros.
func (*DB) SetVote ¶
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 ¶
Template returns the template data for the specified ID. Hidden templates are treated as not found.
func (*DB) TemplateByName ¶
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 ¶
TemplatePath returns the path of the file containing a template image. Hidden templates are included.
func (*DB) Templates ¶
Templates returns all the non-hidden templates in the store. Templates are ordered non-decreasing by ID.
func (*DB) TemplatesByCreator ¶
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 ¶
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 ¶
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.
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.