Make BlobHandle substruct of Blob

This commit is contained in:
Alexander Weiss 2020-11-05 21:52:34 +01:00 committed by Alexander Neumann
parent c844580e0f
commit 92bd448691
8 changed files with 67 additions and 43 deletions

View File

@ -231,7 +231,7 @@ func prune(opts PruneOptions, gopts GlobalOptions, repo restic.Repository, usedB
// iterate over all blobs in index to find out which blobs are duplicates // iterate over all blobs in index to find out which blobs are duplicates
for blob := range repo.Index().Each(ctx) { for blob := range repo.Index().Each(ctx) {
bh := blob.Handle() bh := blob.BlobHandle
size := uint64(blob.Length) size := uint64(blob.Length)
switch { switch {
case usedBlobs.Has(bh): // used blob, move to keepBlobs case usedBlobs.Has(bh): // used blob, move to keepBlobs
@ -280,7 +280,7 @@ func prune(opts PruneOptions, gopts GlobalOptions, repo restic.Repository, usedB
ip.tpe = restic.InvalidBlob ip.tpe = restic.InvalidBlob
} }
bh := blob.Handle() bh := blob.BlobHandle
size := uint64(blob.Length) size := uint64(blob.Length)
switch { switch {
case duplicateBlobs.Has(bh): // duplicate blob case duplicateBlobs.Has(bh): // duplicate blob

View File

@ -35,7 +35,7 @@ func (p *Packer) Add(t restic.BlobType, id restic.ID, data []byte) (int, error)
p.m.Lock() p.m.Lock()
defer p.m.Unlock() defer p.m.Unlock()
c := restic.Blob{Type: t, ID: id} c := restic.Blob{BlobHandle: restic.BlobHandle{Type: t, ID: id}}
n, err := p.wr.Write(data) n, err := p.wr.Write(data)
c.Length = uint(n) c.Length = uint(n)

View File

@ -165,8 +165,8 @@ func (idx *Index) StorePack(id restic.ID, blobs []restic.Blob) {
func (idx *Index) toPackedBlob(e *indexEntry, typ restic.BlobType) restic.PackedBlob { func (idx *Index) toPackedBlob(e *indexEntry, typ restic.BlobType) restic.PackedBlob {
return restic.PackedBlob{ return restic.PackedBlob{
Blob: restic.Blob{ Blob: restic.Blob{
ID: e.id, BlobHandle: restic.BlobHandle{ID: e.id,
Type: typ, Type: typ},
Length: uint(e.length), Length: uint(e.length),
Offset: uint(e.offset), Offset: uint(e.offset),
}, },
@ -596,8 +596,8 @@ func DecodeIndex(buf []byte, id restic.ID) (idx *Index, oldFormat bool, err erro
for _, blob := range pack.Blobs { for _, blob := range pack.Blobs {
idx.store(packID, restic.Blob{ idx.store(packID, restic.Blob{
Type: blob.Type, BlobHandle: restic.BlobHandle{Type: blob.Type,
ID: blob.ID, ID: blob.ID},
Offset: blob.Offset, Offset: blob.Offset,
Length: blob.Length, Length: blob.Length,
}) })
@ -640,8 +640,9 @@ func decodeOldIndex(buf []byte) (idx *Index, err error) {
for _, blob := range pack.Blobs { for _, blob := range pack.Blobs {
idx.store(packID, restic.Blob{ idx.store(packID, restic.Blob{
Type: blob.Type, BlobHandle: restic.BlobHandle{
ID: blob.ID, Type: blob.Type,
ID: blob.ID},
Offset: blob.Offset, Offset: blob.Offset,
Length: blob.Length, Length: blob.Length,
}) })

View File

@ -32,8 +32,10 @@ func TestIndexSerialize(t *testing.T) {
length := uint(i*100 + j) length := uint(i*100 + j)
idx.Store(restic.PackedBlob{ idx.Store(restic.PackedBlob{
Blob: restic.Blob{ Blob: restic.Blob{
Type: restic.DataBlob, BlobHandle: restic.BlobHandle{
ID: id, Type: restic.DataBlob,
ID: id,
},
Offset: pos, Offset: pos,
Length: length, Length: length,
}, },
@ -105,8 +107,10 @@ func TestIndexSerialize(t *testing.T) {
length := uint(i*100 + j) length := uint(i*100 + j)
idx.Store(restic.PackedBlob{ idx.Store(restic.PackedBlob{
Blob: restic.Blob{ Blob: restic.Blob{
Type: restic.DataBlob, BlobHandle: restic.BlobHandle{
ID: id, Type: restic.DataBlob,
ID: id,
},
Offset: pos, Offset: pos,
Length: length, Length: length,
}, },
@ -178,8 +182,10 @@ func TestIndexSize(t *testing.T) {
length := uint(i*100 + j) length := uint(i*100 + j)
idx.Store(restic.PackedBlob{ idx.Store(restic.PackedBlob{
Blob: restic.Blob{ Blob: restic.Blob{
Type: restic.DataBlob, BlobHandle: restic.BlobHandle{
ID: id, Type: restic.DataBlob,
ID: id,
},
Offset: pos, Offset: pos,
Length: length, Length: length,
}, },
@ -395,8 +401,10 @@ func TestIndexPacks(t *testing.T) {
packID := restic.NewRandomID() packID := restic.NewRandomID()
idx.Store(restic.PackedBlob{ idx.Store(restic.PackedBlob{
Blob: restic.Blob{ Blob: restic.Blob{
Type: restic.DataBlob, BlobHandle: restic.BlobHandle{
ID: restic.NewRandomID(), Type: restic.DataBlob,
ID: restic.NewRandomID(),
},
Offset: 0, Offset: 0,
Length: 23, Length: 23,
}, },
@ -431,8 +439,10 @@ func createRandomIndex(rng *rand.Rand, packfiles int) (idx *repository.Index, lo
size := 2000 + rng.Intn(4*1024*1024) size := 2000 + rng.Intn(4*1024*1024)
id := NewRandomTestID(rng) id := NewRandomTestID(rng)
blobs = append(blobs, restic.Blob{ blobs = append(blobs, restic.Blob{
Type: restic.DataBlob, BlobHandle: restic.BlobHandle{
ID: id, Type: restic.DataBlob,
ID: id,
},
Length: uint(size), Length: uint(size),
Offset: uint(offset), Offset: uint(offset),
}) })
@ -511,8 +521,10 @@ func TestIndexHas(t *testing.T) {
length := uint(i*100 + j) length := uint(i*100 + j)
idx.Store(restic.PackedBlob{ idx.Store(restic.PackedBlob{
Blob: restic.Blob{ Blob: restic.Blob{
Type: restic.DataBlob, BlobHandle: restic.BlobHandle{
ID: id, Type: restic.DataBlob,
ID: id,
},
Offset: pos, Offset: pos,
Length: length, Length: length,
}, },

View File

@ -21,8 +21,10 @@ func TestMasterIndex(t *testing.T) {
blob1 := restic.PackedBlob{ blob1 := restic.PackedBlob{
PackID: restic.NewRandomID(), PackID: restic.NewRandomID(),
Blob: restic.Blob{ Blob: restic.Blob{
Type: restic.DataBlob, BlobHandle: restic.BlobHandle{
ID: idInIdx1, Type: restic.DataBlob,
ID: idInIdx1,
},
Length: uint(restic.CiphertextLength(10)), Length: uint(restic.CiphertextLength(10)),
Offset: 0, Offset: 0,
}, },
@ -31,8 +33,10 @@ func TestMasterIndex(t *testing.T) {
blob2 := restic.PackedBlob{ blob2 := restic.PackedBlob{
PackID: restic.NewRandomID(), PackID: restic.NewRandomID(),
Blob: restic.Blob{ Blob: restic.Blob{
Type: restic.DataBlob, BlobHandle: restic.BlobHandle{
ID: idInIdx2, Type: restic.DataBlob,
ID: idInIdx2,
},
Length: uint(restic.CiphertextLength(100)), Length: uint(restic.CiphertextLength(100)),
Offset: 10, Offset: 10,
}, },
@ -41,8 +45,10 @@ func TestMasterIndex(t *testing.T) {
blob12a := restic.PackedBlob{ blob12a := restic.PackedBlob{
PackID: restic.NewRandomID(), PackID: restic.NewRandomID(),
Blob: restic.Blob{ Blob: restic.Blob{
Type: restic.TreeBlob, BlobHandle: restic.BlobHandle{
ID: idInIdx12, Type: restic.TreeBlob,
ID: idInIdx12,
},
Length: uint(restic.CiphertextLength(123)), Length: uint(restic.CiphertextLength(123)),
Offset: 110, Offset: 110,
}, },
@ -51,8 +57,10 @@ func TestMasterIndex(t *testing.T) {
blob12b := restic.PackedBlob{ blob12b := restic.PackedBlob{
PackID: restic.NewRandomID(), PackID: restic.NewRandomID(),
Blob: restic.Blob{ Blob: restic.Blob{
Type: restic.TreeBlob, BlobHandle: restic.BlobHandle{
ID: idInIdx12, Type: restic.TreeBlob,
ID: idInIdx12,
},
Length: uint(restic.CiphertextLength(123)), Length: uint(restic.CiphertextLength(123)),
Offset: 50, Offset: 50,
}, },
@ -139,8 +147,10 @@ func TestMasterMergeFinalIndexes(t *testing.T) {
blob1 := restic.PackedBlob{ blob1 := restic.PackedBlob{
PackID: restic.NewRandomID(), PackID: restic.NewRandomID(),
Blob: restic.Blob{ Blob: restic.Blob{
Type: restic.DataBlob, BlobHandle: restic.BlobHandle{
ID: idInIdx1, Type: restic.DataBlob,
ID: idInIdx1,
},
Length: 10, Length: 10,
Offset: 0, Offset: 0,
}, },
@ -149,8 +159,10 @@ func TestMasterMergeFinalIndexes(t *testing.T) {
blob2 := restic.PackedBlob{ blob2 := restic.PackedBlob{
PackID: restic.NewRandomID(), PackID: restic.NewRandomID(),
Blob: restic.Blob{ Blob: restic.Blob{
Type: restic.DataBlob, BlobHandle: restic.BlobHandle{
ID: idInIdx2, Type: restic.DataBlob,
ID: idInIdx2,
},
Length: 100, Length: 100,
Offset: 10, Offset: 10,
}, },

View File

@ -296,9 +296,11 @@ func BenchmarkLoadIndex(b *testing.B) {
for i := 0; i < 5000; i++ { for i := 0; i < 5000; i++ {
idx.Store(restic.PackedBlob{ idx.Store(restic.PackedBlob{
Blob: restic.Blob{ Blob: restic.Blob{
Type: restic.DataBlob, BlobHandle: restic.BlobHandle{
Type: restic.DataBlob,
ID: restic.NewRandomID(),
},
Length: 1234, Length: 1234,
ID: restic.NewRandomID(),
Offset: 1235, Offset: 1235,
}, },
PackID: restic.NewRandomID(), PackID: restic.NewRandomID(),

View File

@ -8,9 +8,8 @@ import (
// Blob is one part of a file or a tree. // Blob is one part of a file or a tree.
type Blob struct { type Blob struct {
Type BlobType BlobHandle
Length uint Length uint
ID ID
Offset uint Offset uint
} }
@ -19,10 +18,6 @@ func (b Blob) String() string {
b.Type, b.ID.Str(), b.Offset, b.Length) b.Type, b.ID.Str(), b.Offset, b.Length)
} }
func (b Blob) Handle() BlobHandle {
return BlobHandle{ID: b.ID, Type: b.Type}
}
// PackedBlob is a blob stored within a file. // PackedBlob is a blob stored within a file.
type PackedBlob struct { type PackedBlob struct {
Blob Blob

View File

@ -92,8 +92,10 @@ func newTestRepo(content []TestFile) *TestRepo {
if _, found := pack.blobs[blobID]; !found { if _, found := pack.blobs[blobID]; !found {
blobData := seal([]byte(blob.data)) blobData := seal([]byte(blob.data))
pack.blobs[blobID] = restic.Blob{ pack.blobs[blobID] = restic.Blob{
Type: restic.DataBlob, BlobHandle: restic.BlobHandle{
ID: blobID, Type: restic.DataBlob,
ID: blobID,
},
Length: uint(len(blobData)), Length: uint(len(blobData)),
Offset: uint(len(pack.data)), Offset: uint(len(pack.data)),
} }