mirror of
https://github.com/kopia/kopia.git
synced 2026-01-24 14:28:06 -05:00
* refactor(repository): ensure we always parse content.ID and object.ID
This changes the types to be incompatible with string to prevent direct
conversion to and from string.
This has the additional benefit of reducing number of memory allocations
and bytes for all IDs.
content.ID went from 2 allocations to 1:
typical case 32 characters + 16 bytes per-string overhead
worst-case 65 characters + 16 bytes per-string overhead
now: 34 bytes
object.ID went from 2 allocations to 1:
typical case 32 characters + 16 bytes per-string overhead
worst-case 65 characters + 16 bytes per-string overhead
now: 36 bytes
* move index.{ID,IDRange} methods to separate files
* replaced index.IDFromHash with content.IDFromHash externally
* minor tweaks and additional tests
* Update repo/content/index/id_test.go
Co-authored-by: Julio Lopez <1953782+julio-lopez@users.noreply.github.com>
* Update repo/content/index/id_test.go
Co-authored-by: Julio Lopez <1953782+julio-lopez@users.noreply.github.com>
* pr feedback
* post-merge fixes
* pr feedback
* pr feedback
* fixed subtle regression in sortedContents()
This was actually not producing invalid results because of how base36
works, just not sorting as efficiently as it could.
Co-authored-by: Julio Lopez <1953782+julio-lopez@users.noreply.github.com>
77 lines
2.5 KiB
Go
77 lines
2.5 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/kopia/kopia/repo"
|
|
"github.com/kopia/kopia/repo/blob"
|
|
"github.com/kopia/kopia/repo/content"
|
|
"github.com/kopia/kopia/repo/maintenance"
|
|
)
|
|
|
|
type commandContentRewrite struct {
|
|
contentRewriteIDs []string
|
|
contentRewriteParallelism int
|
|
contentRewriteShortPacks bool
|
|
contentRewriteFormatVersion int
|
|
contentRewritePackPrefix string
|
|
contentRewriteDryRun bool
|
|
contentRewriteSafety maintenance.SafetyParameters
|
|
|
|
contentRange contentRangeFlags
|
|
svc appServices
|
|
}
|
|
|
|
func (c *commandContentRewrite) setup(svc appServices, parent commandParent) {
|
|
cmd := parent.Command("rewrite", "Rewrite content using most recent format")
|
|
cmd.Arg("contentID", "Identifiers of contents to rewrite").StringsVar(&c.contentRewriteIDs)
|
|
cmd.Flag("parallelism", "Number of parallel workers").Default("16").IntVar(&c.contentRewriteParallelism)
|
|
|
|
cmd.Flag("short", "Rewrite contents from short packs").BoolVar(&c.contentRewriteShortPacks)
|
|
cmd.Flag("format-version", "Rewrite contents using the provided format version").Default("-1").IntVar(&c.contentRewriteFormatVersion)
|
|
cmd.Flag("pack-prefix", "Only rewrite contents from pack blobs with a given prefix").StringVar(&c.contentRewritePackPrefix)
|
|
cmd.Flag("dry-run", "Do not actually rewrite, only print what would happen").Short('n').BoolVar(&c.contentRewriteDryRun)
|
|
c.contentRange.setup(cmd)
|
|
safetyFlagVar(cmd, &c.contentRewriteSafety)
|
|
cmd.Action(svc.directRepositoryWriteAction(c.runContentRewriteCommand))
|
|
|
|
c.svc = svc
|
|
}
|
|
|
|
func (c *commandContentRewrite) runContentRewriteCommand(ctx context.Context, rep repo.DirectRepositoryWriter) error {
|
|
c.svc.advancedCommand(ctx)
|
|
|
|
contentIDs, err := toContentIDs(c.contentRewriteIDs)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// nolint:wrapcheck
|
|
return maintenance.RewriteContents(ctx, rep, &maintenance.RewriteContentsOptions{
|
|
ContentIDRange: c.contentRange.contentIDRange(),
|
|
ContentIDs: contentIDs,
|
|
FormatVersion: c.contentRewriteFormatVersion,
|
|
PackPrefix: blob.ID(c.contentRewritePackPrefix),
|
|
Parallel: c.contentRewriteParallelism,
|
|
ShortPacks: c.contentRewriteShortPacks,
|
|
DryRun: c.contentRewriteDryRun,
|
|
}, c.contentRewriteSafety)
|
|
}
|
|
|
|
func toContentIDs(s []string) ([]content.ID, error) {
|
|
var result []content.ID
|
|
|
|
for _, cidStr := range s {
|
|
cid, err := content.ParseID(cidStr)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "error parsing content ID")
|
|
}
|
|
|
|
result = append(result, cid)
|
|
}
|
|
|
|
return result, nil
|
|
}
|