-
Notifications
You must be signed in to change notification settings - Fork 454
/
options.go
193 lines (163 loc) · 5.16 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
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package namespace
import (
"errors"
"github.com/m3db/m3/src/dbnode/retention"
)
const (
// Namespace requires bootstrapping by default.
defaultBootstrapEnabled = true
// Namespace requires flushing by default.
defaultFlushEnabled = true
// Namespace requires snapshotting disabled by default.
defaultSnapshotEnabled = true
// Namespace writes go to commit logs by default.
defaultWritesToCommitLog = true
// Namespace requires fileset/snapshot cleanup by default.
defaultCleanupEnabled = true
// Namespace requires repair disabled by default.
defaultRepairEnabled = false
)
var (
errIndexBlockSizePositive = errors.New("index block size must positive")
errIndexBlockSizeTooLarge = errors.New("index block size needs to be <= namespace retention period")
errIndexBlockSizeMustBeAMultipleOfDataBlockSize = errors.New("index block size must be a multiple of data block size")
)
type options struct {
bootstrapEnabled bool
flushEnabled bool
snapshotEnabled bool
writesToCommitLog bool
cleanupEnabled bool
repairEnabled bool
retentionOpts retention.Options
indexOpts IndexOptions
}
// NewOptions creates a new namespace options
func NewOptions() Options {
return &options{
bootstrapEnabled: defaultBootstrapEnabled,
flushEnabled: defaultFlushEnabled,
snapshotEnabled: defaultSnapshotEnabled,
writesToCommitLog: defaultWritesToCommitLog,
cleanupEnabled: defaultCleanupEnabled,
repairEnabled: defaultRepairEnabled,
retentionOpts: retention.NewOptions(),
indexOpts: NewIndexOptions(),
}
}
func (o *options) Validate() error {
if err := o.retentionOpts.Validate(); err != nil {
return err
}
if !o.indexOpts.Enabled() {
return nil
}
var (
retention = o.retentionOpts.RetentionPeriod()
dataBlockSize = o.retentionOpts.BlockSize()
indexBlockSize = o.indexOpts.BlockSize()
)
if indexBlockSize <= 0 {
return errIndexBlockSizePositive
}
if retention < indexBlockSize {
return errIndexBlockSizeTooLarge
}
if indexBlockSize%dataBlockSize != 0 {
return errIndexBlockSizeMustBeAMultipleOfDataBlockSize
}
return nil
}
func (o *options) Equal(value Options) bool {
return o.bootstrapEnabled == value.BootstrapEnabled() &&
o.flushEnabled == value.FlushEnabled() &&
o.writesToCommitLog == value.WritesToCommitLog() &&
o.snapshotEnabled == value.SnapshotEnabled() &&
o.cleanupEnabled == value.CleanupEnabled() &&
o.repairEnabled == value.RepairEnabled() &&
o.retentionOpts.Equal(value.RetentionOptions()) &&
o.indexOpts.Equal(value.IndexOptions())
}
func (o *options) SetBootstrapEnabled(value bool) Options {
opts := *o
opts.bootstrapEnabled = value
return &opts
}
func (o *options) BootstrapEnabled() bool {
return o.bootstrapEnabled
}
func (o *options) SetFlushEnabled(value bool) Options {
opts := *o
opts.flushEnabled = value
return &opts
}
func (o *options) FlushEnabled() bool {
return o.flushEnabled
}
func (o *options) SetSnapshotEnabled(value bool) Options {
opts := *o
opts.snapshotEnabled = value
return &opts
}
func (o *options) SnapshotEnabled() bool {
return o.snapshotEnabled
}
func (o *options) SetWritesToCommitLog(value bool) Options {
opts := *o
opts.writesToCommitLog = value
return &opts
}
func (o *options) WritesToCommitLog() bool {
return o.writesToCommitLog
}
func (o *options) SetCleanupEnabled(value bool) Options {
opts := *o
opts.cleanupEnabled = value
return &opts
}
func (o *options) CleanupEnabled() bool {
return o.cleanupEnabled
}
func (o *options) SetRepairEnabled(value bool) Options {
opts := *o
opts.repairEnabled = value
return &opts
}
func (o *options) RepairEnabled() bool {
return o.repairEnabled
}
func (o *options) SetRetentionOptions(value retention.Options) Options {
opts := *o
opts.retentionOpts = value
return &opts
}
func (o *options) RetentionOptions() retention.Options {
return o.retentionOpts
}
func (o *options) SetIndexOptions(value IndexOptions) Options {
opts := *o
opts.indexOpts = value
return &opts
}
func (o *options) IndexOptions() IndexOptions {
return o.indexOpts
}