repository: deduplicate index progress bar initializaton

This commit is contained in:
Michael Eischer
2025-09-21 17:43:44 +02:00
parent b459d66288
commit 52eb66929f
21 changed files with 41 additions and 48 deletions

View File

@@ -549,8 +549,7 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts GlobalOptions, ter
printer.V("load index files")
}
bar := ui.NewIndexCounter(printer)
err = repo.LoadIndex(ctx, bar)
err = repo.LoadIndex(ctx, printer)
if err != nil {
return err
}

View File

@@ -168,8 +168,7 @@ func runCat(ctx context.Context, gopts GlobalOptions, args []string, term ui.Ter
return err
case "blob":
bar := ui.NewIndexCounter(printer)
err = repo.LoadIndex(ctx, bar)
err = repo.LoadIndex(ctx, printer)
if err != nil {
return err
}
@@ -196,8 +195,7 @@ func runCat(ctx context.Context, gopts GlobalOptions, args []string, term ui.Ter
return errors.Fatalf("could not find snapshot: %v", err)
}
bar := ui.NewIndexCounter(printer)
err = repo.LoadIndex(ctx, bar)
err = repo.LoadIndex(ctx, printer)
if err != nil {
return err
}

View File

@@ -249,8 +249,7 @@ func runCheck(ctx context.Context, opts CheckOptions, gopts GlobalOptions, args
}
printer.P("load indexes\n")
bar := ui.NewIndexCounter(printer)
hints, errs := chkr.LoadIndex(ctx, bar)
hints, errs := chkr.LoadIndex(ctx, printer)
if ctx.Err() != nil {
return summary, ctx.Err()
}

View File

@@ -101,13 +101,11 @@ func runCopy(ctx context.Context, opts CopyOptions, gopts GlobalOptions, args []
}
debug.Log("Loading source index")
bar := ui.NewIndexCounter(printer)
if err := srcRepo.LoadIndex(ctx, bar); err != nil {
if err := srcRepo.LoadIndex(ctx, printer); err != nil {
return err
}
bar = ui.NewIndexCounter(printer)
debug.Log("Loading destination index")
if err := dstRepo.LoadIndex(ctx, bar); err != nil {
if err := dstRepo.LoadIndex(ctx, printer); err != nil {
return err
}

View File

@@ -484,8 +484,7 @@ func runDebugExamine(ctx context.Context, gopts GlobalOptions, opts DebugExamine
return errors.Fatal("no pack files to examine")
}
bar := ui.NewIndexCounter(printer)
err = repo.LoadIndex(ctx, bar)
err = repo.LoadIndex(ctx, printer)
if err != nil {
return err
}

View File

@@ -391,8 +391,7 @@ func runDiff(ctx context.Context, opts DiffOptions, gopts GlobalOptions, args []
if !gopts.JSON {
printer.P("comparing snapshot %v to %v:\n\n", sn1.ID().Str(), sn2.ID().Str())
}
bar := ui.NewIndexCounter(printer)
if err = repo.LoadIndex(ctx, bar); err != nil {
if err = repo.LoadIndex(ctx, printer); err != nil {
return err
}

View File

@@ -160,8 +160,7 @@ func runDump(ctx context.Context, opts DumpOptions, gopts GlobalOptions, args []
return errors.Fatalf("failed to find snapshot: %v", err)
}
bar := ui.NewIndexCounter(printer)
err = repo.LoadIndex(ctx, bar)
err = repo.LoadIndex(ctx, printer)
if err != nil {
return err
}

View File

@@ -623,8 +623,7 @@ func runFind(ctx context.Context, opts FindOptions, gopts GlobalOptions, args []
if err != nil {
return err
}
bar := ui.NewIndexCounter(printer)
if err = repo.LoadIndex(ctx, bar); err != nil {
if err = repo.LoadIndex(ctx, printer); err != nil {
return err
}

View File

@@ -373,8 +373,7 @@ func runLs(ctx context.Context, opts LsOptions, gopts GlobalOptions, args []stri
return err
}
bar := ui.NewIndexCounter(termPrinter)
if err = repo.LoadIndex(ctx, bar); err != nil {
if err = repo.LoadIndex(ctx, termPrinter); err != nil {
return err
}

View File

@@ -146,8 +146,7 @@ func runMount(ctx context.Context, opts MountOptions, gopts GlobalOptions, args
}
defer unlock()
bar := ui.NewIndexCounter(printer)
err = repo.LoadIndex(ctx, bar)
err = repo.LoadIndex(ctx, printer)
if err != nil {
return err
}

View File

@@ -190,8 +190,7 @@ func runPruneWithRepo(ctx context.Context, opts PruneOptions, repo *repository.R
}
// loading the index before the snapshots is ok, as we use an exclusive lock here
bar := ui.NewIndexCounter(printer)
err := repo.LoadIndex(ctx, bar)
err := repo.LoadIndex(ctx, printer)
if err != nil {
return err
}

View File

@@ -66,8 +66,7 @@ func runRecover(ctx context.Context, gopts GlobalOptions, term ui.Terminal) erro
}
printer.P("load index files\n")
bar := ui.NewIndexCounter(printer)
if err = repo.LoadIndex(ctx, bar); err != nil {
if err = repo.LoadIndex(ctx, printer); err != nil {
return err
}
@@ -85,7 +84,7 @@ func runRecover(ctx context.Context, gopts GlobalOptions, term ui.Terminal) erro
}
printer.P("load %d trees\n", len(trees))
bar = printer.NewCounter("trees loaded")
bar := printer.NewCounter("trees loaded")
bar.SetMax(uint64(len(trees)))
for id := range trees {
tree, err := restic.LoadTree(ctx, repo, id)

View File

@@ -59,8 +59,7 @@ func runRepairPacks(ctx context.Context, gopts GlobalOptions, term ui.Terminal,
}
defer unlock()
bar := ui.NewIndexCounter(printer)
err = repo.LoadIndex(ctx, bar)
err = repo.LoadIndex(ctx, printer)
if err != nil {
return errors.Fatalf("%s", err)
}

View File

@@ -87,8 +87,7 @@ func runRepairSnapshots(ctx context.Context, gopts GlobalOptions, opts RepairOpt
return err
}
bar := ui.NewIndexCounter(printer)
if err := repo.LoadIndex(ctx, bar); err != nil {
if err := repo.LoadIndex(ctx, printer); err != nil {
return err
}

View File

@@ -151,8 +151,7 @@ func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions,
return errors.Fatalf("failed to find snapshot: %v", err)
}
bar := ui.NewIndexCounter(printer)
err = repo.LoadIndex(ctx, bar)
err = repo.LoadIndex(ctx, printer)
if err != nil {
return err
}

View File

@@ -318,8 +318,7 @@ func runRewrite(ctx context.Context, opts RewriteOptions, gopts GlobalOptions, a
return err
}
bar := ui.NewIndexCounter(printer)
if err = repo.LoadIndex(ctx, bar); err != nil {
if err = repo.LoadIndex(ctx, printer); err != nil {
return err
}

View File

@@ -111,8 +111,7 @@ func runStats(ctx context.Context, opts StatsOptions, gopts GlobalOptions, args
if err != nil {
return err
}
bar := ui.NewIndexCounter(printer)
if err = repo.LoadIndex(ctx, bar); err != nil {
if err = repo.LoadIndex(ctx, printer); err != nil {
return err
}

View File

@@ -93,11 +93,16 @@ func computePackTypes(ctx context.Context, idx restic.ListBlobser) (map[restic.I
}
// LoadIndex loads all index files.
func (c *Checker) LoadIndex(ctx context.Context, p *progress.Counter) (hints []error, errs []error) {
func (c *Checker) LoadIndex(ctx context.Context, p restic.TerminalCounterFactory) (hints []error, errs []error) {
debug.Log("Start")
var bar *progress.Counter
if p != nil {
bar = p.NewCounterTerminalOnly("index files loaded")
}
packToIndex := make(map[restic.ID]restic.IDSet)
err := c.masterIndex.Load(ctx, c.repo, p, func(id restic.ID, idx *index.Index, err error) error {
err := c.masterIndex.Load(ctx, c.repo, bar, func(id restic.ID, idx *index.Index, err error) error {
debug.Log("process index %v, err %v", id, err)
err = errors.Wrapf(err, "error loading index %v", id)

View File

@@ -639,13 +639,18 @@ func (r *Repository) clearIndex() {
}
// LoadIndex loads all index files from the backend in parallel and stores them
func (r *Repository) LoadIndex(ctx context.Context, p *progress.Counter) error {
func (r *Repository) LoadIndex(ctx context.Context, p restic.TerminalCounterFactory) error {
debug.Log("Loading index")
// reset in-memory index before loading it from the repository
r.clearIndex()
err := r.idx.Load(ctx, r, p, nil)
var bar *progress.Counter
if p != nil {
bar = p.NewCounterTerminalOnly("index files loaded")
}
err := r.idx.Load(ctx, r, bar, nil)
if err != nil {
return err
}

View File

@@ -21,7 +21,7 @@ type Repository interface {
Config() Config
Key() *crypto.Key
LoadIndex(ctx context.Context, p *progress.Counter) error
LoadIndex(ctx context.Context, p TerminalCounterFactory) error
SetIndex(mi MasterIndex) error
LookupBlob(t BlobType, id ID) []PackedBlob
@@ -131,6 +131,12 @@ type PackBlobs struct {
Blobs []Blob
}
type TerminalCounterFactory interface {
// NewCounterTerminalOnly returns a new progress counter that is only shown if stdout points to a
// terminal. It is not shown if --quiet or --json is specified.
NewCounterTerminalOnly(description string) *progress.Counter
}
// MasterIndex keeps track of the blobs are stored within files.
type MasterIndex interface {
Has(bh BlobHandle) bool

View File

@@ -76,7 +76,3 @@ func NewProgressPrinter(json bool, verbosity uint, term Terminal) progress.Print
show: verbosity > 0,
}
}
func NewIndexCounter(printer progress.Printer) *progress.Counter {
return printer.NewCounterTerminalOnly("index files loaded")
}