From d68c274d302ace8dd45323a492780c76eff6adf7 Mon Sep 17 00:00:00 2001 From: Josh W Lewis Date: Fri, 23 Jun 2023 09:33:14 -0500 Subject: [PATCH] Don't re-index unchanged entries --- cmd/index-buildpacks/main.go | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/cmd/index-buildpacks/main.go b/cmd/index-buildpacks/main.go index 14bf710..12d5c54 100644 --- a/cmd/index-buildpacks/main.go +++ b/cmd/index-buildpacks/main.go @@ -93,11 +93,21 @@ func buildIndex(entries []Entry) { defer db.Close() ch := make(chan IndexRecord) + updateCount := 0 for _, e := range entries { + found, err := FindEntry(db, e); + if err != nil { + fmt.Printf("at=buildIndex level=error msg='unable to query for existing entry' entry='%s/%s/%s' reason='%s'\n", e.Namespace, e.Name, e.Version, err) + } + if found { + fmt.Printf("at=buildIndex level=info msg='entry already up to date' entry='%s/%s/%s'\n", e.Namespace, e.Name, e.Version) + continue; + } go handleMetadata(e, remote.Image, ch) + updateCount++ } - for range entries { + for c := 0; c < updateCount; c++ { i := <-ch if i.err != nil { fmt.Printf("at=handleMetadata level=warn msg='failed to fetch config' entry='%s/%s@%s' reason='%s'\n", i.entry.Namespace, i.entry.Name, i.entry.Version, i.err) @@ -168,6 +178,28 @@ func FetchBuildpackConfig(e Entry, imageFn ImageFunction) (Metadata, error) { return m, nil } +func FindEntry(db *sql.DB, e Entry) (bool, error) { + query := ` + select exists( + select name + from buildpacks + where buildpacks.namespace = $1 + and buildpacks.name = $2 + and buildpacks.version = $3 + and buildpacks.addr = $4 + ) as "exists"; + `; + var found bool + err := db.QueryRow( + query, + e.Namespace, + e.Name, + e.Version, + e.Address, + ).Scan(&found); + return found, err; +} + func UpsertMetadata(db *sql.DB, e Entry, m Metadata) error { upsert := ` INSERT INTO buildpacks (