kv

package
v0.0.0-...-9742f5a Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2020 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package kv implements a simple key/value store with backing drivers. Each storage system can operate on value types in an independent way, allowing for a variety of schemas to be represented.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnmarshal is returned when unmarshaling to a native type failed.
	ErrUnmarshal = errors.New("unmarshaling failed")
	// ErrMarshal is returned when marshaling from a native type failed.
	ErrMarshal = errors.New("marshaling failed")
)
View Source
var (
	// ErrAlreadySet refers to create operations that would overwrite an intended value.
	ErrAlreadySet = errors.New("value is already set")
	// ErrInvalidType covers types that are invalid for exchange with this library.
	ErrInvalidType = errors.New("invalid type in k/v operation")
	// ErrUnsetValue is returned when nil is sent because the value is unset.
	ErrUnsetValue = errors.New("unset value")
	// ErrNotEqual indicates that a CAS operation failed its compare.
	ErrNotEqual = errors.New("original value is not equal")
)

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is the typical end-user entry into the k/v system. This allows for the standard operations on drivers.

func NewClient

func NewClient(member string, driver Driver) *Client

NewClient creates a new client.

func (*Client) CASBytes

func (c *Client) CASBytes(ctx context.Context, key string, nonce, origValue, value []byte) ([]byte, error)

CASBytes implements compare-and-swap semantics with marshaled data.

func (*Client) CASFloat64

func (c *Client) CASFloat64(ctx context.Context, key string, nonce []byte, origValue, value float64) ([]byte, error)

CASFloat64 compares and swaps float64s.

func (*Client) CASInt64

func (c *Client) CASInt64(ctx context.Context, key string, nonce []byte, origValue, value int64) ([]byte, error)

CASInt64 compares and swaps uint64 values.

func (*Client) CASString

func (c *Client) CASString(ctx context.Context, key string, nonce []byte, origValue, value string) ([]byte, error)

CASString compares and swaps strings.

func (*Client) CASUint64

func (c *Client) CASUint64(ctx context.Context, key string, nonce []byte, origValue, value uint64) ([]byte, error)

CASUint64 compares and swaps uint64 values.

func (*Client) CreateBytes

func (c *Client) CreateBytes(ctx context.Context, key string, value []byte) ([]byte, error)

CreateBytes creates a key for the k/v store as raw bytes.

func (*Client) CreateFloat64

func (c *Client) CreateFloat64(ctx context.Context, key string, out float64) ([]byte, error)

CreateFloat64 creates a float64 where there wasn't one before.

func (*Client) CreateInt64

func (c *Client) CreateInt64(ctx context.Context, key string, out int64) ([]byte, error)

CreateInt64 creates a new key as int64.

func (*Client) CreateString

func (c *Client) CreateString(ctx context.Context, key, value string) ([]byte, error)

CreateString creates a string where there wasn't one before.

func (*Client) CreateUint64

func (c *Client) CreateUint64(ctx context.Context, key string, out uint64) ([]byte, error)

CreateUint64 creates a new key as uint64.

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, key string, nonce []byte) error

Delete deletes any key, regardless of type; pass a nonce to delete conditionally.

func (*Client) GetBytes

func (c *Client) GetBytes(ctx context.Context, key string) ([]byte, []byte, error)

GetBytes retrieves a key for the k/v store as marshaled data. The `out` argument must be a non-nil byte array.

func (*Client) GetFloat64

func (c *Client) GetFloat64(ctx context.Context, key string) (float64, []byte, error)

GetFloat64 retrieves the marshaled data for key and then converts it to float64.

func (*Client) GetInt64

func (c *Client) GetInt64(ctx context.Context, key string) (int64, []byte, error)

GetInt64 retrieves the marshaled data for key and then converts it to int64.

func (*Client) GetString

func (c *Client) GetString(ctx context.Context, key string) (string, []byte, error)

GetString retrieves the marshaled data for key and then converts it to string.

func (*Client) GetUint64

