github.com/m3db/m3@v1.5.0/src/cluster/kv/store.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  	xwatch "github.com/m3db/m3/src/x/watch"
    27  )
    28  
    29  var (
    30  	errEmptyZone        = errors.New("empty kv zone")
    31  	errEmptyEnvironment = errors.New("empty kv environment")
    32  	errEmptyNamespace   = errors.New("empty kv namespace")
    33  )
    34  
    35  type valueWatch struct {
    36  	w xwatch.Watch
    37  }
    38  
    39  // newValueWatch creates a new ValueWatch
    40  func newValueWatch(w xwatch.Watch) ValueWatch {
    41  	return &valueWatch{w: w}
    42  }
    43  
    44  func (v *valueWatch) Close() {
    45  	v.w.Close()
    46  }
    47  
    48  func (v *valueWatch) C() <-chan struct{} {
    49  	return v.w.C()
    50  }
    51  
    52  func (v *valueWatch) Get() Value {
    53  	return valueFromWatch(v.w.Get())
    54  }
    55  
    56  type valueWatchable struct {
    57  	w xwatch.Watchable
    58  }
    59  
    60  // NewValueWatchable creates a new ValueWatchable
    61  func NewValueWatchable() ValueWatchable {
    62  	return &valueWatchable{w: xwatch.NewWatchable()}
    63  }
    64  
    65  func (w *valueWatchable) IsClosed() bool {
    66  	return w.w.IsClosed()
    67  }
    68  
    69  func (w *valueWatchable) Close() {
    70  	w.w.Close()
    71  }
    72  
    73  func (w *valueWatchable) Get() Value {
    74  	return valueFromWatch(w.w.Get())
    75  }
    76  
    77  func (w *valueWatchable) Watch() (Value, ValueWatch, error) {
    78  	value, watch, err := w.w.Watch()
    79  	if err != nil {
    80  		return nil, nil, err
    81  	}
    82  
    83  	return valueFromWatch(value), newValueWatch(watch), nil
    84  }
    85  
    86  func (w *valueWatchable) NumWatches() int {
    87  	return w.w.NumWatches()
    88  }
    89  
    90  func (w *valueWatchable) Update(v Value) error {
    91  	return w.w.Update(v)
    92  }
    93  
    94  func valueFromWatch(value interface{}) Value {
    95  	if value != nil {
    96  		return value.(Value)
    97  	}
    98  
    99  	return nil
   100  }
   101  
   102  type overrideOptions struct {
   103  	zone      string
   104  	env       string
   105  	namespace string
   106  }
   107  
   108  // NewOverrideOptions creates a new kv Options.
   109  func NewOverrideOptions() OverrideOptions {
   110  	return overrideOptions{}
   111  }
   112  
   113  func (opts overrideOptions) Zone() string {
   114  	return opts.zone
   115  }
   116  
   117  func (opts overrideOptions) SetZone(value string) OverrideOptions {
   118  	opts.zone = value
   119  	return opts
   120  }
   121  
   122  func (opts overrideOptions) Environment() string {
   123  	return opts.env
   124  }
   125  
   126  func (opts overrideOptions) SetEnvironment(env string) OverrideOptions {
   127  	opts.env = env
   128  	return opts
   129  }
   130  
   131  func (opts overrideOptions) Namespace() string {
   132  	return opts.namespace
   133  }
   134  
   135  func (opts overrideOptions) SetNamespace(namespace string) OverrideOptions {
   136  	opts.namespace = namespace
   137  	return opts
   138  }
   139  
   140  func (opts overrideOptions) Validate() error {
   141  	if opts.zone == "" {
   142  		return errEmptyZone
   143  	}
   144  	if opts.env == "" {
   145  		return errEmptyEnvironment
   146  	}
   147  	if opts.namespace == "" {
   148  		return errEmptyNamespace
   149  	}
   150  	return nil
   151  }