From 2e7ec717c18acd24e68fec7e303f1be83c2c0c15 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sat, 31 Mar 2018 09:50:45 +0200 Subject: [PATCH] repository: Move cache preparation into function --- internal/repository/repository.go | 103 ++++++++++++++++++------------ 1 file changed, 62 insertions(+), 41 deletions(-) diff --git a/internal/repository/repository.go b/internal/repository/repository.go index fc31c8392..ee3e3b549 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -413,52 +413,73 @@ func (r *Repository) LoadIndex(ctx context.Context) error { r.idx.Insert(idx) } - if r.Cache != nil { - // clear old index files - err := r.Cache.Clear(restic.IndexFile, validIndex) - if err != nil { - fmt.Fprintf(os.Stderr, "error clearing index files in cache: %v\n", err) - } - - packs := restic.NewIDSet() - for _, idx := range r.idx.All() { - for id := range idx.Packs() { - packs.Insert(id) - } - } - - // clear old data files - err = r.Cache.Clear(restic.DataFile, packs) - if err != nil { - fmt.Fprintf(os.Stderr, "error clearing data files in cache: %v\n", err) - } - - treePacks := restic.NewIDSet() - for _, idx := range r.idx.All() { - for _, id := range idx.TreePacks() { - treePacks.Insert(id) - } - } - - // use readahead - cache := r.Cache.(*cache.Cache) - cache.PerformReadahead = func(h restic.Handle) bool { - if h.Type != restic.DataFile { - return false - } - - id, err := restic.ParseID(h.Name) - if err != nil { - return false - } - - return treePacks.Has(id) - } + 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, indexIDs) + if err != nil { + fmt.Fprintf(os.Stderr, "error clearing index files in cache: %v\n", err) + } + + packs := restic.NewIDSet() + for _, idx := range r.idx.All() { + for id := range idx.Packs() { + packs.Insert(id) + } + } + + // clear old data files + err = r.Cache.Clear(restic.DataFile, packs) + if err != nil { + fmt.Fprintf(os.Stderr, "error clearing data files in cache: %v\n", err) + } + + treePacks := restic.NewIDSet() + for _, idx := range r.idx.All() { + for _, id := range idx.TreePacks() { + treePacks.Insert(id) + } + } + + // 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 + } + + 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 nil +} + // LoadIndex loads the index id from backend and returns it. func LoadIndex(ctx context.Context, repo restic.Repository, id restic.ID) (*Index, error) { idx, err := LoadIndexWithDecoder(ctx, repo, id, DecodeIndex)