github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/msg/topic/service.go (about)

     1  // Copyright (c) 2018 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 topic
    22  
    23  import (
    24  	"errors"
    25  
    26  	"github.com/m3db/m3/src/cluster/kv"
    27  )
    28  
    29  var (
    30  	defaultNamespace     = "/topic"
    31  	errTopicNotAvailable = errors.New("topic is not available")
    32  )
    33  
    34  type service struct {
    35  	store kv.Store
    36  }
    37  
    38  // NewService creates a topic service.
    39  func NewService(sOpts ServiceOptions) (Service, error) {
    40  	kvOpts := sanitizeKVOptions(sOpts.KVOverrideOptions())
    41  	store, err := sOpts.ConfigService().Store(kvOpts)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	return &service{
    46  		store: store,
    47  	}, nil
    48  }
    49  
    50  func (s *service) Get(name string) (Topic, error) {
    51  	value, err := s.store.Get(key(name))
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	t, err := NewTopicFromValue(value)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	return t, nil
    60  }
    61  
    62  func (s *service) CheckAndSet(t Topic, version int) (Topic, error) {
    63  	if err := t.Validate(); err != nil {
    64  		return nil, err
    65  	}
    66  	pb, err := ToProto(t)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	version, err = s.store.CheckAndSet(key(t.Name()), version, pb)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  	return t.SetVersion(version), nil
    75  }
    76  
    77  func (s *service) Delete(name string) error {
    78  	_, err := s.store.Delete(key(name))
    79  	return err
    80  }
    81  
    82  func (s *service) Watch(name string) (Watch, error) {
    83  	w, err := s.store.Watch(key(name))
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  	return NewWatch(w), nil
    88  }
    89  
    90  func key(name string) string {
    91  	return name
    92  }
    93  
    94  func sanitizeKVOptions(opts kv.OverrideOptions) kv.OverrideOptions {
    95  	if opts.Namespace() == "" {
    96  		opts = opts.SetNamespace(defaultNamespace)
    97  	}
    98  	return opts
    99  }
   100  
   101  // NewWatch creates a new topic watch.
   102  func NewWatch(w kv.ValueWatch) Watch {
   103  	return &watch{w}
   104  }
   105  
   106  type watch struct {
   107  	kv.ValueWatch
   108  }
   109  
   110  func (w *watch) Get() (Topic, error) {
   111  	value := w.ValueWatch.Get()
   112  	if value == nil {
   113  		return nil, errTopicNotAvailable
   114  	}
   115  	t, err := NewTopicFromValue(value)
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  	return t, nil
   120  }