Decouple index/ and repository/

This commit is contained in:
Alexander Neumann 2016-08-14 16:11:59 +02:00
parent 3b57075109
commit 1f263a7683
3 changed files with 37 additions and 15 deletions

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"os" "os"
"restic"
"restic/backend" "restic/backend"
"restic/debug" "restic/debug"
"restic/pack" "restic/pack"
@ -39,8 +40,14 @@ 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 *repository.Repository) (*Index, error) { func New(repo restic.Repository) (*Index, error) {
done := make(chan struct{}) done := make(chan struct{})
defer close(done) defer close(done)
@ -56,16 +63,16 @@ func New(repo *repository.Repository) (*Index, error) {
continue continue
} }
j := job.Result.(repository.ListAllPacksResult) j := job.Result.(listAllPacksResult)
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()))
err := idx.AddPack(packID, j.Size, j.Entries) err := idx.AddPack(packID, j.Size(), j.Entries())
if err != nil { if err != nil {
return nil, err return nil, err
} }
p := Pack{Entries: j.Entries, Size: j.Size} p := Pack{Entries: j.Entries(), Size: j.Size()}
idx.Packs[packID] = p idx.Packs[packID] = p
} }
@ -91,7 +98,7 @@ type indexJSON struct {
Packs []*packJSON `json:"packs"` Packs []*packJSON `json:"packs"`
} }
func loadIndexJSON(repo *repository.Repository, id backend.ID) (*indexJSON, error) { func loadIndexJSON(repo restic.Repository, id backend.ID) (*indexJSON, error) {
debug.Log("index.loadIndexJSON", "process index %v\n", id.Str()) debug.Log("index.loadIndexJSON", "process index %v\n", id.Str())
var idx indexJSON var idx indexJSON
@ -104,7 +111,7 @@ func loadIndexJSON(repo *repository.Repository, id backend.ID) (*indexJSON, erro
} }
// Load creates an index by loading all index files from the repo. // Load creates an index by loading all index files from the repo.
func Load(repo *repository.Repository) (*Index, error) { func Load(repo restic.Repository) (*Index, error) {
debug.Log("index.Load", "loading indexes") debug.Log("index.Load", "loading indexes")
done := make(chan struct{}) done := make(chan struct{})

View File

@ -31,13 +31,13 @@ func RebuildIndex(repo *Repository) error {
res := job.Result.(ListAllPacksResult) res := job.Result.(ListAllPacksResult)
for _, entry := range res.Entries { for _, entry := range res.Entries() {
pb := PackedBlob{ pb := PackedBlob{
ID: entry.ID, ID: entry.ID,
Type: entry.Type, Type: entry.Type,
Length: entry.Length, Length: entry.Length,
Offset: entry.Offset, Offset: entry.Offset,
PackID: res.PackID, PackID: res.PackID(),
} }
idx.Store(pb) idx.Store(pb)
} }

View File

@ -16,9 +16,24 @@ type Lister interface {
// ListAllPacksResult is returned in the channel from LoadBlobsFromAllPacks. // ListAllPacksResult is returned in the channel from LoadBlobsFromAllPacks.
type ListAllPacksResult struct { type ListAllPacksResult 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.
func (l ListAllPacksResult) PackID() backend.ID {
return l.packID
}
// Size ruturns the size of the pack.
func (l ListAllPacksResult) Size() int64 {
return l.size
}
// Entries returns a list of all blobs saved in the pack.
func (l ListAllPacksResult) Entries() []pack.Blob {
return l.entries
} }
// ListAllPacks sends the contents of all packs to ch. // ListAllPacks sends the contents of all packs to ch.
@ -28,9 +43,9 @@ func ListAllPacks(repo Lister, ch chan<- worker.Job, done <-chan struct{}) {
entries, size, err := repo.ListPack(packID) entries, size, err := repo.ListPack(packID)
return ListAllPacksResult{ return ListAllPacksResult{
PackID: packID, packID: packID,
Size: size, size: size,
Entries: entries, entries: entries,
}, err }, err
} }