Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/entry_verb.go
T
jschoubben cf93d30150 Names N3 (indexer): the indexer names its own buckets from its credential's module
Its 26 `<Module>_<state>` bucket constants (candidates, manifests, verdicts, packages, taken, the
control records, pins, pending and the rest), its consumers, findings and first views were named
from the constant Module; they are now index.Bucket(cred.Module, <state>), and its words name
the state where they named the bucket.

Builds: design 47 G A-296

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 19:18:56 +01:00

149 lines
6.0 KiB
Go

package index
import (
"context"
"encoding/json"
"errors"
"fmt"
"slices"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/keys"
"git.novox.be/novox/mesh-sdk/go/process"
)
// The seat mesh-catalog's verb entry finds a catalog graph entry by its build-input fingerprint (design 47 G §675, §443):
// the indexer keeps, in its old state section entry-index, one key per fingerprint holding the digest of its entry's
// object, written when it stores the entry; entry reads the key and then the object. The state is capped at 8 MiB.
const (
// EntryIndexState is the old state section of the entries' digests by fingerprint.
EntryIndexState = "entry-index"
// EntryIndexTool is the tool that reads a fingerprint's key and its object's digest (class read).
EntryIndexTool = "indexer_entries"
)
// EntryIndexValue is one key of entry-index: the digest of the entry object of the fingerprint the key names.
type EntryIndexValue struct {
V int `json:"v"`
Digest string `json:"digest"`
}
// EntryIndexKey is the key of a fingerprint in entry-index, the fingerprint escaped as a key's name is (§73).
func EntryIndexKey(fingerprint string) string { return keys.EscapeName(fingerprint) }
// indexEntries writes the entry-index key of each entry of rev that has a fingerprint, the digest of its object; a key that
// already holds that digest is not written again. A fingerprint two entries carry holds the later module's, which is the
// newest entry written.
func (ix *Indexer) indexEntries(ctx context.Context, rev meshcatalog.CatalogRevision, entries map[string]meshcatalog.CatalogEntry) error {
if ix.EntryIndex == nil {
return nil
}
var errs []error
for _, ref := range rev.Entries {
fp := entries[ref.Module].Fp
if fp == "" {
continue
}
data, err := json.Marshal(EntryIndexValue{V: keys.Version, Digest: ref.Digest})
if err != nil {
return err
}
err = write(ctx, ix.EntryIndex, EntryIndexKey(fp), func([]byte, bool) ([]byte, bool, error) { return data, false, nil })
if errors.Is(err, process.ErrNotYet) {
ix.logf("catalog graph: the state %s is not on the bus yet, so the entries of this revision are not found by their fingerprint", EntryIndexState)
return nil
}
if err != nil {
errs = append(errs, fmt.Errorf("entry-index of %s: %w", ref.Module, err))
}
}
return errors.Join(errs...)
}
// Entry answers the verb entry: the entry whose key entry-index holds for the fingerprint, read from its object.
func Entry(ctx context.Context, o Objects, index Store, in meshcatalog.EntryInput) (meshcatalog.EntryAnswer, error) {
var out meshcatalog.EntryAnswer
if in.Fingerprint == "" {
return out, errors.New("name the entry's fingerprint (fingerprint)")
}
value, _, found, err := index.Get(ctx, EntryIndexKey(in.Fingerprint))
if err != nil {
return out, fmt.Errorf("the state %s cannot be read: %w", EntryIndexState, err)
}
var v EntryIndexValue
if !found || json.Unmarshal(value, &v) != nil || v.Digest == "" {
return out, fmt.Errorf("no entry of fingerprint %s is kept", in.Fingerprint)
}
data, err := o.Get(ctx, EntryObject(v.Digest))
if errors.Is(err, ErrNoObject) {
return out, fmt.Errorf("the entry object %s of fingerprint %s is not kept", v.Digest, in.Fingerprint)
}
if err != nil {
return out, fmt.Errorf("the entry object %s: %w", v.Digest, err)
}
if err := json.Unmarshal(data, &out.Entry); err != nil {
return out, fmt.Errorf("the entry object %s cannot be read: %w", v.Digest, err)
}
return out, nil
}
// EntryTool is the verb entry as the tool mesh-catalog.entry of the indexer's own runtime, as RevisionTool is revision.
func EntryTool(o Objects, index Store) stdio.Tool {
return stdio.Tool{Name: meshcatalog.Seat + "." + meshcatalog.VerbEntry, Run: func(args map[string]any) (any, error) {
var in meshcatalog.EntryInput
if raw, err := json.Marshal(args); err == nil {
_ = json.Unmarshal(raw, &in)
}
return Entry(context.Background(), o, index, in)
}}
}
// EntriesAnswer is what indexer_entries answers: for a fingerprint its key and its object's digest, and whether the object
// is kept; with none given, every fingerprint the state holds.
type EntriesAnswer struct {
Fingerprint string `json:"fingerprint,omitempty"`
Digest string `json:"digest,omitempty"`
Kept bool `json:"kept,omitempty"`
// Named is the set of `<node>.<module>` whose want or running build names the fingerprint (named.go, §675).
Named []string `json:"named,omitempty"`
Keys []string `json:"keys,omitempty"`
}
// EntriesTool is indexer_entries (class read): one fingerprint's key and the digest of its object, or every key of
// entry-index with none given.
func EntriesTool(o Objects, index, named Store) stdio.Tool {
return stdio.Tool{Name: EntryIndexTool, Description: "Read the indexer's entry-index: the digest of the catalog graph entry of a fingerprint, whether its object is kept and the set of `<node>.<module>` whose want or running build names it (the state named, which keeps the entry while it is not empty); with no fingerprint, every key it holds. indexer_named_drop removes one member.",
Input: map[string]any{"type": "object", "properties": map[string]any{"fingerprint": map[string]any{"type": "string", "description": "the entry's fingerprint; none lists every key"}}},
Run: func(args map[string]any) (any, error) {
ctx := context.Background()
fp, _ := args["fingerprint"].(string)
if fp == "" {
all, err := index.Keys(ctx)
if err != nil {
return nil, err
}
slices.Sort(all)
return EntriesAnswer{Keys: all}, nil
}
out := EntriesAnswer{Fingerprint: fp}
value, _, found, err := index.Get(ctx, EntryIndexKey(fp))
if err != nil {
return nil, err
}
var v EntryIndexValue
if found && json.Unmarshal(value, &v) == nil {
out.Digest = v.Digest
if _, err := o.Get(ctx, EntryObject(v.Digest)); err == nil {
out.Kept = true
}
}
if named != nil {
if out.Named, err = NamedMembers(ctx, named, fp); err != nil {
return nil, err
}
}
return out, nil
}}
}