keyvaluestore

package module
v0.0.0-...-ba1b225 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2019 License: MIT Imports: 2 Imported by: 0

README

keyvaluestore

This package provides an interface that can be used to build robust applications on top of key-value stores. It supports profiling, caching, batching, eventual consistency, and transactions. It has implementations for Redis, which is ideal for dev environments, DynamoDB, which is ideal in production, and an in-memory store, which is ideal for tests.

Documentation

Index

Constants

View Source
const MaxAtomicWriteOperations = 10

DynamoDB can't do more than 10 operations in an atomic write. So all stores should enforce this limit.

Variables

This section is empty.

Functions

func ToString

func ToString(v interface{}) *string

Types

type AtomicWriteOperation

type AtomicWriteOperation interface {
	SetNX(key string, value interface{}) AtomicWriteResult
	CAS(key string, oldValue, newValue string) AtomicWriteResult
	Delete(key string) AtomicWriteResult

	// Executes the operation. If a condition failed, returns false.
	Exec() (bool, error)
}

type AtomicWriteResult

type AtomicWriteResult interface {
	// Returns false if the transaction failed due to this operation's conditional failing.
	ConditionalFailed() bool
}

type Backend

type Backend interface {
	// Batch allows you to batch up simple operations for better performance potential. Use this
	// only for possible performance benefits. Read isolation is implementation-defined and other
	// properties such as atomicity should not be assumed.
	Batch() BatchOperation

	// AtomicWrite executes up to 10 write operations atomically, failing entirely if any
	// conditional operations (e.g. SetNX) are not executed.
	AtomicWrite() AtomicWriteOperation

	Delete(key string) (success bool, err error)
	Get(key string) (*string, error)
	Set(key string, value interface{}) error

	// CAS performs a compare-and-swap operation. It gets the given key, allows you to transform its
	// value, then updates it only if it hasn't changed. Returning nil from the transform function
	// performs no action, causing CAS to return true, nil.
	CAS(key string, transform func(v *string) (interface{}, error)) (success bool, err error)

	// Add an integer to an integer value. Or set if the key doesn't exist.
	AddInt(key string, n int64) (int64, error)

	// Set if the key already exists.
	SetXX(key string, value interface{}) (bool, error)

	// Set if the key doesn't exist.
	SetNX(key string, value interface{}) (bool, error)

	// Add to or create a set. Sets are ideal for small sizes and fast read access. Sorted sets
	// should be considered instead for large, write-heavy applications.
	SAdd(key string, member interface{}, members ...interface{}) error

	// Remove from a set.
	SRem(key string, member interface{}, members ...interface{}) error

	// Get members of a set.
	SMembers(key string) ([]string, error)

	// Add to or create a sorted set.
	ZAdd(key string, member interface{}, score float64) error

	// Gets the score for a member added via ZAdd.
	ZScore(key string, member interface{}) (*float64, error)

	// Remove from a sorted set.
	ZRem(key string, member interface{}) error

	// Increment a score in a sorted set or set the score if the member doesn't exist.
	ZIncrBy(key string, member string, n float64) (float64, error)

	// Get members of a sorted set by ascending score.
	ZRangeByScore(key string, min, max float64, limit int) ([]string, error)

	// Get members (and their scores) of a sorted set by ascending score.
	ZRangeByScoreWithScores(key string, min, max float64, limit int) (ScoredMembers, error)

	// Get members of a sorted set by descending score.
	ZRevRangeByScore(key string, min, max float64, limit int) ([]string, error)

	// Get members (and their scores) of a sorted set by descending score.
	ZRevRangeByScoreWithScores(key string, min, max float64, limit int) (ScoredMembers, error)

	// Gets the number of members with scores between min and max, inclusive.
	ZCount(key string, min, max float64) (int, error)

	// Gets the number of members between min and max. All members of the set must have been added
	// with a zero score. min and max must begin with '(' or '[' to indicate exclusive or inclusive.
	// Alternatively, min can be "-" and max can be "+" to represent infinities.
	ZLexCount(key string, min, max string) (int, error)

	// Get members of a sorted set by lexicographical order. All members of the set must have been
	// added with a zero score. min and max must begin with '(' or '[' to indicate exclusive or
	// inclusive. Alternatively, min can be "-" and max can be "+" to represent infinities.
	ZRangeByLex(key string, min, max string, limit int) ([]string, error)

	// Get members of a sorted set by reverse lexicographical order. All members of the set must
	// have been added with a zero score. min and max must begin with '(' or '[' to indicate
	// exclusive or inclusive. Alternatively, min can be "-" and max can be "+" to represent
	// infinities.
	ZRevRangeByLex(key string, min, max string, limit int) ([]string, error)
}

type BatchOperation

type BatchOperation interface {
	Get(key string) GetResult
	Delete(key string) DeleteResult
	Set(key string, value interface{}) ErrorResult
	SMembers(key string) SMembersResult
	SAdd(key string, member interface{}, members ...interface{}) ErrorResult
	SRem(key string, member interface{}, members ...interface{}) ErrorResult
	ZAdd(key string, member interface{}, score float64) ErrorResult
	ZRem(key string, member interface{}) ErrorResult

	Exec() error
}

type DeleteResult

type DeleteResult interface {
	Result() (bool, error)
}

type ErrorResult

type ErrorResult interface {
	Result() error
}

type FallbackBatchOperation

type FallbackBatchOperation struct {
	Backend Backend
	// contains filtered or unexported fields
}

FallbackBatchOperation provides a suitable fallback for stores that don't supported optimized batching.

func (*FallbackBatchOperation) Delete

func (op *FallbackBatchOperation) Delete(key string) DeleteResult

func (*FallbackBatchOperation) Exec

func (op *FallbackBatchOperation) Exec() error

func (*FallbackBatchOperation) Get

func (*FallbackBatchOperation) SAdd

func (op *FallbackBatchOperation) SAdd(key string, member interface{}, members ...interface{}) ErrorResult

func (*FallbackBatchOperation) SMembers

func (op *FallbackBatchOperation) SMembers(key string) SMembersResult

func (*FallbackBatchOperation) SRem

func (op *FallbackBatchOperation) SRem(key string, member interface{}, members ...interface{}) ErrorResult

func (*FallbackBatchOperation) Set

func (op *FallbackBatchOperation) Set(key string, value interface{}) ErrorResult

func (*FallbackBatchOperation) ZAdd

func (op *FallbackBatchOperation) ZAdd(key string, member interface{}, score float64) ErrorResult

func (*FallbackBatchOperation) ZRem

func (op *FallbackBatchOperation) ZRem(key string, member interface{}) ErrorResult

type GetResult

type GetResult interface {
	Result() (*string, error)
}

type SMembersResult

type SMembersResult interface {
	Result() ([]string, error)
}

type ScoredMember

type ScoredMember struct {
	Score float64
	Value string
}

type ScoredMembers

type ScoredMembers []*ScoredMember

func (ScoredMembers) Values

func (m ScoredMembers) Values() []string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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