From d06835bf930e0cab0360deb836bb5f62a380241d Mon Sep 17 00:00:00 2001 From: jochen Date: Sat, 21 Nov 2026 11:42:45 +0100 Subject: [PATCH] Design 47 H1: a retired kind may name the ids its writer once wrote MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Where another writer of the same state owns a retired kind now, clearing every key of it would clear that writer's findings. A RetiredKind entry names its kind and, optionally, the exact ids and id prefixes this writer wrote under it; Start clears only those keys, every key of the kind when none are named. Builds: design 47 H H-A-247 (3), H-A-249, G ยง580 --- go/views/writer.go | 39 ++++++++++++++++++++++++++++++++------- go/views/writer_test.go | 17 +++++++++++++++-- 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/go/views/writer.go b/go/views/writer.go index 6a8d680f..838ce720 100644 --- a/go/views/writer.go +++ b/go/views/writer.go @@ -168,10 +168,11 @@ type WriterConfig[F, C, S, A any] struct { // naming it, its statement carries them as `kinds` and deletes only keys of them. None given, it owns every kind. // The kinds of a views reader (ViewKinds) are the reader's own and are always the writer's besides. Kinds []string - // Retired are kinds no code of the part raises any more, last owned by this writer (design 47 H H-A-247 (3)): a held - // writer's Start clears their keys, saying finding-cleared so the keeper clears their conditions, and never seeds them; - // a Write of one is refused as of any kind not owned. - Retired []string + // Retired are kinds no code of the part raises any more, last written by this writer (design 47 H H-A-247 (3), + // H-A-249): a held writer's Start clears their keys, saying finding-cleared so the keeper clears their conditions, and + // never seeds them; a Write of one is refused as of any kind not owned. An entry naming ids clears only the keys of + // those ids, where another writer owns the kind now. + Retired []RetiredKind // JS is the bus connection's JetStream; Conn, when given, lets the writer hear the connection made again. JS jetstream.JetStream Conn *nats.Conn @@ -484,10 +485,34 @@ func (w *Writer[F, C, S, A]) statedKinds() []string { } // ownsKey says whether key is one of a kind the writer owns: its last part is the kind (`..`). -// retiredKey says whether key is of a kind in the writer's retired list (H-A-247 (3)). +// RetiredKind is a kind a writer once wrote and no code raises any more (H-A-247 (3)). Where another writer of the same state +// owns the kind now, IDs and Prefixes name the ids this writer wrote under it, as they stand in a key (H-A-249): only a key +// whose id is one of IDs or starts with one of Prefixes is cleared. Neither given, every key of the kind is. +type RetiredKind struct { + Kind string + IDs []string + Prefixes []string +} + +// covers says whether the key of scope, id and kind is one the entry retires. +func (r RetiredKind) covers(id, kind string) bool { + if kind != r.Kind { + return false + } + if len(r.IDs) == 0 && len(r.Prefixes) == 0 { + return true + } + return slices.Contains(r.IDs, id) || slices.ContainsFunc(r.Prefixes, func(p string) bool { return strings.HasPrefix(id, p) }) +} + +// retiredKey says whether key is one the writer's retired list clears (H-A-247 (3), H-A-249). func (w *Writer[F, C, S, A]) retiredKey(key string) bool { - i := strings.LastIndex(key, ".") - return i >= 0 && slices.Contains(w.cfg.Retired, key[i+1:]) + _, rest, _ := strings.Cut(key, ".") + i := strings.LastIndex(rest, ".") + if i < 0 { + return false + } + return slices.ContainsFunc(w.cfg.Retired, func(r RetiredKind) bool { return r.covers(rest[:i], rest[i+1:]) }) } func (w *Writer[F, C, S, A]) ownsKey(key string) bool { diff --git a/go/views/writer_test.go b/go/views/writer_test.go index 148d780a..ddeb493b 100644 --- a/go/views/writer_test.go +++ b/go/views/writer_test.go @@ -681,11 +681,19 @@ func subHeldWriterSeedsAtStart(t *testing.T) { gone.Kind = "r" rawGone, _ := json.Marshal(gone) owned.m["module.gone.r"] = rawGone + // H-A-249: a retired entry with ids clears only those keys of its kind; the same kind's key of another writer stays + for _, id := range []string{"mine", "mine2.x", "theirs"} { + f := finding(id, "kind s") + f.Kind = "s" + rawS, _ := json.Marshal(f) + owned.m["module."+id+".s"] = rawS + } read := &readState{fakeState: owned} var logs []string var sent []said - ocfg := testConfig{Module: "m", State: read, Node: "n1", Asked: asked, Kinds: []string{"k"}, Retired: []string{"r"}, - Logf: func(f string, a ...any) { mu.Lock(); logs = append(logs, fmt.Sprintf(f, a...)); mu.Unlock() }, + ocfg := testConfig{Module: "m", State: read, Node: "n1", Asked: asked, Kinds: []string{"k"}, + Retired: []views.RetiredKind{{Kind: "r"}, {Kind: "s", IDs: []string{"mine"}, Prefixes: []string{"mine2."}}}, + Logf: func(f string, a ...any) { mu.Lock(); logs = append(logs, fmt.Sprintf(f, a...)); mu.Unlock() }, Publish: func(_ context.Context, subject string, data []byte, _ string) error { mu.Lock() sent = append(sent, said{subject, data}) @@ -707,6 +715,11 @@ func subHeldWriterSeedsAtStart(t *testing.T) { if e, _ := owned.Get("module.gone.r"); e != nil { t.Fatal("the retired kind's key stays after Start") } + for key, stays := range map[string]bool{"module.mine.s": false, "module.mine2.x.s": false, "module.theirs.s": true} { + if e, _ := owned.Get(key); (e != nil) != stays { + t.Errorf("after Start the key %s stands: %v, want %v", key, e != nil, stays) + } + } mu.Lock() clearedGone := slices.ContainsFunc(sent, func(e said) bool { return e.subject == subjects.Cleared && strings.Contains(string(e.data), `"kind":"r"`) && strings.Contains(string(e.data), `"id":"gone"`)