state

package
v0.14.4-dev.35 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package state owns gateway state; launch setup must never open this store.

Index

Constants

View Source
const LocalNodeSQL = "(SELECT node_id FROM local_node WHERE id=1)"

LocalNodeSQL reads inside the caller's transaction, including already-running workers.

Variables

View Source
var ErrArchivePending = errors.New("newer archived history awaits the main gateway; retry after archive handover")
View Source
var ErrChangeVersionTooLarge = errors.New("replicated transaction exceeds the change page byte limit")
View Source
var ErrMainChanged = errors.New("main or archive gateway changed; review the current roles before trying again")

Functions

func OpenArchive

func OpenArchive(home string) (*sql.DB, error)

OpenArchive is plain SQLite; it never loads cr-sqlite or changes an existing archive's schema.

func ReadArchive

func ReadArchive(home string) (*sql.DB, error)

ReadArchive never creates a missing archive or enables writes.

func RememberArchiveClocks

func RememberArchiveClocks(ctx context.Context, tx *sql.Tx, id string) error

RememberArchiveClocks retains original event clocks before archive pruning creates tombstones.

func SchemaVersion

func SchemaVersion() int

func Transaction

func Transaction(ctx context.Context, db *sql.DB, run func(*sql.Tx) error) error

Transaction commits only when every operation succeeds; panics also roll back.

func VerifyArchive

func VerifyArchive(home string) error

VerifyArchive validates a copied archive without creating or changing it.

Types

type Change

type Change struct {
	Table      string      `json:"table"`
	PK         []byte      `json:"pk"`
	Column     string      `json:"cid"`
	Value      SQLiteValue `json:"val"`
	ColVersion int64       `json:"col_version"`
	DBVersion  int64       `json:"db_version"`
	SiteID     []byte      `json:"site_id"`
	CL         int64       `json:"cl"`
	Seq        int64       `json:"seq"`
	TS         *string     `json:"ts"`
}

type ChangeVersionTooLargeError

type ChangeVersionTooLargeError struct {
	Version int64
	Limit   int
}

func (*ChangeVersionTooLargeError) Error

func (*ChangeVersionTooLargeError) Unwrap

func (e *ChangeVersionTooLargeError) Unwrap() error

type MainState

type MainState struct {
	MainNode    string
	ArchiveNode string
	MainName    string
	ArchivePath string
	ArchiveName string
}

type SQLiteValue

type SQLiteValue struct {
	Type string          `json:"type"`
	Data json.RawMessage `json:"data"`
}

SQLiteValue keeps integers exact and distinguishes an empty blob from NULL.

type Store

type Store struct {
	DB     *sql.DB
	Reader *sql.DB
	// contains filtered or unexported fields
}

func Open

func Open(home string) (*Store, error)

func (*Store) Apply

func (s *Store) Apply(ctx context.Context, changes []Change) error

func (*Store) ApplyPage

func (s *Store) ApplyPage(ctx context.Context, changes []Change, site []byte, next int64) error

ApplyPage commits the received data and its cursor together, or neither.

func (*Store) ArchiveReady

func (s *Store) ArchiveReady(ctx context.Context, home string, empty bool) error

func (*Store) BindNode

func (s *Store) BindNode(ctx context.Context, id string) error

BindNode replaces the offline site identity once, when Tailscale identifies this machine.

func (*Store) Changes

func (s *Store) Changes(ctx context.Context, since int64, exclude []byte, maxBytes int) ([]Change, int64, error)

Changes never splits a source version; next also advances past excluded-only versions.

func (*Store) Close

func (s *Store) Close() error

func (*Store) Cursor

func (s *Store) Cursor(ctx context.Context, site []byte) (int64, error)

func (*Store) DBVersion

func (s *Store) DBVersion(ctx context.Context) (int64, error)

The persisted counter cannot fall when a newer value supersedes the last row of an older version.

func (*Store) DrainArchiveEdits

func (s *Store) DrainArchiveEdits(ctx context.Context) (result error)

DrainArchiveEdits acknowledges a relayed edit only after archive.db has durably committed it.

func (*Store) Main

func (s *Store) Main(ctx context.Context) (MainState, error)

func (*Store) NodeID

func (s *Store) NodeID(ctx context.Context) (string, error)

func (*Store) SetCursor

func (s *Store) SetCursor(ctx context.Context, site []byte, version int64) error

func (*Store) SetMain

func (s *Store) SetMain(ctx context.Context, expected MainState, home string) error

SetMain compares the role shown by the CLI before committing its confirmed switch.

func (*Store) SiteID

func (s *Store) SiteID(ctx context.Context) ([]byte, error)

func (*Store) WriteVersion

func (s *Store) WriteVersion(ctx context.Context, version int64, exclude []byte, out io.Writer) error

WriteVersion freezes one oversized source version in a read snapshot without buffering it all.

Jump to

Keyboard shortcuts

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