2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-27 04:32:40 +00:00

internal/restic: Use IDSet.Clone + use maps package

One place where IDSet.Clone is useful was reinventing it, using a
conversion to list, a sort, and a conversion back to map.

Also, use the stdlib "maps" package to implement as much of IDSet as
possible. This requires changing one caller, which assumed that cloning
nil would return a non-nil IDSet.
This commit is contained in:
greatroar 2024-09-30 22:43:04 +02:00
parent efec1a5e96
commit b5c28a7ba2
3 changed files with 11 additions and 27 deletions

View File

@ -347,6 +347,9 @@ func (mi *MasterIndex) Rewrite(ctx context.Context, repo restic.Unpacked, exclud
// copy excludePacks to prevent unintended sideeffects
excludePacks = excludePacks.Clone()
if excludePacks == nil {
excludePacks = restic.NewIDSet()
}
debug.Log("start rebuilding index of %d indexes, excludePacks: %v", len(indexes), excludePacks)
wg, wgCtx := errgroup.WithContext(ctx)

View File

@ -1,6 +1,9 @@
package restic
import "sort"
import (
"maps"
"sort"
)
// IDSet is a set of IDs.
type IDSet map[ID]struct{}
@ -44,28 +47,10 @@ func (s IDSet) List() IDs {
}
// Equals returns true iff s equals other.
func (s IDSet) Equals(other IDSet) bool {
if len(s) != len(other) {
return false
}
for id := range s {
if _, ok := other[id]; !ok {
return false
}
}
// length + one-way comparison is sufficient implication of equality
return true
}
func (s IDSet) Equals(other IDSet) bool { return maps.Equal(s, other) }
// Merge adds the blobs in other to the current set.
func (s IDSet) Merge(other IDSet) {
for id := range other {
s.Insert(id)
}
}
func (s IDSet) Merge(other IDSet) { maps.Copy(s, other) }
// Intersect returns a new set containing the IDs that are present in both sets.
func (s IDSet) Intersect(other IDSet) (result IDSet) {
@ -106,8 +91,4 @@ func (s IDSet) String() string {
return "{" + str[1:len(str)-1] + "}"
}
func (s IDSet) Clone() IDSet {
c := NewIDSet()
c.Merge(s)
return c
}
func (s IDSet) Clone() IDSet { return maps.Clone(s) }

View File

@ -187,7 +187,7 @@ func (l *Lock) checkForOtherLocks(ctx context.Context) error {
// Store updates in new IDSet to prevent data races
var m sync.Mutex
newCheckedIDs := NewIDSet(checkedIDs.List()...)
newCheckedIDs := checkedIDs.Clone()
err = ForAllLocks(ctx, l.repo, checkedIDs, func(id ID, lock *Lock, err error) error {
if err != nil {
// if we cannot load a lock then it is unclear whether it can be ignored