From 1f263a7683cb56e690b52ab8483c6ae183327e5d Mon Sep 17 00:00:00 2001
From: Alexander Neumann <alexander@bumpern.de>
Date: Sun, 14 Aug 2016 16:11:59 +0200
Subject: [PATCH] Decouple index/ and repository/

---
 src/restic/index/index.go              | 21 +++++++++++++-------
 src/restic/repository/index_rebuild.go |  4 ++--
 src/restic/repository/list.go          | 27 ++++++++++++++++++++------
 3 files changed, 37 insertions(+), 15 deletions(-)

diff --git a/src/restic/index/index.go b/src/restic/index/index.go
index bbd5c981..c0b404a3 100644
--- a/src/restic/index/index.go
+++ b/src/restic/index/index.go
@@ -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{})
diff --git a/src/restic/repository/index_rebuild.go b/src/restic/repository/index_rebuild.go
index 36e42ddf..fa33c5c8 100644
--- a/src/restic/repository/index_rebuild.go
+++ b/src/restic/repository/index_rebuild.go
@@ -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)
 		}
diff --git a/src/restic/repository/list.go b/src/restic/repository/list.go
index 9d6f44e4..a3c0c5d9 100644
--- a/src/restic/repository/list.go
+++ b/src/restic/repository/list.go
@@ -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
 	}