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

     1  // Copyright (c) 2016 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  	"math"
    25  
    26  	"github.com/m3db/m3/src/dbnode/generated/thrift/rpc"
    27  	"github.com/m3db/m3/src/x/ident"
    28  	"github.com/m3db/m3/src/x/pool"
    29  )
    30  
    31  var (
    32  	// NB(bl): use an invalid shardID for the zerod op
    33  	writeOperationZeroed = writeOperation{shardID: math.MaxUint32}
    34  )
    35  
    36  type writeOperation struct {
    37  	namespace    ident.ID
    38  	shardID      uint32
    39  	request      rpc.WriteBatchRawRequestElement
    40  	requestV2    rpc.WriteBatchRawV2RequestElement
    41  	datapoint    rpc.Datapoint
    42  	completionFn completionFn
    43  	pool         *writeOperationPool
    44  }
    45  
    46  func (w *writeOperation) reset() {
    47  	*w = writeOperationZeroed
    48  	w.request.Datapoint = &w.datapoint
    49  	w.requestV2.Datapoint = &w.datapoint
    50  }
    51  
    52  func (w *writeOperation) Close() {
    53  	p := w.pool
    54  	w.reset()
    55  	if p != nil {
    56  		p.Put(w)
    57  	}
    58  }
    59  
    60  func (w *writeOperation) Size() int {
    61  	// Writes always represent a single write
    62  	return 1
    63  }
    64  
    65  func (w *writeOperation) CompletionFn() completionFn {
    66  	return w.completionFn
    67  }
    68  
    69  func (w *writeOperation) SetCompletionFn(fn completionFn) {
    70  	w.completionFn = fn
    71  }
    72  
    73  func (w *writeOperation) ShardID() uint32 {
    74  	return w.shardID
    75  }
    76  
    77  type writeOperationPool struct {
    78  	pool pool.ObjectPool
    79  }
    80  
    81  func newWriteOperationPool(opts pool.ObjectPoolOptions) *writeOperationPool {
    82  	p := pool.NewObjectPool(opts)
    83  	return &writeOperationPool{pool: p}
    84  }
    85  
    86  func (p *writeOperationPool) Init() {
    87  	p.pool.Init(func() interface{} {
    88  		w := &writeOperation{}
    89  		w.reset()
    90  		return w
    91  	})
    92  }
    93  
    94  func (p *writeOperationPool) Get() *writeOperation {
    95  	w := p.pool.Get().(*writeOperation)
    96  	w.pool = p
    97  	return w
    98  }
    99  
   100  func (p *writeOperationPool) Put(w *writeOperation) {
   101  	w.reset()
   102  	p.pool.Put(w)
   103  }