kv

package
v1.1.19 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotSupported is returned when a method is not implemented/supported by the current backend
	ErrNotSupported = errors.New("not supported")
	// ErrKeyModified is returned during an atomic operation if the index does not match the one in the store
	ErrKeyModified = errors.New("key was modified")
	// ErrKeyNotFound is returned when the key is not found in the store during a Get operation
	ErrKeyNotFound = errors.New("key not found")
	// ErrKeyExists is returned when the previous value exists in the case of an AtomicPut
	ErrKeyExists = errors.New("key exists")
	// ErrUnableToLock is returned when there is an error when acquiring a lock on a key
	ErrUnableToLock = errors.New("failed to acquire the lock")
)

Functions

func CreateEndpoints

func CreateEndpoints(addrs []string, scheme string) (entries []string)

CreateEndpoints creates a list of endpoints given the right scheme

func GetDirectory

func GetDirectory(key string) string

GetDirectory gets the full directory part of the key to the form:

/path/to/

func NewKeyMaker added in v1.1.19

func NewKeyMaker(separator string, pkgName string, tags ...string) keyMaker

NewKey given separator, package name, and tags to generate the key

Example
keyMaker := NewKeyMaker("/", "package", "tag1", "tag2")
fmt.Println(keyMaker("golang"))
Output:
package/tag1/tag2/golang

func Normalize

func Normalize(key string) string

Normalize the key for each store to the form:

/path/to/key

func SplitKey

func SplitKey(key string) (path []string)

SplitKey splits the key to extract path informations

Types

type KeyValue

type KeyValue struct {
	Key      string
	Value    []byte
	Revision uint64
}

KeyValue represents {Key, Value, Revision} tuple

type LockOption

type LockOption func(*LockOptions)

func LockExpiration

func LockExpiration(t time.Duration) LockOption

func LockValue

func LockValue(value []byte) LockOption

func RenewLock

func RenewLock(r chan struct{}) LockOption

type LockOptions

type LockOptions struct {
	Value     []byte        // Optional, value to associate with the lock
	TTL       time.Duration // Optional, expiration time associated with the lock
	RenewLock chan struct{} // Optional, chan used to control and stop the session ttl renewal for the lock
}

type Locker

type Locker interface {
	Lock(stopChan chan struct{}) (<-chan struct{}, error)
	Unlock() error
}

Locker provides locking mechanism on top of the backend.

type PutOption

type PutOption func(*PutOptions)

func IsPrefix

func IsPrefix(isPrefix bool) PutOption

func PutExpiration

func PutExpiration(t time.Duration) PutOption

type PutOptions

type PutOptions struct {
	IsPrefix bool          // Optional, indicate if the given key is a prefix
	TTL      time.Duration // Optional, expiration time associated with the key
}

type Store

type Store interface {
	// Put a value with the specified key
	Put(key string, value []byte, opts ...PutOption) error

	// AtomicPut puts a single value and gets previous one if exists.
	// Pass previous = nil to create a new key.
	AtomicPut(key string, value []byte, expected *KeyValue, opts ...PutOption) (*KeyValue, error)

	// Get a value with given key
	Get(key string) (*KeyValue, error)

	// List the content with given prefix
	List(prefix string) ([]*KeyValue, error)

	// Delete the value with the specified key
	Delete(key string) error

	// Atomic delete of a single value
	AtomicDelete(key string, expected *KeyValue) (bool, error)

	// DeleteTree deletes a range of keys under a given prefix
	DeleteTree(prefix string) error

	// Exists checks if a key exists in the backend
	Exists(key string) (bool, error)

	// Watch for changes on a key
	Watch(key string, stopCh <-chan struct{}) (<-chan *KeyValue, error)

	// WatchTree watches for changes on child nodes under a given prefix
	WatchTree(prefix string, stopCh <-chan struct{}) (<-chan []*KeyValue, error)

	// Lock locks the given key.
	// The returned Locker is not held and must be acquired
	Lock(key string, opts ...LockOption) (Locker, error)

	// Close the connection
	Close()
}

Store is the interface to access the backend

func New

func New() Store

New creates in-memory key-value store

Directories

Path Synopsis
Code generated by mockery v1.0.0
Code generated by mockery v1.0.0

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL