storage

package
v0.0.0-...-0fc66b4 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2023 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Pong                         = "PONG"
	RedisScanBatchSize           = 1000
	MaxElapsedTime               = 6 * time.Second
	RedisAddressOption OptionKey = "redis-address-option"
)
View Source
const (
	Bolt        Type = "bolt"
	DatabaseSQL Type = "database_sql"
	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 Join

func Join(parts ...string) string

Join combines all parts using `:` as the separator.

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.

func Update

func Update(ctx context.Context, s ServiceStorage, namespace, key string, m map[string]any) ([]byte, error)

func UpdateValueAndOperation

func UpdateValueAndOperation(ctx context.Context, s ServiceStorage, 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.

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) 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 EncryptedWrapper

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

func NewEncryptedWrapper

func NewEncryptedWrapper(s ServiceStorage, encrypter encryption.Encrypter, decrypter encryption.Decrypter) *EncryptedWrapper

func (EncryptedWrapper) Close

func (e EncryptedWrapper) Close() error

func (EncryptedWrapper) Delete

func (e EncryptedWrapper) Delete(ctx context.Context, namespace, key string) error

func (EncryptedWrapper) DeleteNamespace

func (e EncryptedWrapper) DeleteNamespace(ctx context.Context, namespace string) error

func (EncryptedWrapper) Execute

func (e EncryptedWrapper) Execute(ctx context.Context, businessLogicFunc BusinessLogicFunc, watchKeys []WatchKey) (any, error)

func (EncryptedWrapper) Exists

func (e EncryptedWrapper) Exists(ctx context.Context, namespace, key string) (bool, error)

func (EncryptedWrapper) Init

func (e EncryptedWrapper) Init(opts ...Option) error

func (EncryptedWrapper) IsOpen

func (e EncryptedWrapper) IsOpen() bool

func (EncryptedWrapper) Read

func (e EncryptedWrapper) Read(ctx context.Context, namespace, key string) ([]byte, error)

func (EncryptedWrapper) ReadAll

func (e EncryptedWrapper) ReadAll(ctx context.Context, namespace string) (map[string][]byte, error)

func (EncryptedWrapper) ReadAllKeys

func (e EncryptedWrapper) ReadAllKeys(ctx context.Context, namespace string) ([]string, error)

func (EncryptedWrapper) ReadPage

func (e EncryptedWrapper) ReadPage(ctx context.Context, namespace string, pageToken string, pageSize int) (results map[string][]byte, nextPageToken string, err error)

func (EncryptedWrapper) ReadPrefix

func (e EncryptedWrapper) ReadPrefix(ctx context.Context, namespace, prefix string) (map[string][]byte, error)

func (EncryptedWrapper) Type

func (e EncryptedWrapper) Type() Type

func (EncryptedWrapper) URI

func (e EncryptedWrapper) URI() string

func (EncryptedWrapper) Write

func (e EncryptedWrapper) Write(ctx context.Context, namespace, key string, value []byte) error

func (EncryptedWrapper) WriteMany

func (e EncryptedWrapper) WriteMany(ctx context.Context, namespace, keys []string, values [][]byte) error

type ExecContext

type ExecContext interface {
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, 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"
)
const (
	SQLConnectionString OptionKey = "sql-connection-string-option"
	SQLDriverName       OptionKey = "sql-driver-name-option"
)

type QueryRow

type QueryRow interface {
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

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 SQLDB

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

func (*SQLDB) Close

func (s *SQLDB) Close() error

func (*SQLDB) Delete

func (s *SQLDB) Delete(ctx context.Context, namespace, key string) error

func (*SQLDB) DeleteNamespace

func (s *SQLDB) DeleteNamespace(ctx context.Context, namespace string) error

func (*SQLDB) Execute

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

func (*SQLDB) Exists

func (s *SQLDB) Exists(ctx context.Context, namespace, key string) (bool, error)

func (*SQLDB) Init

func (s *SQLDB) Init(opts ...Option) error

func (*SQLDB) IsOpen

func (s *SQLDB) IsOpen() bool

func (*SQLDB) Read

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

func (*SQLDB) ReadAll

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

func (*SQLDB) ReadAllKeys

func (s *SQLDB) ReadAllKeys(ctx context.Context, namespace string) ([]string, error)

func (*SQLDB) ReadPage

func (s *SQLDB) ReadPage(ctx context.Context, namespace string, pageToken string, pageSize int) (results map[string][]byte, nextPageToken string, err error)

func (*SQLDB) ReadPrefix

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

func (*SQLDB) Type

func (s *SQLDB) Type() Type

func (*SQLDB) URI

func (s *SQLDB) URI() string

func (*SQLDB) Update

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

func (*SQLDB) UpdateValueAndOperation

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

func (*SQLDB) Write

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

func (*SQLDB) WriteMany

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

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
	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