GitHubCommencer

La référence API provient des commentaires de documentation Go du code source et est rédigée en anglais.

package sqlstore

import "github.com/nsgo-dev/game/pkg/online/leaderboard/selfhost/sqlstore"

Vue d’ensemble

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.

Index

Constantes

const SchemaVersion = 3

SchemaVersion 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.

Variables

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.

var SQLite = Dialect{Name: "sqlite", Blob: "BLOB"}

SQLite is the dialect of SQLite databases.

Types

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) Close() error

Close closes the store's database.

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) DB() *sql.DB

DB returns the store's database.

func (s *Store) DeleteAccount(ctx context.Context, board, period, account string) error

DeleteAccount 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) error

DeleteHidden deletes the hiding of the account on board.

func (s *Store) Drop(ctx context.Context, board, period string) error

Drop deletes every record of board in period.

func (s *Store) Load(ctx context.Context, fn func(selfhost.Record) error) error

Load 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) error

Save 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) error

SaveHidden stores h, replacing an earlier hiding of the same board and account.

func (s *Store) SaveReset(ctx context.Context, r selfhost.BoardReset) error

SaveReset stores a manual reset; storing the same board and time again changes nothing.