mirror of
https://github.com/restic/restic.git
synced 2026-11-10 16:53:09 +00:00
Simplify cache logic
This commit is contained in:
1 parent
ec2e3b260e
commit
81876d5c1b
10 files changed
+64
-100
No files matched your search
@@ -42,10 +42,10 @@ import (
|
||||
|
||||
// Index holds lookup tables for id -> pack.
|
||||
type Index struct {
|
||||
m sync.Mutex
|
||||
byType [restic.NumBlobTypes]indexMap
|
||||
packs restic.IDs
|
||||
treePacks restic.IDs
|
||||
m sync.Mutex
|
||||
byType [restic.NumBlobTypes]indexMap
|
||||
packs restic.IDs
|
||||
mixedPacks restic.IDSet
|
||||
// only used by Store, StorePacks does not check for already saved packIDs
|
||||
packIDToIndex map[restic.ID]int
|
||||
|
||||
@@ -59,6 +59,7 @@ type Index struct {
|
||||
func NewIndex() *Index {
|
||||
return &Index{
|
||||
packIDToIndex: make(map[restic.ID]int),
|
||||
mixedPacks: restic.NewIDSet(),
|
||||
created: time.Now(),
|
||||
}
|
||||
}
|
||||
@@ -511,9 +512,9 @@ func (idx *Index) Dump(w io.Writer) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// TreePacks returns a list of packs that contain only tree blobs.
|
||||
func (idx *Index) TreePacks() restic.IDs {
|
||||
return idx.treePacks
|
||||
// MixedPacks returns an IDSet that contain packs which have mixed blobs.
|
||||
func (idx *Index) MixedPacks() restic.IDSet {
|
||||
return idx.mixedPacks
|
||||
}
|
||||
|
||||
// merge() merges indexes, i.e. idx.merge(idx2) merges the contents of idx2 into idx.
|
||||
@@ -558,7 +559,7 @@ func (idx *Index) merge(idx2 *Index) error {
|
||||
})
|
||||
}
|
||||
|
||||
idx.treePacks = append(idx.treePacks, idx2.treePacks...)
|
||||
idx.mixedPacks.Merge(idx2.mixedPacks)
|
||||
idx.ids = append(idx.ids, idx2.ids...)
|
||||
idx.supersedes = append(idx.supersedes, idx2.supersedes...)
|
||||
|
||||
@@ -612,8 +613,8 @@ func DecodeIndex(buf []byte, id restic.ID) (idx *Index, oldFormat bool, err erro
|
||||
}
|
||||
}
|
||||
|
||||
if !data && tree {
|
||||
idx.treePacks = append(idx.treePacks, pack.ID)
|
||||
if data && tree {
|
||||
idx.mixedPacks.Insert(pack.ID)
|
||||
}
|
||||
}
|
||||
idx.supersedes = idxJSON.Supersedes
|
||||
@@ -657,8 +658,8 @@ func decodeOldIndex(buf []byte) (idx *Index, err error) {
|
||||
}
|
||||
}
|
||||
|
||||
if !data && tree {
|
||||
idx.treePacks = append(idx.treePacks, pack.ID)
|
||||
if data && tree {
|
||||
idx.mixedPacks.Insert(pack.ID)
|
||||
}
|
||||
}
|
||||
idx.final = true
|
||||
|
||||
@@ -100,6 +100,18 @@ func (mi *MasterIndex) Has(bh restic.BlobHandle) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (mi *MasterIndex) IsMixedPack(packID restic.ID) bool {
|
||||
mi.idxMutex.RLock()
|
||||
defer mi.idxMutex.RUnlock()
|
||||
|
||||
for _, idx := range mi.idx {
|
||||
if idx.MixedPacks().Has(packID) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Packs returns all packs that are covered by the index.
|
||||
// If packBlacklist is given, those packs are only contained in the
|
||||
// resulting IDSet if they are contained in a non-final (newly written) index.
|
||||
|
||||
@@ -113,7 +113,8 @@ func (r *Repository) savePacker(ctx context.Context, t restic.BlobType, p *Packe
|
||||
}
|
||||
|
||||
id := restic.IDFromHash(p.hw.Sum(nil))
|
||||
h := restic.Handle{Type: restic.PackFile, Name: id.String()}
|
||||
h := restic.Handle{Type: restic.PackFile, Name: id.String(),
|
||||
ContainedBlobType: t}
|
||||
var beHash []byte
|
||||
if p.beHw != nil {
|
||||
beHash = p.beHw.Sum(nil)
|
||||
@@ -131,20 +132,6 @@ func (r *Repository) savePacker(ctx context.Context, t restic.BlobType, p *Packe
|
||||
|
||||
debug.Log("saved as %v", h)
|
||||
|
||||
if t == restic.TreeBlob && r.Cache != nil {
|
||||
debug.Log("saving tree pack file in cache")
|
||||
|
||||
_, err = p.tmpfile.Seek(0, 0)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Seek")
|
||||
}
|
||||
|
||||
err := r.Cache.Save(h, p.tmpfile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
err = p.tmpfile.Close()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "close tempfile")
|
||||
|
||||
@@ -180,7 +180,12 @@ func (r *Repository) LoadBlob(ctx context.Context, t restic.BlobType, id restic.
|
||||
}
|
||||
|
||||
// load blob from pack
|
||||
h := restic.Handle{Type: restic.PackFile, Name: blob.PackID.String()}
|
||||
bt := t
|
||||
if r.idx.IsMixedPack(blob.PackID) {
|
||||
bt = restic.InvalidBlob
|
||||
}
|
||||
h := restic.Handle{Type: restic.PackFile,
|
||||
Name: blob.PackID.String(), ContainedBlobType: bt}
|
||||
|
||||
switch {
|
||||
case cap(buf) < int(blob.Length):
|
||||
@@ -570,36 +575,6 @@ func (r *Repository) PrepareCache(indexIDs restic.IDSet) error {
|
||||
fmt.Fprintf(os.Stderr, "error clearing pack 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.PerformReadahead = func(h restic.Handle) bool {
|
||||
if h.Type != restic.PackFile {
|
||||
debug.Log("no readahead for %v, is not a pack 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
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user