storage

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2023 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NamespaceKeySeparator           = ":"
	Pong                            = "PONG"
	RedisScanBatchSize              = 1000
	MaxElapsedTime                  = 6 * time.Second
	RedisAddressOption    OptionKey = "redis-address-option"
)
View Source
const (
	Bolt  Type = "bolt"
	Redis Type = "redis"

	PasswordOption OptionKey = "storage-password-option"
)

Variables

This section is empty.

Functions

func IsStorageAvailable

func IsStorageAvailable(storage Type) bool

IsStorageAvailable determines whether a given storage provider is available for instantiation.

func MakeNamespace

func MakeNamespace(ns ...string) string

MakeNamespace takes a set of possible namespace values and combines them as a convention

func RegisterStorage

func RegisterStorage(storage ServiceStorage) error

RegisterStorage registers a storage dynamically by its Type.

Types

type BoltDB

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

func (*BoltDB) Close

func (b *BoltDB) Close() error

func (*BoltDB) Delete

func (b *BoltDB) Delete(_ context.Context, namespace, key string) error

func (*BoltDB) DeleteNamespace

func (b *BoltDB) DeleteNamespace(_ context.Context, namespace string) error

func (*BoltDB) Execute

func (b *BoltDB) Execute(ctx context.Context, businessLogicFunc BusinessLogicFunc, _ []WatchKey) (any, error)

Execute runs the provided function within a transaction. Any failure during execution results in a rollback. It is recommended to not open transactions within businessLogicFunc, as there are situation in which the interplay between transactions may cause deadlocks.

func (*BoltDB) Exists

func (b *BoltDB) Exists(_ context.Context, namespace, key string) (bool, error)

func (*BoltDB) Init

func (b *BoltDB) Init(opts ...Option) error

Init instantiates a file-based storage instance for Bolt https://github.com/boltdb/bolt

func (*BoltDB) IsOpen

func (b *BoltDB) IsOpen() bool

IsOpen return if db was opened

func (*BoltDB) Read

func (b *BoltDB) Read(_ context.Context, namespace, key string) ([]byte, error)

func (*BoltDB) ReadAll

func (b *BoltDB) ReadAll(_ context.Context, namespace string) (map[string][]byte, error)

func (*BoltDB) ReadAllKeys

func (b *BoltDB) ReadAllKeys(_ context.Context, namespace string) ([]string, error)

func (*BoltDB) ReadPage

func (b *BoltDB) ReadPage(_ context.Context, namespace string, pageToken string, pageSize int) (map[string][]byte, string, error)

func (*BoltDB) ReadPrefix

func (b *BoltDB) ReadPrefix(_ context.Context, namespace, prefix string) (map[string][]byte, error)

ReadPrefix does a prefix query within a namespace.

func (*BoltDB) Type

func (b *BoltDB) Type() Type

func (*BoltDB) URI

func (b *BoltDB) URI() string

URI return filepath of boltDB,

func (*BoltDB) Update

func (b *BoltDB) Update(_ context.Context, namespace string, key string, values map[string]any) ([]byte, error)

func (*BoltDB) UpdateValueAndOperation

func (b *BoltDB) UpdateValueAndOperation(_ context.Context, namespace, key string, updater Updater, opNamespace, opKey string, opUpdater ResponseSettingUpdater) (first, op []byte, err error)

UpdateValueAndOperation updates the value stored in (namespace,key) with the new values specified in the map. The updated value is then stored inside the (opNamespace, opKey), and the "done" value is set to true.

func (*BoltDB) Write

func (b *BoltDB) Write(_ context.Context, namespace string, key string, value []byte) error

func (*BoltDB) WriteMany

func (b *BoltDB) WriteMany(_ context.Context, namespaces, keys []string, values [][]byte) error

type BusinessLogicFunc

type BusinessLogicFunc func(ctx context.Context, tx Tx) (any, error)

type FilterVarsMapper

type FilterVarsMapper interface {
	FilterVariablesMap() map[string]any
}

FilterVarsMapper is an interface that encapsulates the FilterVariablesMap method. This interface is meant to be implemented by any object that wants to include support for filtering.

type IncludeFunc

type IncludeFunc func(FilterVarsMapper) (bool, error)

IncludeFunc is a function that given a mapper object, decides whether the object should be included in the result.

func NewIncludeFunc

func NewIncludeFunc(filter filtering.Filter) (IncludeFunc, error)

NewIncludeFunc creates an IncludeFunc given a filter object. The result function is constructed as an evaluation of a cel program. The environment created matches standard construction of a filter. The result function runs the program evaluation on a given object that implements the FilterVarsMapper interface. Evaluation errors bubbled up so clients can decide what to do.

type Option

type Option struct {
	ID     OptionKey `json:"id,omitempty"`
	Option any       `json:"option,omitempty"`
}

Option represents a single option that may be required for a storage provider

type OptionKey

type OptionKey string

OptionKey uniquely represents an option to be used in a storage provider

const (
	DBFilePrefix                   = "ssi-service"
	BoltDBFilePathOption OptionKey = "boltdb-filepath-option"
)

type RedisDB

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

func (*RedisDB) Close

func (b *RedisDB) Close() error

func (*RedisDB) Delete

func (b *RedisDB) Delete(ctx context.Context, namespace, key string) error

func (*RedisDB) DeleteNamespace

func (b *RedisDB) DeleteNamespace(ctx context.Context, namespace string) error

func (*RedisDB) Execute

func (b *RedisDB) Execute(ctx context.Context, businessLogicFunc BusinessLogicFunc, watchKeys []WatchKey) (any, error)

func (*RedisDB) Exists

