示例#1
0
文件: repacker.go 项目: marete/restic
// Repack runs the process of finding still used blobs in packs with unused
// blobs, extracts them and creates new packs with just the still-in-use blobs.
func (r *Repacker) Repack() error {
	debug.Log("Repacker.Repack", "searching packs for %v", r.unusedBlobs)

	unneededPacks, err := FindPacksForBlobs(r.repo, r.unusedBlobs)
	if err != nil {
		return err
	}

	debug.Log("Repacker.Repack", "found packs: %v", unneededPacks)

	blobs, err := FindBlobsForPacks(r.repo, unneededPacks)
	if err != nil {
		return err
	}

	debug.Log("Repacker.Repack", "found blobs: %v", blobs)

	for id := range r.unusedBlobs {
		debug.Log("Repacker.Repack", "remove unused blob %v", id.Str())
		blobs.Delete(id)
	}

	debug.Log("Repacker.Repack", "need to repack blobs: %v", blobs)

	err = RepackBlobs(r.repo, r.repo, blobs)
	if err != nil {
		return err
	}

	debug.Log("Repacker.Repack", "remove unneeded packs: %v", unneededPacks)
	for packID := range unneededPacks {
		err = r.repo.Backend().Remove(backend.Data, packID.String())
		if err != nil {
			return err
		}
	}

	debug.Log("Repacker.Repack", "rebuild index, unneeded packs: %v", unneededPacks)
	idx, err := r.repo.Index().RebuildIndex(unneededPacks)

	newIndexID, err := repository.SaveIndex(r.repo, idx)
	debug.Log("Repacker.Repack", "saved new index at %v, err %v", newIndexID.Str(), err)
	if err != nil {
		return err
	}

	debug.Log("Repacker.Repack", "remove old indexes: %v", idx.Supersedes())
	for _, id := range idx.Supersedes() {
		err = r.repo.Backend().Remove(backend.Index, id.String())
		if err != nil {
			debug.Log("Repacker.Repack", "error removing index %v: %v", id.Str(), err)
			return err
		}

		debug.Log("Repacker.Repack", "removed index %v", id.Str())
	}

	return nil
}
示例#2
0
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
}