mirror of
https://github.com/octoleo/restic.git
synced 2024-11-26 14:56:29 +00:00
Decouple index/ and repository/
This commit is contained in:
parent
3b57075109
commit
1f263a7683
@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"restic"
|
||||
"restic/backend"
|
||||
"restic/debug"
|
||||
"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.
|
||||
func New(repo *repository.Repository) (*Index, error) {
|
||||
func New(repo restic.Repository) (*Index, error) {
|
||||
done := make(chan struct{})
|
||||
defer close(done)
|
||||
|
||||
@ -56,16 +63,16 @@ func New(repo *repository.Repository) (*Index, error) {
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
p := Pack{Entries: j.Entries, Size: j.Size}
|
||||
p := Pack{Entries: j.Entries(), Size: j.Size()}
|
||||
idx.Packs[packID] = p
|
||||
}
|
||||
|
||||
@ -91,7 +98,7 @@ type indexJSON struct {
|
||||
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())
|
||||
|
||||
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.
|
||||
func Load(repo *repository.Repository) (*Index, error) {
|
||||
func Load(repo restic.Repository) (*Index, error) {
|
||||
debug.Log("index.Load", "loading indexes")
|
||||
|
||||
done := make(chan struct{})
|
||||
|
@ -31,13 +31,13 @@ func RebuildIndex(repo *Repository) error {
|
||||
|
||||
res := job.Result.(ListAllPacksResult)
|
||||
|
||||
for _, entry := range res.Entries {
|
||||
for _, entry := range res.Entries() {
|
||||
pb := PackedBlob{
|
||||
ID: entry.ID,
|
||||
Type: entry.Type,
|
||||
Length: entry.Length,
|
||||
Offset: entry.Offset,
|
||||
PackID: res.PackID,
|
||||
PackID: res.PackID(),
|
||||
}
|
||||
idx.Store(pb)
|
||||
}
|
||||
|
@ -16,9 +16,24 @@ type Lister interface {
|
||||
|
||||
// ListAllPacksResult is returned in the channel from LoadBlobsFromAllPacks.
|
||||
type ListAllPacksResult struct {
|
||||
PackID backend.ID
|
||||
Size int64
|
||||
Entries []pack.Blob
|
||||
packID backend.ID
|
||||
size int64
|
||||
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.
|
||||
@ -28,9 +43,9 @@ func ListAllPacks(repo Lister, ch chan<- worker.Job, done <-chan struct{}) {
|
||||
entries, size, err := repo.ListPack(packID)
|
||||
|
||||
return ListAllPacksResult{
|
||||
PackID: packID,
|
||||
Size: size,
|
||||
Entries: entries,
|
||||
packID: packID,
|
||||
size: size,
|
||||
entries: entries,
|
||||
}, err
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user