github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/internal/options/options_pool.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 options
    16  
    17  import (
    18  	"encoding/binary"
    19  	"os"
    20  	"time"
    21  
    22  	"github.com/zuoyebang/bitalosdb/internal/base"
    23  	"github.com/zuoyebang/bitalosdb/internal/bitask"
    24  	"github.com/zuoyebang/bitalosdb/internal/compress"
    25  	"github.com/zuoyebang/bitalosdb/internal/consts"
    26  	"github.com/zuoyebang/bitalosdb/internal/statemachine"
    27  	"github.com/zuoyebang/bitalosdb/internal/vfs"
    28  )
    29  
    30  var DefaultBitableOptions = &BitableOptions{
    31  	MemTableSize:                consts.BitableMemTableSize,
    32  	MemTableStopWritesThreshold: consts.BitableMemTableStopWritesThreshold,
    33  	L0FileSize:                  consts.BitableL0FileSize,
    34  	CacheSize:                   consts.BitableCacheSize,
    35  	L0CompactionFileThreshold:   consts.BitableL0CompactionFileThreshold,
    36  	L0CompactionThreshold:       consts.BitableL0CompactionThreshold,
    37  	L0StopWritesThreshold:       consts.BitableL0StopWritesThreshold,
    38  	LBaseMaxBytes:               consts.BitableLBaseMaxBytes,
    39  	MaxOpenFiles:                consts.BitableMaxOpenFiles,
    40  }
    41  
    42  var DefaultBdbOptions = &BdbOptions{
    43  	Options: &Options{
    44  		Logger: base.DefaultLogger,
    45  		Cmp:    base.DefaultComparer.Compare,
    46  	},
    47  	Timeout:      0,
    48  	NoGrowSync:   false,
    49  	FreelistType: consts.BdbFreelistArrayType,
    50  }
    51  
    52  var DefaultKeyHashFunc = func(k []byte) int {
    53  	if len(k) < 2 {
    54  		return int(k[0])
    55  	}
    56  	return int(binary.BigEndian.Uint16(k[0:2]))
    57  }
    58  
    59  var DefaultKvCheckExpireFunc = func(id int, k, v []byte) bool { return false }
    60  
    61  var DefaultCheckExpireFunc = func(k, v []byte) bool { return false }
    62  
    63  var DefaultKvTimestampFunc = func(v []byte, t uint8) (bool, uint64) {
    64  	if t == 2 {
    65  		return false, uint64(time.Now().UnixMilli())
    66  	}
    67  	return false, 0
    68  }
    69  
    70  var DefaultIOWriteLoadThresholdFunc = func() bool {
    71  	return true
    72  }
    73  
    74  var DefaultKeyPrefixDeleteFunc = func(k []byte) uint64 {
    75  	return 0
    76  }
    77  
    78  var TestKvCheckExpireFunc = func(id int, k, v []byte) bool {
    79  	if len(v) == 0 {
    80  		return false
    81  	} else if uint8(v[0]) == 1 {
    82  		timestamp := binary.BigEndian.Uint64(v[1:9])
    83  		if timestamp == 0 {
    84  			return false
    85  		}
    86  		if timestamp <= uint64(time.Now().UnixMilli()) {
    87  			return true
    88  		}
    89  		return false
    90  	}
    91  
    92  	return false
    93  }
    94  
    95  var TestKvTimestampFunc = func(v []byte, t uint8) (bool, uint64) {
    96  	if t == 2 {
    97  		return false, uint64(time.Now().UnixMilli())
    98  	}
    99  
   100  	if uint8(v[0]) != 1 {
   101  		return false, 0
   102  	}
   103  
   104  	return true, binary.BigEndian.Uint64(v[1:9])
   105  }
   106  
   107  var TestKeyHashFunc = func(k []byte) int {
   108  	return int(binary.BigEndian.Uint16(k[0:2]))
   109  }
   110  
   111  var TestKeyPrefixDeleteFunc = func(k []byte) uint64 {
   112  	if len(k) < 10 {
   113  		return 0
   114  	}
   115  	return binary.LittleEndian.Uint64(k[2:10])
   116  }
   117  
   118  type CacheOptions struct {
   119  	Size     int64
   120  	Shards   int
   121  	HashSize int
   122  	Logger   base.Logger
   123  }
   124  
   125  type Options struct {
   126  	Id                    int
   127  	FS                    vfs.FS
   128  	Cmp                   base.Compare
   129  	Logger                base.Logger
   130  	Compressor            compress.Compressor
   131  	UseBithash            bool
   132  	UseBitable            bool
   133  	UseMapIndex           bool
   134  	UsePrefixCompress     bool
   135  	UseBlockCompress      bool
   136  	BitpageBlockCacheSize int64
   137  	BytesPerSync          int
   138  	KvSeparateSize        int
   139  	BitpageFlushSize      uint64
   140  	BitpageSplitSize      uint64
   141  	BitpageTaskPushFunc   func(*bitask.BitpageTaskData)
   142  	DbState               *statemachine.DbStateMachine
   143  	DeleteFilePacer       *base.DeletionFileLimiter
   144  	KeyHashFunc           func([]byte) int
   145  	KvCheckExpireFunc     func(int, []byte, []byte) bool
   146  	KvTimestampFunc       func([]byte, uint8) (bool, uint64)
   147  	KeyPrefixDeleteFunc   func([]byte) uint64
   148  }
   149  
   150  func (o *Options) KvCheckExpire(key, value []byte) bool {
   151  	return o.KvCheckExpireFunc(o.Id, key, value)
   152  }
   153  
   154  type BdbOptions struct {
   155  	*Options
   156  	Index             int
   157  	Timeout           time.Duration
   158  	NoGrowSync        bool
   159  	NoFreelistSync    bool
   160  	FreelistType      string
   161  	ReadOnly          bool
   162  	MmapFlags         int
   163  	InitialMmapSize   int
   164  	PageSize          int
   165  	NoSync            bool
   166  	OpenFile          func(string, int, os.FileMode) (*os.File, error)
   167  	Mlock             bool
   168  	CheckPageSplitted func(uint32) bool
   169  }
   170  
   171  type BithashOptions struct {
   172  	*Options
   173  	TableMaxSize int
   174  	Index        int
   175  }
   176  
   177  type BitpageOptions struct {
   178  	*Options
   179  	Index           int
   180  	BithashDeleteCB func(uint32) error
   181  	BitableDeleteCB func([]byte) error
   182  	CheckExpireCB   func([]byte, []byte) bool
   183  }
   184  
   185  type BitableOptions struct {
   186  	*Options
   187  	Index                       int
   188  	MemTableSize                int
   189  	MemTableStopWritesThreshold int
   190  	L0CompactionFileThreshold   int
   191  	L0CompactionThreshold       int
   192  	L0StopWritesThreshold       int
   193  	LBaseMaxBytes               int64
   194  	L0FileSize                  int64
   195  	CacheSize                   int64
   196  	MaxOpenFiles                int
   197  	CheckExpireCB               func([]byte, []byte) bool
   198  }
   199  
   200  type BitreeOptions struct {
   201  	*Options
   202  	Index              int
   203  	BdbOpts            *BdbOptions
   204  	BitpageOpts        *BitpageOptions
   205  	BithashOpts        *BithashOptions
   206  	BitableOpts        *BitableOptions
   207  	IsFlushedBitableCB func() bool
   208  }
   209  
   210  type OptionsPool struct {
   211  	BaseOptions    *Options
   212  	BitableOptions *BitableOptions
   213  	BithashOptions *BithashOptions
   214  	BitpageOptions *BitpageOptions
   215  	BdbOptions     *BdbOptions
   216  	BitreeOptions  *BitreeOptions
   217  	DbState        *statemachine.DbStateMachine
   218  }
   219  
   220  func (o *OptionsPool) CloneBitreeOptions() *BitreeOptions {
   221  	bropts := &BitreeOptions{}
   222  	*bropts = *(o.BitreeOptions)
   223  	bdbopts := &BdbOptions{}
   224  	*bdbopts = *(o.BdbOptions)
   225  	bpopts := &BitpageOptions{}
   226  	*bpopts = *(o.BitpageOptions)
   227  	bhopts := &BithashOptions{}
   228  	*bhopts = *(o.BithashOptions)
   229  	btopts := &BitableOptions{}
   230  	*btopts = *(o.BitableOptions)
   231  
   232  	bropts.BdbOpts = bdbopts
   233  	bropts.BitpageOpts = bpopts
   234  	bropts.BithashOpts = bhopts
   235  	bropts.BitableOpts = btopts
   236  	return bropts
   237  }
   238  
   239  func (o *OptionsPool) CloneBitpageOptions() *BitpageOptions {
   240  	bpopts := &BitpageOptions{}
   241  	*bpopts = *(o.BitpageOptions)
   242  	return bpopts
   243  }
   244  
   245  func (o *OptionsPool) CloneBithashOptions() *BithashOptions {
   246  	bhopts := &BithashOptions{}
   247  	*bhopts = *(o.BithashOptions)
   248  	return bhopts
   249  }
   250  
   251  func (o *OptionsPool) Close() {
   252  	o.BaseOptions.DeleteFilePacer.Close()
   253  }
   254  
   255  func InitDefaultsOptionsPool() *OptionsPool {
   256  	optspool := &OptionsPool{
   257  		DbState: statemachine.NewDbStateMachine(),
   258  	}
   259  
   260  	optspool.BaseOptions = &Options{
   261  		FS:                    vfs.Default,
   262  		Cmp:                   base.DefaultComparer.Compare,
   263  		Logger:                base.DefaultLogger,
   264  		Compressor:            compress.NoCompressor,
   265  		UseBithash:            true,
   266  		UseBitable:            false,
   267  		UseMapIndex:           true,
   268  		UsePrefixCompress:     true,
   269  		UseBlockCompress:      false,
   270  		BitpageBlockCacheSize: consts.BitpageDefaultBlockCacheSize,
   271  		BytesPerSync:          consts.DefaultBytesPerSync,
   272  		KvSeparateSize:        consts.KvSeparateSize,
   273  		BitpageFlushSize:      consts.BitpageFlushSize,
   274  		BitpageSplitSize:      consts.BitpageSplitSize,
   275  		DbState:               optspool.DbState,
   276  		DeleteFilePacer:       NewDefaultDeletionFileLimiter(),
   277  		KeyHashFunc:           DefaultKeyHashFunc,
   278  		KvCheckExpireFunc:     DefaultKvCheckExpireFunc,
   279  		KvTimestampFunc:       DefaultKvTimestampFunc,
   280  		KeyPrefixDeleteFunc:   DefaultKeyPrefixDeleteFunc,
   281  	}
   282  
   283  	brOpts := &BitreeOptions{
   284  		Options:            optspool.BaseOptions,
   285  		IsFlushedBitableCB: func() bool { return false },
   286  	}
   287  
   288  	bdbOpts := &BdbOptions{
   289  		Options:           optspool.BaseOptions,
   290  		Timeout:           time.Second,
   291  		InitialMmapSize:   consts.BdbInitialSize,
   292  		NoSync:            true,
   293  		NoGrowSync:        true,
   294  		FreelistType:      consts.BdbFreelistMapType,
   295  		PageSize:          consts.BdbPageSize,
   296  		CheckPageSplitted: func(uint32) bool { return false },
   297  	}
   298  
   299  	bpOpts := &BitpageOptions{
   300  		Options:         optspool.BaseOptions,
   301  		BithashDeleteCB: func(uint32) error { return nil },
   302  		BitableDeleteCB: func([]byte) error { return nil },
   303  		CheckExpireCB:   DefaultCheckExpireFunc,
   304  	}
   305  
   306  	bhOpts := &BithashOptions{
   307  		Options:      optspool.BaseOptions,
   308  		TableMaxSize: consts.BithashTableMaxSize,
   309  	}
   310  
   311  	btOpts := DefaultBitableOptions
   312  	btOpts.Options = optspool.BaseOptions
   313  	btOpts.CheckExpireCB = DefaultCheckExpireFunc
   314  
   315  	optspool.BdbOptions = bdbOpts
   316  	optspool.BitpageOptions = bpOpts
   317  	optspool.BitreeOptions = brOpts
   318  	optspool.BithashOptions = bhOpts
   319  	optspool.BitableOptions = btOpts
   320  
   321  	return optspool
   322  }
   323  
   324  func InitTestDefaultsOptionsPool() *OptionsPool {
   325  	optsPool := InitDefaultsOptionsPool()
   326  	optsPool.BaseOptions.DeleteFilePacer.Run(nil)
   327  	optsPool.BaseOptions.KeyPrefixDeleteFunc = TestKeyPrefixDeleteFunc
   328  	return optsPool
   329  }
   330  
   331  func NewDefaultDeletionFileLimiter() *base.DeletionFileLimiter {
   332  	dflOpts := &base.DFLOption{
   333  		Logger:                 base.DefaultLogger,
   334  		IOWriteLoadThresholdCB: DefaultIOWriteLoadThresholdFunc,
   335  		DeleteInterval:         consts.DeletionFileInterval,
   336  	}
   337  	return base.NewDeletionFileLimiter(dflOpts)
   338  }