-
Notifications
You must be signed in to change notification settings - Fork 104
/
options.go
130 lines (117 loc) · 3.22 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Warp (C) 2019-2020 MinIO, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package generator
import (
"errors"
"math/rand"
)
// Options provides options.
// Use WithXXX functions to set them.
type Options struct {
src func(o Options) (Source, error)
customPrefix string
random RandomOpts
csv CsvOpts
minSize int64
totalSize int64
randomPrefix int
randSize bool
}
// OptionApplier allows to abstract generator options.
type OptionApplier interface {
Apply() Option
}
// getSize will return a size for an object.
func (o Options) getSize(rng *rand.Rand) int64 {
if !o.randSize {
return o.totalSize
}
return GetExpRandSize(rng, o.minSize, o.totalSize)
}
func defaultOptions() Options {
o := Options{
src: newRandom,
totalSize: 1 << 20,
csv: csvOptsDefaults(),
random: randomOptsDefaults(),
randomPrefix: 0,
}
return o
}
// WithMinMaxSize sets the min and max size of the generated data.
func WithMinMaxSize(min, max int64) Option {
return func(o *Options) error {
if min <= 0 {
return errors.New("WithSize: minSize must be >= 0")
}
if max < 0 {
return errors.New("WithSize: maxSize must be > 0")
}
if min > max {
return errors.New("WithSize: minSize must be < maxSize")
}
if o.randSize && max < 256 {
return errors.New("WithSize: random sized objects should be at least 256 bytes")
}
o.totalSize = max
o.minSize = min
return nil
}
}
// WithSize sets the size of the generated data.
func WithSize(n int64) Option {
return func(o *Options) error {
if n <= 0 {
return errors.New("WithSize: size must be > 0")
}
if o.randSize && o.totalSize < 256 {
return errors.New("WithSize: random sized objects should be at least 256 bytes")
}
o.totalSize = n
return nil
}
}
// WithRandomSize will randomize the size from 1 byte to the total size set.
func WithRandomSize(b bool) Option {
return func(o *Options) error {
if o.totalSize > 0 && o.totalSize < 256 {
return errors.New("WithRandomSize: Random sized objects should be at least 256 bytes")
}
o.randSize = b
return nil
}
}
// 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 {
if n < 0 {
return errors.New("WithPrefixSize: size must be >= 0 and <= 16")
}
if n > 16 {
return errors.New("WithPrefixSize: size must be >= 0 and <= 16")
}
o.randomPrefix = n
return nil
}
}