The indexer's CAS Findings store is the held writer through the small FindingWriter interface (Write, Clear, Held): the unread-call, accepted-unsaid, seat-definition and out-of-step findings are raised and cleared through it, the unsaid ones read from Held. followAssignments follows the assigner's assignments through the module's ProcessReader (the reader raises and clears the first view's finding), the emitter and the family-not-heard finding write through the same writer, and the view-waits finding of the seat git is written by hand through Write and Clear (A-28). The old resolved words go (A-22). The integration bed has the findings stream and the test sees finding-written said on it. Builds: design 47 G A-29, A-26, A-28, A-22, A-3, A-14, A-17, A-30, A-37, A-38, §689, §687, §461 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
128 lines
3.1 KiB
Go
128 lines
3.1 KiB
Go
package index
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"maps"
|
|
"slices"
|
|
"sync"
|
|
|
|
mcc "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient"
|
|
mcseat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
|
|
"git.novox.be/novox/mesh-sdk/go/views"
|
|
)
|
|
|
|
// rawList is the list a kept predefined value holds, as it was written.
|
|
func rawList(value []byte) json.RawMessage {
|
|
var kept struct {
|
|
List json.RawMessage `json:"list"`
|
|
}
|
|
_ = json.Unmarshal(value, &kept)
|
|
return kept.List
|
|
}
|
|
|
|
// memObjects is an object bucket in memory with a cap: a put past the cap is refused as the bus's DiscardNew refuses it.
|
|
type memObjects struct {
|
|
mu sync.Mutex
|
|
objs map[string][]byte
|
|
puts int
|
|
capacity int64
|
|
}
|
|
|
|
func newMemObjects(capacity int64) *memObjects {
|
|
return &memObjects{objs: map[string][]byte{}, capacity: capacity}
|
|
}
|
|
|
|
func (m *memObjects) used() int64 {
|
|
var n int64
|
|
for _, d := range m.objs {
|
|
n += int64(len(d))
|
|
}
|
|
return n
|
|
}
|
|
|
|
func (m *memObjects) Put(_ context.Context, name string, data []byte) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if m.capacity > 0 && m.used()-int64(len(m.objs[name]))+int64(len(data)) > m.capacity {
|
|
return errObjectsFullForTest
|
|
}
|
|
m.objs[name] = slices.Clone(data)
|
|
m.puts++
|
|
return nil
|
|
}
|
|
|
|
func (m *memObjects) Get(_ context.Context, name string) ([]byte, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
d, ok := m.objs[name]
|
|
if !ok {
|
|
return nil, ErrNoObject
|
|
}
|
|
return slices.Clone(d), nil
|
|
}
|
|
|
|
func (m *memObjects) Delete(_ context.Context, name string) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
delete(m.objs, name)
|
|
return nil
|
|
}
|
|
|
|
func (m *memObjects) Names(context.Context) ([]string, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
return slices.Sorted(maps.Keys(m.objs)), nil
|
|
}
|
|
|
|
func (m *memObjects) Fill(context.Context) (int64, int64, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
return m.used(), m.capacity, nil
|
|
}
|
|
|
|
var errObjectsFullForTest = &objectsFullError{}
|
|
|
|
type objectsFullError struct{}
|
|
|
|
func (*objectsFullError) Error() string { return "maximum bytes exceeded" }
|
|
|
|
// memFindings is the finding writer in memory (FindingWriter): what is written is kept by its key as the finding's JSON, what
|
|
// is cleared is gone, and Held answers what is kept.
|
|
type memFindings struct {
|
|
vals map[string][]byte
|
|
err error // every write's error
|
|
}
|
|
|
|
func newFindings() *memFindings { return &memFindings{vals: map[string][]byte{}} }
|
|
|
|
func (m *memFindings) Write(_ context.Context, f mcseat.Finding) error {
|
|
if m.err != nil {
|
|
return m.err
|
|
}
|
|
raw, err := json.Marshal(f)
|
|
m.vals[views.FindingKey(f.Scope, f.ID, f.Kind)] = raw
|
|
return err
|
|
}
|
|
|
|
func (m *memFindings) Clear(_ context.Context, scope, id, kind, _ string) error {
|
|
delete(m.vals, views.FindingKey(scope, id, kind))
|
|
return nil
|
|
}
|
|
|
|
func (m *memFindings) Held() []mcseat.Finding {
|
|
var out []mcseat.Finding
|
|
for _, k := range slices.Sorted(maps.Keys(m.vals)) {
|
|
var f mcseat.Finding
|
|
if json.Unmarshal(m.vals[k], &f) == nil {
|
|
out = append(out, f)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// unsaidKey is the key of module's unsaid finding.
|
|
func unsaidKey(module string) string {
|
|
return views.FindingKey(mcc.ScopeModule, keyID(unsaidID(module)), UnsaidKind)
|
|
}
|