package sqlstore
import "github.com/nsgo-dev/game/pkg/cloudsave/sqlstore"Overview
Package sqlstore is a cloudsave.Store over database/sql, for SQLite (OpenSQLite, with the pure-Go modernc.org/sqlite driver) and Postgres (OpenPostgres, with github.com/jackc/pgx/v5).
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.
Index
- Constants
- Variables
- type Dialect
- type Store
- func New(ctx context.Context, db *sql.DB, d Dialect) (*Store, error)
- func OpenPostgres(ctx context.Context, dsn string) (*Store, error)
- func OpenSQLite(ctx context.Context, path string) (*Store, error)
- func (s *Store) Append(ctx context.Context, account, slot string, w cloudsave.Write) (cloudsave.Version, error)
- func (s *Store) Close() error
- func (s *Store) DB() *sql.DB
- func (s *Store) Head(ctx context.Context, account, slot string) (cloudsave.Version, error)
- func (s *Store) Purge(ctx context.Context, account, slot string) error
- func (s *Store) Slots(ctx context.Context, account string) ([]cloudsave.Version, error)
- func (s *Store) Version(ctx context.Context, account, slot string, number int64) (cloudsave.Version, error)
- func (s *Store) Versions(ctx context.Context, account, slot string) ([]cloudsave.Version, error)
Constants
Variables
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 (OpenSQLite), which serializes writes without an account lock.
Types
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 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 OpenPostgres(ctx context.Context, dsn string) (*Store, error)OpenPostgres connects to the Postgres database dsn names (a URL or a key=value string, as pgx parses it) and returns a store on it with the schema created. Several service processes may share the database. Keep the DSN, which may hold a password, out of logs and command lines: read it from the environment or a file. Close the store to close the connections.
func OpenSQLite(ctx context.Context, path string) (*Store, error)OpenSQLite opens (creating when missing) the SQLite database at path, or a private in-memory database for ":memory:", and returns a store on it with the schema created. The database uses write-ahead logging and waits up to five seconds for a lock another process holds.
The store uses one connection with immediate transactions, so its writes run one at a time. It suits a single service process; several processes sharing one database should use Postgres. OpenSQLite is not available on the web, which has no file system to keep the database in.
func (s *Store) Append(ctx context.Context, account, slot string, w cloudsave.Write) (cloudsave.Version, error)Append implements cloudsave.Store.
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) errorPurge implements cloudsave.Store.
func (s *Store) Slots(ctx context.Context, account string) ([]cloudsave.Version, error)Slots implements cloudsave.Store.
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.