Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't re-index unchanged entries #116

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion cmd/index-buildpacks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Copy link
Contributor

@edmorley edmorley Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not super-familiar with how indexes work for Postgres, so I could be completely mis-remembering/making this up, but does the WHERE column order need to match the column order in the index for it to work? Or does that only affect when using eg GROUP BY?

ie: The existing index has the "name" column first:
https://github.com/buildpacks/registry-api/blob/80f586fee0b869a6d1c59fc4ba61fc1522926a2e/db/schema.rb#L34C14-L34C44

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My experience tells me that postgres will figure it out, but I don't mind reordering just in case it helps in some small bit.

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 (
Expand Down