diff --git a/go/catalogue/caps.go b/go/catalogue/caps.go new file mode 100644 index 00000000..8c0faa7b --- /dev/null +++ b/go/catalogue/caps.go @@ -0,0 +1,58 @@ +package catalogue + +import ( + "encoding/json" + "strconv" +) + +// Every kept store of a delivery module names its cap (design 47 H2 §812, §814): the manifest validator refuses a +// delivery module's state, log or objects entry with no cap. This is what it reads of a manifest; which module is a +// delivery module (one claiming a seat of the delivery system) and the foundation parts' old sections are its own to +// judge. + +// Uncapped is one state, log or objects entry of a manifest that names no cap. +type Uncapped struct { + // Name is the entry's local name. + Name string `json:"name"` + // Kind is state, log or objects (the template's, or the old section's). + Kind string `json:"kind"` + // OldSection says the entry is in the old section of its kind (`state`, `logs`, `objects`), not an entry of + // `requires`. + OldSection bool `json:"old-section,omitempty"` +} + +// UncappedStores is every state, log and objects entry of m that names no cap, the old sections' (max-mib) and the +// bus-state entries' (the option max-mib) alike, in the manifest's order. A log's default cap is not one it names. +func (m Manifest) UncappedStores() []Uncapped { + var out []Uncapped + for _, s := range m.State { + if s.MaxMiB == 0 { + out = append(out, Uncapped{Name: s.Name, Kind: TemplateState, OldSection: true}) + } + } + for _, l := range m.Logs { + if l.MaxMiB == 0 { + out = append(out, Uncapped{Name: l.Name, Kind: TemplateLog, OldSection: true}) + } + } + for _, o := range m.Objects { + if o.MaxMiB == 0 { + out = append(out, Uncapped{Name: o.Name, Kind: TemplateObjects, OldSection: true}) + } + } + for _, e := range m.BusState { + switch e.Template { + case TemplateState, TemplateLog, TemplateObjects: + if !namesCap(e.Options[OptionCapMiB]) { + out = append(out, Uncapped{Name: e.Name, Kind: e.Template}) + } + } + } + return out +} + +// namesCap is whether an option's value is a cap above zero. +func namesCap(raw json.RawMessage) bool { + n, err := strconv.ParseFloat(string(raw), 64) + return err == nil && n > 0 +} diff --git a/go/catalogue/caps_test.go b/go/catalogue/caps_test.go new file mode 100644 index 00000000..92d051f4 --- /dev/null +++ b/go/catalogue/caps_test.go @@ -0,0 +1,27 @@ +package catalogue + +import ( + "reflect" + "testing" +) + +// Design 47 H2 §812, §814: the validator reads every state, log and objects entry of a manifest that names no cap, in +// the old sections and in requires alike; a capped entry, and an entry of another template, is none of them. +func TestAManifestSaysEachKeptStoreThatNamesNoCap(t *testing.T) { + m, err := decodeForTest([]byte(`{"module":"x","version":"1", + "state":["seen",{"name":"held","max-mib":8}], + "logs":[{"name":"acts"},{"name":"kept","max-mib":64}], + "objects":[{"name":"outputs","max-mib":256}], + "requires":[{"name":"performed","storage":"key-value","template":"state","options":{"history":1}}, + {"name":"taken","storage":"key-value","template":"state","options":{"max-mib":8,"key-ttl":true}}, + {"name":"archives","storage":"object-store","template":"objects"}, + {"name":"finding-events","storage":"stream","template":"events"}]}`)) + if err != nil { + t.Fatal(err) + } + want := []Uncapped{{Name: "seen", Kind: "state", OldSection: true}, {Name: "acts", Kind: "log", OldSection: true}, + {Name: "performed", Kind: "state"}, {Name: "archives", Kind: "objects"}} + if got := m.UncappedStores(); !reflect.DeepEqual(got, want) { + t.Fatalf("the uncapped stores are %+v, want %+v", got, want) + } +}