github.com/nutsdb/nutsdb@v1.0.4/options.go (about) 1 // Copyright 2019 The nutsdb Author. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package nutsdb 16 17 import "time" 18 19 // EntryIdxMode represents entry index mode. 20 type EntryIdxMode int 21 22 const ( 23 // HintKeyValAndRAMIdxMode represents ram index (key and value) mode. 24 HintKeyValAndRAMIdxMode EntryIdxMode = iota 25 26 // HintKeyAndRAMIdxMode represents ram index (only key) mode. 27 HintKeyAndRAMIdxMode 28 ) 29 30 type ExpiredDeleteType uint8 31 32 const ( 33 // TimeWheel represents use time wheel to do expired deletion 34 TimeWheel ExpiredDeleteType = iota 35 36 // TimeHeap represents use time heap to do expired deletion 37 TimeHeap 38 ) 39 40 // An ErrorHandler handles an error occurred during transaction. 41 type ErrorHandler interface { 42 HandleError(err error) 43 } 44 45 // The ErrorHandlerFunc type is an adapter to ErrorHandler. 46 type ErrorHandlerFunc func(err error) 47 48 func (fn ErrorHandlerFunc) HandleError(err error) { 49 fn(err) 50 } 51 52 type LessFunc func(l, r string) bool 53 54 // Options records params for creating DB object. 55 type Options struct { 56 // Dir represents Open the database located in which dir. 57 Dir string 58 59 // EntryIdxMode represents using which mode to index the entries. 60 EntryIdxMode EntryIdxMode 61 62 // RWMode represents the read and write mode. 63 // RWMode includes two options: FileIO and MMap. 64 // FileIO represents the read and write mode using standard I/O. 65 // MMap represents the read and write mode using mmap. 66 RWMode RWMode 67 SegmentSize int64 68 69 // NodeNum represents the node number. 70 // Default NodeNum is 1. NodeNum range [1,1023]. 71 NodeNum int64 72 73 // SyncEnable represents if call Sync() function. 74 // if SyncEnable is false, high write performance but potential data loss likely. 75 // if SyncEnable is true, slower but persistent. 76 SyncEnable bool 77 78 // MaxFdNumsInCache represents the max numbers of fd in cache. 79 MaxFdNumsInCache int 80 81 // CleanFdsCacheThreshold represents the maximum threshold for recycling fd, it should be between 0 and 1. 82 CleanFdsCacheThreshold float64 83 84 // BufferSizeOfRecovery represents the buffer size of recoveryReader buffer Size 85 BufferSizeOfRecovery int 86 87 // CcWhenClose represent initiative GC when calling db.Close() 88 GCWhenClose bool 89 90 // CommitBufferSize represent allocated memory for tx 91 CommitBufferSize int64 92 93 // ErrorHandler handles an error occurred during transaction. 94 // Example: 95 // func triggerAlertError(err error) { 96 // if errors.Is(err, targetErr) { 97 // alertManager.TriggerAlert() 98 // } 99 // }) 100 ErrorHandler ErrorHandler 101 102 // LessFunc is a function that sorts keys. 103 LessFunc LessFunc 104 105 // MergeInterval represent the interval for automatic merges, with 0 meaning automatic merging is disabled. 106 MergeInterval time.Duration 107 108 // MaxBatchCount represents max entries in batch 109 MaxBatchCount int64 110 111 // MaxBatchSize represents max batch size in bytes 112 MaxBatchSize int64 113 114 // ExpiredDeleteType represents the data structure used for expired deletion 115 // TimeWheel means use the time wheel, You can use it when you need high performance or low memory usage 116 // TimeHeap means use the time heap, You can use it when you need to delete precisely or memory usage will be high 117 ExpiredDeleteType ExpiredDeleteType 118 119 // max write record num 120 MaxWriteRecordCount int64 121 122 // cache size for HintKeyAndRAMIdxMode 123 HintKeyAndRAMIdxCacheSize int 124 } 125 126 const ( 127 B = 1 128 129 KB = 1024 * B 130 131 MB = 1024 * KB 132 133 GB = 1024 * MB 134 ) 135 136 // defaultSegmentSize is default data file size. 137 var defaultSegmentSize int64 = 256 * MB 138 139 // DefaultOptions represents the default options. 140 var DefaultOptions = func() Options { 141 return Options{ 142 EntryIdxMode: HintKeyValAndRAMIdxMode, 143 SegmentSize: defaultSegmentSize, 144 NodeNum: 1, 145 RWMode: FileIO, 146 SyncEnable: true, 147 CommitBufferSize: 4 * MB, 148 MergeInterval: 2 * time.Hour, 149 MaxBatchSize: (15 * defaultSegmentSize / 4) / 100, 150 MaxBatchCount: (15 * defaultSegmentSize / 4) / 100 / 100, 151 HintKeyAndRAMIdxCacheSize: 0, 152 ExpiredDeleteType: TimeWheel, 153 } 154 }() 155 156 type Option func(*Options) 157 158 func WithDir(dir string) Option { 159 return func(opt *Options) { 160 opt.Dir = dir 161 } 162 } 163 164 func WithEntryIdxMode(entryIdxMode EntryIdxMode) Option { 165 return func(opt *Options) { 166 opt.EntryIdxMode = entryIdxMode 167 } 168 } 169 170 func WithRWMode(rwMode RWMode) Option { 171 return func(opt *Options) { 172 opt.RWMode = rwMode 173 } 174 } 175 176 func WithSegmentSize(size int64) Option { 177 return func(opt *Options) { 178 opt.SegmentSize = size 179 } 180 } 181 182 func WithMaxBatchCount(count int64) Option { 183 return func(opt *Options) { 184 opt.MaxBatchCount = count 185 } 186 } 187 188 func WithHintKeyAndRAMIdxCacheSize(size int) Option { 189 return func(opt *Options) { 190 opt.HintKeyAndRAMIdxCacheSize = size 191 } 192 } 193 194 func WithMaxBatchSize(size int64) Option { 195 return func(opt *Options) { 196 opt.MaxBatchSize = size 197 } 198 } 199 200 func WithNodeNum(num int64) Option { 201 return func(opt *Options) { 202 opt.NodeNum = num 203 } 204 } 205 206 func WithSyncEnable(enable bool) Option { 207 return func(opt *Options) { 208 opt.SyncEnable = enable 209 } 210 } 211 212 func WithMaxFdNumsInCache(num int) Option { 213 return func(opt *Options) { 214 opt.MaxFdNumsInCache = num 215 } 216 } 217 218 func WithCleanFdsCacheThreshold(threshold float64) Option { 219 return func(opt *Options) { 220 opt.CleanFdsCacheThreshold = threshold 221 } 222 } 223 224 func WithBufferSizeOfRecovery(size int) Option { 225 return func(opt *Options) { 226 opt.BufferSizeOfRecovery = size 227 } 228 } 229 230 func WithGCWhenClose(enable bool) Option { 231 return func(opt *Options) { 232 opt.GCWhenClose = enable 233 } 234 } 235 236 func WithErrorHandler(errorHandler ErrorHandler) Option { 237 return func(opt *Options) { 238 opt.ErrorHandler = errorHandler 239 } 240 } 241 242 func WithCommitBufferSize(commitBufferSize int64) Option { 243 return func(opt *Options) { 244 opt.CommitBufferSize = commitBufferSize 245 } 246 } 247 248 func WithLessFunc(lessFunc LessFunc) Option { 249 return func(opt *Options) { 250 opt.LessFunc = lessFunc 251 } 252 } 253 254 func WithMaxWriteRecordCount(maxWriteRecordCount int64) Option { 255 return func(opt *Options) { 256 opt.MaxWriteRecordCount = maxWriteRecordCount 257 } 258 }