github.com/m3db/m3@v1.5.0/src/dbnode/client/aggregate_op.go (about)

     1  // Copyright (c) 2019 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 client
    22  
    23  import (
    24  	"context"
    25  
    26  	"github.com/m3db/m3/src/dbnode/generated/thrift/rpc"
    27  	"github.com/m3db/m3/src/x/pool"
    28  )
    29  
    30  var (
    31  	aggregateOpRequestZeroed = rpc.AggregateQueryRawRequest{}
    32  )
    33  
    34  type aggregateOp struct {
    35  	refCounter
    36  	context      context.Context
    37  	request      rpc.AggregateQueryRawRequest
    38  	completionFn completionFn
    39  
    40  	pool aggregateOpPool
    41  }
    42  
    43  func (f *aggregateOp) Size() int                  { return 1 }
    44  func (f *aggregateOp) CompletionFn() completionFn { return f.completionFn }
    45  
    46  func (f *aggregateOp) update(
    47  	ctx context.Context,
    48  	req rpc.AggregateQueryRawRequest,
    49  	fn completionFn,
    50  ) {
    51  	f.context = ctx
    52  	f.request = req
    53  	f.completionFn = fn
    54  }
    55  
    56  func (f *aggregateOp) requestSeriesLimit(defaultValue int) int {
    57  	if f.request.SeriesLimit == nil {
    58  		return defaultValue
    59  	}
    60  	return int(*f.request.SeriesLimit)
    61  }
    62  
    63  func (f *aggregateOp) close() {
    64  	f.completionFn = nil
    65  	f.request = aggregateOpRequestZeroed
    66  	// return to pool
    67  	if f.pool == nil {
    68  		return
    69  	}
    70  	f.pool.Put(f)
    71  }
    72  
    73  func newAggregateOp(p aggregateOpPool) *aggregateOp {
    74  	f := &aggregateOp{pool: p}
    75  	f.destructorFn = f.close
    76  	return f
    77  }
    78  
    79  type aggregateOpPool interface {
    80  	Init()
    81  	Get() *aggregateOp
    82  	Put(*aggregateOp)
    83  }
    84  
    85  type aggregateOpPoolImpl struct {
    86  	pool pool.ObjectPool
    87  }
    88  
    89  func newAggregateOpPool(
    90  	opts pool.ObjectPoolOptions,
    91  ) aggregateOpPool {
    92  	p := pool.NewObjectPool(opts)
    93  	return &aggregateOpPoolImpl{pool: p}
    94  }
    95  
    96  func (p *aggregateOpPoolImpl) Init() {
    97  	p.pool.Init(func() interface{} {
    98  		return newAggregateOp(p)
    99  	})
   100  }
   101  
   102  func (p *aggregateOpPoolImpl) Get() *aggregateOp {
   103  	return p.pool.Get().(*aggregateOp)
   104  }
   105  
   106  func (p *aggregateOpPoolImpl) Put(f *aggregateOp) {
   107  	p.pool.Put(f)
   108  }