Files
upstream-corteza/server/compose/envoy/record_getter.go
upstream-mirror 7e385fc126
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
2026-09-18 01:22:23 +05:00

118 lines
2.6 KiB
Go

package envoy
import (
"context"
"github.com/cortezaproject/corteza/server/compose/dalutils"
"github.com/cortezaproject/corteza/server/compose/types"
"github.com/cortezaproject/corteza/server/pkg/dal"
"github.com/cortezaproject/corteza/server/pkg/envoyx"
"github.com/spf13/cast"
)
type (
// recordGetter is a utility struct to resolve record references from
// different parts of the system such as the dep graph and the database
recordGetter struct {
// Bits to get data from the dep graph
relDatasource *RecordDatasource
// Bits to get data from the database
dalSvc dal.FullService
relModule types.Module
baseFilter types.RecordFilter
// resolved refs, including misses; imports resolve the same handful
// of refs over and over so this saves a query per value
cache map[string]uint64
}
)
func makeRecordGetter(dalSvc dal.FullService, tt envoyx.Traverser, n *envoyx.Node, modRef, dsRef envoyx.Ref) (g *recordGetter) {
g = &recordGetter{
dalSvc: dalSvc,
cache: make(map[string]uint64),
}
// Resolve from dep graph
auxDs := tt.ParentForRef(n, dsRef)
if auxDs != nil {
g.relDatasource = auxDs.Datasource.(*RecordDatasource)
}
// Resolve from the database
mod := g.getRefMod(tt, n, modRef)
if mod != nil {
g.baseFilter = types.RecordFilter{
ModuleID: mod.ID,
NamespaceID: mod.NamespaceID,
}
}
return
}
// resolve resolves the provided reference into a record ID; 0 if can't be resolved
func (g *recordGetter) resolve(ctx context.Context, ref any) (out uint64, err error) {
key := cast.ToString(ref)
if out, ok := g.cache[key]; ok {
return out, nil
}
defer func() {
if err == nil {
g.cache[key] = out
}
}()
// Try to get from datasource
if g.relDatasource != nil {
out, err = g.getDS(ref)
if err != nil {
return
}
}
if out > 0 {
return
}
// Fallback to the store
out, err = g.getDB(ctx, ref)
if err != nil {
return
}
if out > 0 {
return
}
return
}
func (g *recordGetter) getDS(ref any) (out uint64, err error) {
return g.relDatasource.ResolveRef(ref)
}
func (g *recordGetter) getDB(ctx context.Context, ref any) (out uint64, err error) {
// @note the old version only resolved IDs so that's what we're doing here also
// @todo consider expanding this
id, err := cast.ToUint64E(ref)
if err != nil {
return 0, nil
}
aux, err := dalutils.ComposeRecordsFind(ctx, g.dalSvc, &g.relModule, id)
if err != nil {
return
}
out = aux.ID
return
}
func (g *recordGetter) getRefMod(tt envoyx.Traverser, n *envoyx.Node, ref envoyx.Ref) (refMod *types.Module) {
aux := tt.ParentForRef(n, ref)
if aux == nil {
return nil
}
return aux.Resource.(*types.Module)
}