Files
kopia/cli/command_index_optimize.go
Jarek Kowalski fcd507a56d Refactored most of the CLI tests to run in-process as opposed to using sub-processes (#1059)
* cli: fixed remaining testability indirections for output and logging

* cli: added cli.RunSubcommand() which is used in testing to execute a subcommand in the same process

* tests: refactored most e2e tests to invoke kopia subcommands in-process

* Makefile: enable code coverage for cli/ and internal/

* testing: pass 'testing' tag to unit tests which uses much faster (insecure) password hashing scheme

* Makefile: push coverage from PRs again

* tests: disable buffer management to reduce memory usage on ARM

* cli: fixed misaligned atomic field on ARMHF

also temporarily fixed statup-time benign race condition when setting
default on the timeZone variable, which is the last global variable.
2021-05-11 22:26:28 -07:00

46 lines
1.5 KiB
Go

package cli
import (
"context"
"time"
"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/repo/content"
)
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)
opt := content.CompactOptions{
MaxSmallBlobs: c.optimizeMaxSmallBlobs,
AllIndexes: c.optimizeAllIndexes,
DropContents: toContentIDs(c.optimizeDropContents),
}
if age := c.optimizeDropDeletedOlderThan; age > 0 {
opt.DropDeletedBefore = rep.Time().Add(-age)
}
return rep.ContentManager().CompactIndexes(ctx, opt)
}