-
Notifications
You must be signed in to change notification settings - Fork 10
/
options.go
252 lines (216 loc) · 6.93 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
/*
* Copyright 2020 Saffat Technologies, Ltd.
*
* 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 unitdb
import (
"time"
"github.com/unit-io/unitdb/message"
)
// _Flags holds various DB flags.
type _Flags struct {
// immutable set immutable flag on database.
immutable bool
// encryption flag to encrypt keys.
encryption bool
// backgroundKeyExpiry sets flag to run key expirer.
backgroundKeyExpiry bool
}
// _BatchOptions is used to set options when using batch operation.
type _BatchOptions struct {
contract uint32
encryption bool
writeInterval time.Duration
}
// _QueryOptions is used to set options for DB query.
type _QueryOptions struct {
// defaultQueryLimit limits maximum number of records to fetch if the DB Get or DB Iterator method does not specify a limit.
defaultQueryLimit int
// maxQueryLimit limits maximum number of records to fetch if the DB Get or DB Iterator method does not specify a limit or specify a limit larger than MaxQueryResults.
maxQueryLimit int
}
// _Options holds the optional DB parameters.
type _Options struct {
flags _Flags
batchOptions _BatchOptions
queryOptions _QueryOptions
// maxSyncDurations sets the amount of time between background fsync() calls.
//
// Setting the value to 0 disables the automatic background synchronization.
// Setting the value to -1 makes the DB call fsync() after every write operation.
maxSyncDurations int
// syncDurationType set duration type to run sync for example syncDurationType is Second and maxSyncDuration is 5 then
// all entries are sync to DB in 5 seconds.
syncDurationType time.Duration
// encryptionKey is used for message encryption.
encryptionKey []byte
// bufferSize sets Size of buffer to use for pooling.
bufferSize int64
// memdbSize sets Size of blockcache.
memdbSize int64
// freeBlockSize minimum freeblocks size before free blocks are allocated and reused.
freeBlockSize int64
}
// Options it contains configurable options and flags for DB.
type Options interface {
set(*_Options)
}
// fOption wraps a function that modifies options and flags into an
// implementation of the Options interface.
type fOption struct {
f func(*_Options)
}
func (fo *fOption) set(o *_Options) {
fo.f(o)
}
func newFuncOption(f func(*_Options)) *fOption {
return &fOption{
f: f,
}
}
// WithDefaultFlags will open DB with some default values.
// immutable: True
// encryption: False
// backgroundKeyExpiry: False
func WithDefaultFlags() Options {
return newFuncOption(func(o *_Options) {
o.flags.immutable = true
o.flags.encryption = false
o.flags.backgroundKeyExpiry = false
})
}
// WithMutable sets Immutable flag to false.
func WithMutable() Options {
return newFuncOption(func(o *_Options) {
o.flags.immutable = false
})
}
// WithEncryption sets encryption on DB.
func WithEncryption() Options {
return newFuncOption(func(o *_Options) {
o.flags.encryption = true
})
}
// WithBackgroundKeyExpiry sets background key expiry for DB.
func WithBackgroundKeyExpiry() Options {
return newFuncOption(func(o *_Options) {
o.flags.backgroundKeyExpiry = true
})
}
// WithDefaultBatchOptions will set some default values for Batch operation.
// contract: MasterContract
// encryption: False
func WithDefaultBatchOptions() Options {
return newFuncOption(func(o *_Options) {
o.batchOptions.contract = message.MasterContract
o.batchOptions.encryption = false
})
}
// WithBatchContract sets contract for batch operation.
func WithBatchContract(contract uint32) Options {
return newFuncOption(func(o *_Options) {
o.batchOptions.contract = contract
})
}
// WithBatchEncryption sets encryption on batch operation.
func WithBatchEncryption() Options {
return newFuncOption(func(o *_Options) {
o.batchOptions.encryption = true
})
}
// WithBatchWriteInterval sets batch write interval to partial write large batch.
func WithBatchWriteInterval(dur time.Duration) Options {
return newFuncOption(func(o *_Options) {
o.batchOptions.writeInterval = dur
})
}
// WithDefaultQueryOptions will set some default values for Query operation.
// defaultQueryLimit: 1000
// maxQueryLimit: 100000
func WithDefaultQueryOptions() Options {
return newFuncOption(func(o *_Options) {
o.queryOptions.defaultQueryLimit = 1000
o.queryOptions.maxQueryLimit = 100000
})
}
// WithDefaultOptions will open DB with some default values.
func WithDefaultOptions() Options {
return newFuncOption(func(o *_Options) {
if o.maxSyncDurations == 0 {
o.maxSyncDurations = 1
}
if o.syncDurationType == 0 {
o.syncDurationType = time.Second
}
if o.bufferSize == 0 {
o.bufferSize = 1 << 32 // maximum size of a buffer to use in bufferpool (4GB).
}
if o.memdbSize == 0 {
o.memdbSize = 1 << 33 // maximum size of blockcache (8GB).
}
if o.freeBlockSize == 0 {
o.freeBlockSize = 1 << 27 // minimum size of (128MB).
}
if o.encryptionKey == nil {
o.encryptionKey = []byte("4BWm1vZletvrCDGWsF6mex8oBSd59m6I")
}
})
}
// WithMaxSyncDuration sets the amount of time between background fsync() calls.
func WithMaxSyncDuration(dur time.Duration, interval int) Options {
return newFuncOption(func(o *_Options) {
o.maxSyncDurations = interval
o.syncDurationType = dur
})
}
// WithDefaultQueryLimit limits maximum number of records to fetch
// if the DB Get or DB Iterator method does not specify a limit.
func WithDefaultQueryLimit(limit int) Options {
return newFuncOption(func(o *_Options) {
o.queryOptions.defaultQueryLimit = limit
})
}
// WithMaxQueryLimit limits maximum number of records to fetch
// if the DB Get or DB Iterator method does not specify
// a limit or specify a limit larger than MaxQueryResults.
func WithMaxQueryLimit(limit int) Options {
return newFuncOption(func(o *_Options) {
o.queryOptions.maxQueryLimit = limit
})
}
// WithBufferSize sets Size of buffer to use for pooling.
func WithBufferSize(size int64) Options {
return newFuncOption(func(o *_Options) {
o.bufferSize = size
})
}
// WithMemdbSize sets Size of blockcache.
func WithMemdbSize(size int64) Options {
return newFuncOption(func(o *_Options) {
o.memdbSize = size
})
}
// WithFreeBlockSize sets minimum freeblocks size
// before free blocks are allocated and reused.
func WithFreeBlockSize(size int64) Options {
return newFuncOption(func(o *_Options) {
o.freeBlockSize = size
})
}
// WithEncryptionKey sets encryption key to use for data encryption.
func WithEncryptionKey(key []byte) Options {
return newFuncOption(func(o *_Options) {
o.encryptionKey = key
})
}