forked from TrueCloudLab/restic
Simplify rebuild-index code
This commit is contained in:
parent
68b74e359e
commit
d8d2cc6dd9
1 changed files with 21 additions and 31 deletions
|
@ -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,44 +71,33 @@ 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 {
|
||||||
|
size, ok := packSizeFromIndex[id]
|
||||||
err = repo.List(ctx, restic.PackFile, func(id restic.ID, packSize int64) error {
|
if !ok || size != packSize {
|
||||||
size, ok := packSizeFromIndex[id]
|
// Pack was not referenced in index or size does not match
|
||||||
if !ok || size != packSize {
|
packSizeFromList[id] = packSize
|
||||||
// Pack was not referenced in index or size does not match
|
|
||||||
packSizeFromList[id] = packSize
|
|
||||||
removePacks.Insert(id)
|
|
||||||
}
|
|
||||||
delete(packSizeFromIndex, id)
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for id := range packSizeFromIndex {
|
|
||||||
// forget pack files that are referenced in the index but do not exist
|
|
||||||
// when rebuilding the index
|
|
||||||
removePacks.Insert(id)
|
removePacks.Insert(id)
|
||||||
}
|
}
|
||||||
|
delete(packSizeFromIndex, id)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for id := range packSizeFromIndex {
|
||||||
|
// forget pack files that are referenced in the index but do not exist
|
||||||
|
// when rebuilding the index
|
||||||
|
removePacks.Insert(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(packSizeFromList) > 0 {
|
if len(packSizeFromList) > 0 {
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue