The indexer consumes node-build-go and node-build-node imports-reported; a report of the
fingerprint main's modules state holds for the module keeps the set on the module (Imports,
ImportForm, as the build log path did) and in the new state imports (one key per module,
{module, fingerprint, commit, imports: [{package, digest}]}, the digests the import form is
computed with), deleted with the module's modules key. A report of another fingerprint is
left; one naming no module or fingerprint is said and acknowledged.
Builds: design 47 H2 H-A-517, H-A-518, H-A-519, H-A-527, H-A-529, H-A-532 DQ-3, DQ-4, DQ-13, DMC-2
106 lines
4.3 KiB
Go
106 lines
4.3 KiB
Go
package index
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"slices"
|
|
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo"
|
|
)
|
|
|
|
// 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"
|
|
|
|
// 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
|
|
}
|