github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/settings/float.go (about) 1 // Copyright 2017 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package settings 12 13 import ( 14 "math" 15 16 "github.com/cockroachdb/errors" 17 ) 18 19 // FloatSetting is the interface of a setting variable that will be 20 // updated automatically when the corresponding cluster-wide setting 21 // of type "float" is updated. 22 type FloatSetting struct { 23 common 24 defaultValue float64 25 validateFn func(float64) error 26 } 27 28 var _ extendedSetting = &FloatSetting{} 29 30 // Get retrieves the float value in the setting. 31 func (f *FloatSetting) Get(sv *Values) float64 { 32 return math.Float64frombits(uint64(sv.getInt64(f.slotIdx))) 33 } 34 35 func (f *FloatSetting) String(sv *Values) string { 36 return EncodeFloat(f.Get(sv)) 37 } 38 39 // Encoded returns the encoded value of the current value of the setting. 40 func (f *FloatSetting) Encoded(sv *Values) string { 41 return f.String(sv) 42 } 43 44 // EncodedDefault returns the encoded value of the default value of the setting. 45 func (f *FloatSetting) EncodedDefault() string { 46 return EncodeFloat(f.defaultValue) 47 } 48 49 // Typ returns the short (1 char) string denoting the type of setting. 50 func (*FloatSetting) Typ() string { 51 return "f" 52 } 53 54 // Override changes the setting panicking if validation fails and also overrides 55 // the default value. 56 // 57 // For testing usage only. 58 func (f *FloatSetting) Override(sv *Values, v float64) { 59 if err := f.set(sv, v); err != nil { 60 panic(err) 61 } 62 sv.setDefaultOverrideInt64(f.slotIdx, int64(math.Float64bits(v))) 63 } 64 65 // Validate that a value conforms with the validation function. 66 func (f *FloatSetting) Validate(v float64) error { 67 if f.validateFn != nil { 68 if err := f.validateFn(v); err != nil { 69 return err 70 } 71 } 72 return nil 73 } 74 75 func (f *FloatSetting) set(sv *Values, v float64) error { 76 if err := f.Validate(v); err != nil { 77 return err 78 } 79 sv.setInt64(f.slotIdx, int64(math.Float64bits(v))) 80 return nil 81 } 82 83 func (f *FloatSetting) setToDefault(sv *Values) { 84 // See if the default value was overridden. 85 ok, val, _ := sv.getDefaultOverride(f.slotIdx) 86 if ok { 87 // As per the semantics of override, these values don't go through 88 // validation. 89 _ = f.set(sv, math.Float64frombits(uint64((val)))) 90 return 91 } 92 if err := f.set(sv, f.defaultValue); err != nil { 93 panic(err) 94 } 95 } 96 97 // Default returns the default value. 98 func (f *FloatSetting) Default() float64 { 99 return f.defaultValue 100 } 101 102 // RegisterFloatSetting defines a new setting with type float. 103 func RegisterFloatSetting(key, desc string, defaultValue float64) *FloatSetting { 104 return RegisterValidatedFloatSetting(key, desc, defaultValue, nil) 105 } 106 107 // RegisterNonNegativeFloatSetting defines a new setting with type float. 108 func RegisterNonNegativeFloatSetting(key, desc string, defaultValue float64) *FloatSetting { 109 return RegisterValidatedFloatSetting(key, desc, defaultValue, func(v float64) error { 110 if v < 0 { 111 return errors.Errorf("cannot set %s to a negative value: %f", key, v) 112 } 113 return nil 114 }) 115 } 116 117 // RegisterValidatedFloatSetting defines a new setting with type float. 118 func RegisterValidatedFloatSetting( 119 key, desc string, defaultValue float64, validateFn func(float64) error, 120 ) *FloatSetting { 121 if validateFn != nil { 122 if err := validateFn(defaultValue); err != nil { 123 panic(errors.Wrap(err, "invalid default")) 124 } 125 } 126 setting := &FloatSetting{ 127 defaultValue: defaultValue, 128 validateFn: validateFn, 129 } 130 register(key, desc, setting) 131 return setting 132 }