From 80bcae44e2b844ca0e34e52761c81d3a45419db9 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 14 Aug 2016 17:59:20 +0200 Subject: [PATCH] Decouple ListAllPacks from repository --- src/restic/index/index.go | 14 ++++---------- src/restic/{repository => list}/list.go | 18 +++++++++--------- src/restic/repository.go | 1 + src/restic/repository/index_rebuild.go | 5 +++-- 4 files changed, 17 insertions(+), 21 deletions(-) rename src/restic/{repository => list}/list.go (70%) diff --git a/src/restic/index/index.go b/src/restic/index/index.go index c0b404a3f..9426fced4 100644 --- a/src/restic/index/index.go +++ b/src/restic/index/index.go @@ -8,8 +8,8 @@ import ( "restic" "restic/backend" "restic/debug" + "restic/list" "restic/pack" - "restic/repository" "restic/worker" ) @@ -40,19 +40,13 @@ func newIndex() *Index { } } -type listAllPacksResult interface { - PackID() backend.ID - Entries() []pack.Blob - Size() int64 -} - // New creates a new index for repo from scratch. func New(repo restic.Repository) (*Index, error) { done := make(chan struct{}) defer close(done) ch := make(chan worker.Job) - go repository.ListAllPacks(repo, ch, done) + go list.AllPacks(repo, ch, done) idx := newIndex() @@ -63,7 +57,7 @@ func New(repo restic.Repository) (*Index, error) { continue } - j := job.Result.(listAllPacksResult) + j := job.Result.(list.Result) debug.Log("Index.New", "pack %v contains %d blobs", packID.Str(), len(j.Entries())) @@ -274,7 +268,7 @@ func (idx *Index) FindBlob(h pack.Handle) ([]Location, error) { } // Save writes a new index containing the given packs. -func Save(repo *repository.Repository, packs map[backend.ID][]pack.Blob, supersedes backend.IDs) (backend.ID, error) { +func Save(repo restic.Repository, packs map[backend.ID][]pack.Blob, supersedes backend.IDs) (backend.ID, error) { idx := &indexJSON{ Supersedes: supersedes, Packs: make([]*packJSON, 0, len(packs)), diff --git a/src/restic/repository/list.go b/src/restic/list/list.go similarity index 70% rename from src/restic/repository/list.go rename to src/restic/list/list.go index a3c0c5d99..e3a14798f 100644 --- a/src/restic/repository/list.go +++ b/src/restic/list/list.go @@ -1,4 +1,4 @@ -package repository +package list import ( "restic/backend" @@ -14,35 +14,35 @@ type Lister interface { ListPack(backend.ID) ([]pack.Blob, int64, error) } -// ListAllPacksResult is returned in the channel from LoadBlobsFromAllPacks. -type ListAllPacksResult struct { +// Result is returned in the channel from LoadBlobsFromAllPacks. +type Result struct { packID backend.ID size int64 entries []pack.Blob } // PackID returns the pack ID of this result. -func (l ListAllPacksResult) PackID() backend.ID { +func (l Result) PackID() backend.ID { return l.packID } // Size ruturns the size of the pack. -func (l ListAllPacksResult) Size() int64 { +func (l Result) Size() int64 { return l.size } // Entries returns a list of all blobs saved in the pack. -func (l ListAllPacksResult) Entries() []pack.Blob { +func (l Result) Entries() []pack.Blob { return l.entries } -// ListAllPacks sends the contents of all packs to ch. -func ListAllPacks(repo Lister, ch chan<- worker.Job, done <-chan struct{}) { +// AllPacks sends the contents of all packs to ch. +func AllPacks(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{ + return Result{ packID: packID, size: size, entries: entries, diff --git a/src/restic/repository.go b/src/restic/repository.go index 1fba0ded7..e35de0e94 100644 --- a/src/restic/repository.go +++ b/src/restic/repository.go @@ -8,6 +8,7 @@ import ( // Repository manages encrypted and packed data stored in a backend. type Repository interface { LoadJSONUnpacked(backend.Type, backend.ID, interface{}) error + SaveJSONUnpacked(backend.Type, interface{}) (backend.ID, error) Lister } diff --git a/src/restic/repository/index_rebuild.go b/src/restic/repository/index_rebuild.go index fa33c5c83..99c281484 100644 --- a/src/restic/repository/index_rebuild.go +++ b/src/restic/repository/index_rebuild.go @@ -5,6 +5,7 @@ import ( "os" "restic/backend" "restic/debug" + "restic/list" "restic/worker" ) @@ -18,7 +19,7 @@ func RebuildIndex(repo *Repository) error { defer close(done) ch := make(chan worker.Job) - go ListAllPacks(repo, ch, done) + go list.AllPacks(repo, ch, done) idx := NewIndex() for job := range ch { @@ -29,7 +30,7 @@ func RebuildIndex(repo *Repository) error { continue } - res := job.Result.(ListAllPacksResult) + res := job.Result.(list.Result) for _, entry := range res.Entries() { pb := PackedBlob{