github.com/scottcagno/storage@v1.8.0/pkg/lsmt/conf.go (about)

     1  package lsmt
     2  
     3  import (
     4  	"encoding/json"
     5  	"math"
     6  )
     7  
     8  const (
     9  	SizeKB   = 1<<10 - 1
    10  	SizeMB   = 1<<20 - 1
    11  	Size64KB = math.MaxUint16
    12  	Size4GB  = math.MaxUint32
    13  )
    14  
    15  const (
    16  
    17  	// path defaults
    18  	defaultBaseDir = "data"
    19  	defaultWalDir  = "log"
    20  	defaultSstDir  = "sst"
    21  
    22  	// syncing
    23  	defaultSyncOnWrite  = false
    24  	defaultLoggingLevel = LevelError
    25  
    26  	// default sizes
    27  	defaultFlushThreshold  = 2 * SizeMB
    28  	defaultBloomFilterSize = 4 * SizeMB
    29  	defaultMaxKeySize      = maxKeySizeAllowed
    30  	defaultMaxValueSize    = maxValueSizeAllowed
    31  
    32  	// minimum size bounds
    33  	minFlushThresholdAllowed  = maxValueSizeAllowed * 16
    34  	minBloomFilterSizeAllowed = minFlushThresholdAllowed
    35  	minKeySizeAllowed         = 1
    36  	minValueSizeAllowed       = 1
    37  
    38  	// maximum size bounds
    39  	maxFlushThresholdAllowed  = 8 * SizeMB
    40  	maxBloomFilterSizeAllowed = 8 * SizeMB
    41  	maxKeySizeAllowed         = math.MaxUint8  //    255 B
    42  	maxValueSizeAllowed       = math.MaxUint16 // 65,535 B
    43  )
    44  
    45  // default config
    46  var defaultLSMConfig = &LSMConfig{
    47  	BaseDir:         defaultBaseDir,
    48  	SyncOnWrite:     defaultSyncOnWrite,
    49  	LoggingLevel:    defaultLoggingLevel,
    50  	FlushThreshold:  defaultFlushThreshold,
    51  	BloomFilterSize: defaultBloomFilterSize,
    52  	MaxKeySize:      defaultMaxKeySize,
    53  	MaxValueSize:    defaultMaxValueSize,
    54  }
    55  
    56  func DefaultConfig(path string) *LSMConfig {
    57  	defaultLSMConfig.BaseDir = path
    58  	return defaultLSMConfig
    59  }
    60  
    61  // LSMConfig holds configuration settings for an LSMTree instance
    62  type LSMConfig struct {
    63  	BaseDir         string   // base directory
    64  	SyncOnWrite     bool     // perform sync every time an entry is written
    65  	LoggingLevel    logLevel // enable logging
    66  	FlushThreshold  int64    // mem-table flush threshold
    67  	BloomFilterSize uint     // specify the bloom filter size
    68  	MaxKeySize      int64    // the max allowed key size
    69  	MaxValueSize    int64    // the maximum allowed value size
    70  }
    71  
    72  func (conf *LSMConfig) String() string {
    73  	data, err := json.MarshalIndent(conf, "", "\t")
    74  	if err != nil {
    75  		return err.Error()
    76  	}
    77  	return string(data)
    78  }
    79  
    80  // checkLSMConfig is a helper to make sure the configuration
    81  // options are correct and handles and missing options
    82  func checkLSMConfig(conf *LSMConfig) *LSMConfig {
    83  	if conf == nil {
    84  		return defaultLSMConfig
    85  	}
    86  	if conf.BaseDir == *new(string) {
    87  		conf.BaseDir = defaultBaseDir
    88  	}
    89  	if conf.LoggingLevel <= 0 {
    90  		conf.LoggingLevel = defaultLoggingLevel
    91  	}
    92  	if conf.FlushThreshold <= 0 {
    93  		conf.FlushThreshold = defaultFlushThreshold
    94  	}
    95  	if conf.FlushThreshold < minFlushThresholdAllowed {
    96  		conf.FlushThreshold = minFlushThresholdAllowed
    97  	}
    98  	if conf.FlushThreshold > maxFlushThresholdAllowed {
    99  		conf.FlushThreshold = maxFlushThresholdAllowed
   100  	}
   101  	if conf.BloomFilterSize <= 0 {
   102  		conf.BloomFilterSize = defaultBloomFilterSize
   103  	}
   104  	if conf.BloomFilterSize < minBloomFilterSizeAllowed {
   105  		conf.BloomFilterSize = minBloomFilterSizeAllowed
   106  	}
   107  	if conf.BloomFilterSize > maxBloomFilterSizeAllowed {
   108  		conf.BloomFilterSize = maxBloomFilterSizeAllowed
   109  	}
   110  	if conf.MaxKeySize <= 0 {
   111  		conf.MaxKeySize = defaultMaxKeySize
   112  	}
   113  	if conf.MaxKeySize < minKeySizeAllowed {
   114  		conf.MaxKeySize = minKeySizeAllowed
   115  	}
   116  	if conf.MaxKeySize > maxKeySizeAllowed {
   117  		conf.MaxKeySize = maxKeySizeAllowed
   118  	}
   119  	if conf.MaxValueSize <= 0 {
   120  		conf.MaxValueSize = defaultMaxValueSize
   121  	}
   122  	if conf.MaxValueSize < minValueSizeAllowed {
   123  		conf.MaxValueSize = minValueSizeAllowed
   124  	}
   125  	if conf.MaxValueSize > maxValueSizeAllowed {
   126  		conf.MaxValueSize = maxValueSizeAllowed
   127  	}
   128  	if conf.MaxValueSize+conf.MaxKeySize >= conf.FlushThreshold {
   129  		conf.FlushThreshold = maxFlushThresholdAllowed
   130  	}
   131  	return conf
   132  }