Skip to content

Commit

Permalink
add custom prefix support (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
harshavardhana authored Apr 15, 2021
1 parent 0685a08 commit 2b2ea0d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
4 changes: 4 additions & 0 deletions cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ var ioFlags = []cli.Flag{
Name: "noprefix",
Usage: "Do not use separate prefix for each thread",
},
cli.StringFlag{
Name: "prefix",
Usage: "Use a custom prefix for each thread",
},
cli.BoolFlag{
Name: "disable-multipart",
Usage: "disable multipart uploads",
Expand Down
2 changes: 2 additions & 0 deletions cli/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func newGenSourceCSV(ctx *cli.Context) func() generator.Source {
size, err := toSize(ctx.String("obj.size"))
fatalIf(probe.NewError(err), "Invalid obj.size specified")
src, err := generator.NewFn(g.Apply(),
generator.WithCustomPrefix(ctx.String("prefix")),
generator.WithPrefixSize(prefixSize),
generator.WithSize(int64(size)),
generator.WithRandomSize(ctx.Bool("obj.randsize")),
Expand Down Expand Up @@ -79,6 +80,7 @@ func newGenSource(ctx *cli.Context) func() generator.Source {
size, err := toSize(ctx.String("obj.size"))
fatalIf(probe.NewError(err), "Invalid obj.size specified")
src, err := generator.NewFn(g.Apply(),
generator.WithCustomPrefix(ctx.String("prefix")),
generator.WithPrefixSize(prefixSize),
generator.WithSize(int64(size)),
generator.WithRandomSize(ctx.Bool("obj.randsize")),
Expand Down
5 changes: 3 additions & 2 deletions pkg/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io"
"math"
"math/rand"
"path"
"runtime"
)

Expand Down Expand Up @@ -94,13 +95,13 @@ func MergeObjectPrefixes(o []Objects) []string {

func (o *Object) setPrefix(opts Options) {
if opts.randomPrefix <= 0 {
o.Prefix = ""
o.Prefix = opts.customPrefix
return
}
b := make([]byte, opts.randomPrefix)
rng := rand.New(rand.NewSource(int64(rand.Uint64())))
randASCIIBytes(b, rng)
o.Prefix = string(b)
o.Prefix = path.Join(opts.customPrefix, string(b))
}

func (o *Object) setName(s string) {
Expand Down
9 changes: 9 additions & 0 deletions pkg/generator/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Options struct {
src func(o Options) (Source, error)
totalSize int64
randSize bool
customPrefix string
csv CsvOpts
random RandomOpts
randomPrefix int
Expand Down Expand Up @@ -83,6 +84,14 @@ func WithRandomSize(b bool) Option {
}
}

// WithCustomPrefix adds custom prefix under bucket where all warp content is created.
func WithCustomPrefix(prefix string) Option {
return func(o *Options) error {
o.customPrefix = prefix
return nil
}
}

// WithPrefixSize sets prefix size.
func WithPrefixSize(n int) Option {
return func(o *Options) error {
Expand Down

0 comments on commit 2b2ea0d

Please sign in to comment.