/
int.go
144 lines (125 loc) · 3.98 KB
/
int.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2017 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package settings
import "github.com/cockroachdb/errors"
// IntSetting is the interface of a setting variable that will be
// updated automatically when the corresponding cluster-wide setting
// of type "int" is updated.
type IntSetting struct {
common
defaultValue int64
validateFn func(int64) error
}
var _ extendedSetting = &IntSetting{}
// Get retrieves the int value in the setting.
func (i *IntSetting) Get(sv *Values) int64 {
return sv.container.getInt64(i.slotIdx)
}
func (i *IntSetting) String(sv *Values) string {
return EncodeInt(i.Get(sv))
}
// Encoded returns the encoded value of the current value of the setting.
func (i *IntSetting) Encoded(sv *Values) string {
return i.String(sv)
}
// EncodedDefault returns the encoded value of the default value of the setting.
func (i *IntSetting) EncodedDefault() string {
return EncodeInt(i.defaultValue)
}
// Typ returns the short (1 char) string denoting the type of setting.
func (*IntSetting) Typ() string {
return "i"
}
// Validate that a value conforms with the validation function.
func (i *IntSetting) Validate(v int64) error {
if i.validateFn != nil {
if err := i.validateFn(v); err != nil {
return err
}
}
return nil
}
// Override changes the setting without validation and also overrides the
// default value.
//
// For testing usage only.
func (i *IntSetting) Override(sv *Values, v int64) {
sv.setInt64(i.slotIdx, v)
sv.setDefaultOverrideInt64(i.slotIdx, v)
}
func (i *IntSetting) set(sv *Values, v int64) error {
if err := i.Validate(v); err != nil {
return err
}
sv.setInt64(i.slotIdx, v)
return nil
}
func (i *IntSetting) setToDefault(sv *Values) {
// See if the default value was overridden.
ok, val, _ := sv.getDefaultOverride(i.slotIdx)
if ok {
// As per the semantics of override, these values don't go through
// validation.
_ = i.set(sv, val)
return
}
if err := i.set(sv, i.defaultValue); err != nil {
panic(err)
}
}
// Default returns the default value.
func (i *IntSetting) Default() int64 {
return i.defaultValue
}
// RegisterIntSetting defines a new setting with type int.
func RegisterIntSetting(key, desc string, defaultValue int64) *IntSetting {
return RegisterValidatedIntSetting(key, desc, defaultValue, nil)
}
// RegisterPublicIntSetting defines a new setting with type int and makes it public.
func RegisterPublicIntSetting(key, desc string, defaultValue int64) *IntSetting {
s := RegisterValidatedIntSetting(key, desc, defaultValue, nil)
s.SetVisibility(Public)
return s
}
// RegisterNonNegativeIntSetting defines a new setting with type int.
func RegisterNonNegativeIntSetting(key, desc string, defaultValue int64) *IntSetting {
return RegisterValidatedIntSetting(key, desc, defaultValue, func(v int64) error {
if v < 0 {
return errors.Errorf("cannot set %s to a negative value: %d", key, v)
}
return nil
})
}
// RegisterPositiveIntSetting defines a new setting with type int.
func RegisterPositiveIntSetting(key, desc string, defaultValue int64) *IntSetting {
return RegisterValidatedIntSetting(key, desc, defaultValue, func(v int64) error {
if v < 1 {
return errors.Errorf("cannot set %s to a value < 1: %d", key, v)
}
return nil
})
}
// RegisterValidatedIntSetting defines a new setting with type int with a
// validation function.
func RegisterValidatedIntSetting(
key, desc string, defaultValue int64, validateFn func(int64) error,
) *IntSetting {
if validateFn != nil {
if err := validateFn(defaultValue); err != nil {
panic(errors.Wrap(err, "invalid default"))
}
}
setting := &IntSetting{
defaultValue: defaultValue,
validateFn: validateFn,
}
register(key, desc, setting)
return setting
}