From d92e2c5769bb2cb5eefef2dbb9d8761da977f5a4 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 8 Feb 2020 20:51:50 +0100 Subject: [PATCH] simplify index code --- internal/repository/index.go | 45 ++++++++++------------------- internal/repository/master_index.go | 13 --------- 2 files changed, 16 insertions(+), 42 deletions(-) diff --git a/internal/repository/index.go b/internal/repository/index.go index d6038e2e1..cf3f1e560 100644 --- a/internal/repository/index.go +++ b/internal/repository/index.go @@ -140,6 +140,19 @@ func (idx *Index) StorePack(id restic.ID, blobs []restic.Blob) { } } +// ListPack returns a list of blobs contained in a pack. +func indexEntryToPackedBlob(h restic.BlobHandle, entry indexEntry) restic.PackedBlob { + return restic.PackedBlob{ + Blob: restic.Blob{ + ID: h.ID, + Type: h.Type, + Length: entry.length, + Offset: entry.offset, + }, + PackID: entry.packID, + } +} + // Lookup queries the index for the blob ID and returns a restic.PackedBlob. func (idx *Index) Lookup(id restic.ID, tpe restic.BlobType) (blobs []restic.PackedBlob, found bool) { idx.m.Lock() @@ -151,17 +164,7 @@ func (idx *Index) Lookup(id restic.ID, tpe restic.BlobType) (blobs []restic.Pack blobs = make([]restic.PackedBlob, 0, len(packs)) for _, p := range packs { - blob := restic.PackedBlob{ - Blob: restic.Blob{ - Type: tpe, - Length: p.length, - ID: id, - Offset: p.offset, - }, - PackID: p.packID, - } - - blobs = append(blobs, blob) + blobs = append(blobs, indexEntryToPackedBlob(h, p)) } return blobs, true @@ -178,15 +181,7 @@ func (idx *Index) ListPack(id restic.ID) (list []restic.PackedBlob) { for h, packList := range idx.pack { for _, entry := range packList { if entry.packID == id { - list = append(list, restic.PackedBlob{ - Blob: restic.Blob{ - ID: h.ID, - Type: h.Type, - Length: entry.length, - Offset: entry.offset, - }, - PackID: entry.packID, - }) + list = append(list, indexEntryToPackedBlob(h, entry)) } } } @@ -254,15 +249,7 @@ func (idx *Index) Each(ctx context.Context) <-chan restic.PackedBlob { select { case <-ctx.Done(): return - case ch <- restic.PackedBlob{ - Blob: restic.Blob{ - ID: h.ID, - Type: h.Type, - Offset: blob.offset, - Length: blob.length, - }, - PackID: blob.packID, - }: + case ch <- indexEntryToPackedBlob(h, blob): } } } diff --git a/internal/repository/master_index.go b/internal/repository/master_index.go index 1caa42957..24762ffb2 100644 --- a/internal/repository/master_index.go +++ b/internal/repository/master_index.go @@ -132,19 +132,6 @@ func (mi *MasterIndex) Insert(idx *Index) { mi.idx = append(mi.idx, idx) } -// Remove deletes an index from the MasterIndex. -func (mi *MasterIndex) Remove(index *Index) { - mi.idxMutex.Lock() - defer mi.idxMutex.Unlock() - - for i, idx := range mi.idx { - if idx == index { - mi.idx = append(mi.idx[:i], mi.idx[i+1:]...) - return - } - } -} - // Store remembers the id and pack in the index. func (mi *MasterIndex) StorePack(id restic.ID, blobs []restic.Blob) { mi.idxMutex.Lock()