store

package module
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2023 License: MIT Imports: 5 Imported by: 0

README

store

Go Report Card Coverage Status Build GoDoc

Install

go get -v github.com/postfinance/store

Documentation

Overview

Package store provides the interface for a key-value store with different backends

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrKeyNotFound is returned when key was not found.
	ErrKeyNotFound = errors.New("key not found")
	// ErrResponseChannelClosed will be returned if the response channel of the keep-alive is closed
	ErrResponseChannelClosed = errors.New("keepalive response channel has been closed")
)

Functions

func Put

func Put(b Backend, key string, v interface{}, opts ...PutOption) (bool, error)

Put is a wrapper around the Backend interface's Put method. This wrapper JSON marhals the interface v and uses the generated byte array as value.

func WithContext

func WithContext(ctx context.Context) interface {
	GetOption
	PutOption
	DelOption
	WatchOption
}

WithContext is an options to set a context for a request.

func WithErrorHandler

func WithErrorHandler(h ErrorFunc) interface {
	WatchOption
}

WithErrorHandler is an option to use an ErrorFunc on each error not returned

func WithFilter added in v0.3.0

func WithFilter(f FilterFunc) interface {
	GetOption
}

WithFilter is an option to use an FilterFunc on each key-value pair during a Get request.

func WithHandler

func WithHandler(h HandlerFunc) interface {
	GetOption
}

WithHandler is an option to use an HandlerFunc on each key-value pair during a Get request.

func WithInsert

func WithInsert() interface {
	PutOption
}

WithInsert is an option to put a non existing key. If the key already exists, nothing is done and false is returned. If key does not exist key and value are added and true is returned.

func WithKeepAlive added in v0.1.3

func WithKeepAlive(errChan chan<- error) interface {
	PutOption
}

WithKeepAlive is an option to start a keep-alive for a key. The keep-alive will only start if errChan != nil

func WithNotifyCreated

func WithNotifyCreated(c NotifyCallback) interface {
	WatchOption
}

WithNotifyCreated is an option to use a NotifyCallback as soon as the Watch is established and ready to receive events

func WithPrefix

func WithPrefix() interface {
	GetOption
	DelOption
	WatchOption
}

WithPrefix is an option to perform a request with prefix.

func WithTTL

func WithTTL(ttl time.Duration) interface {
	PutOption
}

WithTTL is an option to add a time to live.

func WithUnmarshal

func WithUnmarshal(v interface{}) interface {
	GetOption
}

WithUnmarshal unmarshals the byte array in the store into v. It panics if v is not a pointer .

In combination with WithPrefix, v should be a pointer to a slice.

Types

type Backend

type Backend interface {
	// Put is used to insert or update an entry.
	//
	// The entry is added if the key exists or not.
	// If value is changed, true is returned, false is returned only if
	// value stays unchanged.
	//
	// If WithInsert option is used and the key already
	// exists, nothing is done and false is returned. If key does not exist
	// the entry is added and true is returned.
	Put(*Entry, ...PutOption) (bool, error)

	// Get is used to fetch an entry. If key is not found and WithPrefix is absent ErrKeyNotFound is
	// returned.
	Get(string, ...GetOption) ([]Entry, error)

	// Delete is used to permanently delte an entry. The number of deleted keys will be returned.
	Del(string, ...DelOption) (int64, error)

	// Watch a key
	Watch(string, Watcher, ...WatchOption) error

	// WatchChan creates a watcher for a key or prefix and unmarshals events into channel.
	// The channel elements have to implement the store.KeyOpSetter interface.
	WatchChan(string, interface{}, chan error, ...WatchOption) (WatchStarter, error)

	// Close closes the connection.
	Close() error
}

Backend is the interface required for a key value store.

type BackendKeyer added in v0.1.3

type BackendKeyer interface {
	Backend
	RelKey(k string) string
	AbsKey(k string) string
	JoinKey(args ...string) string
	SplitKey(key string) []string
	KeyLeaf(key string) string
}

