API リファレンスはソースコード内の Go ドキュメントコメントから生成されており、英語で記述されています。
package sqlstore
import "github.com/nsgo-dev/game/pkg/online/leaderboard/selfhost/sqlstore"概要
Package sqlstore is experimental: its API and documented formats may change in a minor release. See the API stability policy. persists self-hosted leaderboard records in SQLite or Postgres.
目次
- 定数
- 変数
- type Dialect
- type Store
- func New(ctx context.Context, db *sql.DB, d Dialect) (*Store, error)
- func (s *Store) Close() error
- func (s *Store) Configure(boards []selfhost.Board)
- func (s *Store) DB() *sql.DB
- func (s *Store) DeleteAccount(ctx context.Context, board, period, account string) error
- func (s *Store) DeleteHidden(ctx context.Context, board, account string) error
- func (s *Store) Drop(ctx context.Context, board, period string) error
- func (s *Store) Load(ctx context.Context, fn func(selfhost.Record) error) error
- func (s *Store) LoadAdmin(ctx context.Context) (selfhost.AdminState, error)
- func (s *Store) Records(ctx context.Context, board, period string) ([]selfhost.Record, error)
- func (s *Store) Save(ctx context.Context, rec selfhost.Record) error
- func (s *Store) SaveHidden(ctx context.Context, h selfhost.Hidden) error
- func (s *Store) SaveReset(ctx context.Context, r selfhost.BoardReset) error
定数
const SchemaVersion = 3SchemaVersion is the version of the leaderboard schema New migrates a database to. Version 2 adds each record's subscore and metadata; version 3 adds the tables of hidden accounts and manual resets.
変数
var Postgres = Dialect{Name: "postgres", Blob: "BIGINT", Numbered: true, MigrateLock: `SELECT pg_advisory_xact_lock(16801680)`, LockRecord: `SELECT pg_advisory_xact_lock(16801680, hashtext(?))`}Postgres is the dialect of Postgres databases.
型
type Dialect struct {
Name, Blob string
Numbered bool
MigrateLock, LockRecord string
}Dialect is what differs between the SQL databases a Store supports: the name, the column type of blobs, whether placeholders are numbered ($1) rather than ?, and the statements, if any, that lock the migration and one record in a transaction.
type Store struct {
// contains filtered or unexported fields
}Store is a selfhost.Store in an SQL database: one row per board, period and account in leaderboard_records, with the account's score, subscore and metadata, one row per hidden account in leaderboard_hidden and one per manual reset in leaderboard_resets.
func New(ctx context.Context, db *sql.DB, d Dialect) (*Store, error)New migrates db's leaderboard schema to SchemaVersion under dialect d and returns a store over it. A schema newer than SchemaVersion is refused.
func (s *Store) Configure(boards []selfhost.Board)Configure tells the store each board's order and operator. On a best board Save keeps the better score, the lowest of an ascending board and the highest of any other, with a higher subscore breaking a tie; on a set, increment or decrement board Save stores the record the service computed.
func (s *Store) DeleteAccount(ctx context.Context, board, period, account string) errorDeleteAccount deletes the account's record in board's period, or in every period of board when period is empty.
func (s *Store) DeleteHidden(ctx context.Context, board, account string) errorDeleteHidden deletes the hiding of the account on board.
func (s *Store) Drop(ctx context.Context, board, period string) errorDrop deletes every record of board in period.
func (s *Store) Load(ctx context.Context, fn func(selfhost.Record) error) errorLoad calls fn with every stored record, ordered by board, period and account, stopping at fn's first error.
func (s *Store) LoadAdmin(ctx context.Context) (selfhost.AdminState, error)LoadAdmin returns the hidden accounts, ordered by board and account, and the manual resets, ordered by board and time.
func (s *Store) Records(ctx context.Context, board, period string) ([]selfhost.Record, error)Records returns records in deterministic order and is intended for diagnostics and tests.
func (s *Store) Save(ctx context.Context, rec selfhost.Record) errorSave stores rec. On a best board it leaves the stored record when the account already has a better score in that period, or an equal score with an equal or higher subscore, so concurrent writers keep the best; on any other board it replaces the record.
func (s *Store) SaveHidden(ctx context.Context, h selfhost.Hidden) errorSaveHidden stores h, replacing an earlier hiding of the same board and account.
func (s *Store) SaveReset(ctx context.Context, r selfhost.BoardReset) errorSaveReset stores a manual reset; storing the same board and time again changes nothing.