2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-05 12:57:53 +00:00

Merge pull request #2713 from greatroar/unused

Move Index.FindBlob to tests
This commit is contained in:
MichaelEischer 2020-05-01 20:41:59 +02:00 committed by GitHub
commit f54db5d796
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 35 deletions

View File

@ -312,36 +312,6 @@ func (idx *Index) PacksForBlobs(blobs restic.BlobSet) (packs restic.IDSet) {
return packs
}
// Location describes the location of a blob in a pack.
type Location struct {
PackID restic.ID
restic.Blob
}
// ErrBlobNotFound is return by FindBlob when the blob could not be found in
// the index.
var ErrBlobNotFound = errors.New("blob not found in index")
// FindBlob returns a list of packs and positions the blob can be found in.
func (idx *Index) FindBlob(h restic.BlobHandle) (result []Location, err error) {
for id, p := range idx.Packs {
for _, entry := range p.Entries {
if entry.ID.Equal(h.ID) && entry.Type == h.Type {
result = append(result, Location{
PackID: id,
Blob: entry,
})
}
}
}
if len(result) == 0 {
return nil, ErrBlobNotFound
}
return result, nil
}
const maxEntries = 3000
// Saver saves structures as JSON.

View File

@ -363,6 +363,28 @@ func TestIndexSave(t *testing.T) {
}
}
// Location describes the location of a blob in a pack.
type location struct {
PackID restic.ID
restic.Blob
}
// FindBlob returns a list of packs and positions the blob can be found in.
func (idx *Index) findBlob(h restic.BlobHandle) (result []location) {
for id, p := range idx.Packs {
for _, entry := range p.Entries {
if entry.ID.Equal(h.ID) && entry.Type == h.Type {
result = append(result, location{
PackID: id,
Blob: entry,
})
}
}
}
return result
}
func TestIndexAddRemovePack(t *testing.T) {
repo, cleanup := createFilledRepo(t, 3, 0)
defer cleanup()
@ -393,8 +415,8 @@ func TestIndexAddRemovePack(t *testing.T) {
for _, blob := range blobs {
h := restic.BlobHandle{ID: blob.ID, Type: blob.Type}
_, err := idx.FindBlob(h)
if err == nil {
locs := idx.findBlob(h)
if len(locs) != 0 {
t.Errorf("removed blob %v found in index", h)
}
}
@ -447,9 +469,9 @@ func TestIndexLoadDocReference(t *testing.T) {
idx := loadIndex(t, repo)
blobID := restic.TestParseID("d3dc577b4ffd38cc4b32122cabf8655a0223ed22edfd93b353dc0c3f2b0fdf66")
locs, err := idx.FindBlob(restic.BlobHandle{ID: blobID, Type: restic.DataBlob})
if err != nil {
t.Errorf("FindBlob() returned error %v", err)
locs := idx.findBlob(restic.BlobHandle{ID: blobID, Type: restic.DataBlob})
if len(locs) == 0 {
t.Error("blob not found in index")
}
if len(locs) != 1 {