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/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)),

View File

@ -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,

View File

@ -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
}

View File

@ -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{