records

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: 6 Imported by: 0 modules, 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 records is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.

Package records provides a server-authoritative, versioned JSON record core.

Index

Examples

Constants

View Source
const AnyVersion int64 = -1

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

View Source
const Server = ""

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

Variables

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

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

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

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

ErrNotFound is returned by Get when there is no record.

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

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

Functions

This section is empty.

Types

type Delete

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

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

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

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

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.

Example

A Store versions each write, refuses a write against a stale version and hides records from readers their permission excludes.

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"time"

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

func main() {
	ctx := context.Background()
	store := records.New(func() time.Time { return time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC) })

	save := records.Record{Owner: "ana", Collection: "saves", Key: "slot1", Value: json.RawMessage(`{"level":1}`), Read: records.OwnerRead}
	badge := records.Record{Owner: "ana", Collection: "saves", Key: "badge", Value: json.RawMessage(`"gold"`), Read: records.PublicRead}
	fmt.Println(store.Apply(ctx, records.Tx{Puts: []records.Put{{Record: save}, {Record: badge}}}))

	// A second write expects version 1; repeating it with the same expectation conflicts.
	save.Value = json.RawMessage(`{"level":2}`)
	fmt.Println(store.Apply(ctx, records.Tx{Puts: []records.Put{{Record: save, Expected: 1}}}))
	fmt.Println(store.Apply(ctx, records.Tx{Puts: []records.Put{{Record: save, Expected: 1}}}))

	r, _ := store.Get(ctx, "ana", "saves", "slot1", "ana")
	fmt.Println(r.Version, string(r.Value), r.Updated.Format(time.DateOnly))
	_, err := store.Get(ctx, "ana", "saves", "slot1", "ben")
	fmt.Println(err)
	for _, r := range store.List(ctx, "ana", "saves", "ben") {
		fmt.Println(r.Key, r.Version)
	}

	fmt.Println(store.Apply(ctx, records.Tx{Deletes: []records.Delete{{Owner: "ana", Collection: "saves", Key: "badge", Expected: records.AnyVersion}}}))
	_, err = store.Get(ctx, "ana", "saves", "badge", records.Server)
	fmt.Println(err)
}
Output:
<nil>
<nil>
records: version conflict
2 {"level":2} 2026-01-02
records: permission denied
badge 1
<nil>
records: not found

func New

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

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

func (*Store) Apply

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

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

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

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

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

Jump to

Keyboard shortcuts

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