Files
kopia/cli/command_index_optimize.go
Jarek Kowalski f8be8f6a56 refactor(repository): extract parts repo/content into packages (#2651)
- repolog package
- blobcrypto package
- indexblob package

Minor cleanups:

- removed dead code
- introduced New*() methods for object construction
2022-12-17 16:19:12 +00:00

52 lines
1.6 KiB
Go

package cli
import (
"context"
"time"
"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/repo/content/indexblob"
)
type commandIndexOptimize struct {
optimizeMaxSmallBlobs int
optimizeDropDeletedOlderThan time.Duration
optimizeDropContents []string
optimizeAllIndexes bool
svc appServices
}
func (c *commandIndexOptimize) setup(svc appServices, parent commandParent) {
cmd := parent.Command("optimize", "Optimize indexes blobs.")
cmd.Flag("max-small-blobs", "Maximum number of small index blobs that can be left after compaction.").Default("1").IntVar(&c.optimizeMaxSmallBlobs)
cmd.Flag("drop-deleted-older-than", "Drop deleted contents above given age").DurationVar(&c.optimizeDropDeletedOlderThan)
cmd.Flag("drop-contents", "Drop contents with given IDs").StringsVar(&c.optimizeDropContents)
cmd.Flag("all", "Optimize all indexes, even those above maximum size.").BoolVar(&c.optimizeAllIndexes)
cmd.Action(svc.directRepositoryWriteAction(c.runOptimizeCommand))
c.svc = svc
}
func (c *commandIndexOptimize) runOptimizeCommand(ctx context.Context, rep repo.DirectRepositoryWriter) error {
c.svc.advancedCommand(ctx)
contentIDs, err := toContentIDs(c.optimizeDropContents)
if err != nil {
return err
}
opt := indexblob.CompactOptions{
MaxSmallBlobs: c.optimizeMaxSmallBlobs,
AllIndexes: c.optimizeAllIndexes,
DropContents: contentIDs,
}
if age := c.optimizeDropDeletedOlderThan; age > 0 {
opt.DropDeletedBefore = rep.Time().Add(-age)
}
//nolint:wrapcheck
return rep.ContentManager().CompactIndexes(ctx, opt)
}