repository: Move cache preparation into function

This commit is contained in:
Alexander Neumann 2018-03-31 09:50:45 +02:00
parent 22d5061df2
commit 2e7ec717c1
1 changed files with 62 additions and 41 deletions

View File

@ -413,9 +413,23 @@ func (r *Repository) LoadIndex(ctx context.Context) error {
r.idx.Insert(idx) 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 // clear old index files
err := r.Cache.Clear(restic.IndexFile, validIndex) err := r.Cache.Clear(restic.IndexFile, indexIDs)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "error clearing index files in cache: %v\n", err) 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 // use readahead
debug.Log("using readahead")
cache := r.Cache.(*cache.Cache) cache := r.Cache.(*cache.Cache)
cache.PerformReadahead = func(h restic.Handle) bool { cache.PerformReadahead = func(h restic.Handle) bool {
if h.Type != restic.DataFile { if h.Type != restic.DataFile {
debug.Log("no readahead for %v, is not data file", h)
return false return false
} }
id, err := restic.ParseID(h.Name) id, err := restic.ParseID(h.Name)
if err != nil { if err != nil {
debug.Log("no readahead for %v, invalid ID", h)
return false 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. // LoadIndex loads the index id from backend and returns it.