github.com/m3db/m3@v1.5.0/src/cluster/kv/op.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 kv
    22  
    23  import "github.com/golang/protobuf/proto"
    24  
    25  type condition struct {
    26  	targetType  TargetType
    27  	compareType CompareType
    28  	key         string
    29  	value       interface{}
    30  }
    31  
    32  // NewCondition returns a new Condition
    33  func NewCondition() Condition { return condition{} }
    34  
    35  func (c condition) TargetType() TargetType                 { return c.targetType }
    36  func (c condition) CompareType() CompareType               { return c.compareType }
    37  func (c condition) Key() string                            { return c.key }
    38  func (c condition) Value() interface{}                     { return c.value }
    39  func (c condition) SetTargetType(t TargetType) Condition   { c.targetType = t; return c }
    40  func (c condition) SetCompareType(t CompareType) Condition { c.compareType = t; return c }
    41  func (c condition) SetKey(key string) Condition            { c.key = key; return c }
    42  func (c condition) SetValue(value interface{}) Condition   { c.value = value; return c }
    43  
    44  type opBase struct {
    45  	ot  OpType
    46  	key string
    47  }
    48  
    49  // nolint: unparam
    50  func newOpBase(t OpType, key string) opBase { return opBase{ot: t, key: key} }
    51  
    52  func (r opBase) Type() OpType         { return r.ot }
    53  func (r opBase) Key() string          { return r.key }
    54  func (r opBase) SetType(t OpType) Op  { r.ot = t; return r }
    55  func (r opBase) SetKey(key string) Op { r.key = key; return r }
    56  
    57  // SetOp is a Op with OpType Set
    58  type SetOp struct {
    59  	opBase
    60  
    61  	Value proto.Message
    62  }
    63  
    64  // NewSetOp returns a SetOp
    65  func NewSetOp(key string, value proto.Message) SetOp {
    66  	return SetOp{opBase: newOpBase(OpSet, key), Value: value}
    67  }
    68  
    69  type opResponse struct {
    70  	Op
    71  
    72  	value interface{}
    73  }
    74  
    75  // NewOpResponse creates a new OpResponse
    76  func NewOpResponse(op Op) OpResponse {
    77  	return opResponse{Op: op}
    78  }
    79  
    80  func (r opResponse) Value() interface{}                { return r.value }
    81  func (r opResponse) SetValue(v interface{}) OpResponse { r.value = v; return r }
    82  
    83  type response struct {
    84  	opr []OpResponse
    85  }
    86  
    87  // NewResponse creates a new transaction Response
    88  func NewResponse() Response { return response{} }
    89  
    90  func (r response) Responses() []OpResponse                 { return r.opr }
    91  func (r response) SetResponses(oprs []OpResponse) Response { r.opr = oprs; return r }