1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2024-12-24 00:37:28 +00:00
restic/internal/index/index_parallel.go

37 lines
1.1 KiB
Go
Raw Normal View History

package index
2020-12-18 18:37:08 +00:00
import (
"context"
"runtime"
2020-12-18 18:37:08 +00:00
"sync"
"github.com/restic/restic/internal/restic"
)
// ForAllIndexes loads all index files in parallel and calls the given callback.
// It is guaranteed that the function is not run concurrently. If the callback
// returns an error, this function is cancelled and also returns that error.
func ForAllIndexes(ctx context.Context, lister restic.Lister, repo restic.ListerLoaderUnpacked,
2020-12-18 18:37:08 +00:00
fn func(id restic.ID, index *Index, oldFormat bool, err error) error) error {
// decoding an index can take quite some time such that this can be both CPU- or IO-bound
// as the whole index is kept in memory anyways, a few workers too much don't matter
workerCount := repo.Connections() + uint(runtime.GOMAXPROCS(0))
2020-12-18 18:37:08 +00:00
var m sync.Mutex
2024-02-10 21:58:10 +00:00
return restic.ParallelList(ctx, lister, restic.IndexFile, workerCount, func(ctx context.Context, id restic.ID, _ int64) error {
var err error
var idx *Index
oldFormat := false
buf, err := repo.LoadUnpacked(ctx, restic.IndexFile, id)
if err == nil {
idx, oldFormat, err = DecodeIndex(buf, id)
2020-12-18 18:37:08 +00:00
}
m.Lock()
defer m.Unlock()
return fn(id, idx, oldFormat, err)
})
2020-12-18 18:37:08 +00:00
}