Download this file
| 1 | package seeds |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | ) |
| 7 | |
| 8 | // RowSeed is what a registry row carries of its record's seed (column |
| 9 | // seed_source): enough for the activator to place the snapshot on first |
| 10 | // wake (spec §3.4) without reading the record. |
| 11 | type RowSeed struct { |
| 12 | ID string `json:"id"` |
| 13 | Kind string `json:"kind"` |
| 14 | SHA256 string `json:"sha256,omitempty"` |
| 15 | Bytes int64 `json:"bytes,omitempty"` |
| 16 | State string `json:"state"` |
| 17 | } |
| 18 | |
| 19 | // Encode is the column's value: compact JSON in field order. |
| 20 | func (r RowSeed) Encode() string { |
| 21 | b, _ := json.Marshal(r) // strings and an int64 cannot fail to marshal |
| 22 | return string(b) |
| 23 | } |
| 24 | |
| 25 | // ParseRowSeed reads a seed_source column. "" is no seed: (nil, nil). |
| 26 | func ParseRowSeed(s string) (*RowSeed, error) { |
| 27 | if s == "" { |
| 28 | return nil, nil |
| 29 | } |
| 30 | var r RowSeed |
| 31 | if err := json.Unmarshal([]byte(s), &r); err != nil { |
| 32 | return nil, fmt.Errorf("seeds: seed_source column: %w", err) |
| 33 | } |
| 34 | if !ValidID(r.ID) { |
| 35 | return nil, fmt.Errorf("seeds: seed_source column: id %q is not a seed id", r.ID) |
| 36 | } |
| 37 | return &r, nil |
| 38 | } |
| 39 | |
| 40 | // Placeable reports whether the restore fallback applies (spec §3.4: a row |
| 41 | // whose record seed is ready, swapped or placed). |
| 42 | func (r RowSeed) Placeable() bool { |
| 43 | switch r.State { |
| 44 | case StateReady, StateSwapped, StatePlaced: |
| 45 | return true |
| 46 | } |
| 47 | return false |
| 48 | } |
| 49 | |