sqlstore

package
v0.4.0-rc.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 10 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package sqlstore is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.

Package sqlstore 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.

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(sqlstore.SchemaVersion)
}
Output:
1

Index

Examples

Constants

View Source
const SchemaVersion = 1

SchemaVersion is the schema version New creates and accepts.

Variables

View Source
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).

View Source
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.

Functions

This section is empty.

Types

type Dialect

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

type Stats = cloudsave.Stats

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

func ReadStats

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

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

Store is a cloudsave.Store in a SQL database.

func New

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 (*Store) Append

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

Append implements cloudsave.Store.

func (*Store) Close

func (s *Store) Close() error

Close closes the store's database.

func (*Store) DB

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

DB returns the store's database.

func (*Store) Head

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 (*Store) Purge

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 (*Store) PurgeAccount

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 (*Store) Slots

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

Slots implements cloudsave.Store.

func (*Store) Stats

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 (*Store) Version

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

Version implements cloudsave.Store.

func (*Store) Versions

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

Versions implements cloudsave.Store.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL