Example #1
0
// 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
}
Example #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
}
func (cmd CmdRebuildIndex) rebuildIndex() error {
	debug.Log("RebuildIndex.RebuildIndex", "start rebuilding index")

	packs := loadBlobsFromPacks(cmd.repo)
	cmd.global.Verbosef("loaded blobs from %d packs\n", len(packs))

	idx := repository.NewIndex()
	for packID, entries := range packs {
		for _, entry := range entries {
			pb := repository.PackedBlob{
				ID:     entry.ID,
				Type:   entry.Type,
				Length: entry.Length,
				Offset: entry.Offset,
				PackID: packID,
			}
			idx.Store(pb)
		}
	}

	oldIndexes := listIndexIDs(cmd.repo)
	idx.AddToSupersedes(oldIndexes...)
	cmd.global.Printf("  saving new index\n")
	id, err := repository.SaveIndex(cmd.repo, idx)
	if err != nil {
		debug.Log("RebuildIndex.RebuildIndex", "error saving index: %v", err)
		return err
	}
	debug.Log("RebuildIndex.RebuildIndex", "new index saved as %v", id.Str())

	for _, indexID := range oldIndexes {
		err := cmd.repo.Backend().Remove(backend.Index, indexID.String())
		if err != nil {
			cmd.global.Warnf("unable to remove index %v: %v\n", indexID.Str(), err)
		}
	}

	return nil
}