github.com/m3db/m3@v1.5.0/src/m3ninx/index/segment/mem/options.go (about)

     1  // Copyright (c) 2017 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 mem
    22  
    23  import (
    24  	"github.com/m3db/m3/src/m3ninx/postings"
    25  	"github.com/m3db/m3/src/m3ninx/postings/roaring"
    26  	"github.com/m3db/m3/src/m3ninx/util"
    27  	"github.com/m3db/m3/src/m3ninx/x/bytes"
    28  	"github.com/m3db/m3/src/x/instrument"
    29  	"github.com/m3db/m3/src/x/pool"
    30  )
    31  
    32  const (
    33  	defaultInitialCapacity        = 1024
    34  	defaultBytesArrayPoolCapacity = 1024
    35  	// This pool is used in a single-threaded manner.
    36  	defaultBytesArrayPoolSize = 1
    37  	// 2<<23 * 16 bytes (byte slice pointer) * 2 (Golang G.C) ~=
    38  	// 0.5 GiB max memory usage.
    39  	defaultBytesArrayPoolMaxArrayCapacity = 2 << 23
    40  )
    41  
    42  // Options is a collection of knobs for an in-memory segment.
    43  type Options interface {
    44  	// SetInstrumentOptions sets the instrument options.
    45  	SetInstrumentOptions(value instrument.Options) Options
    46  
    47  	// InstrumentOptions returns the instrument options.
    48  	InstrumentOptions() instrument.Options
    49  
    50  	// SetBytesSliceArrayPool sets the bytes slice array pool.
    51  	SetBytesSliceArrayPool(value bytes.SliceArrayPool) Options
    52  
    53  	// BytesSliceArrayPool returns the bytes slice array pool.
    54  	BytesSliceArrayPool() bytes.SliceArrayPool
    55  
    56  	// SetPostingsListPool sets the postings list pool.
    57  	SetPostingsListPool(value postings.Pool) Options
    58  
    59  	// PostingsListPool returns the postings list pool.
    60  	PostingsListPool() postings.Pool
    61  
    62  	// SetInitialCapacity sets the initial capacity.
    63  	SetInitialCapacity(value int) Options
    64  
    65  	// InitialCapacity returns the initial capacity.
    66  	InitialCapacity() int
    67  
    68  	// SetNewUUIDFn sets the function used to generate new UUIDs.
    69  	SetNewUUIDFn(value util.NewUUIDFn) Options
    70  
    71  	// NewUUIDFn returns the function used to generate new UUIDs.
    72  	NewUUIDFn() util.NewUUIDFn
    73  }
    74  
    75  type opts struct {
    76  	iopts             instrument.Options
    77  	bytesSliceArrPool bytes.SliceArrayPool
    78  	postingsPool      postings.Pool
    79  	initialCapacity   int
    80  	newUUIDFn         util.NewUUIDFn
    81  }
    82  
    83  // NewOptions returns new options.
    84  func NewOptions() Options {
    85  	arrPool := bytes.NewSliceArrayPool(bytes.SliceArrayPoolOpts{
    86  		Capacity:    defaultBytesArrayPoolCapacity,
    87  		MaxCapacity: defaultBytesArrayPoolMaxArrayCapacity,
    88  		Options: pool.NewObjectPoolOptions().
    89  			SetSize(defaultBytesArrayPoolSize).
    90  			SetRefillLowWatermark(0).
    91  			SetRefillHighWatermark(0),
    92  	})
    93  	arrPool.Init()
    94  	return &opts{
    95  		iopts:             instrument.NewOptions(),
    96  		bytesSliceArrPool: arrPool,
    97  		postingsPool:      postings.NewPool(nil, roaring.NewPostingsList),
    98  		initialCapacity:   defaultInitialCapacity,
    99  		newUUIDFn:         util.NewUUID,
   100  	}
   101  }
   102  
   103  func (o *opts) SetInstrumentOptions(v instrument.Options) Options {
   104  	opts := *o
   105  	opts.iopts = v
   106  	return &opts
   107  }
   108  
   109  func (o *opts) InstrumentOptions() instrument.Options {
   110  	return o.iopts
   111  }
   112  
   113  func (o *opts) SetBytesSliceArrayPool(value bytes.SliceArrayPool) Options {
   114  	opts := *o
   115  	opts.bytesSliceArrPool = value
   116  	return &opts
   117  }
   118  
   119  func (o *opts) BytesSliceArrayPool() bytes.SliceArrayPool {
   120  	return o.bytesSliceArrPool
   121  }
   122  
   123  func (o *opts) SetPostingsListPool(v postings.Pool) Options {
   124  	opts := *o
   125  	opts.postingsPool = v
   126  	return &opts
   127  }
   128  
   129  func (o *opts) PostingsListPool() postings.Pool {
   130  	return o.postingsPool
   131  }
   132  
   133  func (o *opts) SetInitialCapacity(v int) Options {
   134  	opts := *o
   135  	opts.initialCapacity = v
   136  	return &opts
   137  }
   138  
   139  func (o *opts) InitialCapacity() int {
   140  	return o.initialCapacity
   141  }
   142  
   143  func (o *opts) SetNewUUIDFn(v util.NewUUIDFn) Options {
   144  	opts := *o
   145  	opts.newUUIDFn = v
   146  	return &opts
   147  }
   148  
   149  func (o *opts) NewUUIDFn() util.NewUUIDFn {
   150  	return o.newUUIDFn
   151  }