github.com/zhyoulun/cilium@v1.6.12/pkg/kvstore/kvstore.go (about)

     1  // Copyright 2016-2020 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package kvstore
    16  
    17  import (
    18  	"context"
    19  )
    20  
    21  // Value is an abstraction of the data stored in the kvstore as well as the
    22  // mod revision of that data.
    23  type Value struct {
    24  	Data        []byte
    25  	ModRevision uint64
    26  	LeaseID     int64
    27  	SessionID   string
    28  }
    29  
    30  // KeyValuePairs is a map of key=value pairs
    31  type KeyValuePairs map[string]Value
    32  
    33  // Capabilities is a bitmask to indicate the capabilities of a backend
    34  type Capabilities uint32
    35  
    36  const (
    37  	// CapabilityCreateIfExists is true if CreateIfExists is functional
    38  	CapabilityCreateIfExists Capabilities = 1 << 0
    39  
    40  	// CapabilityDeleteOnZeroCount is true if DeleteOnZeroCount is functional
    41  	CapabilityDeleteOnZeroCount Capabilities = 1 << 1
    42  
    43  	// BaseKeyPrefix is the base prefix that should be used for all keys
    44  	BaseKeyPrefix = "cilium"
    45  
    46  	// InitLockPath is the path to the init lock to test quorum
    47  	InitLockPath = BaseKeyPrefix + "/.initlock"
    48  )
    49  
    50  // Get returns value of key
    51  func Get(key string) ([]byte, error) {
    52  	v, err := Client().Get(key)
    53  	return v, err
    54  }
    55  
    56  // GetIfLocked returns value of key if the client is still holding the given lock.
    57  func GetIfLocked(key string, lock KVLocker) ([]byte, error) {
    58  	v, err := Client().GetIfLocked(key, lock)
    59  	return v, err
    60  }
    61  
    62  // GetPrefix returns the first key which matches the prefix and its value.
    63  func GetPrefix(ctx context.Context, prefix string) (k string, v []byte, err error) {
    64  	k, v, err = Client().GetPrefix(ctx, prefix)
    65  	return
    66  }
    67  
    68  // GetPrefixIfLocked returns the first key which matches the prefix and its value if the client is still holding the given lock.
    69  func GetPrefixIfLocked(ctx context.Context, prefix string, lock KVLocker) (k string, v []byte, err error) {
    70  	k, v, err = Client().GetPrefixIfLocked(ctx, prefix, lock)
    71  	return
    72  }
    73  
    74  // ListPrefix returns the list of keys matching the prefix
    75  func ListPrefix(prefix string) (KeyValuePairs, error) {
    76  	v, err := Client().ListPrefix(prefix)
    77  	return v, err
    78  }
    79  
    80  // ListPrefixIfLocked  returns a list of keys matching the prefix only if the client is still holding the given lock.
    81  func ListPrefixIfLocked(prefix string, lock KVLocker) (KeyValuePairs, error) {
    82  	v, err := Client().ListPrefixIfLocked(prefix, lock)
    83  	return v, err
    84  }
    85  
    86  // CreateOnly atomically creates a key or fails if it already exists
    87  func CreateOnly(ctx context.Context, key string, value []byte, lease bool) (bool, error) {
    88  	success, err := Client().CreateOnly(ctx, key, value, lease)
    89  	return success, err
    90  }
    91  
    92  // CreateOnlyIfLocked atomically creates a key if the client is still holding the given lock or fails if it already exists
    93  func CreateOnlyIfLocked(ctx context.Context, key string, value []byte, lease bool, lock KVLocker) (bool, error) {
    94  	success, err := Client().CreateOnlyIfLocked(ctx, key, value, lease, lock)
    95  	return success, err
    96  }
    97  
    98  // Update creates or updates a key value pair
    99  func Update(ctx context.Context, key string, value []byte, lease bool) error {
   100  	err := Client().Update(ctx, key, value, lease)
   101  	return err
   102  }
   103  
   104  // UpdateIfDifferent updates a key if the value is different
   105  func UpdateIfDifferent(ctx context.Context, key string, value []byte, lease bool) (bool, error) {
   106  	recreated, err := Client().UpdateIfDifferent(ctx, key, value, lease)
   107  	return recreated, err
   108  }
   109  
   110  // UpdateIfDifferentIfLocked updates a key if the value is different and if the client is still holding the given lock.
   111  func UpdateIfDifferentIfLocked(ctx context.Context, key string, value []byte, lease bool, lock KVLocker) (bool, error) {
   112  	recreated, err := Client().UpdateIfDifferentIfLocked(ctx, key, value, lease, lock)
   113  	return recreated, err
   114  }
   115  
   116  // CreateIfExists creates a key with the value only if key condKey exists
   117  func CreateIfExists(condKey, key string, value []byte, lease bool) error {
   118  	err := Client().CreateIfExists(condKey, key, value, lease)
   119  	return err
   120  }
   121  
   122  // Set sets the value of a key
   123  func Set(key string, value []byte) error {
   124  	err := Client().Set(key, value)
   125  	return err
   126  }
   127  
   128  // Delete deletes a key
   129  func Delete(key string) error {
   130  	err := Client().Delete(key)
   131  	return err
   132  }
   133  
   134  // DeleteIfLocked deletes a key if the client is still holding the given lock.
   135  func DeleteIfLocked(key string, lock KVLocker) error {
   136  	err := Client().DeleteIfLocked(key, lock)
   137  	return err
   138  }
   139  
   140  // DeletePrefix deletes all keys matching a prefix
   141  func DeletePrefix(prefix string) error {
   142  	err := Client().DeletePrefix(prefix)
   143  	return err
   144  }
   145  
   146  // GetCapabilities returns the capabilities of the backend
   147  func GetCapabilities() Capabilities {
   148  	return Client().GetCapabilities()
   149  }
   150  
   151  // Encode encodes a binary slice into a character set that the backend supports
   152  func Encode(in []byte) string {
   153  	out := Client().Encode(in)
   154  	return out
   155  }
   156  
   157  // Decode decodes a key previously encoded back into the original binary slice
   158  func Decode(in string) ([]byte, error) {
   159  	out, err := Client().Decode(in)
   160  	return out, err
   161  }
   162  
   163  // Close closes the kvstore client
   164  func Close() {
   165  	defaultClient.Close()
   166  }