forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.go
138 lines (120 loc) · 3.54 KB
/
updater.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
// Copyright 2017 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package settings
import (
"strconv"
"time"
"github.com/pkg/errors"
)
// EncodeDuration encodes a duration in the format parseRaw expects.
func EncodeDuration(d time.Duration) string {
return d.String()
}
// EncodeBool encodes a bool in the format parseRaw expects.
func EncodeBool(b bool) string {
return strconv.FormatBool(b)
}
// EncodeInt encodes an int in the format parseRaw expects.
func EncodeInt(i int64) string {
return strconv.FormatInt(i, 10)
}
// EncodeFloat encodes a bool in the format parseRaw expects.
func EncodeFloat(f float64) string {
return strconv.FormatFloat(f, 'E', -1, 64)
}
type updater struct {
sv *Values
m map[string]struct{}
}
// Updater is a helper for updating the in-memory settings.
//
// RefreshSettings passes the serialized representations of all individual
// settings -- e.g. the rows read from the system.settings table. We update the
// wrapped atomic settings values as we go and note which settings were updated,
// then set the rest to default in ResetRemaining().
type Updater interface {
Set(k, rawValue, valType string) error
ResetRemaining()
}
// A NoopUpdater ignores all updates.
type NoopUpdater struct{}
// Set implements Updater. It is a no-op.
func (u NoopUpdater) Set(_, _, _ string) error { return nil }
// ResetRemaining implements Updater. It is a no-op.
func (u NoopUpdater) ResetRemaining() {}
// NewUpdater makes an Updater.
func NewUpdater(sv *Values) Updater {
return updater{
m: make(map[string]struct{}, len(Registry)),
sv: sv,
}
}
// Set attempts to parse and update a setting and notes that it was updated.
func (u updater) Set(key, rawValue string, vt string) error {
d, ok := Registry[key]
if !ok {
// Likely a new setting this old node doesn't know about.
return errors.Errorf("unknown setting '%s'", key)
}
u.m[key] = struct{}{}
if expected := d.Typ(); vt != expected {
return errors.Errorf("setting '%s' defined as type %s, not %s", key, expected, vt)
}
switch setting := d.(type) {
case *StringSetting:
return setting.set(u.sv, rawValue)
case *BoolSetting:
b, err := strconv.ParseBool(rawValue)
if err != nil {
return err
}
setting.set(u.sv, b)
return nil
case numericSetting:
i, err := strconv.Atoi(rawValue)
if err != nil {
return err
}
return setting.set(u.sv, int64(i))
case *FloatSetting:
f, err := strconv.ParseFloat(rawValue, 64)
if err != nil {
return err
}
return setting.set(u.sv, f)
case *DurationSetting:
d, err := time.ParseDuration(rawValue)
if err != nil {
return err
}
return setting.set(u.sv, d)
case *EnumSetting:
i, err := strconv.Atoi(rawValue)
if err != nil {
return err
}
return setting.set(u.sv, int64(i))
case *StateMachineSetting:
return setting.set(u.sv, []byte(rawValue))
}
return nil
}
// ResetRemaining sets all settings not updated by the updater to their default values.
func (u updater) ResetRemaining() {
for k, v := range Registry {
if _, ok := u.m[k]; !ok {
v.setToDefault(u.sv)
}
}
}