Simplify rebuild-index code

This commit is contained in:
Alexander Weiss 2020-12-05 16:10:18 +01:00 committed by Michael Eischer
parent 68b74e359e
commit d8d2cc6dd9

View file

@ -59,10 +59,11 @@ func rebuildIndex(opts RebuildIndexOptions, gopts GlobalOptions, repo *repositor
var obsoleteIndexes restic.IDs var obsoleteIndexes restic.IDs
packSizeFromList := make(map[restic.ID]int64) packSizeFromList := make(map[restic.ID]int64)
packSizeFromIndex := make(map[restic.ID]int64)
removePacks := restic.NewIDSet() removePacks := restic.NewIDSet()
if opts.ReadAllPacks { if opts.ReadAllPacks {
// get old index files // get list of old index files but start with empty index
err := repo.List(ctx, restic.IndexFile, func(id restic.ID, size int64) error { err := repo.List(ctx, restic.IndexFile, func(id restic.ID, size int64) error {
obsoleteIndexes = append(obsoleteIndexes, id) obsoleteIndexes = append(obsoleteIndexes, id)
return nil return nil
@ -70,27 +71,17 @@ func rebuildIndex(opts RebuildIndexOptions, gopts GlobalOptions, repo *repositor
if err != nil { if err != nil {
return err return err
} }
Verbosef("finding pack files in repo...\n")
err = repo.List(ctx, restic.PackFile, func(id restic.ID, size int64) error {
packSizeFromList[id] = size
removePacks.Insert(id)
return nil
})
if err != nil {
return err
}
} else { } else {
Verbosef("loading indexes...\n") Verbosef("loading indexes...\n")
err := repo.LoadIndex(gopts.ctx) err := repo.LoadIndex(gopts.ctx)
if err != nil { if err != nil {
return err return err
} }
packSizeFromIndex = repo.Index().PackSize(ctx, false)
}
Verbosef("getting pack files to read...\n") Verbosef("getting pack files to read...\n")
packSizeFromIndex := repo.Index().PackSize(ctx, false) err := repo.List(ctx, restic.PackFile, func(id restic.ID, packSize int64) error {
err = repo.List(ctx, restic.PackFile, func(id restic.ID, packSize int64) error {
size, ok := packSizeFromIndex[id] size, ok := packSizeFromIndex[id]
if !ok || size != packSize { if !ok || size != packSize {
// Pack was not referenced in index or size does not match // Pack was not referenced in index or size does not match
@ -108,7 +99,6 @@ func rebuildIndex(opts RebuildIndexOptions, gopts GlobalOptions, repo *repositor
// when rebuilding the index // when rebuilding the index
removePacks.Insert(id) removePacks.Insert(id)
} }
}
if len(packSizeFromList) > 0 { if len(packSizeFromList) > 0 {
Verbosef("reading pack files\n") Verbosef("reading pack files\n")
@ -123,7 +113,7 @@ func rebuildIndex(opts RebuildIndexOptions, gopts GlobalOptions, repo *repositor
} }
} }
err := rebuildIndexFiles(gopts, repo, removePacks, obsoleteIndexes) err = rebuildIndexFiles(gopts, repo, removePacks, obsoleteIndexes)
if err != nil { if err != nil {
return err return err
} }