From 550d1eeac3540812a811a3ba9fac2eb9a9923f20 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sun, 19 May 2024 15:55:42 +0200 Subject: [PATCH] repository: remove SaveIndex from interface The method is now only indirectly accessible via Prune or RepairIndex. --- internal/index/master_index.go | 9 ++++++- internal/index/master_index_test.go | 2 +- internal/repository/repack_test.go | 41 ++++++----------------------- internal/repository/repair_index.go | 6 +++-- internal/repository/repair_pack.go | 2 +- internal/repository/repository.go | 4 --- internal/restic/repository.go | 10 ------- 7 files changed, 22 insertions(+), 52 deletions(-) diff --git a/internal/index/master_index.go b/internal/index/master_index.go index 796559fd7..981a2b31b 100644 --- a/internal/index/master_index.go +++ b/internal/index/master_index.go @@ -312,10 +312,17 @@ func (mi *MasterIndex) Load(ctx context.Context, r restic.ListerLoaderUnpacked, return mi.MergeFinalIndexes() } +type MasterIndexSaveOpts struct { + SaveProgress *progress.Counter + DeleteProgress func() *progress.Counter + DeleteReport func(id restic.ID, err error) + SkipDeletion bool +} + // Save saves all known indexes to index files, leaving out any // packs whose ID is contained in packBlacklist from finalized indexes. // It also removes the old index files and those listed in extraObsolete. -func (mi *MasterIndex) Save(ctx context.Context, repo restic.SaverRemoverUnpacked, excludePacks restic.IDSet, extraObsolete restic.IDs, opts restic.MasterIndexSaveOpts) error { +func (mi *MasterIndex) Save(ctx context.Context, repo restic.SaverRemoverUnpacked, excludePacks restic.IDSet, extraObsolete restic.IDs, opts MasterIndexSaveOpts) error { p := opts.SaveProgress p.SetMax(uint64(len(mi.Packs(excludePacks)))) diff --git a/internal/index/master_index_test.go b/internal/index/master_index_test.go index 3a7f3da88..41f4cc534 100644 --- a/internal/index/master_index_test.go +++ b/internal/index/master_index_test.go @@ -364,7 +364,7 @@ func testIndexSave(t *testing.T, version uint) { blobs[pb] = struct{}{} })) - rtest.OK(t, idx.Save(context.TODO(), repo, nil, nil, restic.MasterIndexSaveOpts{})) + rtest.OK(t, idx.Save(context.TODO(), repo, nil, nil, index.MasterIndexSaveOpts{})) idx = index.NewMasterIndex() rtest.OK(t, idx.Load(context.TODO(), repo, nil, nil)) diff --git a/internal/repository/repack_test.go b/internal/repository/repack_test.go index 96b75ca46..476e63b47 100644 --- a/internal/repository/repack_test.go +++ b/internal/repository/repack_test.go @@ -7,10 +7,10 @@ import ( "time" "github.com/restic/restic/internal/backend" - "github.com/restic/restic/internal/index" "github.com/restic/restic/internal/repository" "github.com/restic/restic/internal/restic" rtest "github.com/restic/restic/internal/test" + "github.com/restic/restic/internal/ui/progress" "golang.org/x/sync/errgroup" ) @@ -173,35 +173,12 @@ func repack(t *testing.T, repo restic.Repository, packs restic.IDSet, blobs rest } } -func rebuildIndex(t *testing.T, repo restic.Repository) { - err := repo.SetIndex(index.NewMasterIndex()) - rtest.OK(t, err) +func rebuildAndReloadIndex(t *testing.T, repo *repository.Repository) { + rtest.OK(t, repository.RepairIndex(context.TODO(), repo, repository.RepairIndexOptions{ + ReadAllPacks: true, + }, &progress.NoopPrinter{})) - packs := make(map[restic.ID]int64) - err = repo.List(context.TODO(), restic.PackFile, func(id restic.ID, size int64) error { - packs[id] = size - return nil - }) - rtest.OK(t, err) - - _, err = repo.(*repository.Repository).CreateIndexFromPacks(context.TODO(), packs, nil) - rtest.OK(t, err) - - var obsoleteIndexes restic.IDs - err = repo.List(context.TODO(), restic.IndexFile, func(id restic.ID, size int64) error { - obsoleteIndexes = append(obsoleteIndexes, id) - return nil - }) - rtest.OK(t, err) - - err = repo.SaveIndex(context.TODO(), restic.NewIDSet(), obsoleteIndexes, restic.MasterIndexSaveOpts{}) - rtest.OK(t, err) -} - -func reloadIndex(t *testing.T, repo restic.Repository) { - if err := repo.LoadIndex(context.TODO(), nil); err != nil { - t.Fatalf("error loading new index: %v", err) - } + rtest.OK(t, repo.LoadIndex(context.TODO(), nil)) } func TestRepack(t *testing.T) { @@ -236,8 +213,7 @@ func testRepack(t *testing.T, version uint) { removePacks := findPacksForBlobs(t, repo, removeBlobs) repack(t, repo, removePacks, keepBlobs) - rebuildIndex(t, repo) - reloadIndex(t, repo) + rebuildAndReloadIndex(t, repo) packsAfter = listPacks(t, repo) for id := range removePacks { @@ -307,8 +283,7 @@ func testRepackCopy(t *testing.T, version uint) { if err != nil { t.Fatal(err) } - rebuildIndex(t, dstRepo) - reloadIndex(t, dstRepo) + rebuildAndReloadIndex(t, dstRepo) for h := range keepBlobs { list := dstRepo.LookupBlob(h.Type, h.ID) diff --git a/internal/repository/repair_index.go b/internal/repository/repair_index.go index c20641d50..5674a3963 100644 --- a/internal/repository/repair_index.go +++ b/internal/repository/repair_index.go @@ -28,6 +28,8 @@ func RepairIndex(ctx context.Context, repo *Repository, opts RepairIndexOptions, if err != nil { return err } + repo.clearIndex() + } else { printer.P("loading indexes...\n") mi := index.NewMasterIndex() @@ -111,11 +113,11 @@ func RepairIndex(ctx context.Context, repo *Repository, opts RepairIndexOptions, return nil } -func rebuildIndexFiles(ctx context.Context, repo restic.Repository, removePacks restic.IDSet, extraObsolete restic.IDs, skipDeletion bool, printer progress.Printer) error { +func rebuildIndexFiles(ctx context.Context, repo *Repository, removePacks restic.IDSet, extraObsolete restic.IDs, skipDeletion bool, printer progress.Printer) error { printer.P("rebuilding index\n") bar := printer.NewCounter("packs processed") - return repo.SaveIndex(ctx, removePacks, extraObsolete, restic.MasterIndexSaveOpts{ + return repo.idx.Save(ctx, repo, removePacks, extraObsolete, index.MasterIndexSaveOpts{ SaveProgress: bar, DeleteProgress: func() *progress.Counter { return printer.NewCounter("old indexes deleted") diff --git a/internal/repository/repair_pack.go b/internal/repository/repair_pack.go index cac7aac10..7cb9d9f3e 100644 --- a/internal/repository/repair_pack.go +++ b/internal/repository/repair_pack.go @@ -10,7 +10,7 @@ import ( "golang.org/x/sync/errgroup" ) -func RepairPacks(ctx context.Context, repo restic.Repository, ids restic.IDSet, printer progress.Printer) error { +func RepairPacks(ctx context.Context, repo *Repository, ids restic.IDSet, printer progress.Printer) error { wg, wgCtx := errgroup.WithContext(ctx) repo.StartPackUploader(wgCtx, wg) diff --git a/internal/repository/repository.go b/internal/repository/repository.go index bd7de0de4..7abc5911a 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -587,10 +587,6 @@ func (r *Repository) LookupBlobSize(tpe restic.BlobType, id restic.ID) (uint, bo return r.idx.LookupSize(restic.BlobHandle{Type: tpe, ID: id}) } -func (r *Repository) SaveIndex(ctx context.Context, excludePacks restic.IDSet, extraObsolete restic.IDs, opts restic.MasterIndexSaveOpts) error { - return r.idx.Save(ctx, r, excludePacks, extraObsolete, opts) -} - // ListBlobs runs fn on all blobs known to the index. When the context is cancelled, // the index iteration returns immediately with ctx.Err(). This blocks any modification of the index. func (r *Repository) ListBlobs(ctx context.Context, fn func(restic.PackedBlob)) error { diff --git a/internal/restic/repository.go b/internal/restic/repository.go index 9e6d6b99b..b18b036a7 100644 --- a/internal/restic/repository.go +++ b/internal/restic/repository.go @@ -23,7 +23,6 @@ type Repository interface { LoadIndex(ctx context.Context, p *progress.Counter) error SetIndex(mi MasterIndex) error - SaveIndex(ctx context.Context, excludePacks IDSet, extraObsolete IDs, opts MasterIndexSaveOpts) error LookupBlob(t BlobType, id ID) []PackedBlob LookupBlobSize(t BlobType, id ID) (size uint, exists bool) @@ -106,13 +105,6 @@ type PackBlobs struct { Blobs []Blob } -type MasterIndexSaveOpts struct { - SaveProgress *progress.Counter - DeleteProgress func() *progress.Counter - DeleteReport func(id ID, err error) - SkipDeletion bool -} - // MasterIndex keeps track of the blobs are stored within files. type MasterIndex interface { Has(bh BlobHandle) bool @@ -122,8 +114,6 @@ type MasterIndex interface { // the index iteration returns immediately with ctx.Err(). This blocks any modification of the index. Each(ctx context.Context, fn func(PackedBlob)) error ListPacks(ctx context.Context, packs IDSet) <-chan PackBlobs - - Save(ctx context.Context, repo SaverRemoverUnpacked, excludePacks IDSet, extraObsolete IDs, opts MasterIndexSaveOpts) error } // Lister allows listing files in a backend.