github.com/m3db/m3@v1.5.0/src/cluster/kv/types.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 kv
    22  
    23  import (
    24  	"errors"
    25  
    26  	"github.com/golang/protobuf/proto"
    27  )
    28  
    29  const (
    30  	// UninitializedVersion is the version of an uninitialized kv value.
    31  	UninitializedVersion = 0
    32  )
    33  
    34  var (
    35  	// ErrVersionMismatch is returned when attempting a CheckAndSet and the
    36  	// key is not at the provided version
    37  	ErrVersionMismatch = errors.New("key is not at the specified version")
    38  
    39  	// ErrAlreadyExists is returned when attempting a SetIfEmpty and the key
    40  	// already has a value
    41  	ErrAlreadyExists = errors.New("key already has a value")
    42  
    43  	// ErrNotFound is returned when attempting a Get but no value is found for
    44  	// the given key
    45  	ErrNotFound = errors.New("key not found")
    46  
    47  	// ErrUnknownTargetType is returned when an unknown TargetType is requested
    48  	ErrUnknownTargetType = errors.New("unknown target type")
    49  
    50  	// ErrUnknownCompareType is returned when an unknown CompareType is requested
    51  	ErrUnknownCompareType = errors.New("unknown compare type")
    52  
    53  	// ErrUnknownOpType is returned when an unknown OpType is requested
    54  	ErrUnknownOpType = errors.New("unknown op type")
    55  
    56  	// ErrConditionCheckFailed is returned when condition check failed
    57  	ErrConditionCheckFailed = errors.New("condition check failed")
    58  )
    59  
    60  // A Value provides access to a versioned value in the configuration store
    61  type Value interface {
    62  	// Unmarshal retrieves the stored value
    63  	Unmarshal(v proto.Message) error
    64  
    65  	// Version returns the current version of the value
    66  	Version() int
    67  
    68  	// IsNewer returns if this Value is newer than the other Value
    69  	IsNewer(other Value) bool
    70  }
    71  
    72  // ValueWatch provides updates to a Value
    73  type ValueWatch interface {
    74  	// C returns the notification channel
    75  	C() <-chan struct{}
    76  	// Get returns the latest version of the value
    77  	Get() Value
    78  	// Close stops watching for value updates
    79  	Close()
    80  }
    81  
    82  // ValueWatchable can be watched for Value changes
    83  type ValueWatchable interface {
    84  	// Get returns the latest Value
    85  	Get() Value
    86  	// Watch returns the Value and a ValueWatch that will be notified on updates
    87  	Watch() (Value, ValueWatch, error)
    88  	// NumWatches returns the number of watches on the Watchable
    89  	NumWatches() int
    90  	// Update sets the Value and notify Watches
    91  	Update(Value) error
    92  	// IsClosed returns true if the Watchable is closed
    93  	IsClosed() bool
    94  	// Close stops watching for value updates
    95  	Close()
    96  }
    97  
    98  // OverrideOptions provides a set of options to override the default configurations of a KV store.
    99  type OverrideOptions interface {
   100  	// Zone returns the zone of the KV store.
   101  	Zone() string
   102  
   103  	// SetZone sets the zone of the KV store.
   104  	SetZone(value string) OverrideOptions
   105  
   106  	// Namespace returns the namespace of the KV store.
   107  	Namespace() string
   108  
   109  	// SetNamespace sets the namespace of the KV store.
   110  	SetNamespace(namespace string) OverrideOptions
   111  
   112  	// Environment returns the environment of the KV store.
   113  	Environment() string
   114  
   115  	// SetEnvironment sets the environment of the KV store.
   116  	SetEnvironment(env string) OverrideOptions
   117  
   118  	// Validate validates the Options.
   119  	Validate() error
   120  }
   121  
   122  // Store provides access to the configuration store
   123  type Store interface {
   124  	// Get retrieves the value for the given key
   125  	Get(key string) (Value, error)
   126  
   127  	// Watch adds a watch for value updates for given key. This is a non-blocking
   128  	// call - a notification will be sent to ValueWatch.C() once a value is
   129  	// available
   130  	Watch(key string) (ValueWatch, error)
   131  
   132  	// Set stores the value for the given key
   133  	Set(key string, v proto.Message) (int, error)
   134  
   135  	// SetIfNotExists sets the value for the given key only if no value already
   136  	// exists
   137  	SetIfNotExists(key string, v proto.Message) (int, error)
   138  
   139  	// CheckAndSet stores the value for the given key if the current version
   140  	// matches the provided version
   141  	CheckAndSet(key string, version int, v proto.Message) (int, error)
   142  
   143  	// Delete deletes a key in the store and returns the last value before deletion
   144  	Delete(key string) (Value, error)
   145  
   146  	// History returns the value for a key in version range [from, to)
   147  	History(key string, from, to int) ([]Value, error)
   148  }
   149  
   150  // TargetType is the type of the comparison target in the condition
   151  type TargetType int
   152  
   153  // list of supported TargetTypes
   154  const (
   155  	TargetVersion TargetType = iota
   156  )
   157  
   158  // CompareType is the type of the comparison in the condition
   159  type CompareType string
   160  
   161  func (t CompareType) String() string {
   162  	return string(t)
   163  }
   164  
   165  // list of supported CompareType
   166  const (
   167  	CompareEqual CompareType = "="
   168  )
   169  
   170  // Condition defines the prerequisite for a transaction
   171  type Condition interface {
   172  	// TargetType returns the type of the TargetType
   173  	TargetType() TargetType
   174  	// SetTargetType sets the type of the TargetType
   175  	SetTargetType(t TargetType) Condition
   176  
   177  	// CompareType returns the type of the CompareType
   178  	CompareType() CompareType
   179  	// SetCompareType sets the type of the CompareType
   180  	SetCompareType(t CompareType) Condition
   181  
   182  	// Key returns the key in the condition
   183  	Key() string
   184  	// SetKey sets the key in the condition
   185  	SetKey(key string) Condition
   186  
   187  	// Value returns the value for comparison
   188  	Value() interface{}
   189  	// SetValue sets the value for comparison
   190  	SetValue(value interface{}) Condition
   191  }
   192  
   193  // OpType is the type of the operation
   194  type OpType int
   195  
   196  // list of supported OpTypes
   197  const (
   198  	OpSet OpType = iota
   199  )
   200  
   201  // Op is the operation to be performed in a transaction
   202  type Op interface {
   203  	// Type returns the type of the operation
   204  	Type() OpType
   205  	// SetType sets the type of the operation
   206  	SetType(ot OpType) Op
   207  
   208  	// Key returns the key used in the operation
   209  	Key() string
   210  	// SetKey sets the key in the operation
   211  	SetKey(key string) Op
   212  }
   213  
   214  // OpResponse is the response of a transaction operation
   215  type OpResponse interface {
   216  	Op
   217  
   218  	Value() interface{}
   219  	SetValue(v interface{}) OpResponse
   220  }
   221  
   222  // Response captures the response of the transaction
   223  type Response interface {
   224  	Responses() []OpResponse
   225  	SetResponses(oprs []OpResponse) Response
   226  }
   227  
   228  // TxnStore supports transactions on top of Store interface
   229  type TxnStore interface {
   230  	Store
   231  
   232  	Commit([]Condition, []Op) (Response, error)
   233  }