Skip to content

Commit

Permalink
De-duplicate fingerprints and icons
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaavi committed Sep 12, 2023
1 parent 1c34716 commit 6d4f8fa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
20 changes: 18 additions & 2 deletions profile/fingerprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ const (

func deriveProfileID(fps []Fingerprint) string {
// Sort the fingerprints.
sortFingerprints(fps)
sortAndCompactFingerprints(fps)

// Compile data for hashing.
c := container.New(nil)
Expand Down Expand Up @@ -398,7 +398,8 @@ func deriveProfileID(fps []Fingerprint) string {
return h.Base58()
}

func sortFingerprints(fps []Fingerprint) {
func sortAndCompactFingerprints(fps []Fingerprint) []Fingerprint {
// Sort.
slices.SortFunc[[]Fingerprint, Fingerprint](fps, func(a, b Fingerprint) int {
switch {
case a.Type != b.Type:
Expand All @@ -415,4 +416,19 @@ func sortFingerprints(fps []Fingerprint) {
return 0
}
})

// De-duplicate.
// Important: Even if the fingerprint is the same, but MergedFrom is
// different, we need to keep the separate fingerprint, so that new installs
// will cleanly update to the synced state: Auto-generated profiles need to
// be automatically replaced by the merged version.
fps = slices.CompactFunc[[]Fingerprint, Fingerprint](fps, func(a, b Fingerprint) bool {
return a.Type == b.Type &&
a.Key == b.Key &&
a.Operation == b.Operation &&
a.Value == b.Value &&
a.MergedFrom == b.MergedFrom
})

return fps
}
10 changes: 9 additions & 1 deletion profile/icon.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func (t IconType) sortOrder() int {
}
}

func sortIcons(icons []Icon) {
func sortAndCompactIcons(icons []Icon) []Icon {
// Sort.
slices.SortFunc[[]Icon, Icon](icons, func(a, b Icon) int {
aOrder := a.Type.sortOrder()
bOrder := b.Type.sortOrder()
Expand All @@ -49,4 +50,11 @@ func sortIcons(icons []Icon) {
return 0
}
})

// De-duplicate.
icons = slices.CompactFunc[[]Icon, Icon](icons, func(a, b Icon) bool {
return a.Type == b.Type && a.Value == b.Value
})

return icons
}
4 changes: 2 additions & 2 deletions profile/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ func MergeProfiles(primary *Profile, secondaries ...*Profile) (newProfile *Profi
for _, sp := range secondaries {
newProfile.Icons = append(newProfile.Icons, sp.Icons...)
}
sortIcons(newProfile.Icons)
newProfile.Icons = sortAndCompactIcons(newProfile.Icons)

// Collect all fingerprints.
newProfile.Fingerprints = make([]Fingerprint, 0, len(secondaries)+1) // Guess the needed space.
newProfile.Fingerprints = addFingerprints(newProfile.Fingerprints, primary.Fingerprints, primary.ScopedID())
for _, sp := range secondaries {
newProfile.Fingerprints = addFingerprints(newProfile.Fingerprints, sp.Fingerprints, sp.ScopedID())
}
sortFingerprints(newProfile.Fingerprints)
newProfile.Fingerprints = sortAndCompactFingerprints(newProfile.Fingerprints)

// Save new profile.
newProfile = New(newProfile)
Expand Down

0 comments on commit 6d4f8fa

Please sign in to comment.