mirror of
https://github.com/octoleo/restic.git
synced 2024-12-29 05:13:09 +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:
parent
efec1a5e96
commit
b5c28a7ba2
@ -347,6 +347,9 @@ func (mi *MasterIndex) Rewrite(ctx context.Context, repo restic.Unpacked, exclud
|
|||||||
|
|
||||||
// copy excludePacks to prevent unintended sideeffects
|
// copy excludePacks to prevent unintended sideeffects
|
||||||
excludePacks = excludePacks.Clone()
|
excludePacks = excludePacks.Clone()
|
||||||
|
if excludePacks == nil {
|
||||||
|
excludePacks = restic.NewIDSet()
|
||||||
|
}
|
||||||
debug.Log("start rebuilding index of %d indexes, excludePacks: %v", len(indexes), excludePacks)
|
debug.Log("start rebuilding index of %d indexes, excludePacks: %v", len(indexes), excludePacks)
|
||||||
wg, wgCtx := errgroup.WithContext(ctx)
|
wg, wgCtx := errgroup.WithContext(ctx)
|
||||||
|
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package restic
|
package restic
|
||||||
|
|
||||||
import "sort"
|
import (
|
||||||
|
"maps"
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
// IDSet is a set of IDs.
|
// IDSet is a set of IDs.
|
||||||
type IDSet map[ID]struct{}
|
type IDSet map[ID]struct{}
|
||||||
@ -44,28 +47,10 @@ func (s IDSet) List() IDs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Equals returns true iff s equals other.
|
// Equals returns true iff s equals other.
|
||||||
func (s IDSet) Equals(other IDSet) bool {
|
func (s IDSet) Equals(other IDSet) bool { return maps.Equal(s, other) }
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// Merge adds the blobs in other to the current set.
|
// Merge adds the blobs in other to the current set.
|
||||||
func (s IDSet) Merge(other IDSet) {
|
func (s IDSet) Merge(other IDSet) { maps.Copy(s, other) }
|
||||||
for id := range other {
|
|
||||||
s.Insert(id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Intersect returns a new set containing the IDs that are present in both sets.
|
// Intersect returns a new set containing the IDs that are present in both sets.
|
||||||
func (s IDSet) Intersect(other IDSet) (result IDSet) {
|
func (s IDSet) Intersect(other IDSet) (result IDSet) {
|
||||||
@ -106,8 +91,4 @@ func (s IDSet) String() string {
|
|||||||
return "{" + str[1:len(str)-1] + "}"
|
return "{" + str[1:len(str)-1] + "}"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s IDSet) Clone() IDSet {
|
func (s IDSet) Clone() IDSet { return maps.Clone(s) }
|
||||||
c := NewIDSet()
|
|
||||||
c.Merge(s)
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
@ -187,7 +187,7 @@ func (l *Lock) checkForOtherLocks(ctx context.Context) error {
|
|||||||
|
|
||||||
// Store updates in new IDSet to prevent data races
|
// Store updates in new IDSet to prevent data races
|
||||||
var m sync.Mutex
|
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 {
|
err = ForAllLocks(ctx, l.repo, checkedIDs, func(id ID, lock *Lock, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// if we cannot load a lock then it is unclear whether it can be ignored
|
// if we cannot load a lock then it is unclear whether it can be ignored
|
||||||
|
Loading…
Reference in New Issue
Block a user