runtime

package
v0.16.3 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2022 License: GPL-3.0 Imports: 14 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrReadOnly should be returned from ValueProvider.Set if a
	// runtime record is considered read-only.
	ErrReadOnly = errors.New("runtime record is read-only")
	// ErrWriteOnly should be returned from ValueProvider.Get if
	// a runtime record is considered write-only.
	ErrWriteOnly = errors.New("runtime record is write-only")
)
View Source
var (
	// ErrKeyTaken is returned when trying to register
	// a value provider at database key or prefix that
	// is already occupied by another provider.
	ErrKeyTaken = errors.New("runtime key or prefix already used")
	// ErrKeyUnmanaged is returned when a Put operation
	// on an unmanaged key is performed.
	ErrKeyUnmanaged = errors.New("runtime key not managed by any provider")
	// ErrInjected is returned by Registry.InjectAsDatabase
	// if the registry has already been injected.
	ErrInjected = errors.New("registry already injected")
)
View Source
var DefaultRegistry = NewRegistry()

DefaultRegistry is the default registry that is used by the module-level API.

Functions

This section is empty.

Types

type ModulesIntegration added in v0.10.0

type ModulesIntegration struct{}

ModulesIntegration provides integration with the modules system.

func (*ModulesIntegration) Get added in v0.10.0

func (mi *ModulesIntegration) Get(keyOrPrefix string) ([]record.Record, error)

Get should return one or more records that match keyOrPrefix. keyOrPrefix is guaranteed to be at least the prefix used to register the ValueProvider.

func (*ModulesIntegration) Set added in v0.10.0

Set is called when the value is set from outside. If the runtime value is considered read-only ErrReadOnly should be returned. It is guaranteed that the key of the record passed to Set is prefixed with the key used to register the value provider.

type PushFunc

type PushFunc func(...record.Record)

PushFunc is returned when registering a new value provider and can be used to inform the database system about the availability of a new runtime record value. Similar to database.Controller.PushUpdate, the caller must hold the lock for each record passed to PushFunc.

func Register

func Register(key string, provider ValueProvider) (PushFunc, error)

Register is like Registry.Register but uses the package DefaultRegistry.

type Registry

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

Registry keeps track of registered runtime value providers and exposes them via an injected database. Users normally just need to use the defaul registry provided by this package but may consider creating a dedicated runtime registry on their own. Registry uses a radix tree for value providers and their chosen database key/prefix.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns a new registry.

func (*Registry) DatabaseName

func (r *Registry) DatabaseName() string

DatabaseName returns the name of the database where the registry has been injected. It returns an empty string if InjectAsDatabase has not been called.

func (*Registry) Get

func (r *Registry) Get(key string) (record.Record, error)

Get returns the runtime value that is identified by key. It implements the storage.Interface.

func (*Registry) GetRegistrationKeys

func (r *Registry) GetRegistrationKeys() []string

GetRegistrationKeys returns a list of all provider registration keys or prefixes.

func (*Registry) InjectAsDatabase

func (r *Registry) InjectAsDatabase(name string) error

InjectAsDatabase injects the registry as the storage database for name.

func (*Registry) Put

func (r *Registry) Put(m record.Record) (record.Record, error)

Put stores the record m in the runtime database. Note that ErrReadOnly is returned if there's no value provider responsible for m.Key().

func (*Registry) Query

func (r *Registry) Query(q *query.Query, local, internal bool) (*iterator.Iterator, error)

Query performs a query on the runtime registry returning all records across all value providers that match q. Query implements the storage.Storage interface.

func (*Registry) Register

func (r *Registry) Register(keyOrPrefix string, p ValueProvider) (PushFunc, error)

Register registers a new value provider p under keyOrPrefix. The returned PushFunc can be used to send update notitifcations to database subscribers. Note that keyOrPrefix must end in '/' to be accepted as a prefix.

type SimpleValueGetterFunc

type SimpleValueGetterFunc func(keyOrPrefix string) ([]record.Record, error)

SimpleValueGetterFunc is a convenience type for implementing a read-only value provider.

func (SimpleValueGetterFunc) Get

func (fn SimpleValueGetterFunc) Get(keyOrPrefix string) ([]record.Record, error)

Get implements ValueProvider.Get and calls fn.

func (SimpleValueGetterFunc) Set

Set implements ValueProvider.Set and returns ErrReadOnly.

type SimpleValueSetterFunc

type SimpleValueSetterFunc func(record.Record) (record.Record, error)

SimpleValueSetterFunc is a convenience type for implementing a write-only value provider.

func (SimpleValueSetterFunc) Get

Get implements ValueProvider.Get and returns ErrWriteOnly.

func (SimpleValueSetterFunc) Set

Set implements ValueProvider.Set and calls fn.

type ValueProvider

type ValueProvider interface {
	// Set is called when the value is set from outside.
	// If the runtime value is considered read-only ErrReadOnly
	// should be returned. It is guaranteed that the key of
	// the record passed to Set is prefixed with the key used
	// to register the value provider.
	Set(record.Record) (record.Record, error)
	// Get should return one or more records that match keyOrPrefix.
	// keyOrPrefix is guaranteed to be at least the prefix used to
	// register the ValueProvider.
	Get(keyOrPrefix string) ([]record.Record, error)
}

ValueProvider provides access to a runtime-computed database record.

func ProvideRecord

func ProvideRecord(r record.Record) ValueProvider

ProvideRecord returns a ValueProvider the exposes read-only access to r. Users of ProvideRecord need to ensure the lock the whole record before performing modifications on it.

Example:

type MyValue struct {
	record.Base
	Value string
}
r := new(MyValue)
pushUpdate, _ := runtime.Register("my/key", ProvideRecord(r))
r.Lock()
r.Value = "foobar"
pushUpdate(r)
r.Unlock()

func TraceProvider

func TraceProvider(vp ValueProvider) ValueProvider

TraceProvider returns a new ValueProvider that wraps vp but traces all Set and Get methods calls.

Jump to

Keyboard shortcuts

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