state

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package state owns BrokerKit's local transactional database and process lease.

Index

Constants

This section is empty.

Variables

View Source
var ErrGrantStateConflict = errors.New("grant state revision conflict")
View Source
var ErrNotFound = sql.ErrNoRows
View Source
var ErrStateInUse = errors.New("broker state directory is already in use")

Functions

This section is empty.

Types

type Database

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

func Open

func Open(ctx context.Context, directory string, options Options) (*Database, error)

func (*Database) Close

func (d *Database) Close() error

func (*Database) CountOperations

func (d *Database) CountOperations(ctx context.Context) (int64, error)

func (*Database) DeleteTerminalOperationsBefore

func (d *Database) DeleteTerminalOperationsBefore(ctx context.Context, cutoff time.Time) (int64, error)

func (*Database) GrantSnapshot

func (d *Database) GrantSnapshot(ctx context.Context) (GrantSnapshot, error)

func (*Database) InsertOperation

func (d *Database) InsertOperation(ctx context.Context, record OperationRecord) error

func (*Database) InsertOperationWithPlan

func (d *Database) InsertOperationWithPlan(ctx context.Context, record OperationRecord, plan PlanRecord) error

InsertOperationWithPlan commits an immutable plan and its operation in one transaction so execution can never observe an unbound operation.

func (*Database) IntegrityCheck

func (d *Database) IntegrityCheck(ctx context.Context) (string, error)

IntegrityCheck runs SQLite's operational consistency check. PRAGMA queries are intentionally kept outside sqlc because sqlc does not generate them.

func (*Database) OperationByID

func (d *Database) OperationByID(ctx context.Context, id string) (OperationRecord, error)

func (*Database) OperationByIdempotency

func (d *Database) OperationByIdempotency(ctx context.Context, clientID, key string) (OperationRecord, error)

func (*Database) OperationForClient

func (d *Database) OperationForClient(ctx context.Context, id, clientID string) (OperationRecord, error)

func (*Database) Plan

func (d *Database) Plan(ctx context.Context, digest string) (PlanRecord, error)

func (*Database) PutPlan

func (d *Database) PutPlan(ctx context.Context, schemaName string, canonical []byte, createdAt time.Time) (string, error)

func (*Database) Queries

func (d *Database) Queries() *dbsql.Queries

func (*Database) SQL

func (d *Database) SQL() *sql.DB

func (*Database) SaveGrantSnapshot

func (d *Database) SaveGrantSnapshot(ctx context.Context, before, after GrantSnapshot) error

SaveGrantSnapshot atomically persists one lifecycle mutation. before is checked inside the SQL transaction so a stale in-memory snapshot cannot overwrite a newer grant revision.

func (*Database) SaveGrantSnapshotWithPlan

func (d *Database) SaveGrantSnapshotWithPlan(ctx context.Context, before, after GrantSnapshot, plan PlanRecord) error

SaveGrantSnapshotWithPlan commits an immutable plan and its grant lifecycle mutation together. A failure in either half rolls the complete request back.

func (*Database) UnfinishedOperations

func (d *Database) UnfinishedOperations(ctx context.Context) ([]OperationRecord, error)

func (*Database) UpdateOperation

func (d *Database) UpdateOperation(ctx context.Context, record OperationRecord, expectedRevision int64) (bool, error)

func (*Database) UpdateOperationWithPlan

func (d *Database) UpdateOperationWithPlan(ctx context.Context, record OperationRecord, expectedRevision int64, plan PlanRecord) (bool, error)

UpdateOperationWithPlan commits an immutable plan and its operation binding in one transaction.

type GrantDecisionRecord

type GrantDecisionRecord struct {
	Scope, RequestID, Action, IdempotencyKey, CommandHash string
	ResultJSON, PreviousJSON                              []byte
	EventCursor                                           string
	CommittedAt                                           time.Time
}

type GrantLifecycleRecord

type GrantLifecycleRecord struct {
	Sequence    uint64
	Cursor      string
	GrantID     string
	Kind        string
	Revision    int64
	OccurredAt  time.Time
	PayloadJSON []byte
}

type GrantRecord

type GrantRecord struct {
	ID, DecisionTokenVerifier, Client, ClientRequestID, Operation string
	TargetJSON, AttrsJSON, MetadataJSON                           []byte
	PlanDigest, Reason, Status                                    string
	Revision                                                      int64
	CreatedAt, PendingExpiresAt                                   time.Time
	ExpiresAt                                                     time.Time
	Duration, RequestedDuration, PendingTimeout                   time.Duration
	DecidedAt                                                     time.Time
	DecidedBy, DecidedOnBehalfOf                                  string
	UsedAt                                                        time.Time
	UsedCount, UseRevision, ReservedCount                         int
	ReservedAt                                                    time.Time
	ReservationRetained                                           bool
	ReservationRevision                                           int
	MaxUses, RequestedMaxUses                                     *int
	RequestedMaxUsesDefaulted                                     bool
	ExpiredFrom                                                   string
	NotificationJSON                                              []byte
	NotificationStatus                                            string
	NotificationClaimedAt, NotificationClaimUntil                 time.Time
	NotificationDeliveryUnresolved                                bool
}

type GrantSnapshot

type GrantSnapshot struct {
	Grants    []GrantRecord
	Events    []GrantLifecycleRecord
	Decisions []GrantDecisionRecord
	Outbox    []NotificationOutboxRecord
}

type NotificationOutboxRecord

type NotificationOutboxRecord struct {
	ID                        int64
	GrantID, Kind             string
	PayloadJSON               []byte
	IdempotencyKey, Status    string
	Attempts                  int
	AvailableAt, ClaimedUntil time.Time
	DeliveredAt               time.Time
	LastErrorCode             string
	CreatedAt, UpdatedAt      time.Time
}

type OperationRecord

type OperationRecord struct {
	ID               string
	APIVersion       string
	Broker           string
	ClientID         string
	IdempotencyKey   string
	Operation        string
	TargetJSON       []byte
	ArgumentsJSON    []byte
	Reason           string
	State            string
	Revision         int64
	CreatedAt        time.Time
	UpdatedAt        time.Time
	TerminalAt       *time.Time
	ApprovalID       string
	PresentationJSON []byte
	ResultJSON       []byte
	ErrorJSON        []byte
	PlanDigest       string
}

type Options

type Options struct {
	BusyTimeout time.Duration
}

type PlanRecord

type PlanRecord struct {
	Digest     string
	SchemaName string
	Canonical  []byte
	CreatedAt  time.Time
}

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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