/
conf.go
132 lines (119 loc) · 3.49 KB
/
conf.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
package lsmt
import (
"encoding/json"
"math"
)
const (
SizeKB = 1<<10 - 1
SizeMB = 1<<20 - 1
Size64KB = math.MaxUint16
Size4GB = math.MaxUint32
)
const (
// path defaults
defaultBaseDir = "data"
defaultWalDir = "log"
defaultSstDir = "sst"
// syncing
defaultSyncOnWrite = false
defaultLoggingLevel = LevelError
// default sizes
defaultFlushThreshold = 2 * SizeMB
defaultBloomFilterSize = 4 * SizeMB
defaultMaxKeySize = maxKeySizeAllowed
defaultMaxValueSize = maxValueSizeAllowed
// minimum size bounds
minFlushThresholdAllowed = maxValueSizeAllowed * 16
minBloomFilterSizeAllowed = minFlushThresholdAllowed
minKeySizeAllowed = 1
minValueSizeAllowed = 1
// maximum size bounds
maxFlushThresholdAllowed = 8 * SizeMB
maxBloomFilterSizeAllowed = 8 * SizeMB
maxKeySizeAllowed = math.MaxUint8 // 255 B
maxValueSizeAllowed = math.MaxUint16 // 65,535 B
)
// default config
var defaultLSMConfig = &LSMConfig{
BaseDir: defaultBaseDir,
SyncOnWrite: defaultSyncOnWrite,
LoggingLevel: defaultLoggingLevel,
FlushThreshold: defaultFlushThreshold,
BloomFilterSize: defaultBloomFilterSize,
MaxKeySize: defaultMaxKeySize,
MaxValueSize: defaultMaxValueSize,
}
func DefaultConfig(path string) *LSMConfig {
defaultLSMConfig.BaseDir = path
return defaultLSMConfig
}
// LSMConfig holds configuration settings for an LSMTree instance
type LSMConfig struct {
BaseDir string // base directory
SyncOnWrite bool // perform sync every time an entry is written
LoggingLevel logLevel // enable logging
FlushThreshold int64 // mem-table flush threshold
BloomFilterSize uint // specify the bloom filter size
MaxKeySize int64 // the max allowed key size
MaxValueSize int64 // the maximum allowed value size
}
func (conf *LSMConfig) String() string {
data, err := json.MarshalIndent(conf, "", "\t")
if err != nil {
return err.Error()
}
return string(data)
}
// checkLSMConfig is a helper to make sure the configuration
// options are correct and handles and missing options
func checkLSMConfig(conf *LSMConfig) *LSMConfig {
if conf == nil {
return defaultLSMConfig
}
if conf.BaseDir == *new(string) {
conf.BaseDir = defaultBaseDir
}
if conf.LoggingLevel <= 0 {
conf.LoggingLevel = defaultLoggingLevel
}
if conf.FlushThreshold <= 0 {
conf.FlushThreshold = defaultFlushThreshold
}
if conf.FlushThreshold < minFlushThresholdAllowed {
conf.FlushThreshold = minFlushThresholdAllowed
}
if conf.FlushThreshold > maxFlushThresholdAllowed {
conf.FlushThreshold = maxFlushThresholdAllowed
}
if conf.BloomFilterSize <= 0 {
conf.BloomFilterSize = defaultBloomFilterSize
}
if conf.BloomFilterSize < minBloomFilterSizeAllowed {
conf.BloomFilterSize = minBloomFilterSizeAllowed
}
if conf.BloomFilterSize > maxBloomFilterSizeAllowed {
conf.BloomFilterSize = maxBloomFilterSizeAllowed
}
if conf.MaxKeySize <= 0 {
conf.MaxKeySize = defaultMaxKeySize
}
if conf.MaxKeySize < minKeySizeAllowed {
conf.MaxKeySize = minKeySizeAllowed
}
if conf.MaxKeySize > maxKeySizeAllowed {
conf.MaxKeySize = maxKeySizeAllowed
}
if conf.MaxValueSize <= 0 {
conf.MaxValueSize = defaultMaxValueSize
}
if conf.MaxValueSize < minValueSizeAllowed {
conf.MaxValueSize = minValueSizeAllowed
}
if conf.MaxValueSize > maxValueSizeAllowed {
conf.MaxValueSize = maxValueSizeAllowed
}
if conf.MaxValueSize+conf.MaxKeySize >= conf.FlushThreshold {
conf.FlushThreshold = maxFlushThresholdAllowed
}
return conf
}