From 3fa7304e94019ae344ac1a6b45816ed48c4f28dc Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 14 Aug 2016 16:01:42 +0200 Subject: [PATCH] Add interfaces to ListAllPacks --- src/restic/repository/index_rebuild.go | 40 -------------------- src/restic/repository/list.go | 52 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 40 deletions(-) create mode 100644 src/restic/repository/list.go diff --git a/src/restic/repository/index_rebuild.go b/src/restic/repository/index_rebuild.go index dea5be0d3..36e42ddff 100644 --- a/src/restic/repository/index_rebuild.go +++ b/src/restic/repository/index_rebuild.go @@ -5,49 +5,9 @@ import ( "os" "restic/backend" "restic/debug" - "restic/pack" "restic/worker" ) -const rebuildIndexWorkers = 10 - -// ListAllPacksResult is returned in the channel from LoadBlobsFromAllPacks. -type ListAllPacksResult struct { - PackID backend.ID - Size int64 - Entries []pack.Blob -} - -// ListAllPacks sends the contents of all packs to ch. -func ListAllPacks(repo *Repository, ch chan<- worker.Job, done <-chan struct{}) { - f := func(job worker.Job, done <-chan struct{}) (interface{}, error) { - packID := job.Data.(backend.ID) - entries, size, err := repo.ListPack(packID) - - return ListAllPacksResult{ - PackID: packID, - Size: size, - Entries: entries, - }, err - } - - jobCh := make(chan worker.Job) - wp := worker.New(rebuildIndexWorkers, f, jobCh, ch) - - go func() { - defer close(jobCh) - for id := range repo.List(backend.Data, done) { - select { - case jobCh <- worker.Job{Data: id}: - case <-done: - return - } - } - }() - - wp.Wait() -} - // RebuildIndex lists all packs in the repo, writes a new index and removes all // old indexes. This operation should only be done with an exclusive lock in // place. diff --git a/src/restic/repository/list.go b/src/restic/repository/list.go new file mode 100644 index 000000000..9d6f44e47 --- /dev/null +++ b/src/restic/repository/list.go @@ -0,0 +1,52 @@ +package repository + +import ( + "restic/backend" + "restic/pack" + "restic/worker" +) + +const listPackWorkers = 10 + +// Lister combines lists packs in a repo and blobs in a pack. +type Lister interface { + List(backend.Type, <-chan struct{}) <-chan backend.ID + ListPack(backend.ID) ([]pack.Blob, int64, error) +} + +// ListAllPacksResult is returned in the channel from LoadBlobsFromAllPacks. +type ListAllPacksResult struct { + PackID backend.ID + Size int64 + Entries []pack.Blob +} + +// ListAllPacks sends the contents of all packs to ch. +func ListAllPacks(repo Lister, ch chan<- worker.Job, done <-chan struct{}) { + f := func(job worker.Job, done <-chan struct{}) (interface{}, error) { + packID := job.Data.(backend.ID) + entries, size, err := repo.ListPack(packID) + + return ListAllPacksResult{ + PackID: packID, + Size: size, + Entries: entries, + }, err + } + + jobCh := make(chan worker.Job) + wp := worker.New(listPackWorkers, f, jobCh, ch) + + go func() { + defer close(jobCh) + for id := range repo.List(backend.Data, done) { + select { + case jobCh <- worker.Job{Data: id}: + case <-done: + return + } + } + }() + + wp.Wait() +}