mirror of
https://github.com/restic/restic.git
synced 2025-12-10 21:41:46 +00:00
repository: replace SetIndex method with internal loadIndexWithCallback method
This commit is contained in:
@@ -80,15 +80,10 @@ func computePackTypes(ctx context.Context, idx restic.ListBlobser) (map[restic.I
|
|||||||
// LoadIndex loads all index files.
|
// LoadIndex loads all index files.
|
||||||
func (c *Checker) LoadIndex(ctx context.Context, p restic.TerminalCounterFactory) (hints []error, errs []error) {
|
func (c *Checker) LoadIndex(ctx context.Context, p restic.TerminalCounterFactory) (hints []error, errs []error) {
|
||||||
debug.Log("Start")
|
debug.Log("Start")
|
||||||
|
|
||||||
var bar *progress.Counter
|
|
||||||
if p != nil {
|
|
||||||
bar = p.NewCounterTerminalOnly("index files loaded")
|
|
||||||
}
|
|
||||||
|
|
||||||
packToIndex := make(map[restic.ID]restic.IDSet)
|
packToIndex := make(map[restic.ID]restic.IDSet)
|
||||||
masterIndex := index.NewMasterIndex()
|
|
||||||
err := masterIndex.Load(ctx, c.repo, bar, func(id restic.ID, idx *index.Index, err error) error {
|
// Use the repository's internal loadIndexWithCallback to handle per-index errors
|
||||||
|
err := c.repo.loadIndexWithCallback(ctx, p, func(id restic.ID, idx *index.Index, err error) error {
|
||||||
debug.Log("process index %v, err %v", id, err)
|
debug.Log("process index %v, err %v", id, err)
|
||||||
err = errors.Wrapf(err, "error loading index %v", id)
|
err = errors.Wrapf(err, "error loading index %v", id)
|
||||||
|
|
||||||
@@ -116,12 +111,6 @@ func (c *Checker) LoadIndex(ctx context.Context, p restic.TerminalCounterFactory
|
|||||||
return hints, append(errs, err)
|
return hints, append(errs, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = c.repo.SetIndex(masterIndex)
|
|
||||||
if err != nil {
|
|
||||||
debug.Log("SetIndex returned error: %v", err)
|
|
||||||
errs = append(errs, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// compute pack size using index entries
|
// compute pack size using index entries
|
||||||
c.packs, err = pack.Size(ctx, c.repo, false)
|
c.packs, err = pack.Size(ctx, c.repo, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -32,30 +32,18 @@ func RepairIndex(ctx context.Context, repo *Repository, opts RepairIndexOptions,
|
|||||||
|
|
||||||
} else {
|
} else {
|
||||||
printer.P("loading indexes...\n")
|
printer.P("loading indexes...\n")
|
||||||
mi := index.NewMasterIndex()
|
err := repo.loadIndexWithCallback(ctx, nil, func(id restic.ID, _ *index.Index, err error) error {
|
||||||
err := index.ForAllIndexes(ctx, repo, repo, func(id restic.ID, idx *index.Index, err error) error {
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
printer.E("removing invalid index %v: %v\n", id, err)
|
printer.E("removing invalid index %v: %v\n", id, err)
|
||||||
obsoleteIndexes = append(obsoleteIndexes, id)
|
obsoleteIndexes = append(obsoleteIndexes, id)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
mi.Insert(idx)
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = mi.MergeFinalIndexes()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = repo.SetIndex(mi)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
packSizeFromIndex, err = pack.Size(ctx, repo, false)
|
packSizeFromIndex, err = pack.Size(ctx, repo, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -632,18 +632,17 @@ func (r *Repository) ListPacksFromIndex(ctx context.Context, packs restic.IDSet)
|
|||||||
return r.idx.ListPacks(ctx, packs)
|
return r.idx.ListPacks(ctx, packs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetIndex instructs the repository to use the given index.
|
|
||||||
func (r *Repository) SetIndex(i restic.MasterIndex) error {
|
|
||||||
r.idx = i.(*index.MasterIndex)
|
|
||||||
return r.prepareCache()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Repository) clearIndex() {
|
func (r *Repository) clearIndex() {
|
||||||
r.idx = index.NewMasterIndex()
|
r.idx = index.NewMasterIndex()
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadIndex loads all index files from the backend in parallel and stores them
|
// LoadIndex loads all index files from the backend in parallel and stores them
|
||||||
func (r *Repository) LoadIndex(ctx context.Context, p restic.TerminalCounterFactory) error {
|
func (r *Repository) LoadIndex(ctx context.Context, p restic.TerminalCounterFactory) error {
|
||||||
|
return r.loadIndexWithCallback(ctx, p, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// loadIndexWithCallback loads all index files from the backend in parallel and stores them
|
||||||
|
func (r *Repository) loadIndexWithCallback(ctx context.Context, p restic.TerminalCounterFactory, cb func(id restic.ID, idx *index.Index, err error) error) error {
|
||||||
debug.Log("Loading index")
|
debug.Log("Loading index")
|
||||||
|
|
||||||
// reset in-memory index before loading it from the repository
|
// reset in-memory index before loading it from the repository
|
||||||
@@ -654,7 +653,7 @@ func (r *Repository) LoadIndex(ctx context.Context, p restic.TerminalCounterFact
|
|||||||
bar = p.NewCounterTerminalOnly("index files loaded")
|
bar = p.NewCounterTerminalOnly("index files loaded")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := r.idx.Load(ctx, r, bar, nil)
|
err := r.idx.Load(ctx, r, bar, cb)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ type Repository interface {
|
|||||||
Key() *crypto.Key
|
Key() *crypto.Key
|
||||||
|
|
||||||
LoadIndex(ctx context.Context, p TerminalCounterFactory) error
|
LoadIndex(ctx context.Context, p TerminalCounterFactory) error
|
||||||
SetIndex(mi MasterIndex) error
|
|
||||||
|
|
||||||
LookupBlob(t BlobType, id ID) []PackedBlob
|
LookupBlob(t BlobType, id ID) []PackedBlob
|
||||||
LookupBlobSize(t BlobType, id ID) (size uint, exists bool)
|
LookupBlobSize(t BlobType, id ID) (size uint, exists bool)
|
||||||
@@ -137,17 +136,6 @@ type TerminalCounterFactory interface {
|
|||||||
NewCounterTerminalOnly(description string) *progress.Counter
|
NewCounterTerminalOnly(description string) *progress.Counter
|
||||||
}
|
}
|
||||||
|
|
||||||
// MasterIndex keeps track of the blobs are stored within files.
|
|
||||||
type MasterIndex interface {
|
|
||||||
Has(bh BlobHandle) bool
|
|
||||||
Lookup(bh BlobHandle) []PackedBlob
|
|
||||||
|
|
||||||
// Each 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.
|
|
||||||
Each(ctx context.Context, fn func(PackedBlob)) error
|
|
||||||
ListPacks(ctx context.Context, packs IDSet) <-chan PackBlobs
|
|
||||||
}
|
|
||||||
|
|
||||||
// Lister allows listing files in a backend.
|
// Lister allows listing files in a backend.
|
||||||
type Lister interface {
|
type Lister interface {
|
||||||
List(ctx context.Context, t FileType, fn func(ID, int64) error) error
|
List(ctx context.Context, t FileType, fn func(ID, int64) error) error
|
||||||
|
|||||||
Reference in New Issue
Block a user