1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2024-12-28 02:38:33 +00:00

Merge pull request #2841 from aawsome/optimize-getUsedBlobs

Extract get used blobs in prune as separate function
This commit is contained in:
MichaelEischer 2020-08-01 22:40:33 +02:00 committed by GitHub
commit 66d089e239
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -186,29 +186,11 @@ func pruneRepository(gopts GlobalOptions, repo restic.Repository) error {
stats.snapshots = len(snapshots) stats.snapshots = len(snapshots)
Verbosef("find data that is still in use for %d snapshots\n", stats.snapshots) usedBlobs, err := getUsedBlobs(gopts, repo, snapshots)
usedBlobs := restic.NewBlobSet()
bar = newProgressMax(!gopts.Quiet, uint64(len(snapshots)), "snapshots")
bar.Start()
for _, sn := range snapshots {
debug.Log("process snapshot %v", sn.ID())
err = restic.FindUsedBlobs(ctx, repo, *sn.Tree, usedBlobs)
if err != nil { if err != nil {
if repo.Backend().IsNotExist(err) {
return errors.Fatal("unable to load a tree from the repo: " + err.Error())
}
return err return err
} }
debug.Log("processed snapshot %v", sn.ID())
bar.Report(restic.Stat{Blobs: 1})
}
bar.Done()
if len(usedBlobs) > stats.blobs { if len(usedBlobs) > stats.blobs {
return errors.Fatalf("number of used blobs is larger than number of available blobs!\n" + return errors.Fatalf("number of used blobs is larger than number of available blobs!\n" +
"Please report this error (along with the output of the 'prune' run) at\n" + "Please report this error (along with the output of the 'prune' run) at\n" +
@ -303,3 +285,30 @@ func pruneRepository(gopts GlobalOptions, repo restic.Repository) error {
Verbosef("done\n") Verbosef("done\n")
return nil return nil
} }
func getUsedBlobs(gopts GlobalOptions, repo restic.Repository, snapshots []*restic.Snapshot) (usedBlobs restic.BlobSet, err error) {
ctx := gopts.ctx
Verbosef("find data that is still in use for %d snapshots\n", len(snapshots))
usedBlobs = restic.NewBlobSet()
bar := newProgressMax(!gopts.Quiet, uint64(len(snapshots)), "snapshots")
bar.Start()
for _, sn := range snapshots {
debug.Log("process snapshot %v", sn.ID())
err = restic.FindUsedBlobs(ctx, repo, *sn.Tree, usedBlobs)
if err != nil {
if repo.Backend().IsNotExist(err) {
return nil, errors.Fatal("unable to load a tree from the repo: " + err.Error())
}
return nil, err
}
debug.Log("processed snapshot %v", sn.ID())
bar.Report(restic.Stat{Blobs: 1})
}
return usedBlobs, nil
}