Add progress option to index

This commit is contained in:
Alexander Neumann 2016-08-15 21:10:12 +02:00
parent 8d735cf6a9
commit 8de6e5a627
2 changed files with 20 additions and 9 deletions

View file

@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"restic"
"restic/backend"
"restic/debug"
"restic/list"
@ -41,16 +42,21 @@ func newIndex() *Index {
}
// New creates a new index for repo from scratch.
func New(repo types.Repository) (*Index, error) {
func New(repo types.Repository, p *restic.Progress) (*Index, error) {
done := make(chan struct{})
defer close(done)
p.Start()
defer p.Done()
ch := make(chan worker.Job)
go list.AllPacks(repo, ch, done)
idx := newIndex()
for job := range ch {
p.Report(restic.Stat{Blobs: 1})
packID := job.Data.(backend.ID)
if job.Error != nil {
fmt.Fprintf(os.Stderr, "unable to list pack %v: %v\n", packID.Str(), job.Error)
@ -105,9 +111,12 @@ func loadIndexJSON(repo types.Repository, id backend.ID) (*indexJSON, error) {
}
// Load creates an index by loading all index files from the repo.
func Load(repo types.Repository) (*Index, error) {
func Load(repo types.Repository, p *restic.Progress) (*Index, error) {
debug.Log("index.Load", "loading indexes")
p.Start()
defer p.Done()
done := make(chan struct{})
defer close(done)
@ -117,6 +126,8 @@ func Load(repo types.Repository) (*Index, error) {
index := newIndex()
for id := range repo.List(backend.Index, done) {
p.Report(restic.Stat{Blobs: 1})
debug.Log("index.Load", "Load index %v", id.Str())
idx, err := loadIndexJSON(repo, id)
if err != nil {

View file

@ -40,7 +40,7 @@ func TestIndexNew(t *testing.T) {
repo, cleanup := createFilledRepo(t, 3, 0)
defer cleanup()
idx, err := New(repo)
idx, err := New(repo, nil)
if err != nil {
t.Fatalf("New() returned error %v", err)
}
@ -56,7 +56,7 @@ func TestIndexLoad(t *testing.T) {
repo, cleanup := createFilledRepo(t, 3, 0)
defer cleanup()
loadIdx, err := Load(repo)
loadIdx, err := Load(repo, nil)
if err != nil {
t.Fatalf("Load() returned error %v", err)
}
@ -67,7 +67,7 @@ func TestIndexLoad(t *testing.T) {
validateIndex(t, repo, loadIdx)
newIdx, err := New(repo)
newIdx, err := New(repo, nil)
if err != nil {
t.Fatalf("New() returned error %v", err)
}
@ -146,7 +146,7 @@ func BenchmarkIndexNew(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
idx, err := New(repo)
idx, err := New(repo, nil)
if err != nil {
b.Fatalf("New() returned error %v", err)
@ -162,7 +162,7 @@ func TestIndexDuplicateBlobs(t *testing.T) {
repo, cleanup := createFilledRepo(t, 3, 0.01)
defer cleanup()
idx, err := New(repo)
idx, err := New(repo, nil)
if err != nil {
t.Fatal(err)
}
@ -181,7 +181,7 @@ func TestIndexDuplicateBlobs(t *testing.T) {
}
func loadIndex(t testing.TB, repo *repository.Repository) *Index {
idx, err := Load(repo)
idx, err := Load(repo, nil)
if err != nil {
t.Fatalf("Load() returned error %v", err)
}
@ -243,7 +243,7 @@ func TestIndexAddRemovePack(t *testing.T) {
repo, cleanup := createFilledRepo(t, 3, 0)
defer cleanup()
idx, err := Load(repo)
idx, err := Load(repo, nil)
if err != nil {
t.Fatalf("Load() returned error %v", err)
}