2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-22 10:58:55 +00:00

Move Index.FindBlob to tests

This commit is contained in:
greatroar 2020-04-29 10:57:01 +02:00
parent 070d43e290
commit 9f7cd69f13
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 {