2
2
mirror of https://github.com/octoleo/restic.git synced 2024-06-20 09:42:21 +00:00

cache: Store first, then store in the backend

Store the file in the backend, then rewind the reader and store it
in the cache afterwards.
This commit is contained in:
Alexander Neumann 2017-10-17 21:58:24 +02:00
parent 8dc952775e
commit 8e2ef3f38b

View File

@ -5,6 +5,7 @@ import (
"io" "io"
"sync" "sync"
"github.com/pkg/errors"
"github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
) )
@ -43,52 +44,50 @@ func (b *Backend) Remove(ctx context.Context, h restic.Handle) error {
return b.Cache.Remove(h) return b.Cache.Remove(h)
} }
type teeReader struct {
rd io.Reader
wr io.Writer
err error
}
func (t *teeReader) Read(p []byte) (n int, err error) {
n, err = t.rd.Read(p)
if t.err == nil && n > 0 {
_, t.err = t.wr.Write(p[:n])
}
return n, err
}
var autoCacheTypes = map[restic.FileType]struct{}{ var autoCacheTypes = map[restic.FileType]struct{}{
restic.IndexFile: struct{}{}, restic.IndexFile: struct{}{},
restic.SnapshotFile: struct{}{}, restic.SnapshotFile: struct{}{},
} }
// Save stores a new file is the backend and the cache. // Save stores a new file in the backend and the cache.
func (b *Backend) Save(ctx context.Context, h restic.Handle, rd io.Reader) (err error) { func (b *Backend) Save(ctx context.Context, h restic.Handle, rd io.Reader) (err error) {
if _, ok := autoCacheTypes[h.Type]; !ok { if _, ok := autoCacheTypes[h.Type]; !ok {
return b.Backend.Save(ctx, h, rd) return b.Backend.Save(ctx, h, rd)
} }
debug.Log("Save(%v): auto-store in the cache", h) debug.Log("Save(%v): auto-store in the cache", h)
wr, err := b.Cache.SaveWriter(h)
if err != nil { seeker, ok := rd.(io.Seeker)
debug.Log("unable to save %v to cache: %v", h, err) if !ok {
return b.Backend.Save(ctx, h, rd) return errors.New("reader is not a seeker")
} }
tr := &teeReader{rd: rd, wr: wr} pos, err := seeker.Seek(0, io.SeekCurrent)
err = b.Backend.Save(ctx, h, tr) if err != nil {
return errors.Wrapf(err, "Seek")
}
if pos != 0 {
return errors.Errorf("reader is not rewind (pos %d)", pos)
}
err = b.Backend.Save(ctx, h, rd)
if err != nil { if err != nil {
wr.Close()
b.Cache.Remove(h)
return err return err
} }
err = wr.Close() _, err = seeker.Seek(pos, io.SeekStart)
if err != nil { if err != nil {
debug.Log("cache writer returned error: %v", err) return errors.Wrapf(err, "Seek")
_ = b.Cache.Remove(h)
} }
err = b.Cache.Save(h, rd)
if err != nil {
debug.Log("unable to save %v to cache: %v", h, err)
_ = b.Cache.Remove(h)
return nil
}
return nil return nil
} }