Files
beacon-server/db/background_state.go
T
MrAlders0n 8e0fd4b535 Routes: one statement per filter shape, exact bounded retention, stamp-free reconfirm (#235)
* feat(db): background_state table and the (hop_count, iata) route index

* perf(db): one /routes statement per filter shape, hop pair on custom plans

* feat(db): keyset route retention and grace slices at exact cutoffs

* feat(background): hourly route retention with a bounded grace walk and watermark

* fix(background): bound the grace walk at microsecond cutoffs

* feat(db): stamp-free reconfirm sweep over the route primary key

* feat(background): pace route reconfirm at routes.reconfirm_rows_per_hour, no stamps

* test(routes): drive route retention end to end and tidy comments

* fix(profiling): sample during the hourly route retention run
2026-10-19 05:39:38 -04:00

44 lines
1.1 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) {
raw, err := s.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})
}