Documentation
¶
Overview ¶
Package sqlitestore is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.
Package sqlitestore opens a cloudsave.Store in a SQLite database file, with the pure-Go modernc.org/sqlite driver (no cgo). It suits a single cloud save service process; several processes sharing one database should use Postgres (package pgstore).
The package builds for every target but the web: a browser has no file system to keep the database in, and the cloud save service runs on a server.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Open ¶
Open 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.
Example ¶
Open creates the SQLite schema and returns a cloudsave.Store; Append adds a version to a slot when its precondition holds.
package main
import (
"context"
"fmt"
"github.com/nsgo-dev/game/pkg/online/cloudsave"
"github.com/nsgo-dev/game/pkg/online/cloudsave/sqlitestore"
)
func main() {
ctx := context.Background()
store, err := sqlitestore.Open(ctx, ":memory:")
if err != nil {
fmt.Println(err)
return
}
defer store.Close()
version, err := store.Append(ctx, "acct-1", "main", cloudsave.Write{
Data: []byte("level 3, 120 coins"),
Match: cloudsave.MatchAny,
Keep: 1,
MaxSlots: 4,
})
if err != nil {
fmt.Println(err)
return
}
head, err := store.Head(ctx, "acct-1", "main")
fmt.Println(version.Number, string(head.Data), err)
}
Output: 1 level 3, 120 coins <nil>
func OpenReader ¶
OpenReader opens a second handle on the SQLite database file at path, which Open has already opened, for work that must not queue behind the store's single connection: a service's readiness check and the counts its metrics scan. Its connections only read (query_only), and the database's write-ahead log lets them read while the store writes, so neither a long write delays them nor a full-table count delays the store. It refuses ":memory:", a database only the store's own connection sees. Close the handle when the service stops.
Types ¶
This section is empty.