Skip to content

Commit

Permalink
chore(superflag): remove special handling of the flags #1689
Browse files Browse the repository at this point in the history
  • Loading branch information
NamanJain8 authored Apr 5, 2021
1 parent 9b176f1 commit d918b99
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 67 deletions.
69 changes: 6 additions & 63 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,38 +256,6 @@ func parseCompression(cStr string) (options.CompressionType, int, error) {
return 0, 0, errors.Errorf("ERROR: compression type (%s) invalid", cType)
}

// getCachePercentages returns the slice of cache percentages given the "," (comma) separated
// cache percentages(integers) string and expected number of caches.
func getCachePercentages(cpString string, numExpected int) ([]int64, error) {
cp := strings.Split(cpString, ",")
// Sanity checks
if len(cp) != numExpected {
return nil, errors.Errorf("ERROR: expected %d cache percentages, got %d",
numExpected, len(cp))
}

var cachePercent []int64
percentSum := 0
for _, percent := range cp {
x, err := strconv.Atoi(percent)
if err != nil {
return nil, errors.Errorf("ERROR: unable to parse cache percentage(%s)", percent)
}
if x < 0 {
return nil, errors.Errorf("ERROR: cache percentage(%s) cannot be negative", percent)
}
cachePercent = append(cachePercent, int64(x))
percentSum += x
}

if percentSum != 100 {
return nil, errors.Errorf("ERROR: cache percentages (%s) does not sum up to 100",
strings.Join(cp, "+"))
}

return cachePercent, nil
}

// generateSuperFlag generates an identical SuperFlag string from the provided Options.
func generateSuperFlag(options Options) string {
superflag := ""
Expand Down Expand Up @@ -330,27 +298,14 @@ func generateSuperFlag(options Options) string {
// will not fill it with default values. FromSuperFlag only writes to the fields
// present within the superflag string (case insensitive).
//
// Special Handling: compression, cache-mb, cache-percentage sub-flags.
// Valid values for special flags:
// compression: valid options are {none,snappy,zstd:<level>}
// cache-mb: uint
// cache-percentage: 2 comma-separated values summing upto 100. Format: blockCache,indexCache
// goroutines: alias for numgoroutines.
// Example: compression=zstd:3; cache-mb=2048; cache-percentage=70,30; goroutines=8;
// It specially handles compression subflag.
// Valid options are {none,snappy,zstd:<level>}
// Example: compression=zstd:3;
// Unsupported: Options.Logger, Options.EncryptionKey
func (opt Options) FromSuperFlag(superflag string) Options {
var specialFlags = map[string]struct{}{
"compression": {},
"goroutines": {},
"cache-mb": {},
"cache-percentage": {},
}

// currentOptions act as a default value for the options superflag.
currentOptions := generateSuperFlag(opt)
for flag := range specialFlags {
currentOptions += fmt.Sprintf("%s=; ", flag)
}
currentOptions += "compression=;"

flags := z.NewSuperFlag(superflag).MergeAndCheckDefault(currentOptions)
v := reflect.ValueOf(&opt).Elem()
Expand All @@ -361,8 +316,8 @@ func (opt Options) FromSuperFlag(superflag string) Options {
// z.SuperFlag stores keys as lowercase, keep everything case
// insensitive
name := strings.ToLower(optionsStruct.Field(i).Name)
if _, ok := specialFlags[name]; ok {
// We will specially handle these flags later. Skip them here.
if name == "compression" {
// We will specially handle this later. Skip it here.
continue
}
kind := v.Field(i).Kind()
Expand Down Expand Up @@ -396,18 +351,6 @@ func (opt Options) FromSuperFlag(superflag string) Options {
opt.Compression = ctype
}
}
if inputFlag.Has("cache-mb") {
totalCache := flags.GetInt64("cache-mb")
y.AssertTruef(totalCache >= 0, "ERROR: Cache size must be non-negative")
cachePercentage := flags.GetString("cache-percentage")
cachePercent, err := getCachePercentages(cachePercentage, 2)
y.Check(err)
opt.BlockCacheSize = (cachePercent[0] * (totalCache << 20)) / 100
opt.IndexCacheSize = (cachePercent[1] * (totalCache << 20)) / 100
}
if inputFlag.Has("goroutines") {
opt.NumGoroutines = int(flags.GetUint32("goroutines"))
}

return opt
}
Expand Down
5 changes: 1 addition & 4 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,11 @@ func TestOptions(t *testing.T) {
o1.NamespaceOffset = 10
o1.Compression = options.ZSTD
o1.ZSTDCompressionLevel = 2
o1.BlockCacheSize = 70 << 20
o1.IndexCacheSize = 30 << 20
o1.NumGoroutines = 20

o2 := DefaultOptions("")
o2.NamespaceOffset = 10
o2 = o2.FromSuperFlag("compression=zstd:2; cache-mb=100; cache-percentage=70,30; " +
"goroutines=20;")
o2 = o2.FromSuperFlag("compression=zstd:2; numgoroutines=20;")

// make sure they're equal
if !optionsEqual(o1, o2) {
Expand Down

0 comments on commit d918b99

Please sign in to comment.