GitHubLoslegen

Die API-Referenz stammt aus den Go-Doc-Kommentaren im Quellcode und ist auf Englisch verfasst.

package records

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

Übersicht

Package records is experimental: its API and documented formats may change in a minor release. See the API stability policy. provides a server-authoritative, versioned JSON record core.

Index

Konstanten

const AnyVersion int64 = -1

AnyVersion as an expected version matches whatever is stored, including no record.

const Server = ""

Server is the reader name of server code, which reads every record whatever its permission.

Variablen

var ErrChanged = errors.New("records: version conflict")

ErrChanged is returned by Apply when an expected version does not match the stored record.

var ErrInvalid = errors.New("records: invalid transaction")

ErrInvalid is returned by Apply for a transaction with a record that has no owner, collection or key, a value that is not JSON, or a record named twice.

var ErrNotFound = errors.New("records: not found")

ErrNotFound is returned by Get when there is no record.

var ErrPermission = errors.New("records: permission denied")

ErrPermission is returned by Get when the reader may not read the record.

Typen

type Delete struct {
	Owner, Collection, Key string
	Expected               int64
}

Delete removes the record at the owner, collection and key when its version is Expected; an Expected of 0 requires that there be no record, and AnyVersion removes whatever is stored.

type Permission uint8

Permission is who may read a record: server code always may, and Read adds its owner (OwnerRead) or anyone (PublicRead).

const (
	// Private records are read only by server code.
	Private Permission = iota
	// OwnerRead records are read by their owner and by server code.
	OwnerRead
	// PublicRead records are read by anyone.
	PublicRead
)
type Put struct {
	Record   Record
	Expected int64
}

Put writes Record when the stored record's version is Expected, when Expected is 0 and there is no stored record, or whatever is stored when Expected is AnyVersion.

type Record struct {
	Owner, Collection, Key string
	Value                  json.RawMessage
	Version                int64
	Read, Write            Permission
	Updated                time.Time
}

Record is one JSON value stored under an owner, a collection and a key, with the version the store assigns on each write, its read and write permissions and the time it was last written. Apply is server code and does not check Write; a player-facing service checks it before applying a player's write.

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

Store is an in-memory, server-authoritative record store with optimistic concurrency. It is safe for concurrent use.

func New(now func() time.Time) *Store

New returns an empty store that reads the time from now (time.Now when nil).

func (s *Store) Apply(ctx context.Context, tx Tx) error

Apply applies tx's puts and deletes at once, or none of them: ErrInvalid for a malformed transaction and ErrChanged when any expected version does not match. Each put's version becomes the stored version plus 1, or 1 for a new record. A record may appear only once in a transaction, as one put or one delete.

func (s *Store) Get(ctx context.Context, o, c, k, reader string) (Record, error)

Get returns the record of owner o, collection c and key k as reader sees it: ErrNotFound when there is none and ErrPermission when reader may not read it (see Permission; reader Server reads every record).

func (s *Store) List(ctx context.Context, o, c, reader string) []Record

List returns the records of owner o in collection c that reader may read (see Get), sorted by key.

type Tx struct {
	Puts    []Put
	Deletes []Delete
}

Tx is writes and deletions applied together or not at all.