Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/helpers_test.go
T
jschoubben 0fffea89b1 B1 (indexer): the indexer's own name is its credential's module, no constant
index.Module is gone: the indexer and its registry carry the credential's module (findings' observer and ids, its own
entry and graph key, the history subject, the views emitter and reader), the tools take the served-as module.

Builds: design 47 G A-296, §428

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 23:37:54 +01:00

132 lines
3.3 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(testSelf, module)), UnsaidKind)
}
// testSelf is the module the tests' indexer runs as, its credential's: its findings, its own entry and its graph key are named
// from it, never from a constant (design 47 G A-296).
const testSelf = "acme-indexer"