/
options.go
423 lines (368 loc) · 9.7 KB
/
options.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package wiredtiger
import (
"fmt"
"os"
"reflect"
"strconv"
"strings"
"github.com/FavorLabs/favorX/pkg/shed/driver"
)
const (
optionCacheSize = "cache_size"
optionCacheOverhead = "cache_overhead"
optionCheckpoint = "checkpoint"
optionConfigBase = "config_base"
optionCreate = "create"
optionDebugMode = "debug_mode"
optionEviction = "eviction"
optionFileManager = "file_manager"
optionLog = "log"
optionSessionMax = "session_max"
optionStatistics = "statistics"
optionStatisticsLog = "statistics_log"
optionExtensions = "extensions"
optionVerbose = "verbose"
)
type Configuration map[string]string
const (
optionSeparator = ','
optionJoiner = '='
)
func camelToSnake(s string) string {
s = strings.TrimSpace(s)
n := strings.Builder{}
n.Grow(len(s) + 2) // nominal 2 bytes of extra space for inserted delimiters
for i, v := range []byte(s) {
vIsCap := v >= 'A' && v <= 'Z'
vIsLow := v >= 'a' && v <= 'z'
if vIsCap {
v += 'a'
v -= 'A'
}
// treat acronyms as words, eg for JSONData -> JSON is a whole word
if i+1 < len(s) {
next := s[i+1]
vIsNum := v >= '0' && v <= '9'
nextIsCap := next >= 'A' && next <= 'Z'
nextIsLow := next >= 'a' && next <= 'z'
nextIsNum := next >= '0' && next <= '9'
// add underscore if next letter case type is changed
if (vIsCap && (nextIsLow || nextIsNum)) || (vIsLow && (nextIsCap || nextIsNum)) || (vIsNum && (nextIsCap || nextIsLow)) {
if vIsCap && nextIsLow {
if prevIsCap := i > 0 && s[i-1] >= 'A' && s[i-1] <= 'Z'; prevIsCap {
n.WriteByte('_')
}
}
n.WriteByte(v)
if vIsLow || vIsNum || nextIsNum {
n.WriteByte('_')
}
continue
}
}
if v == ' ' || v == '_' || v == '-' || v == '.' {
// replace space/underscore/hyphen/dot with delimiter
n.WriteByte('_')
} else {
n.WriteByte(v)
}
}
return n.String()
}
func structToList(v interface{}, embed bool) string {
rt := reflect.TypeOf(v)
if rt.Kind() != reflect.Struct {
panic(fmt.Errorf("need a struct to parse, given a %s", rt.Kind()))
}
rv := reflect.ValueOf(v)
rs := reflect.TypeOf("")
var res []string
for i := 0; i < rt.NumField(); i++ {
f := rt.Field(i)
ft := rv.Field(i)
var value string
switch ft.Kind() {
case reflect.Bool:
value = strconv.FormatBool(ft.Bool())
case reflect.String:
value = ft.String()
case reflect.Struct:
if stringer, ok := ft.Interface().(fmt.Stringer); ok {
value = stringer.String()
} else {
value = structToList(ft.Interface(), true)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
value = strconv.FormatInt(ft.Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
value = strconv.FormatUint(ft.Uint(), 10)
case reflect.Float32:
value = strconv.FormatFloat(ft.Float(), 'E', -1, 32)
case reflect.Float64:
value = strconv.FormatFloat(ft.Float(), 'E', -1, 64)
default:
if ft.CanInterface() {
if stringer, ok := ft.Interface().(fmt.Stringer); ok {
value = stringer.String()
} else {
panic(fmt.Errorf("field %s not implement fmt.Stringer", f.Name))
}
} else {
if !ft.CanConvert(rs) {
panic(fmt.Errorf("field %s not convert to string, is %s", f.Name, f.Type))
}
value = ft.Convert(rs).String()
}
}
key := f.Tag.Get("key")
if key == "" {
key = camelToSnake(f.Name)
}
if key == "-" {
if !ft.IsZero() {
res = append(res, camelToSnake(f.Name))
}
continue
}
res = append(res, key+string(optionJoiner)+value)
}
if embed {
return "(" + strings.Join(res, string(optionSeparator)) + ")"
}
return strings.Join(res, string(optionSeparator))
}
func (c *Configuration) Options(opts ...driver.Option) map[string]struct{} {
cnf := make(map[string]string, len(opts))
exported := make(map[string]struct{})
for _, opt := range opts {
var value string
switch opt.Identity() {
case optionCacheSize:
value = opt.Value().(DiskSize).String()
case optionCacheOverhead:
value = strconv.Itoa(opt.Value().(int))
case optionConfigBase:
value = strconv.FormatBool(opt.Value().(bool))
case optionCheckpoint:
value = structToList(opt.Value().(Checkpoint), true)
case optionCreate:
value = strconv.FormatBool(opt.Value().(bool))
case optionDebugMode:
value = structToList(opt.Value().(DebugMode), true)
case optionEviction:
value = structToList(opt.Value().(Eviction), true)
case optionFileManager:
value = structToList(opt.Value().(FileManger), true)
case optionLog:
value = structToList(opt.Value().(Log), true)
case optionSessionMax:
value = strconv.Itoa(opt.Value().(int))
case optionStatistics:
var r string
for _, policy := range opt.Value().([]StatisticsPolicy) {
r += string(policy) + ","
}
value = "(" + strings.TrimRight(r, ",") + ")"
case optionStatisticsLog:
value = structToList(opt.Value().(StatisticsLog), true)
case optionExtensions:
value = opt.Value().(string)
case optionVerbose:
value = opt.Value().(string)
default:
panic("unsupported option:" + opt.Identity())
}
if opt.Exported() {
exported[opt.Identity()] = struct{}{}
}
cnf[opt.Identity()] = value
}
*c = cnf
return exported
}
type ByteType int
const (
MB ByteType = iota
GB
TB
)
type DiskSize struct {
Size int
Type ByteType
}
func (ds DiskSize) String() string {
var s string
switch ds.Type {
case MB:
s = fmt.Sprintf("%dMB", ds.Size)
case GB:
s = fmt.Sprintf("%dGB", ds.Size)
case TB:
s = fmt.Sprintf("%dTB", ds.Size)
default:
s = "0"
}
return s
}
func (c *Configuration) SetCacheSize(n DiskSize) driver.Option {
o := driver.NewOption(optionCacheSize, DiskSize{}, true)
if (n.Type == MB && n.Size < 1) || (n.Type == TB && n.Size > 10) {
n = DiskSize{Size: 100, Type: MB}
}
o.Set(n)
return o
}
func (c *Configuration) SetCacheOverhead(n int) driver.Option {
o := driver.NewOption(optionCacheOverhead, int(0), false)
if n < 0 || n > 30 {
n = 8
}
o.Set(n)
return o
}
func (c *Configuration) SetCreate(b bool) driver.Option {
o := driver.NewOption(optionCreate, false, false)
o.Set(b)
return o
}
type Checkpoint struct {
LogSize DiskSize
Wait int
}
func (c *Configuration) SetCheckpoint(cp Checkpoint) driver.Option {
o := driver.NewOption(optionCheckpoint, Checkpoint{}, false)
if cp.LogSize.Size < 0 || (cp.LogSize.Type == GB && cp.LogSize.Size > 2) || cp.LogSize.Type == TB {
cp.LogSize = DiskSize{Size: 0}
}
o.Set(cp)
return o
}
func (c *Configuration) SetConfigBase(b bool) driver.Option {
o := driver.NewOption(optionConfigBase, false, false)
o.Set(b)
return o
}
type DebugMode struct {
CheckpointRetention int
CursorCopy bool
Eviction bool
TableLogging bool
}
func (c *Configuration) SetDebugMode(m DebugMode) driver.Option {
o := driver.NewOption(optionDebugMode, DebugMode{}, true)
if m.CheckpointRetention < 0 || m.CheckpointRetention > 1024 {
m.CheckpointRetention = 0
}
o.Set(m)
return o
}
type Eviction struct {
ThreadsMax int `key:"threads_max"`
ThreadsMin int `key:"threads_min"`
}
func (c *Configuration) SetEviction(e Eviction) driver.Option {
o := driver.NewOption(optionEviction, Eviction{}, true)
if e.ThreadsMax < 1 || e.ThreadsMax > 20 {
e.ThreadsMax = 8
}
if e.ThreadsMin < 1 || e.ThreadsMin > 20 {
e.ThreadsMin = 1
}
if e.ThreadsMax < e.ThreadsMin {
e.ThreadsMin, e.ThreadsMax = e.ThreadsMax, e.ThreadsMin
}
o.Set(e)
return o
}
type FileManger struct {
CloseHandleMinimum int
CloseIdleTime int
CloseScanInterval int
}
func (c *Configuration) SetFileManager(f FileManger) driver.Option {
o := driver.NewOption(optionFileManager, FileManger{}, true)
if f.CloseHandleMinimum < 0 {
f.CloseHandleMinimum = 250
}
if f.CloseIdleTime < 0 || f.CloseIdleTime > 100000 {
f.CloseIdleTime = 30
}
if f.CloseScanInterval < 0 || f.CloseScanInterval > 100000 {
f.CloseScanInterval = 10
}
o.Set(f)
return o
}
type Log struct {
Archive bool
Compressor Compressor
Enabled bool
Path string
}
type Compressor string
const (
NoneCompressor Compressor = "none"
LZ4Compressor Compressor = "lz4"
SnappyCompressor Compressor = "snappy"
ZLibCompressor Compressor = "zlib"
ZstdCompressor Compressor = "zstd"
)
func (c *Configuration) SetLog(l Log) driver.Option {
o := driver.NewOption(optionLog, Log{}, true)
if strings.HasPrefix(l.Path, string(os.PathSeparator)) {
panic("log path should be a relative path under database home")
}
o.Set(l)
return o
}
func (c *Configuration) SetSessionMax(n int) driver.Option {
o := driver.NewOption(optionSessionMax, int(0), false)
if n < 1 {
n = 100
}
o.Set(n)
return o
}
type StatisticsPolicy string
const (
StatisticsAll StatisticsPolicy = "all"
StatisticsCacheWalk StatisticsPolicy = "cache_walk"
StatisticsFast StatisticsPolicy = "fast"
)
func (c *Configuration) SetStatistics(s []StatisticsPolicy) driver.Option {
o := driver.NewOption(optionStatistics, []StatisticsPolicy{StatisticsAll}, true)
o.Set(s)
return o
}
type StatisticsLog struct {
Wait int `key:"wait"`
}
func (c *Configuration) SetStatisticsLog(l StatisticsLog) driver.Option {
o := driver.NewOption(optionStatisticsLog, StatisticsLog{}, true)
if l.Wait < 0 || l.Wait > 100000 {
l.Wait = 0
}
o.Set(l)
return o
}
func (c *Configuration) SetExtensions(s string) driver.Option {
o := driver.NewOption(optionExtensions, "[]", true)
if len(s) != 0 {
o.Set(s)
}
return o
}
func (c *Configuration) SetVerbose(s string) driver.Option {
o := driver.NewOption(optionVerbose, "[]", true)
if len(s) != 0 {
o.Set(s)
}
return o
}
func (c Configuration) String() string {
var opts []string
for k, v := range c {
opts = append(opts, k+string(optionJoiner)+v)
}
return strings.Join(opts, string(optionSeparator))
}