The recompute takes each module's set from the state imports (a branch's copy of another set gives way to it), with main's import form while main's value holds that set, so the form costs no rebuild; a branch's take-in reads the state through an overlay and drops none of it. Builds: design 47 H2 H-A-519, H-A-529, H-A-532 DMC-6
193 lines
8.1 KiB
Go
193 lines
8.1 KiB
Go
package index
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"slices"
|
|
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo"
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
)
|
|
|
|
// ImportsState is the indexer's state imports (design 47 H2 H-A-527, H-A-532 DQ-3): one key per module, the module's
|
|
// modules key, holding the import set main's last build of the module reported (H-A-517, H-A-519) with each package's
|
|
// digest at that build (DQ-4). It is written only when an imports-reported is kept, and its key is deleted with the
|
|
// module's modules key; the tool indexer_imports reads it.
|
|
const ImportsState = "imports"
|
|
|
|
// ImportsTool reads one module's import set (H-A-527, H-A-532 DQ-3).
|
|
const ImportsTool = "indexer_imports"
|
|
|
|
// ImportSet is a value of the state imports: the module, the fingerprint and commit of the build that reported the set,
|
|
// and each imported mesh package with its digest; a package no published module held when it was kept has none.
|
|
type ImportSet struct {
|
|
Module string `json:"module"`
|
|
Fingerprint string `json:"fingerprint"`
|
|
Commit string `json:"commit"`
|
|
Imports []ImportedPackage `json:"imports"`
|
|
}
|
|
|
|
// ImportedPackage is one package of an import set, by import path, with its digest.
|
|
type ImportedPackage struct {
|
|
Package string `json:"package"`
|
|
Digest string `json:"digest,omitempty"`
|
|
}
|
|
|
|
// ErrImportsUnreadable says an imports-reported does not fit its shape: the message is said and acknowledged, never
|
|
// given again.
|
|
var ErrImportsUnreadable = errors.New("not an imports-reported the indexer can keep")
|
|
|
|
// HearImportsReported keeps the import set a build module reported at a build's end (design 47 H2 H-A-517), when its
|
|
// fingerprint is the one main's modules state holds for the module (H-A-532 DMC-2); one of a fingerprint the module no
|
|
// longer holds is left (H-A-519). A body naming no module or fingerprint is ErrImportsUnreadable.
|
|
func (ix *Indexer) HearImportsReported(ctx context.Context, e nodebuildgo.ImportsReported) error {
|
|
if e.Module == "" || e.Fingerprint == "" {
|
|
return fmt.Errorf("%w: it names no module or no fingerprint", ErrImportsUnreadable)
|
|
}
|
|
_, err := ix.keepImports(ctx, reported{module: e.Module, fp: e.Fingerprint, commit: e.Commit, imports: slices.Clone(e.Imports)})
|
|
return err
|
|
}
|
|
|
|
// ImportSetOf is the import set the state imports keeps for the module key; found is false when it keeps none.
|
|
func (ix *Indexer) ImportSetOf(ctx context.Context, key string) (set ImportSet, found bool, err error) {
|
|
if ix.Imports == nil {
|
|
return ImportSet{}, false, nil
|
|
}
|
|
v, _, found, err := ix.Imports.Get(ctx, key)
|
|
if err != nil || !found {
|
|
return ImportSet{}, false, err
|
|
}
|
|
if err := json.Unmarshal(v, &set); err != nil {
|
|
return ImportSet{}, false, fmt.Errorf("%s.%s is not an import set: %w", ImportsState, key, err)
|
|
}
|
|
return set, true, nil
|
|
}
|
|
|
|
// importSetHeld says whether the state imports already holds e for key, the same fingerprint and packages; a store that
|
|
// keeps none holds it, so nothing is written for it.
|
|
func (ix *Indexer) importSetHeld(ctx context.Context, key string, e reported) bool {
|
|
if ix.Imports == nil {
|
|
return true
|
|
}
|
|
set, found, err := ix.ImportSetOf(ctx, key)
|
|
if err != nil || !found || set.Fingerprint != e.fp {
|
|
return false
|
|
}
|
|
return slices.EqualFunc(set.Imports, e.imports, func(p ImportedPackage, imp string) bool { return p.Package == imp })
|
|
}
|
|
|
|
// writeImportSet writes e with its packages' digests into the state imports at key.
|
|
func (ix *Indexer) writeImportSet(ctx context.Context, key string, e reported, digests map[string]string) error {
|
|
if ix.Imports == nil {
|
|
return nil
|
|
}
|
|
set := ImportSet{Module: e.module, Fingerprint: e.fp, Commit: e.commit, Imports: []ImportedPackage{}}
|
|
for _, imp := range e.imports {
|
|
set.Imports = append(set.Imports, ImportedPackage{Package: imp, Digest: digests[imp]})
|
|
}
|
|
data, err := json.Marshal(set)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := write(ctx, ix.Imports, key, func([]byte, bool) ([]byte, bool, error) { return data, false, nil }); err != nil {
|
|
return fmt.Errorf("%s.%s: %w", ImportsState, key, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// dropImportSet deletes the module key's import set, as its modules key is deleted (H-A-532 DQ-3).
|
|
func (ix *Indexer) dropImportSet(ctx context.Context, key string) error {
|
|
if ix.Imports == nil {
|
|
return nil
|
|
}
|
|
if err := write(ctx, ix.Imports, key, func(_ []byte, found bool) ([]byte, bool, error) { return nil, found, nil }); err != nil {
|
|
return fmt.Errorf("%s.%s: %w", ImportsState, key, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ImportsReadTool is indexer_imports (H-A-527): one module's import set from the state imports, each package with its
|
|
// digest and the fingerprint and commit of the build that reported it, or that none is kept, so it moves by its go.mod's
|
|
// requirements (H-A-528).
|
|
func ImportsReadTool(st Store) stdio.Tool {
|
|
return stdio.Tool{Name: ImportsTool,
|
|
Description: "One module's import set: the mesh packages its programs import, as the build of main's fingerprint reported " +
|
|
"them, each with its digest at that build, and that build's fingerprint and commit (design 47 H2 H-A-527); a module " +
|
|
"with none kept moves by its go.mod's requirements until its first report (H-A-528).",
|
|
Input: map[string]any{"type": "object", "required": []string{"module"}, "properties": map[string]any{
|
|
"module": map[string]any{"type": "string", "description": "the module, such as avahi"}}},
|
|
Run: func(args map[string]any) (any, error) {
|
|
module, _ := args["module"].(string)
|
|
key, err := meshcatalogclient.ModulesKey(module)
|
|
if module == "" || err != nil {
|
|
return nil, fmt.Errorf("%s names a module: %q is none", ImportsTool, module)
|
|
}
|
|
set, found, err := (&Indexer{Imports: st}).ImportSetOf(context.Background(), key)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("the state %s cannot be read: %w", ImportsState, err)
|
|
}
|
|
if !found {
|
|
return map[string]any{"module": module, "kept": false,
|
|
"said": "no import set is kept for " + module + ": it moves by its go.mod's requirements until its first report"}, nil
|
|
}
|
|
return map[string]any{"module": module, "kept": true, "set": set}, nil
|
|
}}
|
|
}
|
|
|
|
// withReportedImports is cur with the import set the state imports keeps for key, the set main's last build reported,
|
|
// which every take-in computes with, a watched branch's too (design 47 H2 H-A-519, H-A-532 DMC-6); a value holding
|
|
// another set takes it with main's import form while main's value holds that set (so the form costs no rebuild of its
|
|
// own, H-A-529), else with none, so its next computation writes the import form. cur is left as it is when the state
|
|
// keeps no set for key, or it holds no inputs.
|
|
func (ix *Indexer) withReportedImports(ctx context.Context, key string, cur meshcatalog.ModulesByModuleValue) meshcatalog.ModulesByModuleValue {
|
|
if cur.Inputs2 == nil {
|
|
return cur
|
|
}
|
|
set, found, err := ix.ImportSetOf(ctx, key)
|
|
if err != nil {
|
|
ix.logf("%s.%s cannot be read, so %s keeps the import set its value holds: %v", ImportsState, key, key, err)
|
|
return cur
|
|
}
|
|
if !found {
|
|
return cur
|
|
}
|
|
imports := make([]string, 0, len(set.Imports))
|
|
for _, p := range set.Imports {
|
|
imports = append(imports, p.Package)
|
|
}
|
|
if slices.Equal(cur.Inputs2.Imports, imports) {
|
|
return cur
|
|
}
|
|
in := *cur.Inputs2
|
|
in.Imports, in.ImportForm = imports, ix.trunkImportForm(ctx, key, imports)
|
|
if len(imports) == 0 {
|
|
in.Imports = nil
|
|
}
|
|
cur.Inputs2 = &in
|
|
return cur
|
|
}
|
|
|
|
// trunkImportForm is the import form main's modules value of key holds with imports, "" when it holds another set.
|
|
func (ix *Indexer) trunkImportForm(ctx context.Context, key string, imports []string) string {
|
|
st := ix.trunkModules
|
|
if st == nil {
|
|
st = ix.Modules
|
|
}
|
|
if st == nil {
|
|
return ""
|
|
}
|
|
v, _, found, err := st.Get(ctx, key)
|
|
if err != nil || !found {
|
|
return ""
|
|
}
|
|
m, err := meshcatalogclient.DecodeModule(v)
|
|
if err != nil || m.Inputs2 == nil || !slices.Equal(m.Inputs2.Imports, imports) {
|
|
return ""
|
|
}
|
|
return m.Inputs2.ImportForm
|
|
}
|