mirror of https://github.com/restic/restic.git
Merge pull request #2713 from greatroar/unused
Move Index.FindBlob to tests
This commit is contained in:
commit
f54db5d796
|
@ -312,36 +312,6 @@ func (idx *Index) PacksForBlobs(blobs restic.BlobSet) (packs restic.IDSet) {
|
||||||
return packs
|
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
|
const maxEntries = 3000
|
||||||
|
|
||||||
// Saver saves structures as JSON.
|
// Saver saves structures as JSON.
|
||||||
|
|
|
@ -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) {
|
func TestIndexAddRemovePack(t *testing.T) {
|
||||||
repo, cleanup := createFilledRepo(t, 3, 0)
|
repo, cleanup := createFilledRepo(t, 3, 0)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
@ -393,8 +415,8 @@ func TestIndexAddRemovePack(t *testing.T) {
|
||||||
|
|
||||||
for _, blob := range blobs {
|
for _, blob := range blobs {
|
||||||
h := restic.BlobHandle{ID: blob.ID, Type: blob.Type}
|
h := restic.BlobHandle{ID: blob.ID, Type: blob.Type}
|
||||||
_, err := idx.FindBlob(h)
|
locs := idx.findBlob(h)
|
||||||
if err == nil {
|
if len(locs) != 0 {
|
||||||
t.Errorf("removed blob %v found in index", h)
|
t.Errorf("removed blob %v found in index", h)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -447,9 +469,9 @@ func TestIndexLoadDocReference(t *testing.T) {
|
||||||
idx := loadIndex(t, repo)
|
idx := loadIndex(t, repo)
|
||||||
|
|
||||||
blobID := restic.TestParseID("d3dc577b4ffd38cc4b32122cabf8655a0223ed22edfd93b353dc0c3f2b0fdf66")
|
blobID := restic.TestParseID("d3dc577b4ffd38cc4b32122cabf8655a0223ed22edfd93b353dc0c3f2b0fdf66")
|
||||||
locs, err := idx.FindBlob(restic.BlobHandle{ID: blobID, Type: restic.DataBlob})
|
locs := idx.findBlob(restic.BlobHandle{ID: blobID, Type: restic.DataBlob})
|
||||||
if err != nil {
|
if len(locs) == 0 {
|
||||||
t.Errorf("FindBlob() returned error %v", err)
|
t.Error("blob not found in index")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(locs) != 1 {
|
if len(locs) != 1 {
|
||||||
|
|
Loading…
Reference in New Issue