github.com/zuoyebang/bitalostable@v1.0.1-0.20240229032404-e3b99a834294/sstable/options.go (about)

     1  // Copyright 2019 The LevelDB-Go and Pebble Authors. All rights reserved. Use
     2  // of this source code is governed by a BSD-style license that can be found in
     3  // the LICENSE file.
     4  
     5  package sstable
     6  
     7  import (
     8  	"github.com/zuoyebang/bitalostable/internal/base"
     9  	"github.com/zuoyebang/bitalostable/internal/cache"
    10  )
    11  
    12  // Compression is the per-block compression algorithm to use.
    13  type Compression int
    14  
    15  // The available compression types.
    16  const (
    17  	DefaultCompression Compression = iota
    18  	NoCompression
    19  	SnappyCompression
    20  	ZstdCompression
    21  	NCompression
    22  )
    23  
    24  func (c Compression) String() string {
    25  	switch c {
    26  	case DefaultCompression:
    27  		return "Default"
    28  	case NoCompression:
    29  		return "NoCompression"
    30  	case SnappyCompression:
    31  		return "Snappy"
    32  	case ZstdCompression:
    33  		return "ZSTD"
    34  	default:
    35  		return "Unknown"
    36  	}
    37  }
    38  
    39  // FilterType exports the base.FilterType type.
    40  type FilterType = base.FilterType
    41  
    42  // Exported TableFilter constants.
    43  const (
    44  	TableFilter = base.TableFilter
    45  )
    46  
    47  // FilterWriter exports the base.FilterWriter type.
    48  type FilterWriter = base.FilterWriter
    49  
    50  // FilterPolicy exports the base.FilterPolicy type.
    51  type FilterPolicy = base.FilterPolicy
    52  
    53  // TablePropertyCollector provides a hook for collecting user-defined
    54  // properties based on the keys and values stored in an sstable. A new
    55  // TablePropertyCollector is created for an sstable when the sstable is being
    56  // written.
    57  type TablePropertyCollector interface {
    58  	// Add is called with each new entry added to the sstable. While the sstable
    59  	// is itself sorted by key, do not assume that the entries are added in any
    60  	// order. In particular, the ordering of point entries and range tombstones
    61  	// is unspecified.
    62  	Add(key InternalKey, value []byte) error
    63  
    64  	// Finish is called when all entries have been added to the sstable. The
    65  	// collected properties (if any) should be added to the specified map. Note
    66  	// that in case of an error during sstable construction, Finish may not be
    67  	// called.
    68  	Finish(userProps map[string]string) error
    69  
    70  	// The name of the property collector.
    71  	Name() string
    72  }
    73  
    74  // SuffixReplaceableTableCollector is an extension to the TablePropertyCollector
    75  // interface that allows a table property collector to indicate that it supports
    76  // being *updated* during suffix replacement, i.e. when an existing SST in which
    77  // all keys have the same key suffix is updated to have a new suffix.
    78  //
    79  // A collector which supports being updated in such cases must be able to derive
    80  // its updated value from its old value and the change being made to the suffix,
    81  // without needing to be passed each updated K/V.
    82  //
    83  // For example, a collector that only inspects values can simply copy its
    84  // previously computed property as-is, since key-suffix replacement does not
    85  // change values, while a collector that depends only on key suffixes, like one
    86  // which collected mvcc-timestamp bounds from timestamp-suffixed keys, can just
    87  // set its new bounds from the new suffix, as it is common to all keys, without
    88  // needing to recompute it from every key.
    89  type SuffixReplaceableTableCollector interface {
    90  	// UpdateKeySuffixes is called when a table is updated to change the suffix of
    91  	// all keys in the table, and is passed the old value for that prop, if any,
    92  	// for that table as well as the old and new suffix.
    93  	UpdateKeySuffixes(oldProps map[string]string, oldSuffix, newSuffix []byte) error
    94  }
    95  
    96  // ReaderOptions holds the parameters needed for reading an sstable.
    97  type ReaderOptions struct {
    98  	// Cache is used to cache uncompressed blocks from sstables.
    99  	//
   100  	// The default cache size is a zero-size cache.
   101  	Cache *cache.Cache
   102  
   103  	// Comparer defines a total ordering over the space of []byte keys: a 'less
   104  	// than' relationship. The same comparison algorithm must be used for reads
   105  	// and writes over the lifetime of the DB.
   106  	//
   107  	// The default value uses the same ordering as bytes.Compare.
   108  	Comparer *Comparer
   109  
   110  	// Filters is a map from filter policy name to filter policy. It is used for
   111  	// debugging tools which may be used on multiple databases configured with
   112  	// different filter policies. It is not necessary to populate this filters
   113  	// map during normal usage of a DB.
   114  	Filters map[string]FilterPolicy
   115  
   116  	// Merger defines the associative merge operation to use for merging values
   117  	// written with {Batch,DB}.Merge. The MergerName is checked for consistency
   118  	// with the value stored in the sstable when it was written.
   119  	MergerName string
   120  }
   121  
   122  func (o ReaderOptions) ensureDefaults() ReaderOptions {
   123  	if o.Comparer == nil {
   124  		o.Comparer = base.DefaultComparer
   125  	}
   126  	if o.MergerName == "" {
   127  		o.MergerName = base.DefaultMerger.Name
   128  	}
   129  	return o
   130  }
   131  
   132  // WriterOptions holds the parameters used to control building an sstable.
   133  type WriterOptions struct {
   134  	// BlockRestartInterval is the number of keys between restart points
   135  	// for delta encoding of keys.
   136  	//
   137  	// The default value is 16.
   138  	BlockRestartInterval int
   139  
   140  	// BlockSize is the target uncompressed size in bytes of each table block.
   141  	//
   142  	// The default value is 4096.
   143  	BlockSize int
   144  
   145  	// BlockSizeThreshold finishes a block if the block size is larger than the
   146  	// specified percentage of the target block size and adding the next entry
   147  	// would cause the block to be larger than the target block size.
   148  	//
   149  	// The default value is 90
   150  	BlockSizeThreshold int
   151  
   152  	// Cache is used to cache uncompressed blocks from sstables.
   153  	//
   154  	// The default is a nil cache.
   155  	Cache *cache.Cache
   156  
   157  	// Comparer defines a total ordering over the space of []byte keys: a 'less
   158  	// than' relationship. The same comparison algorithm must be used for reads
   159  	// and writes over the lifetime of the DB.
   160  	//
   161  	// The default value uses the same ordering as bytes.Compare.
   162  	Comparer *Comparer
   163  
   164  	// Compression defines the per-block compression to use.
   165  	//
   166  	// The default value (DefaultCompression) uses snappy compression.
   167  	Compression Compression
   168  
   169  	// FilterPolicy defines a filter algorithm (such as a Bloom filter) that can
   170  	// reduce disk reads for Get calls.
   171  	//
   172  	// One such implementation is bloom.FilterPolicy(10) from the bitalostable/bloom
   173  	// package.
   174  	//
   175  	// The default value means to use no filter.
   176  	FilterPolicy FilterPolicy
   177  
   178  	// FilterType defines whether an existing filter policy is applied at a
   179  	// block-level or table-level. Block-level filters use less memory to create,
   180  	// but are slower to access as a check for the key in the index must first be
   181  	// performed to locate the filter block. A table-level filter will require
   182  	// memory proportional to the number of keys in an sstable to create, but
   183  	// avoids the index lookup when determining if a key is present. Table-level
   184  	// filters should be preferred except under constrained memory situations.
   185  	FilterType FilterType
   186  
   187  	// IndexBlockSize is the target uncompressed size in bytes of each index
   188  	// block. When the index block size is larger than this target, two-level
   189  	// indexes are automatically enabled. Setting this option to a large value
   190  	// (such as math.MaxInt32) disables the automatic creation of two-level
   191  	// indexes.
   192  	//
   193  	// The default value is the value of BlockSize.
   194  	IndexBlockSize int
   195  
   196  	// Merger defines the associative merge operation to use for merging values
   197  	// written with {Batch,DB}.Merge. The MergerName is checked for consistency
   198  	// with the value stored in the sstable when it was written.
   199  	MergerName string
   200  
   201  	// TableFormat specifies the format version for writing sstables. The default
   202  	// is TableFormatRocksDBv2 which creates RocksDB compatible sstables. Use
   203  	// TableFormatLevelDB to create LevelDB compatible sstable which can be used
   204  	// by a wider range of tools and libraries.
   205  	TableFormat TableFormat
   206  
   207  	// TablePropertyCollectors is a list of TablePropertyCollector creation
   208  	// functions. A new TablePropertyCollector is created for each sstable built
   209  	// and lives for the lifetime of the table.
   210  	TablePropertyCollectors []func() TablePropertyCollector
   211  
   212  	// BlockPropertyCollectors is a list of BlockPropertyCollector creation
   213  	// functions. A new BlockPropertyCollector is created for each sstable
   214  	// built and lives for the lifetime of writing that table.
   215  	BlockPropertyCollectors []func() BlockPropertyCollector
   216  
   217  	// Checksum specifies which checksum to use.
   218  	Checksum ChecksumType
   219  
   220  	// Parallelism is used to indicate that the sstable Writer is allowed to
   221  	// compress data blocks and write datablocks to disk in parallel with the
   222  	// Writer client goroutine.
   223  	Parallelism bool
   224  }
   225  
   226  func (o WriterOptions) ensureDefaults() WriterOptions {
   227  	if o.BlockRestartInterval <= 0 {
   228  		o.BlockRestartInterval = base.DefaultBlockRestartInterval
   229  	}
   230  	if o.BlockSize <= 0 {
   231  		o.BlockSize = base.DefaultBlockSize
   232  	}
   233  	if o.BlockSizeThreshold <= 0 {
   234  		o.BlockSizeThreshold = base.DefaultBlockSizeThreshold
   235  	}
   236  	if o.Comparer == nil {
   237  		o.Comparer = base.DefaultComparer
   238  	}
   239  	if o.Compression <= DefaultCompression || o.Compression >= NCompression {
   240  		o.Compression = SnappyCompression
   241  	}
   242  	if o.IndexBlockSize <= 0 {
   243  		o.IndexBlockSize = o.BlockSize
   244  	}
   245  	if o.MergerName == "" {
   246  		o.MergerName = base.DefaultMerger.Name
   247  	}
   248  	if o.Checksum == ChecksumTypeNone {
   249  		o.Checksum = ChecksumTypeCRC32c
   250  	}
   251  	// By default, if the table format is not specified, fall back to using the
   252  	// most compatible format.
   253  	if o.TableFormat == TableFormatUnspecified {
   254  		o.TableFormat = TableFormatRocksDBv2
   255  	}
   256  	return o
   257  }