mirror of
https://github.com/restic/restic.git
synced 2024-12-24 08:44:52 +00:00
repository: Move cache preparation into function
This commit is contained in:
parent
22d5061df2
commit
2e7ec717c1
1 changed files with 62 additions and 41 deletions
|
@ -413,9 +413,23 @@ func (r *Repository) LoadIndex(ctx context.Context) error {
|
|||
r.idx.Insert(idx)
|
||||
}
|
||||
|
||||
if r.Cache != nil {
|
||||
err := r.PrepareCache(validIndex)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return <-errCh
|
||||
}
|
||||
|
||||
// PrepareCache initializes the local cache. indexIDs is the list of IDs of
|
||||
// index files still present in the repo.
|
||||
func (r *Repository) PrepareCache(indexIDs restic.IDSet) error {
|
||||
if r.Cache == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// clear old index files
|
||||
err := r.Cache.Clear(restic.IndexFile, validIndex)
|
||||
err := r.Cache.Clear(restic.IndexFile, indexIDs)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error clearing index files in cache: %v\n", err)
|
||||
}
|
||||
|
@ -441,22 +455,29 @@ func (r *Repository) LoadIndex(ctx context.Context) error {
|
|||
}
|
||||
|
||||
// use readahead
|
||||
debug.Log("using readahead")
|
||||
cache := r.Cache.(*cache.Cache)
|
||||
cache.PerformReadahead = func(h restic.Handle) bool {
|
||||
if h.Type != restic.DataFile {
|
||||
debug.Log("no readahead for %v, is not data file", h)
|
||||
return false
|
||||
}
|
||||
|
||||
id, err := restic.ParseID(h.Name)
|
||||
if err != nil {
|
||||
debug.Log("no readahead for %v, invalid ID", h)
|
||||
return false
|
||||
}
|
||||
|
||||
return treePacks.Has(id)
|
||||
if treePacks.Has(id) {
|
||||
debug.Log("perform readahead for %v", h)
|
||||
return true
|
||||
}
|
||||
debug.Log("no readahead for %v, not tree file", h)
|
||||
return false
|
||||
}
|
||||
|
||||
return <-errCh
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadIndex loads the index id from backend and returns it.
|
||||
|
|
Loading…
Reference in a new issue