github.com/m3db/m3@v1.5.0/src/x/serialize/decoder_options.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package serialize
    22  
    23  import (
    24  	"github.com/m3db/m3/src/dbnode/x/xpool"
    25  	"github.com/m3db/m3/src/x/pool"
    26  )
    27  
    28  const (
    29  	// defaultCheckBytesWrapperPoolSize is the default size of the checked.Bytes wrapper pool.
    30  	defaultCheckBytesWrapperPoolSize = 16384
    31  	// defaultCheckBytesWrapperPoolLowWatermark is the default low watermark of the checked.Bytes wrapper pool.
    32  	defaultCheckBytesWrapperPoolLowWatermark = 0.7
    33  	// defaultCheckBytesWrapperPoolHighWatermark is the default high watermark of the checked.Bytes wrapper pool.
    34  	defaultCheckBytesWrapperPoolHighWatermark = 1.0
    35  )
    36  
    37  type decodeOpts struct {
    38  	wrapperPool xpool.CheckedBytesWrapperPool
    39  	limits      TagSerializationLimits
    40  }
    41  
    42  // TagDecoderOptionsConfig allows for defaults to be set at initialization.
    43  type TagDecoderOptionsConfig struct {
    44  	CheckBytesWrapperPoolSize          *int     `yaml:"checkBytesWrapperPoolSize"`
    45  	CheckBytesWrapperPoolLowWatermark  *float64 `yaml:"checkBytesWrapperPoolLowWatermark"`
    46  	CheckBytesWrapperPoolHighWatermark *float64 `yaml:"checkBytesWrapperPoolHighWatermark"`
    47  }
    48  
    49  // CheckBytesWrapperPoolSizeOrDefault returns config value or default.
    50  func (c TagDecoderOptionsConfig) CheckBytesWrapperPoolSizeOrDefault() int {
    51  	if c.CheckBytesWrapperPoolSize == nil {
    52  		return defaultCheckBytesWrapperPoolSize
    53  	}
    54  	return *c.CheckBytesWrapperPoolSize
    55  }
    56  
    57  // CheckBytesWrapperPoolLowWatermarkOrDefault returns config value or default.
    58  func (c TagDecoderOptionsConfig) CheckBytesWrapperPoolLowWatermarkOrDefault() float64 {
    59  	if c.CheckBytesWrapperPoolLowWatermark == nil {
    60  		return defaultCheckBytesWrapperPoolLowWatermark
    61  	}
    62  	return *c.CheckBytesWrapperPoolLowWatermark
    63  }
    64  
    65  // CheckBytesWrapperPoolHighWatermarkOrDefault returns config value or default.
    66  func (c TagDecoderOptionsConfig) CheckBytesWrapperPoolHighWatermarkOrDefault() float64 {
    67  	if c.CheckBytesWrapperPoolHighWatermark == nil {
    68  		return defaultCheckBytesWrapperPoolHighWatermark
    69  	}
    70  	return *c.CheckBytesWrapperPoolHighWatermark
    71  }
    72  
    73  // NewTagDecoderOptions returns a new TagDecoderOptions.
    74  func NewTagDecoderOptions(cfg TagDecoderOptionsConfig) TagDecoderOptions {
    75  	pool := xpool.NewCheckedBytesWrapperPool(
    76  		pool.NewObjectPoolOptions().
    77  			SetSize(cfg.CheckBytesWrapperPoolSizeOrDefault()).
    78  			SetRefillLowWatermark(cfg.CheckBytesWrapperPoolLowWatermarkOrDefault()).
    79  			SetRefillHighWatermark(cfg.CheckBytesWrapperPoolHighWatermarkOrDefault()))
    80  	pool.Init()
    81  	return &decodeOpts{
    82  		wrapperPool: pool,
    83  		limits:      NewTagSerializationLimits(),
    84  	}
    85  }
    86  
    87  func (o *decodeOpts) SetCheckedBytesWrapperPool(v xpool.CheckedBytesWrapperPool) TagDecoderOptions {
    88  	opts := *o
    89  	opts.wrapperPool = v
    90  	return &opts
    91  }
    92  
    93  func (o *decodeOpts) CheckedBytesWrapperPool() xpool.CheckedBytesWrapperPool {
    94  	return o.wrapperPool
    95  }
    96  
    97  func (o *decodeOpts) SetTagSerializationLimits(v TagSerializationLimits) TagDecoderOptions {
    98  	opts := *o
    99  	opts.limits = v
   100  	return &opts
   101  }
   102  
   103  func (o *decodeOpts) TagSerializationLimits() TagSerializationLimits {
   104  	return o.limits
   105  }