A want counts as applied by its fingerprint and a motion event is keyed by
it; the want's newest and rule, and the state held back read from them, go
with the assigner's holds. A branch's source is {v, source, call, heads}
with no group, its source-moved is read by its source, and the delivered
event names no group. indexer_pending's held take-ins are said in the
tiers' pending words with their branch, source, why and since, naming no
module. The view reads no source stamp on a module. Its seat list, its
first-view tool answers and its manifest name no callers; its source invoke
reads call, not group.
Builds: design 47 H §737, §744, §750, §752, §764 (the gate's groups; the indexer's pending and groups; the assigner's holds; the callers lists); lead answers H-A-26, H-A-37, H-A-48, H-A-66, H-A-78, H-A-102, H-A-109 (hq 3433db0e)
Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
154 lines
5.2 KiB
Go
154 lines
5.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"mesh-delivery-view/internal/view"
|
|
"sync"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/sourcebranches"
|
|
"git.novox.be/novox/mesh-sdk/go/keys"
|
|
"git.novox.be/novox/mesh-sdk/go/views"
|
|
)
|
|
|
|
// sourceState is the state of the view of one watched branch's source in the view's reader: `mesh-delivery.source.<branch>`.
|
|
const sourceState = "source"
|
|
|
|
// adopter is how branchSources puts a branch's view on the view's one reader and takes it off (views.ProcessReader).
|
|
type adopter interface {
|
|
Adopt(f *views.Follower, seat, event string, hear func(context.Context, []byte) error) (*views.Picture, error)
|
|
Drop(owner, state string) error
|
|
}
|
|
|
|
// branchSources follows the source of each watched branch on its own (design 47 G §507, §512, A-173): per branch the
|
|
// first view of the seat mesh-delivery's verb source given that branch, then each source-moved of that branch, asked
|
|
// again until the revision heard is held; each answer goes to the model as the branch's SourceBucket entry. The branches
|
|
// are the ones the branch follower follows (ensure); none is named here.
|
|
type branchSources struct {
|
|
ctx context.Context
|
|
reader adopter
|
|
call func(key string, body any) (json.RawMessage, error)
|
|
model *view.Model
|
|
logger *log.Logger
|
|
|
|
mu sync.Mutex
|
|
branches map[string]*views.Follower // by escaped branch
|
|
}
|
|
|
|
// ensure follows the source of every branch named and drops the view of each followed branch no longer named.
|
|
func (s *branchSources) ensure(followed []string) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if s.branches == nil {
|
|
s.branches = map[string]*views.Follower{}
|
|
}
|
|
named := map[string]bool{}
|
|
for _, branch := range followed {
|
|
esc := keys.EscapeName(branch)
|
|
named[esc] = true
|
|
if s.branches[esc] != nil {
|
|
continue
|
|
}
|
|
f := s.follower(branch)
|
|
if _, err := s.reader.Adopt(f, meshdelivery.Seat, meshdelivery.EventSourceMoved, s.hear(branch, f)); err != nil {
|
|
s.logger.Printf("the source of %s is not followed, and is adopted again at the next change of the catalog half: %v", branch, err)
|
|
continue
|
|
}
|
|
s.branches[esc] = f
|
|
}
|
|
for esc := range s.branches {
|
|
if named[esc] {
|
|
continue
|
|
}
|
|
if err := s.reader.Drop(meshdelivery.Seat, views.BranchState(sourceState, mustUnescape(esc))); err != nil {
|
|
s.logger.Printf("the source of %s is not dropped: %v", esc, err)
|
|
continue
|
|
}
|
|
delete(s.branches, esc)
|
|
s.model.Apply(view.Entry{Bucket: view.SourceBucket, Key: esc, Deleted: true, At: time.Now()})
|
|
}
|
|
}
|
|
|
|
func mustUnescape(esc string) string {
|
|
name, err := keys.UnescapeName(esc)
|
|
if err != nil {
|
|
return esc
|
|
}
|
|
return name
|
|
}
|
|
|
|
// follower is the view of one branch's source: its one value, keyed by the escaped branch, the verb's answer.
|
|
func (s *branchSources) follower(branch string) *views.Follower {
|
|
esc := keys.EscapeName(branch)
|
|
f := &views.Follower{Owner: meshdelivery.Seat, Tool: view.SourceVerb, State: views.BranchState(sourceState, branch)}
|
|
f.Ask = func(context.Context, string) (views.Answer, error) {
|
|
a, err := view.SourceOf(s.call, branch)
|
|
if err != nil {
|
|
return views.Answer{}, err
|
|
}
|
|
raw, err := json.Marshal(a)
|
|
if err != nil {
|
|
return views.Answer{}, err
|
|
}
|
|
return views.Answer{V: 1, Owner: meshdelivery.Seat, State: f.State, Revision: uint64(a.Revision), Created: sourcebranches.Created,
|
|
Values: map[string]json.RawMessage{esc: raw}, At: time.Now()}, nil
|
|
}
|
|
f.Changed = func(_ string, value json.RawMessage, deleted bool, at time.Time) {
|
|
e := view.Entry{Bucket: view.SourceBucket, Key: esc, Deleted: deleted, At: at}
|
|
if !deleted {
|
|
var a meshdelivery.SourceAnswer
|
|
if err := json.Unmarshal(value, &a); err != nil {
|
|
s.logger.Printf("the source of %s is not taken: %v", branch, err)
|
|
return
|
|
}
|
|
se, err := view.SourceEntry(branch, a)
|
|
if err != nil {
|
|
s.logger.Printf("the source of %s is not taken: %v", branch, err)
|
|
return
|
|
}
|
|
se.At = at
|
|
e = se
|
|
}
|
|
s.model.Apply(e)
|
|
}
|
|
return f
|
|
}
|
|
|
|
// revisionOf is the source revision f holds of the branch, 0 when none.
|
|
func revisionOf(f *views.Follower, esc string) int64 {
|
|
raw, ok := f.Get(esc)
|
|
if !ok {
|
|
return 0
|
|
}
|
|
var a meshdelivery.SourceAnswer
|
|
if json.Unmarshal(raw, &a) != nil {
|
|
return 0
|
|
}
|
|
return a.Revision
|
|
}
|
|
|
|
// hear is the handler of source-moved for one branch's view: another branch's event is not its own; a revision it does not
|
|
// hold yet asks the first view again until it does, on its own goroutine, so the consumer is never held.
|
|
func (s *branchSources) hear(branch string, f *views.Follower) func(context.Context, []byte) error {
|
|
esc := keys.EscapeName(branch)
|
|
return func(ctx context.Context, data []byte) error {
|
|
var ev meshdelivery.SourceMovedEvent
|
|
if err := json.Unmarshal(data, &ev); err != nil || ev.Branch == "" {
|
|
return fmt.Errorf("not a %s event: %s", meshdelivery.EventSourceMoved, sourcebranches.ClipBytes(data))
|
|
}
|
|
if ev.Branch != branch && ev.Branch != esc {
|
|
return nil
|
|
}
|
|
held := func() bool { return revisionOf(f, esc) >= ev.Source }
|
|
if held() {
|
|
return nil
|
|
}
|
|
go sourcebranches.CatchUp(s.ctx, f, held, fmt.Sprintf("source of %s moved to %d", branch, ev.Source))
|
|
return nil
|
|
}
|
|
}
|