mirror of
https://github.com/restic/restic.git
synced 2025-10-09 07:33:53 +00:00
copy/find/ls/recover/stats: Memorize snapshot listing before index
These commands filter the snapshots according to some criteria which essentially requires loading the index before filtering the snapshots. Thus create a copy of the snapshots list beforehand and use it later on.
This commit is contained in:
@@ -3,6 +3,7 @@ package backend
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/restic/restic/internal/restic"
|
||||
@@ -57,3 +58,40 @@ func DefaultLoad(ctx context.Context, h restic.Handle, length int, offset int64,
|
||||
}
|
||||
return rd.Close()
|
||||
}
|
||||
|
||||
type memorizedLister struct {
|
||||
fileInfos []restic.FileInfo
|
||||
tpe restic.FileType
|
||||
}
|
||||
|
||||
func (m *memorizedLister) List(ctx context.Context, t restic.FileType, fn func(restic.FileInfo) error) error {
|
||||
if t != m.tpe {
|
||||
return fmt.Errorf("filetype mismatch, expected %s got %s", m.tpe, t)
|
||||
}
|
||||
for _, fi := range m.fileInfos {
|
||||
if ctx.Err() != nil {
|
||||
break
|
||||
}
|
||||
err := fn(fi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
func MemorizeList(ctx context.Context, be restic.Lister, t restic.FileType) (restic.Lister, error) {
|
||||
var fileInfos []restic.FileInfo
|
||||
err := be.List(ctx, t, func(fi restic.FileInfo) error {
|
||||
fileInfos = append(fileInfos, fi)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &memorizedLister{
|
||||
fileInfos: fileInfos,
|
||||
tpe: t,
|
||||
}, nil
|
||||
}
|
||||
|
@@ -3,6 +3,7 @@ package backend_test
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/rand"
|
||||
"testing"
|
||||
@@ -10,6 +11,7 @@ import (
|
||||
"github.com/restic/restic/internal/backend"
|
||||
"github.com/restic/restic/internal/backend/mem"
|
||||
"github.com/restic/restic/internal/errors"
|
||||
"github.com/restic/restic/internal/mock"
|
||||
"github.com/restic/restic/internal/restic"
|
||||
rtest "github.com/restic/restic/internal/test"
|
||||
)
|
||||
@@ -157,3 +159,47 @@ func TestDefaultLoad(t *testing.T) {
|
||||
rtest.Equals(t, true, rd.closed)
|
||||
rtest.Equals(t, "consumer error", err.Error())
|
||||
}
|
||||
|
||||
func TestMemoizeList(t *testing.T) {
|
||||
// setup backend to serve as data source for memoized list
|
||||
be := mock.NewBackend()
|
||||
files := []restic.FileInfo{
|
||||
{Size: 42, Name: restic.NewRandomID().String()},
|
||||
{Size: 45, Name: restic.NewRandomID().String()},
|
||||
}
|
||||
be.ListFn = func(ctx context.Context, t restic.FileType, fn func(restic.FileInfo) error) error {
|
||||
for _, fi := range files {
|
||||
if err := fn(fi); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
mem, err := backend.MemorizeList(context.TODO(), be, restic.SnapshotFile)
|
||||
rtest.OK(t, err)
|
||||
|
||||
err = mem.List(context.TODO(), restic.IndexFile, func(fi restic.FileInfo) error {
|
||||
t.Fatal("file type mismatch")
|
||||
return nil // the memoized lister must return an error by itself
|
||||
})
|
||||
rtest.Assert(t, err != nil, "missing error on file typ mismatch")
|
||||
|
||||
var memFiles []restic.FileInfo
|
||||
err = mem.List(context.TODO(), restic.SnapshotFile, func(fi restic.FileInfo) error {
|
||||
memFiles = append(memFiles, fi)
|
||||
return nil
|
||||
})
|
||||
rtest.OK(t, err)
|
||||
rtest.Equals(t, files, memFiles)
|
||||
}
|
||||
|
||||
func TestMemoizeListError(t *testing.T) {
|
||||
// setup backend to serve as data source for memoized list
|
||||
be := mock.NewBackend()
|
||||
be.ListFn = func(ctx context.Context, t restic.FileType, fn func(restic.FileInfo) error) error {
|
||||
return fmt.Errorf("list error")
|
||||
}
|
||||
_, err := backend.MemorizeList(context.TODO(), be, restic.SnapshotFile)
|
||||
rtest.Assert(t, err != nil, "missing error on list error")
|
||||
}
|
||||
|
Reference in New Issue
Block a user