internal/postgres: factor out symbol history collect

For golang/go#37102

Change-Id: Ie2faa956e3ecb54ef00c197174cc9f16f15a8d2e
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/316989
Trust: Julie Qiu <julie@golang.org>
Run-TryBot: Julie Qiu <julie@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
This commit is contained in:
Julie Qiu 2021-05-04 14:47:23 -04:00
Родитель 1bd478acf1
Коммит e17cb23519
2 изменённых файлов: 20 добавлений и 37 удалений

Просмотреть файл

@ -47,11 +47,23 @@ func getPackageSymbols(ctx context.Context, ddb *database.DB, packagePath, modul
CASE WHEN ps.type='Type' THEN 0 ELSE 1 END,
symbol_name;`
// versionToNameToUnitSymbol contains all of the types for this unit,
// grouped by name and build context. This is used to keep track of the
// parent types, so that we can map the children to those symbols.
sh, collect := collectSymbolHistory(func(sh *internal.SymbolHistory, sm internal.SymbolMeta, v string, build internal.BuildContext) error {
if sm.Section == internal.SymbolSectionTypes && sm.Kind != internal.SymbolKindType {
_, err := sh.GetSymbol(sm.ParentName, v, build)
return err
}
return nil
})
if err := ddb.RunQuery(ctx, query, collect, packagePath, modulePath); err != nil {
return nil, err
}
return sh, nil
}
func collectSymbolHistory(check func(sh *internal.SymbolHistory, sm internal.SymbolMeta, v string, build internal.BuildContext) error) (*internal.SymbolHistory, func(rows *sql.Rows) error) {
sh := internal.NewSymbolHistory()
collect := func(rows *sql.Rows) error {
return sh, func(rows *sql.Rows) (err error) {
defer derrors.Wrap(&err, "collectSymbolHistory")
var (
sm internal.SymbolMeta
build internal.BuildContext
@ -69,18 +81,12 @@ func getPackageSymbols(ctx context.Context, ddb *database.DB, packagePath, modul
); err != nil {
return fmt.Errorf("row.Scan(): %v", err)
}
if sm.Section == internal.SymbolSectionTypes && sm.Kind != internal.SymbolKindType {
if _, err := sh.GetSymbol(sm.ParentName, v, build); err != nil {
return err
}
if err := check(sh, sm, v, build); err != nil {
return fmt.Errorf("check(): %v", err)
}
sh.AddSymbol(sm, v, build)
return nil
}
if err := ddb.RunQuery(ctx, query, collect, packagePath, modulePath); err != nil {
return nil, err
}
return sh, nil
}
// legacyGetPackageSymbols returns all of the symbols for a given package path and module path.

Просмотреть файл

@ -60,30 +60,7 @@ func GetSymbolHistoryFromTable(ctx context.Context, ddb *database.DB,
return nil, err
}
// versionToNameToUnitSymbol is a map of the version a symbol was
// introduced, to the name and unit symbol.
sh := internal.NewSymbolHistory()
collect := func(rows *sql.Rows) error {
var (
sm internal.SymbolMeta
build internal.BuildContext
v string
)
if err := rows.Scan(
&sm.Name,
&sm.ParentName,
&sm.Section,
&sm.Kind,
&sm.Synopsis,
&v,
&build.GOOS,
&build.GOARCH,
); err != nil {
return fmt.Errorf("row.Scan(): %v", err)
}
sh.AddSymbol(sm, v, build)
return nil
}
sh, collect := collectSymbolHistory(func(*internal.SymbolHistory, internal.SymbolMeta, string, internal.BuildContext) error { return nil })
if err := ddb.RunQuery(ctx, query, collect, args...); err != nil {
return nil, err
}
@ -126,7 +103,7 @@ func (db *DB) LegacyGetSymbolHistory(ctx context.Context, packagePath, modulePat
// LegacyGetSymbolHistoryFromTable is exported for use in tests.
func LegacyGetSymbolHistoryFromTable(ctx context.Context, ddb *database.DB,
packagePath, modulePath string) (_ map[string]map[string]*internal.UnitSymbol, err error) {
defer derrors.WrapStack(&err, "GetSymbolHistoryFromTable(ctx, ddb, %q, %q)", packagePath, modulePath)
defer derrors.WrapStack(&err, "LegacyGetSymbolHistoryFromTable(ctx, ddb, %q, %q)", packagePath, modulePath)
q := squirrel.Select(
"s1.name AS symbol_name",