Files
beacon-server/db/background_state.go
MrAlders0n 5ded8d925f Channel messages by site: a read model that serves region and scope pages while trusted (#246)
* feat(db): channel_message_iatas table and indexes

* feat(ingest): record channel message sites at insert and on every later hearing

* feat(db): channel message site backfill and two-range reconcile

* feat(db): serve region and scope message pages from the site read model when trusted

* feat: boot repair, periodic reconcile and first-time verification for the message site read model

* test(planguard): channel message site statements, diff and storediff cases

* feat(background): log when a read-only server trusts the message site read model

* test(planguard): storediff vars in query values, and message cases read from the data

* fix(ingest): record a stored hearing's channel message site right after the observation insert

* fix: bounded message dispatch for filtered all-channel pages, fresher read-only trust, earlier invalidation

* fix: record a hearing's site after its path is marked and skip the backstop when the message was found; read-only trust ends at the writer's deadline
2026-10-19 21:20:21 -04:00

49 lines
1.3 KiB
Go

// Copyright 2026 Beacon Contributors
// SPDX-License-Identifier: AGPL-3.0-or-later
package db
import (
"context"
"encoding/json"
"errors"
sqlc "github.com/MeshCore-Beacon/beacon-server/db/sqlc"
"github.com/jackc/pgx/v5"
)
// LoadState unmarshals the background_state value under key into v. It returns false, nil when
// the key has no row.
func (s *Store) LoadState(ctx context.Context, key string, v any) (bool, error) {
return loadState(ctx, s.q, key, v)
}
// loadState reads key through q, which may be bound to one connection or a transaction.
func loadState(ctx context.Context, q sqlc.Querier, key string, v any) (bool, error) {
raw, err := q.GetBackgroundState(ctx, key)
if errors.Is(err, pgx.ErrNoRows) {
return false, nil
}
if err != nil {
return false, err
}
if err := json.Unmarshal(raw, v); err != nil {
return false, err
}
return true, nil
}
// SaveState stores v as JSON under key.
func (s *Store) SaveState(ctx context.Context, key string, v any) error {
return saveState(ctx, s.q, key, v)
}
// saveState marshals v and upserts it through q, which may be bound to a transaction.
func saveState(ctx context.Context, q sqlc.Querier, key string, v any) error {
raw, err := json.Marshal(v)
if err != nil {
return err
}
return q.UpsertBackgroundState(ctx, sqlc.UpsertBackgroundStateParams{Key: key, Value: raw})
}