github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/options.go (about)

     1  // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors.
     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 bitalosdb
    16  
    17  import (
    18  	"time"
    19  
    20  	"github.com/zuoyebang/bitalosdb/internal/base"
    21  	"github.com/zuoyebang/bitalosdb/internal/compress"
    22  	"github.com/zuoyebang/bitalosdb/internal/consts"
    23  	"github.com/zuoyebang/bitalosdb/internal/options"
    24  	"github.com/zuoyebang/bitalosdb/internal/vfs"
    25  )
    26  
    27  type IterOptions = options.IterOptions
    28  
    29  var IterAll = IterOptions{IsAll: true}
    30  
    31  type WriteOptions struct {
    32  	Sync bool
    33  }
    34  
    35  var Sync = &WriteOptions{Sync: true}
    36  
    37  var NoSync = &WriteOptions{Sync: false}
    38  
    39  func (o *WriteOptions) GetSync() bool {
    40  	return o == nil || o.Sync
    41  }
    42  
    43  type CompactEnv struct {
    44  	StartHour     int
    45  	EndHour       int
    46  	DeletePercent float64
    47  	BitreeMaxSize int64
    48  	Interval      int
    49  }
    50  
    51  type BitableOptions = options.BitableOptions
    52  
    53  type Options struct {
    54  	BytesPerSync                int
    55  	Comparer                    *Comparer
    56  	DisableWAL                  bool
    57  	EventListener               EventListener
    58  	MemTableSize                int
    59  	MemTableStopWritesThreshold int
    60  	WALBytesPerSync             int
    61  	WALDir                      string
    62  	WALMinSyncInterval          func() time.Duration
    63  	FS                          vfs.FS
    64  	Logger                      Logger
    65  	Id                          int
    66  	Verbose                     bool
    67  	LogTag                      string
    68  	DataType                    string
    69  	CompressionType             int
    70  	DeleteFileInternal          int
    71  	UseBithash                  bool
    72  	UseBitable                  bool
    73  	BitableOpts                 *options.BitableOptions
    74  	AutoCompact                 bool
    75  	CompactInfo                 CompactEnv
    76  	CacheSize                   int64
    77  	CacheType                   int
    78  	CacheShards                 int
    79  	CacheHashSize               int
    80  	UseMapIndex                 bool
    81  	UsePrefixCompress           bool
    82  	UseBlockCompress            bool
    83  	BlockCacheSize              int64
    84  	FlushReporter               func(int)
    85  	KeyHashFunc                 func([]byte) int
    86  	KvCheckExpireFunc           func(int, []byte, []byte) bool
    87  	KvTimestampFunc             func([]byte, uint8) (bool, uint64)
    88  	IOWriteLoadThresholdFunc    func() bool
    89  	KeyPrefixDeleteFunc         func([]byte) uint64
    90  
    91  	private struct {
    92  		logInit  bool
    93  		optspool *options.OptionsPool
    94  	}
    95  }
    96  
    97  func (o *Options) ensureOptionsPool(optspool *options.OptionsPool) *options.OptionsPool {
    98  	if optspool == nil {
    99  		optspool = options.InitDefaultsOptionsPool()
   100  	}
   101  
   102  	optspool.BaseOptions.Id = o.Id
   103  	optspool.BaseOptions.FS = o.FS
   104  	optspool.BaseOptions.Cmp = o.Comparer.Compare
   105  	optspool.BaseOptions.Logger = o.Logger
   106  	optspool.BaseOptions.BytesPerSync = o.BytesPerSync
   107  	optspool.BaseOptions.Compressor = compress.SetCompressor(o.CompressionType)
   108  	optspool.BaseOptions.UseBithash = o.UseBithash
   109  	optspool.BaseOptions.UseBitable = o.UseBitable
   110  	optspool.BaseOptions.UseMapIndex = o.UseMapIndex
   111  	optspool.BaseOptions.UsePrefixCompress = o.UsePrefixCompress
   112  	optspool.BaseOptions.UseBlockCompress = o.UseBlockCompress
   113  	optspool.BaseOptions.KeyHashFunc = o.KeyHashFunc
   114  	optspool.BaseOptions.BitpageBlockCacheSize = consts.BitpageDefaultBlockCacheSize
   115  	if o.UseBlockCompress && o.BlockCacheSize > 0 {
   116  		bitpageBlockCacheSize := o.BlockCacheSize / int64(consts.DefaultBitowerNum)
   117  		if bitpageBlockCacheSize > consts.BitpageDefaultBlockCacheSize {
   118  			optspool.BaseOptions.BitpageBlockCacheSize = bitpageBlockCacheSize
   119  		}
   120  	}
   121  	if o.KvCheckExpireFunc != nil {
   122  		optspool.BaseOptions.KvCheckExpireFunc = o.KvCheckExpireFunc
   123  	}
   124  	if o.KvTimestampFunc != nil {
   125  		optspool.BaseOptions.KvTimestampFunc = o.KvTimestampFunc
   126  	}
   127  	if o.KeyPrefixDeleteFunc != nil {
   128  		optspool.BaseOptions.KeyPrefixDeleteFunc = o.KeyPrefixDeleteFunc
   129  	}
   130  
   131  	if o.UseBitable {
   132  		o.BitableOpts.Options = optspool.BaseOptions
   133  		optspool.BitableOptions = o.BitableOpts
   134  	}
   135  
   136  	dflOpts := &base.DFLOption{
   137  		Logger:         o.Logger,
   138  		DeleteInterval: o.DeleteFileInternal,
   139  	}
   140  	if o.IOWriteLoadThresholdFunc != nil {
   141  		dflOpts.IOWriteLoadThresholdCB = o.IOWriteLoadThresholdFunc
   142  	} else {
   143  		dflOpts.IOWriteLoadThresholdCB = options.DefaultIOWriteLoadThresholdFunc
   144  	}
   145  	optspool.BaseOptions.DeleteFilePacer.Run(dflOpts)
   146  
   147  	return optspool
   148  }
   149  
   150  func (o *Options) EnsureDefaults() *Options {
   151  	if o == nil {
   152  		o = &Options{}
   153  	}
   154  	if o.BytesPerSync <= 0 {
   155  		o.BytesPerSync = consts.DefaultBytesPerSync
   156  	}
   157  	if o.Comparer == nil {
   158  		o.Comparer = DefaultComparer
   159  	}
   160  	if o.MemTableSize <= 0 {
   161  		o.MemTableSize = consts.DefaultMemTableSize
   162  	}
   163  	o.MemTableSize = o.MemTableSize / consts.DefaultBitowerNum
   164  	if o.MemTableStopWritesThreshold <= 0 {
   165  		o.MemTableStopWritesThreshold = consts.DefaultMemTableStopWritesThreshold
   166  	}
   167  	if o.CacheSize <= 0 {
   168  		o.CacheSize = 0
   169  	} else if o.CacheSize > 0 {
   170  		if o.CacheSize < consts.DefaultCacheSize {
   171  			o.CacheSize = consts.DefaultCacheSize
   172  		}
   173  		if o.CacheType <= 0 || o.CacheType > consts.CacheTypeLfu {
   174  			o.CacheType = consts.CacheTypeLfu
   175  		}
   176  		if o.CacheType == consts.CacheTypeLfu {
   177  			o.CacheShards = consts.DefaultLfuCacheShards
   178  		} else {
   179  			o.CacheShards = consts.DefaultLruCacheShards
   180  			if o.CacheHashSize <= 0 {
   181  				o.CacheHashSize = consts.DefaultLruCacheHashSize
   182  			}
   183  		}
   184  	}
   185  	if o.DeleteFileInternal == 0 {
   186  		o.DeleteFileInternal = consts.DeletionFileInterval
   187  	}
   188  	if !o.private.logInit {
   189  		o.Logger = base.NewLogger(o.Logger, o.LogTag)
   190  		if o.Verbose {
   191  			o.EventListener = MakeLoggingEventListener(o.Logger)
   192  		} else {
   193  			o.EventListener.EnsureDefaults(o.Logger)
   194  		}
   195  		o.private.logInit = true
   196  	}
   197  	if o.FS == nil {
   198  		o.FS = vfs.WithDiskHealthChecks(vfs.Default, 5*time.Second,
   199  			func(name string, duration time.Duration) {
   200  				o.EventListener.DiskSlow(DiskSlowInfo{
   201  					Path:     name,
   202  					Duration: duration,
   203  				})
   204  			})
   205  	}
   206  	if o.CompactInfo.StartHour <= 0 {
   207  		o.CompactInfo.StartHour = 0
   208  	}
   209  	if o.CompactInfo.EndHour <= 0 {
   210  		o.CompactInfo.EndHour = 23
   211  	}
   212  	if o.CompactInfo.EndHour <= o.CompactInfo.StartHour {
   213  		o.CompactInfo.EndHour = o.CompactInfo.StartHour + 1
   214  	}
   215  	if o.CompactInfo.EndHour > 23 {
   216  		o.CompactInfo.EndHour = 23
   217  	}
   218  	if o.CompactInfo.DeletePercent < 0 || o.CompactInfo.DeletePercent >= 1 {
   219  		o.CompactInfo.DeletePercent = consts.DefaultDeletePercent
   220  	}
   221  	if o.CompactInfo.BitreeMaxSize <= 0 {
   222  		o.CompactInfo.BitreeMaxSize = consts.UseBitableBitreeMaxSize
   223  	}
   224  	if o.CompactInfo.Interval == 0 {
   225  		o.CompactInfo.Interval = consts.DefaultCompactInterval
   226  	}
   227  	if o.CompactInfo.Interval < consts.MinCompactInterval {
   228  		o.CompactInfo.Interval = consts.MinCompactInterval
   229  	}
   230  	if o.UseBitable {
   231  		o.BitableOpts = options.DefaultBitableOptions
   232  	}
   233  	if o.IOWriteLoadThresholdFunc == nil {
   234  		o.IOWriteLoadThresholdFunc = options.DefaultIOWriteLoadThresholdFunc
   235  	}
   236  	if o.KeyHashFunc == nil {
   237  		o.KeyHashFunc = options.DefaultKeyHashFunc
   238  	}
   239  
   240  	return o
   241  }
   242  
   243  func (o *Options) Clone() *Options {
   244  	n := &Options{}
   245  	if o != nil {
   246  		*n = *o
   247  	}
   248  	return n
   249  }