repository: refactor ListBlobs to be iterator based (#22208)

This commit is contained in:
Michael Eischer authored and GitHub committed 2026-11-27 22:46:12 +01:00
1 parent 4797768ba9
commit 814ef968d9
19 files changed
+113 -89

No files matched your search

+4 -4
View File
@@ -506,7 +506,10 @@ func (f *Finder) indexPacksToBlobs(ctx context.Context, packIDs map[string]struc
// remember which packs were found in the index
indexPackIDs := make(map[string]struct{})
err := f.repo.ListBlobs(wctx, func(pb restic.PackBlob) {
for pb := range f.repo.ListBlobs() {
if err := wctx.Err(); err != nil {
return nil, err
}
packID := pb.PackID()
idStr := packID.String()
// keep entry in packIDs as Each() returns individual index entries
@@ -525,9 +528,6 @@ func (f *Finder) indexPacksToBlobs(ctx context.Context, packIDs map[string]struc
f.addBlobHandle(pb.Handle())
indexPackIDs[idStr] = struct{}{}
}
})
if err != nil {
return nil, err
}
for id := range indexPackIDs {
+6 -6
View File
@@ -180,15 +180,14 @@ func TestFindPackfile(t *testing.T) {
packID := restic.ID{}
done := false
err = repo.ListBlobs(ctx, func(pb restic.PackBlob) {
for pb := range repo.ListBlobs() {
rtest.OK(t, ctx.Err())
h := pb.Handle()
if !done && h.Type == restic.TreeBlob {
packID = pb.PackID()
done = true
}
})
rtest.OK(t, err)
}
rtest.Assert(t, !packID.IsNull(), "expected a tree packfile ID")
findOptions := FindOptions{PackID: true}
results := testRunFind(t, true, findOptions, env.gopts, packID.String())
@@ -238,14 +237,15 @@ func TestFindPackID(t *testing.T) {
// load Index
rtest.OK(t, repo.LoadIndex(ctx, restic.NoopTerminalCounterFactory))
// go through all index entries and collect data and tree packfile(s)
rtest.OK(t, repo.ListBlobs(ctx, func(blob restic.PackBlob) {
for blob := range repo.ListBlobs() {
rtest.OK(t, ctx.Err())
switch blob.Handle().Type {
case restic.DataBlob:
dataPackID = blob.PackID()
case restic.TreeBlob:
treePackID = blob.PackID()
}
}))
}
return nil
})
rtest.OK(t, err)
+3 -2
View File
@@ -75,9 +75,10 @@ func testListBlobs(t testing.TB, gopts global.Options) (blobSetFromIndex restic.
// get blobs from index
blobSetFromIndex = restic.NewIDSet()
rtest.OK(t, repo.ListBlobs(ctx, func(blob restic.PackBlob) {
for blob := range repo.ListBlobs() {
rtest.OK(t, ctx.Err())
blobSetFromIndex.Insert(blob.Handle().ID)
}))
}
return nil
})
rtest.OK(t, err)
+4 -4
View File
@@ -75,14 +75,14 @@ func runRecover(ctx context.Context, gopts global.Options, term ui.Terminal) err
// tree. If it is not referenced, we have a root tree.
trees := make(map[restic.ID]bool)
err = repo.ListBlobs(ctx, func(blob restic.PackBlob) {
for blob := range repo.ListBlobs() {
if err = ctx.Err(); err != nil {
return err
}
h := blob.Handle()
if h.Type == restic.TreeBlob {
trees[h.ID] = false
}
})
if err != nil {
return err
}
printer.P("load %d trees\n", len(trees))
@@ -40,13 +40,12 @@ func TestRunRepairPackfiles(t *testing.T) {
rtest.OK(t, repo.LoadIndex(ctx, printer))
// load packfiles from master index
err = repo.ListBlobs(ctx, func(blob restic.PackBlob) {
for blob := range repo.ListBlobs() {
rtest.OK(t, ctx.Err())
if blob.Handle().Type == restic.DataBlob {
packfileID = blob.PackID()
return
}
})
rtest.OK(t, err)
}
return nil
})
+6 -3
View File
@@ -419,11 +419,14 @@ func statsDebugBlobs(ctx context.Context, repo restic.Repository) ([restic.NumBl
hist[i] = newSizeHistogram(2 * chunker.MaxSize)
}
err := repo.ListBlobs(ctx, func(pb restic.PackBlob) {
for pb := range repo.ListBlobs() {
if err := ctx.Err(); err != nil {
return hist, err
}
hist[pb.Handle().Type].Add(uint64(pb.CiphertextLength()))
})
}
return hist, err
return hist, nil
}
type sizeClass struct {
+9 -4
View File
@@ -273,11 +273,15 @@ func listTreePacks(gopts global.Options, t *testing.T) restic.IDSet {
rtest.OK(t, r.LoadIndex(ctx, restic.NoopTerminalCounterFactory))
treePacks = restic.NewIDSet()
return r.ListBlobs(ctx, func(pb restic.PackBlob) {
for pb := range r.ListBlobs() {
if err := ctx.Err(); err != nil {
return err
}
if pb.Handle().Type == restic.TreeBlob {
treePacks.Insert(pb.PackID())
}
})
}
return nil
})
rtest.OK(t, err)
return treePacks
@@ -322,11 +326,12 @@ func removePacksExcept(gopts global.Options, t testing.TB, keep restic.IDSet, re
rtest.OK(t, r.LoadIndex(ctx, restic.NoopTerminalCounterFactory))
treePacks := restic.NewIDSet()
rtest.OK(t, r.ListBlobs(ctx, func(pb restic.PackBlob) {
for pb := range r.ListBlobs() {
rtest.OK(t, ctx.Err())
if pb.Handle().Type == restic.TreeBlob {
treePacks.Insert(pb.PackID())
}
}))
}
// remove all packs containing data blobs
return r.List(ctx, restic.PackFile, func(id restic.ID, size int64) error {
+6 -3
View File
@@ -285,15 +285,18 @@ func (c *Checker) UnusedBlobs(ctx context.Context) (blobs restic.BlobHandles, er
ctx, cancel := context.WithCancel(ctx)
defer cancel()
err = c.repo.ListBlobs(ctx, func(blob restic.PackBlob) {
for blob := range c.repo.ListBlobs() {
if err := ctx.Err(); err != nil {
return blobs, err
}
h := blob.Handle()
if !c.blobRefs.M.Has(h) {
debug.Log("blob %v not referenced", h)
blobs = append(blobs, h)
}
})
}
return blobs, err
return blobs, nil
}
// ReadPacks wraps repository.ReadPacks:
+12 -7
View File
@@ -7,6 +7,7 @@ import (
"crypto/sha256"
"fmt"
"io"
"iter"
"github.com/klauspost/compress/zstd"
"github.com/restic/restic/internal/backend"
@@ -86,9 +87,13 @@ func newChecker(repo *Repository) *Checker {
repo: repo,
}
}
func computePackTypes(ctx context.Context, idx restic.ListBlobser) (map[restic.ID]restic.BlobType, error) {
func computePackTypes(ctx context.Context, blobs iter.Seq[restic.PackBlob]) (map[restic.ID]restic.BlobType, error) {
packs := make(map[restic.ID]restic.BlobType)
err := idx.ListBlobs(ctx, func(pb restic.PackBlob) {
for pb := range blobs {
if err := ctx.Err(); err != nil {
return packs, err
}
packID := pb.PackID()
h := pb.Handle()
tpe, exists := packs[packID]
@@ -100,8 +105,8 @@ func computePackTypes(ctx context.Context, idx restic.ListBlobser) (map[restic.I
tpe = h.Type
}
packs[packID] = tpe
})
return packs, err
}
return packs, nil
}
// LoadIndex loads all index files.
@@ -153,7 +158,7 @@ func (c *Checker) LoadIndex(ctx context.Context, p restic.TerminalCounterFactory
return hints, append(errs, err)
}
packTypes, err := computePackTypes(ctx, c.repo)
packTypes, err := computePackTypes(ctx, c.repo.ListBlobs())
if err != nil {
return hints, append(errs, err)
}
@@ -189,7 +194,7 @@ func (c *Checker) Packs(ctx context.Context, errChan chan<- error) {
defer close(errChan)
// compute pack size using index entries
packs, err := pack.Size(ctx, c.repo, false)
packs, err := pack.Size(ctx, c.repo.ListBlobs(), false)
if err != nil {
errChan <- err
return
@@ -249,7 +254,7 @@ func (c *Checker) ReadPacks(ctx context.Context, filter func(packs map[restic.ID
defer close(errChan)
// compute pack size using index entries
packs, err := pack.Size(ctx, c.repo, false)
packs, err := pack.Size(ctx, c.repo.ListBlobs(), false)
if err != nil {
errChan <- err
return
+1 -1
View File
@@ -41,7 +41,7 @@ func TestGapInBlobs(t *testing.T) {
err := repo.LoadIndex(context.TODO(), restic.NoopTerminalCounterFactory)
rtest.OK(t, err)
repoPacks, err := pack.Size(context.TODO(), repo, false)
repoPacks, err := pack.Size(context.TODO(), repo.ListBlobs(), false)
rtest.OK(t, err)
packID := restic.TestParseID("19a731a515618ec8b75fc0ff3b887d8feb83aef1001c9899f6702761142ed068")
+11
View File
@@ -234,6 +234,17 @@ func (mi *MasterIndex) Values() iter.Seq[*pack.PackedBlob] {
}
}
// PackBlobSeq adapts an iterator of index entries to restic.PackBlob.
func PackBlobSeq(blobs iter.Seq[*pack.PackedBlob]) iter.Seq[restic.PackBlob] {
return func(yield func(restic.PackBlob) bool) {
for blob := range blobs {
if !yield(blob) {
return
}
}
}
}
// MergeFinalIndexes merges all final indexes together.
// After calling, there will be only one big final index in MasterIndex
// containing all final index contents.
+2 -2
View File
@@ -26,9 +26,9 @@ func TestAllIndexBlobs(t *testing.T) {
rtest.OK(t, repo.LoadIndex(context.TODO(), restic.NoopTerminalCounterFactory))
fromMaster := restic.NewBlobSet()
rtest.OK(t, repo.ListBlobs(context.TODO(), func(pb restic.PackBlob) {
for pb := range repo.ListBlobs() {
fromMaster.Insert(pb.Handle())
}))
}
rtest.Equals(t, want, fromMaster)
fromStream := restic.NewBlobSet()
+8 -4
View File
@@ -6,6 +6,7 @@ import (
"encoding/binary"
"fmt"
"io"
"iter"
"sync"
"github.com/restic/restic/internal/debug"
@@ -439,10 +440,13 @@ func CalculateHeaderSize(blobs Blobs) int {
// If onlyHdr is set to true, only the size of the header is returned
// Note that this function only gives correct sizes, if there are no
// duplicates in the index.
func Size(ctx context.Context, idx restic.ListBlobser, onlyHdr bool) (map[restic.ID]int64, error) {
func Size(ctx context.Context, blobs iter.Seq[restic.PackBlob], onlyHdr bool) (map[restic.ID]int64, error) {
packSize := make(map[restic.ID]int64)
err := idx.ListBlobs(ctx, func(blob restic.PackBlob) {
for blob := range blobs {
if err := ctx.Err(); err != nil {
return packSize, err
}
packID := blob.PackID()
size, ok := packSize[packID]
if !ok {
@@ -452,7 +456,7 @@ func Size(ctx context.Context, idx restic.ListBlobser, onlyHdr bool) (map[restic
size += int64(blob.CiphertextLength())
}
packSize[packID] = size + int64(CalculateEntrySize(blob.IsCompressed()))
})
}
return packSize, err
return packSize, nil
}
+23 -21
View File
@@ -4,6 +4,7 @@ import (
"cmp"
"context"
"fmt"
"iter"
"math"
"slices"
"sort"
@@ -127,7 +128,7 @@ func PlanPrune(ctx context.Context, opts PruneOptions, repo *Repository, getUsed
}
printer.P("searching used packs...\n")
keepBlobs, indexPack, err := packInfoFromIndex(ctx, repo, usedBlobs, &stats, printer)
keepBlobs, indexPack, err := packInfoFromIndex(ctx, repo.ListBlobs, usedBlobs, &stats, printer)
if err != nil {
return nil, err
}
@@ -141,15 +142,15 @@ func PlanPrune(ctx context.Context, opts PruneOptions, repo *Repository, getUsed
if len(plan.repackPacks) != 0 {
// when repacking, we do not want to keep blobs which are
// already contained in kept packs, so delete them from keepBlobs
err := repo.ListBlobs(ctx, func(blob restic.PackBlob) {
for blob := range repo.ListBlobs() {
if err := ctx.Err(); err != nil {
return nil, err
}
packID := blob.PackID()
if plan.removePacks.Has(packID) || plan.repackPacks.Has(packID) {
return
continue
}
keepBlobs.Delete(blob.Handle())
})
if err != nil {
return nil, err
}
} else {
// keepBlobs is only needed if packs are repacked
@@ -175,11 +176,15 @@ func PlanPrune(ctx context.Context, opts PruneOptions, repo *Repository, getUsed
return &plan, nil
}
func packInfoFromIndex(ctx context.Context, idx restic.ListBlobser, usedBlobs *index.AssociatedSet[uint8], stats *PruneStats, printer restic.Printer) (*index.AssociatedSet[uint8], map[restic.ID]packInfo, error) {
func packInfoFromIndex(ctx context.Context, idx func() iter.Seq[restic.PackBlob], usedBlobs *index.AssociatedSet[uint8], stats *PruneStats, printer restic.Printer) (*index.AssociatedSet[uint8], map[restic.ID]packInfo, error) {
// iterate over all blobs in index to find out which blobs are duplicates
// The counter in usedBlobs describes how many instances of the blob exist in the repository index
// Thus 0 == blob is missing, 1 == blob exists once, >= 2 == duplicates exist
err := idx.ListBlobs(ctx, func(blob restic.PackBlob) {
for blob := range idx() {
if ctx.Err() != nil {
return nil, nil, ctx.Err()
}
bh := blob.Handle()
count, ok := usedBlobs.Get(bh)
if ok {
@@ -192,9 +197,6 @@ func packInfoFromIndex(ctx context.Context, idx restic.ListBlobser, usedBlobs *i
usedBlobs.Set(bh, count)
}
})
if err != nil {
return nil, nil, err
}
// Check if all used blobs have been found in index
@@ -218,7 +220,7 @@ func packInfoFromIndex(ctx context.Context, idx restic.ListBlobser, usedBlobs *i
indexPack := make(map[restic.ID]packInfo)
// save computed pack header size
sz, err := pack.Size(ctx, idx, true)
sz, err := pack.Size(ctx, idx(), true)
if err != nil {
return nil, nil, err
}
@@ -229,7 +231,10 @@ func packInfoFromIndex(ctx context.Context, idx restic.ListBlobser, usedBlobs *i
hasDuplicates := false
// iterate over all blobs in index to generate packInfo
err = idx.ListBlobs(ctx, func(blob restic.PackBlob) {
for blob := range idx() {
if ctx.Err() != nil {
return nil, nil, ctx.Err()
}
packID := blob.PackID()
h := blob.Handle()
@@ -276,9 +281,6 @@ func packInfoFromIndex(ctx context.Context, idx restic.ListBlobser, usedBlobs *i
}
// update indexPack
indexPack[packID] = ip
})
if err != nil {
return nil, nil, err
}
// if duplicate blobs exist, those will be set to either "used" or "unused":
@@ -289,14 +291,17 @@ func packInfoFromIndex(ctx context.Context, idx restic.ListBlobser, usedBlobs *i
// - if there are no used blobs in a pack, possibly mark duplicates as "unused"
if hasDuplicates {
// iterate again over all blobs in index (this is pretty cheap, all in-mem)
err = idx.ListBlobs(ctx, func(blob restic.PackBlob) {
for blob := range idx() {
if ctx.Err() != nil {
return nil, nil, ctx.Err()
}
packID := blob.PackID()
bh := blob.Handle()
count, ok := usedBlobs.Get(bh)
// skip non-duplicate, aka. normal blobs
// count == 0 is used to mark that this was a duplicate blob with only a single occurrence remaining
if !ok || count == 1 {
return
continue
}
ip := indexPack[packID]
@@ -329,9 +334,6 @@ func packInfoFromIndex(ctx context.Context, idx restic.ListBlobser, usedBlobs *i
}
// update indexPack
indexPack[packID] = ip
})
if err != nil {
return nil, nil, err
}
}
+2 -2
View File
@@ -150,7 +150,7 @@ func TestPruneSmall(t *testing.T) {
}))
// gather number of packfiles
repoPacks, err := pack.Size(context.TODO(), repo, false)
repoPacks, err := pack.Size(context.TODO(), repo.ListBlobs(), false)
rtest.OK(t, err)
lenPackfilesBefore := len(repoPacks)
rtest.OK(t, repo.Close())
@@ -188,7 +188,7 @@ func TestPruneSmall(t *testing.T) {
rtest.OK(t, err)
}
repoPacks, err = pack.Size(context.TODO(), repo, false)
repoPacks, err = pack.Size(context.TODO(), repo.ListBlobs(), false)
rtest.OK(t, err)
lenPackfilesAfter := len(repoPacks)
+1 -1
View File
@@ -43,7 +43,7 @@ func RepairIndex(ctx context.Context, repo *Repository, opts RepairIndexOptions,
return err
}
packSizeFromIndex, err = pack.Size(ctx, repo, false)
packSizeFromIndex, err = pack.Size(ctx, repo.ListBlobs(), false)
if err != nil {
return err
}
+4 -4
View File
@@ -15,9 +15,9 @@ import (
func listBlobs(repo restic.Repository) restic.BlobSet {
blobs := restic.NewBlobSet()
_ = repo.ListBlobs(context.TODO(), func(pb restic.PackBlob) {
for pb := range repo.ListBlobs() {
blobs.Insert(pb.Handle())
})
}
return blobs
}
@@ -87,11 +87,11 @@ func testRepairBrokenPack(t *testing.T, version uint) {
// all blobs in the file are broken
damagedBlobs := restic.NewBlobSet()
rtest.OK(t, repo.ListBlobs(context.TODO(), func(pb restic.PackBlob) {
for pb := range repo.ListBlobs() {
if pb.PackID().Equal(damagedID) {
damagedBlobs.Insert(pb.Handle())
}
}))
}
return restic.NewIDSet(damagedID), damagedBlobs
},
}, {
+5 -10
View File
@@ -5,6 +5,7 @@ import (
"context"
"fmt"
"io"
"iter"
"math"
"runtime"
"sync"
@@ -713,16 +714,10 @@ func (r *Repository) LookupBlobSize(bh restic.BlobHandle) (uint, bool) {
return r.idx.LookupSize(bh)
}
// 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.PackBlob)) error {
for blob := range r.idx.Values() {
if ctx.Err() != nil {
return ctx.Err()
}
fn(blob)
}
return nil
// ListBlobs returns an iterator over all blobs known to the index.
// While using the iterator, the index cannot be modified.
func (r *Repository) ListBlobs() iter.Seq[restic.PackBlob] {
return index.PackBlobSeq(r.idx.Values())
}
// listPacksFromIndex returns index entries for the given packs, grouped by pack file.
+3 -7
View File
@@ -25,9 +25,9 @@ type Repository interface {
LookupBlobSize(bh BlobHandle) (size uint, exists bool)
NewAssociatedBlobSet() AssociatedBlobSet
// 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.
ListBlobs(ctx context.Context, fn func(PackBlob)) error
// ListBlobs returns an iterator over all blobs known to the index.
// While using the iterator, the index cannot be modified.
ListBlobs() iter.Seq[PackBlob]
// ListPackHandles returns the blob handles stored in the pack file header.
ListPackHandles(ctx context.Context, id ID, packSize int64) ([]BlobHandle, error)
@@ -108,10 +108,6 @@ type Unpacked[FT FileTypes] interface {
RemoverUnpacked[FT]
}
type ListBlobser interface {
ListBlobs(ctx context.Context, fn func(PackBlob)) error
}
type BlobLoader interface {
LoadBlob(context.Context, BlobHandle, []byte) ([]byte, error)
}