func (c *Client) GetUint64(ctx context.Context, key string) (uint64, []byte, error)

GetUint64 retrieves the marshaled data for key and then converts it to uint64.

func (*Client) SetBytes

func (c *Client) SetBytes(ctx context.Context, key string, value []byte) ([]byte, error)

SetBytes sets a value explicitly from marshaled data, with no checking.

func (*Client) SetFloat64

func (c *Client) SetFloat64(ctx context.Context, key string, value float64) ([]byte, error)

SetFloat64 sets a float64 to a key.

func (*Client) SetInt64

func (c *Client) SetInt64(ctx context.Context, key string, out int64) ([]byte, error)

SetInt64 sets a int64 to key.

func (*Client) SetString

func (c *Client) SetString(ctx context.Context, key, value string) ([]byte, error)

SetString sets a string to a key.

func (*Client) SetUint64

func (c *Client) SetUint64(ctx context.Context, key string, out uint64) ([]byte, error)

SetUint64 sets a uint64 to key.

type DBDriver

type DBDriver struct {
	// contains filtered or unexported fields
}

DBDriver is a database driver.

func (*DBDriver) CAS

func (db *DBDriver) CAS(ctx context.Context, member, key string, nonce, origValue, value []byte) ([]byte, error)

CAS implements a compare-and-swap operation.

func (*DBDriver) Create

func (db *DBDriver) Create(ctx context.Context, member, key string, value []byte) ([]byte, error)

Create creates a key from scratch.

func (*DBDriver) Delete

func (db *DBDriver) Delete(ctx context.Context, member, key string, nonce []byte) error

Delete removes a key. If a nonce is provided, it will be checked.

func (*DBDriver) Get

func (db *DBDriver) Get(ctx context.Context, member, key string) ([]byte, []byte, error)

Get retrieves an item from the store. Users must pass a pointer to the out argument so it can be filled by json.Marshal.

func (*DBDriver) Set

func (db *DBDriver) Set(ctx context.Context, member, key string, value []byte) ([]byte, error)

Set sets a value in the store

type Driver

type Driver interface {
	Create(context.Context, string, string, []byte) ([]byte, error)
	Delete(context.Context, string, string, []byte) error
	Get(context.Context, string, string) ([]byte, []byte, error)
	Set(context.Context, string, string, []byte) ([]byte, error)
	CAS(context.Context, string, string, []byte, []byte, []byte) ([]byte, error)
}

Driver describes the driver implementation of a k/v store's operations.

func NewDBDriver

func NewDBDriver(log *logrus.Logger) Driver

NewDBDriver creates a new DBDriver from a db handle.

func NewMemoryDriver

func NewMemoryDriver(preinit MemoryTable) Driver

NewMemoryDriver returns a Driver that is basically a locked map. You can provide a series of initial values or just pass nil to get an empty set.

type MemoryDriver

type MemoryDriver struct {
	// contains filtered or unexported fields
}

MemoryDriver implements a very basic key/value store over a map.

func (*MemoryDriver) CAS

func (md *MemoryDriver) CAS(ctx context.Context, member, key string, origNonce, origValue, value []byte) ([]byte, error)

CAS implements compare-and-swap for the k/v store.

func (*MemoryDriver) Create

func (md *MemoryDriver) Create(ctx context.Context, member, key string, value []byte) ([]byte, error)

Create creates a key

func (*MemoryDriver) Delete

func (md *MemoryDriver) Delete(ctx context.Context, member, key string, nonce []byte) error

Delete deletes a key with optional CAS nonce

func (*MemoryDriver) Get

func (md *MemoryDriver) Get(ctx context.Context, member, key string) ([]byte, []byte, error)

Get retrieves a value from the k/v store.

func (*MemoryDriver) Set

func (md *MemoryDriver) Set(ctx context.Context, member, key string, value []byte) ([]byte, error)

Set sets the value in the k/v store.

type MemoryTable

type MemoryTable map[string]map[string]casvalue

MemoryTable is the underlying data structure used to manage memory in the MemoryDriver.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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