forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.go
115 lines (102 loc) · 3.03 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
// 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)
}
// 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 Done().
type Updater map[string]struct{}
// MakeUpdater returns a new Updater, pre-alloced to the registry size.
func MakeUpdater() Updater {
return make(Updater, len(registry))
}
// Set attempts to parse and update a setting and notes that it was updated.
func (u Updater) Set(key, rawValue, 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[key] = struct{}{}
if expected := d.setting.Typ(); vt != expected {
return errors.Errorf("setting '%s' defined as type %s, not %s", key, expected, vt)
}
switch setting := d.setting.(type) {
case *StringSetting:
return setting.set(rawValue)
case *BoolSetting:
b, err := strconv.ParseBool(rawValue)
if err != nil {
return err
}
setting.set(b)
return nil
case numericSetting:
i, err := strconv.Atoi(rawValue)
if err != nil {
return err
}
return setting.set(int64(i))
case *FloatSetting:
f, err := strconv.ParseFloat(rawValue, 64)
if err != nil {
return err
}
return setting.set(f)
case *DurationSetting:
d, err := time.ParseDuration(rawValue)
if err != nil {
return err
}
return setting.set(d)
case *EnumSetting:
i, err := strconv.Atoi(rawValue)
if err != nil {
return err
}
return setting.set(int64(i))
}
return nil
}
// Done sets all settings not updated by the updater to their default values.
func (u Updater) Done() {
for k, v := range registry {
if _, ok := u[k]; !ok {
v.setting.setToDefault()
}
}
}