2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-22 10:58:55 +00:00

repository: Save pack files for trees in cache

This commit is contained in:
Alexander Neumann 2017-07-16 20:24:37 +02:00
parent 62ba9f1950
commit 7a5fde8f5a
2 changed files with 33 additions and 12 deletions

View File

@ -89,8 +89,8 @@ func (r *packerManager) insertPacker(p *Packer) {
}
// savePacker stores p in the backend.
func (r *Repository) savePacker(p *Packer) error {
debug.Log("save packer with %d blobs (%d bytes)\n", p.Packer.Count(), p.Packer.Size())
func (r *Repository) savePacker(t restic.BlobType, p *Packer) error {
debug.Log("save packer for %v with %d blobs (%d bytes)\n", t, p.Packer.Count(), p.Packer.Size())
_, err := p.Packer.Finalize()
if err != nil {
return err
@ -112,6 +112,20 @@ func (r *Repository) savePacker(p *Packer) error {
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")

View File

@ -223,7 +223,7 @@ func (r *Repository) SaveAndEncrypt(ctx context.Context, t restic.BlobType, data
}
// else write the pack to the backend
return *id, r.savePacker(packer)
return *id, r.savePacker(t, packer)
}
// SaveJSONUnpacked serialises item as JSON and encrypts and saves it in the
@ -262,20 +262,27 @@ func (r *Repository) SaveUnpacked(ctx context.Context, t restic.FileType, p []by
// Flush saves all remaining packs.
func (r *Repository) Flush() error {
for _, pm := range []*packerManager{r.dataPM, r.treePM} {
pm.pm.Lock()
pms := []struct {
t restic.BlobType
pm *packerManager
}{
{restic.DataBlob, r.dataPM},
{restic.TreeBlob, r.treePM},
}
debug.Log("manually flushing %d packs", len(pm.packers))
for _, p := range pm.packers {
err := r.savePacker(p)
for _, p := range pms {
p.pm.pm.Lock()
debug.Log("manually flushing %d packs", len(p.pm.packers))
for _, packer := range p.pm.packers {
err := r.savePacker(p.t, packer)
if err != nil {
pm.pm.Unlock()
p.pm.pm.Unlock()
return err
}
}
pm.packers = pm.packers[:0]
pm.pm.Unlock()
p.pm.packers = p.pm.packers[:0]
p.pm.pm.Unlock()
}
return nil