GitHub快速開始

API 參考取自原始碼中的 Go 文件註解,以英文撰寫。

package sqlstore

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

概覽

Package sqlstore is experimental: its API and documented formats may change in a minor release. See the API stability policy. is a cloudsave.Store over database/sql, written once for SQLite and Postgres and opened through a driver package: sqlitestore (with the pure-Go modernc.org/sqlite driver) or pgstore (with github.com/jackc/pgx/v5). It imports no driver itself, so New works with any *sql.DB of a supported dialect.

The schema is two tables, created by New when missing and versioned in cloudsave_schema:

  • cloudsave_slots: account_id and slot (the key), and the newest version's number, deleted flag, hash, size and created_at
  • cloudsave_versions: account_id, slot and version (the key), the same metadata and the payload in data

Times are Unix milliseconds. Each write runs in one transaction that compares and sets the slot's newest version number, so of two concurrent writes based on the same version at most one commits; on Postgres it also takes a transaction-level advisory lock on the account, so the slot count and pruning see every concurrent write of that account.

索引

常數

const SchemaVersion = 1

SchemaVersion is the schema version New creates and accepts.

變數

var Postgres = Dialect{
	Name:        "postgres",
	Numbered:    true,
	Blob:        "BYTEA",
	MigrateLock: `SELECT pg_advisory_xact_lock(1668051830)`,
	LockAccount: `SELECT pg_advisory_xact_lock(1668051830, hashtext(?))`,
}

Postgres is Postgres's dialect. Its locks use the arbitrary class key 1668051830 ("clsv" in ASCII).

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

SQLite is SQLite's dialect. A SQLite store uses one connection with immediate transactions (sqlitestore.Open), which serializes writes without an account lock.

型別

type Dialect struct {
	// Name names the database in errors.
	Name string
	// Numbered is true for "$1" placeholders, false for "?".
	Numbered bool
	// Blob is the column type of a byte string, such as BLOB or BYTEA.
	Blob string
	// MigrateLock, when set, runs first in the migration's transaction to serialize migrations of several processes.
	MigrateLock string
	// LockAccount, when set, runs first in each write's transaction with the account ID as its one parameter, to serialize the writes of one account across connections and processes.
	LockAccount string
}

Dialect is what differs between SQL databases.

type Stats = cloudsave.Stats

Stats is how much a store holds: cloudsave.Stats, under the name a service's metrics already use.

func ReadStats(ctx context.Context, db *sql.DB) (Stats, error)

ReadStats is Store.Stats on db, another handle on a store's database, such as the read-only one sqlitestore.OpenReader opens, so the full scans run beside the store's connection rather than on it.

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

Store is a cloudsave.Store in a SQL database.

func New(ctx context.Context, db *sql.DB, d Dialect) (*Store, error)

New returns a store on db, creating the schema when it is missing. It refuses a database whose schema is newer than SchemaVersion.

func (s *Store) Append(ctx context.Context, account, slot string, w cloudsave.Write) (cloudsave.Version, error)

Append implements cloudsave.Store.

func (s *Store) Close() error

Close closes the store's database.

func (s *Store) DB() *sql.DB

DB returns the store's database.

func (s *Store) Head(ctx context.Context, account, slot string) (cloudsave.Version, error)

Head implements cloudsave.Store. It reads the slot's head and its data in one statement, so an Append that commits in between and prunes that version cannot make a live slot look missing.

func (s *Store) Purge(ctx context.Context, account, slot string) (cloudsave.Purged, error)

Purge implements cloudsave.Store. It reads the slot's head and removes its versions and row in one transaction, under the account's lock on Postgres, so what it returns is what it removed.

func (s *Store) PurgeAccount(ctx context.Context, account string) (cloudsave.Purged, error)

PurgeAccount implements cloudsave.Store. It removes the account's versions and slots in one transaction, under the account's lock on Postgres, so a write of the account committing at the same time either lands before and is removed and counted, or after and stays whole.

func (s *Store) Slots(ctx context.Context, account string) ([]cloudsave.Version, error)

Slots implements cloudsave.Store.

func (s *Store) Stats(ctx context.Context) (Stats, error)

Stats implements cloudsave.Store: it counts the accounts with a slot, the live slots and the bytes of the kept versions. It reads both tables in full, so call it at a scrape's pace, not per request.

func (s *Store) Version(ctx context.Context, account, slot string, number int64) (cloudsave.Version, error)

Version implements cloudsave.Store.

func (s *Store) Versions(ctx context.Context, account, slot string) ([]cloudsave.Version, error)

Versions implements cloudsave.Store.