2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-26 14:56:29 +00:00

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

View File

@ -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.