diff --git a/cmd/restic/cmd_rebuild_index.go b/cmd/restic/cmd_rebuild_index.go index aceceecc2..27f484963 100644 --- a/cmd/restic/cmd_rebuild_index.go +++ b/cmd/restic/cmd_rebuild_index.go @@ -22,6 +22,22 @@ func init() { } } +func (cmd CmdRebuildIndex) storeIndex(index *repository.Index) (*repository.Index, error) { + debug.Log("RebuildIndex.RebuildIndex", "saving index") + + cmd.global.Printf(" saving new index\n") + id, err := repository.SaveIndex(cmd.repo, index) + if err != nil { + debug.Log("RebuildIndex.RebuildIndex", "error saving index: %v", err) + return nil, err + } + + debug.Log("RebuildIndex.RebuildIndex", "index saved as %v", id.Str()) + index = repository.NewIndex() + + return index, nil +} + func (cmd CmdRebuildIndex) RebuildIndex() error { debug.Log("RebuildIndex.RebuildIndex", "start") @@ -33,11 +49,16 @@ func (cmd CmdRebuildIndex) RebuildIndex() error { indexIDs.Insert(id) } + cmd.global.Printf("rebuilding index from %d indexes\n", len(indexIDs)) + debug.Log("RebuildIndex.RebuildIndex", "found %v indexes", len(indexIDs)) - var combinedIndex *repository.Index + combinedIndex := repository.NewIndex() + i := 0 for indexID := range indexIDs { + cmd.global.Printf(" loading index %v\n", i) + debug.Log("RebuildIndex.RebuildIndex", "load index %v", indexID.Str()) idx, err := repository.LoadIndex(cmd.repo, indexID.String()) if err != nil { @@ -46,10 +67,6 @@ func (cmd CmdRebuildIndex) RebuildIndex() error { debug.Log("RebuildIndex.RebuildIndex", "adding blobs from index %v", indexID.Str()) - if combinedIndex == nil { - combinedIndex = repository.NewIndex() - } - for packedBlob := range idx.Each(done) { combinedIndex.Store(packedBlob.Type, packedBlob.ID, packedBlob.PackID, packedBlob.Offset, packedBlob.Length) } @@ -57,31 +74,28 @@ func (cmd CmdRebuildIndex) RebuildIndex() error { combinedIndex.AddToSupersedes(indexID) if repository.IndexFull(combinedIndex) { - debug.Log("RebuildIndex.RebuildIndex", "saving full index") - - id, err := repository.SaveIndex(cmd.repo, combinedIndex) + combinedIndex, err = cmd.storeIndex(combinedIndex) if err != nil { - debug.Log("RebuildIndex.RebuildIndex", "error saving index: %v", err) return err } + } - debug.Log("RebuildIndex.RebuildIndex", "index saved as %v", id.Str()) - combinedIndex = nil + i++ + } + + var err error + if combinedIndex.Length() > 0 { + combinedIndex, err = cmd.storeIndex(combinedIndex) + if err != nil { + return err } } - id, err := repository.SaveIndex(cmd.repo, combinedIndex) - if err != nil { - debug.Log("RebuildIndex.RebuildIndex", "error saving index: %v", err) - return err - } - - debug.Log("RebuildIndex.RebuildIndex", "last index saved as %v", id.Str()) - + cmd.global.Printf("removing %d old indexes\n", len(indexIDs)) for id := range indexIDs { debug.Log("RebuildIndex.RebuildIndex", "remove index %v", id.Str()) - err = cmd.repo.Backend().Remove(backend.Index, id.String()) + err := cmd.repo.Backend().Remove(backend.Index, id.String()) if err != nil { debug.Log("RebuildIndex.RebuildIndex", "error removing index %v: %v", id.Str(), err) return err diff --git a/repository/index.go b/repository/index.go index ec714c94c..25e4c5143 100644 --- a/repository/index.go +++ b/repository/index.go @@ -248,6 +248,15 @@ func (idx *Index) Count(t pack.BlobType) (n uint) { return } +// Length returns the number of entries in the Index. +func (idx *Index) Length() uint { + debug.Log("Index.Count", "counting blobs") + idx.m.Lock() + defer idx.m.Unlock() + + return uint(len(idx.pack)) +} + type packJSON struct { ID backend.ID `json:"id"` Blobs []blobJSON `json:"blobs"`