2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-23 11:28:54 +00:00

Decouple ListAllPacks from repository

This commit is contained in:
Alexander Neumann 2016-08-14 17:59:20 +02:00
parent 1f263a7683
commit 80bcae44e2
4 changed files with 17 additions and 21 deletions

View File

@ -8,8 +8,8 @@ import (
"restic" "restic"
"restic/backend" "restic/backend"
"restic/debug" "restic/debug"
"restic/list"
"restic/pack" "restic/pack"
"restic/repository"
"restic/worker" "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. // New creates a new index for repo from scratch.
func New(repo restic.Repository) (*Index, error) { func New(repo restic.Repository) (*Index, error) {
done := make(chan struct{}) done := make(chan struct{})
defer close(done) defer close(done)
ch := make(chan worker.Job) ch := make(chan worker.Job)
go repository.ListAllPacks(repo, ch, done) go list.AllPacks(repo, ch, done)
idx := newIndex() idx := newIndex()
@ -63,7 +57,7 @@ func New(repo restic.Repository) (*Index, error) {
continue continue
} }
j := job.Result.(listAllPacksResult) j := job.Result.(list.Result)
debug.Log("Index.New", "pack %v contains %d blobs", packID.Str(), len(j.Entries())) 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. // 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{ idx := &indexJSON{
Supersedes: supersedes, Supersedes: supersedes,
Packs: make([]*packJSON, 0, len(packs)), Packs: make([]*packJSON, 0, len(packs)),

View File

@ -1,4 +1,4 @@
package repository package list
import ( import (
"restic/backend" "restic/backend"
@ -14,35 +14,35 @@ type Lister interface {
ListPack(backend.ID) ([]pack.Blob, int64, error) ListPack(backend.ID) ([]pack.Blob, int64, error)
} }
// ListAllPacksResult is returned in the channel from LoadBlobsFromAllPacks. // Result is returned in the channel from LoadBlobsFromAllPacks.
type ListAllPacksResult struct { type Result struct {
packID backend.ID packID backend.ID
size int64 size int64
entries []pack.Blob entries []pack.Blob
} }
// PackID returns the pack ID of this result. // PackID returns the pack ID of this result.
func (l ListAllPacksResult) PackID() backend.ID { func (l Result) PackID() backend.ID {
return l.packID return l.packID
} }
// Size ruturns the size of the pack. // Size ruturns the size of the pack.
func (l ListAllPacksResult) Size() int64 { func (l Result) Size() int64 {
return l.size return l.size
} }
// Entries returns a list of all blobs saved in the pack. // 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 return l.entries
} }
// ListAllPacks sends the contents of all packs to ch. // AllPacks sends the contents of all packs to ch.
func ListAllPacks(repo Lister, ch chan<- worker.Job, done <-chan struct{}) { func AllPacks(repo Lister, ch chan<- worker.Job, done <-chan struct{}) {
f := func(job worker.Job, done <-chan struct{}) (interface{}, error) { f := func(job worker.Job, done <-chan struct{}) (interface{}, error) {
packID := job.Data.(backend.ID) packID := job.Data.(backend.ID)
entries, size, err := repo.ListPack(packID) entries, size, err := repo.ListPack(packID)
return ListAllPacksResult{ return Result{
packID: packID, packID: packID,
size: size, size: size,
entries: entries, entries: entries,

View File

@ -8,6 +8,7 @@ import (
// Repository manages encrypted and packed data stored in a backend. // Repository manages encrypted and packed data stored in a backend.
type Repository interface { type Repository interface {
LoadJSONUnpacked(backend.Type, backend.ID, interface{}) error LoadJSONUnpacked(backend.Type, backend.ID, interface{}) error
SaveJSONUnpacked(backend.Type, interface{}) (backend.ID, error)
Lister Lister
} }

View File

@ -5,6 +5,7 @@ import (
"os" "os"
"restic/backend" "restic/backend"
"restic/debug" "restic/debug"
"restic/list"
"restic/worker" "restic/worker"
) )
@ -18,7 +19,7 @@ func RebuildIndex(repo *Repository) error {
defer close(done) defer close(done)
ch := make(chan worker.Job) ch := make(chan worker.Job)
go ListAllPacks(repo, ch, done) go list.AllPacks(repo, ch, done)
idx := NewIndex() idx := NewIndex()
for job := range ch { for job := range ch {
@ -29,7 +30,7 @@ func RebuildIndex(repo *Repository) error {
continue continue
} }
res := job.Result.(ListAllPacksResult) res := job.Result.(list.Result)
for _, entry := range res.Entries() { for _, entry := range res.Entries() {
pb := PackedBlob{ pb := PackedBlob{