BackendKeyer interface extends Backend with key handling

type DelOption

type DelOption interface {
	SetDelOption(*DelOptions)
}

DelOption is the option interface for Del requests.

type DelOptions

type DelOptions struct {
	Prefix  bool
	Context context.Context
}

DelOptions represent all possible options for Del requests.

type Entry

type Entry struct {
	Key   string
	Value []byte
}

Entry is used to represent data stored by the physical backend

type ErrorFunc

type ErrorFunc func(error) error

ErrorFunc is a function called on (each) error not returned

type EventMeta added in v0.3.0

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

EventMeta contains store events meta data.

func (*EventMeta) Key added in v0.3.0

func (e *EventMeta) Key() string

Key gets the key.

func (*EventMeta) Op added in v0.3.0

func (e *EventMeta) Op() Operation

Op gets the operation.

func (*EventMeta) SetKey added in v0.3.0

func (e *EventMeta) SetKey(key string)

SetKey sets the key.

func (*EventMeta) SetOp added in v0.3.0

func (e *EventMeta) SetOp(o Operation)

SetOp sets the operation.

type FilterFunc added in v0.3.0

type FilterFunc func([]byte, []byte) bool

FilterFunc is a function that is called on (each) returned key-value pair during a Get request.

type GetOption

type GetOption interface {
	SetGetOption(*GetOptions)
}

GetOption is the option interface for Get requests.

type GetOptions

type GetOptions struct {
	Prefix    bool
	Filter    FilterFunc
	Handler   HandlerFunc
	Context   context.Context
	Unmarshal *unmarshal
}

GetOptions represent all possible options for Get requests.

type HandlerFunc

type HandlerFunc func([]byte, []byte) error

HandlerFunc is a function that is called on (each) returned key-value pair during a Get request.

type KeyMarshaller added in v0.3.0

type KeyMarshaller interface {
	MarshalKey([]string) error
}

KeyMarshaller sets struct fields from splitted key.

type KeyOpSetter added in v0.3.0

type KeyOpSetter interface {
	SetKey(string)
	SetOp(Operation)
}

KeyOpSetter interface.

type NotifyCallback

type NotifyCallback func()

NotifyCallback is a function that is called after a watch create event is received.

type Operation added in v0.3.0

type Operation string

Operation represents a store operation.

const (
	Create Operation = "create"
	Update Operation = "update"
	Delete Operation = "delete"
)

All supported operations.

type PutOption

type PutOption interface {
	SetPutOption(*PutOptions)
}

PutOption is the option interface for Put requests.

type PutOptions

type PutOptions struct {
	Context context.Context
	TTL     time.Duration
	ErrChan chan<- error
	Insert  bool
}

PutOptions represent all possible options for Put requests.

type WatchOption

type WatchOption interface {
	SetWatchOption(*WatchOptions)
}

WatchOption is the option interface for watchers.

type WatchOptions

type WatchOptions struct {
	Prefix        bool
	Context       context.Context
	NotifyCreated NotifyCallback
	ErrorHandler  ErrorFunc
}

WatchOptions represent all possible options for watchers.

type WatchStarter added in v0.3.0

type WatchStarter interface {
	Start()
}

WatchStarter interface

type Watcher

type Watcher interface {
	BeforeWatch() error
	BeforeLoop() error
	OnDone() error
	OnPut([]byte, []byte) error
	OnDelete([]byte, []byte) error
}

Watcher interface

Directories

Path Synopsis
Package etcd implements the store.Backend interface for the etcd
Package etcd implements the store.Backend interface for the etcd
Package hash implements the store.Backend interface for a hash map
Package hash implements the store.Backend interface for a hash map
internal
common
Package common contains helper methods used by all store implementations.
Package common contains helper methods used by all store implementations.

Jump to

Keyboard shortcuts

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