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

refactor(runtime): reduce loop iteration complexity of checkStoreUpgrade (backport #21791) #21793

Merged
merged 2 commits into from
Sep 18, 2024
Merged
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
28 changes: 15 additions & 13 deletions runtime/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,29 +217,31 @@ func checkStoreUpgrade(storeUpgrades *store.StoreUpgrades) error {
}

// check for duplicates
exists := make(map[string]bool)
addedFilter := make(map[string]struct{})
deletedFilter := make(map[string]struct{})

for _, key := range storeUpgrades.Added {
if exists[key] {
if _, ok := addedFilter[key]; ok {
return fmt.Errorf("store upgrade has duplicate key %s in added", key)
}

if storeUpgrades.IsDeleted(key) {
return fmt.Errorf("store upgrade has key %s in both added and deleted", key)
}

exists[key] = true
addedFilter[key] = struct{}{}
}
exists = make(map[string]bool)
for _, key := range storeUpgrades.Deleted {
if exists[key] {
if _, ok := deletedFilter[key]; ok {
return fmt.Errorf("store upgrade has duplicate key %s in deleted", key)
}
deletedFilter[key] = struct{}{}
}

if storeUpgrades.IsAdded(key) {
for _, key := range storeUpgrades.Added {
if _, ok := deletedFilter[key]; ok {
return fmt.Errorf("store upgrade has key %s in both added and deleted", key)
}
}
for _, key := range storeUpgrades.Deleted {
if _, ok := addedFilter[key]; ok {
return fmt.Errorf("store upgrade has key %s in both added and deleted", key)
}

exists[key] = true
}

return nil
Expand Down
Loading