func (b *RedisDB) Exists(ctx context.Context, namespace, key string) (bool, error)

func (*RedisDB) Init

func (b *RedisDB) Init(opts ...Option) error

func (*RedisDB) IsOpen

func (b *RedisDB) IsOpen() bool

func (*RedisDB) Read

func (b *RedisDB) Read(ctx context.Context, namespace, key string) ([]byte, error)

func (*RedisDB) ReadAll

func (b *RedisDB) ReadAll(ctx context.Context, namespace string) (map[string][]byte, error)

func (*RedisDB) ReadAllKeys

func (b *RedisDB) ReadAllKeys(ctx context.Context, namespace string) ([]string, error)

func (*RedisDB) ReadPage

func (b *RedisDB) ReadPage(ctx context.Context, namespace string, pageToken string, pageSize int) (map[string][]byte, string, error)

func (*RedisDB) ReadPrefix

func (b *RedisDB) ReadPrefix(ctx context.Context, namespace, prefix string) (map[string][]byte, error)

func (*RedisDB) Type

func (b *RedisDB) Type() Type

func (*RedisDB) URI

func (b *RedisDB) URI() string

func (*RedisDB) Update

func (b *RedisDB) Update(ctx context.Context, namespace string, key string, values map[string]any) ([]byte, error)

func (*RedisDB) UpdateValueAndOperation

func (b *RedisDB) UpdateValueAndOperation(ctx context.Context, namespace, key string, updater Updater, opNamespace, opKey string, opUpdater ResponseSettingUpdater) (first, op []byte, err error)

func (*RedisDB) Write

func (b *RedisDB) Write(ctx context.Context, namespace, key string, value []byte) error

func (*RedisDB) WriteMany

func (b *RedisDB) WriteMany(ctx context.Context, namespaces, keys []string, values [][]byte) error

type ResponseSettingUpdater

type ResponseSettingUpdater interface {
	Updater
	// SetUpdatedResponse sets the response that the Update method will later use to modify the data.
	SetUpdatedResponse([]byte)
}

type ServiceStorage

type ServiceStorage interface {
	Init(opts ...Option) error
	Type() Type
	URI() string
	IsOpen() bool
	Close() error
	Write(ctx context.Context, namespace, key string, value []byte) error
	WriteMany(ctx context.Context, namespace, key []string, value [][]byte) error
	Read(ctx context.Context, namespace, key string) ([]byte, error)
	Exists(ctx context.Context, namespace, key string) (bool, error)
	ReadAll(ctx context.Context, namespace string) (map[string][]byte, error)

	// ReadPage returns a page of elements. When pageSize == -1, all elements are returned. Results are returned
	// starting from the pageToken. pageToken may be empty.
	// Note that pageSize is a hint and may not be enforced by the DB implementation. This is intentional, as some DBs
	// do not have a way to enforce this (e.g. Redis), and the DB may decide to return everything in some cases. A more
	// detailed explanation is available at https://redis.io/commands/scan/, section
	// "Why SCAN may return all the items of an aggregate data type in a single call?".
	ReadPage(ctx context.Context, namespace string, pageToken string, pageSize int) (results map[string][]byte, nextPageToken string, err error)
	ReadPrefix(ctx context.Context, namespace, prefix string) (map[string][]byte, error)
	ReadAllKeys(ctx context.Context, namespace string) ([]string, error)
	Delete(ctx context.Context, namespace, key string) error
	DeleteNamespace(ctx context.Context, namespace string) error
	Update(ctx context.Context, namespace string, key string, values map[string]any) ([]byte, error)
	UpdateValueAndOperation(ctx context.Context, namespace, key string, updater Updater, opNamespace, opKey string, opUpdater ResponseSettingUpdater) (first, op []byte, err error)
	Execute(ctx context.Context, businessLogicFunc BusinessLogicFunc, watchKeys []WatchKey) (any, error)
}

ServiceStorage describes the api for storage independent of DB providers

func GetStorage

func GetStorage(storageType Type) ServiceStorage

GetStorage fetches a previously registered storage by storage type.

func NewStorage

func NewStorage(storageProvider Type, opts ...Option) (ServiceStorage, error)

NewStorage returns the instance of the given storageProvider. If it doesn't exist, then a default implementation is created with the given option parameter.

type Tx

type Tx interface {
	Write(ctx context.Context, namespace, key string, value []byte) error
}

type Type

type Type string

func AvailableStorage

func AvailableStorage() []Type

AvailableStorage returns the supported storage providers.

type Updater

type Updater interface {
	Update(v []byte) ([]byte, error)
	// Validate runs after the data has been loaded from disk, but before the write is actually performed.
	Validate(v []byte) error
}

Updater encapsulates the Update method, which take a slice of bytes, and updates it before it's stored in the DB.

type UpdaterWithMap

type UpdaterWithMap struct {
	Values map[string]any
}

UpdaterWithMap is a json map based Updater implementation. The key/values from the map are used to update the unmarshalled JSON representation of the stored data.

func NewUpdater

func NewUpdater(values map[string]any) UpdaterWithMap

NewUpdater creates a new UpdaterWithMap with the given map.

func (UpdaterWithMap) Update

func (u UpdaterWithMap) Update(v []byte) ([]byte, error)

func (UpdaterWithMap) Validate

func (u UpdaterWithMap) Validate(_ []byte) error

Validate is a default implementation for UpdaterWithMap which does no validation. Users can pass embed UpdaterWithMap into a custom struct and redefine this method in order to have custom logic.

type WatchKey

type WatchKey struct {
	Namespace string
	Key       string
}

Jump to

Keyboard shortcuts

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