database

package
v0.42.0 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package database is a generated GoMock package.

Index

Constants

View Source
const (
	UCPScopePrefix    = "scope"
	UCPResourcePrefix = "resource"
)

Variables

This section is empty.

Functions

func DecodeMap

func DecodeMap(in any, out any) error

DecodeMap decodes map[string]interface{} structure to the type of out.

func GetResource

func GetResource[T any](ctx context.Context, databaseClient Client, id string) (*T, error)

GetResource gets the resource data for the provided resource id using the provided client.

Types

type Client

type Client interface {
	// Query executes a query against the data store and returns the results.
	//
	// Queries must provide a root scope and a resource type. Other fields are optional.
	Query(ctx context.Context, query Query, options ...QueryOptions) (*ObjectQueryResult, error)

	// Get retrieves a single resource from the data store by its resource id.
	//
	// Get will return ErrNotFound if the resource is not found.
	Get(ctx context.Context, id string, options ...GetOptions) (*Object, error)

	// Delete removes a single resource from the data store by its resource id.
	//
	// Delete will return ErrNotFound if the resource is not found.
	// When providing an ETag, Delete will return ErrConcurrency if the resource has been
	// modified OR deleted since the ETag was retrieved.
	Delete(ctx context.Context, id string, options ...DeleteOptions) error

	// Save persists a single resource to the data store. Same is a logical PUT
	// operation and will either create a new entry or update the existing entry.
	//
	// Save operations must set the ID field of the obj parameter.
	// The ETag field of the obj parameter is read-only and will be updated by the Save operation.
	//
	// Use the options to pass an ETag if you want to enforce optimistic concurrency control.
	//
	// Save will return ErrNotFound if the resource is not found.
	// When providing an ETag, Save will return ErrConcurrency if the resource has been
	// modified OR deleted since the ETag was retrieved.
	Save(ctx context.Context, obj *Object, options ...SaveOptions) error
}

Client is the interface for persisting and querying resource data.

The Client is purpose-built to work with resource data and understands concepts like scopes, resource types, and resource ids. This is a higher level abstraction than a generic key-value store, but low-level enough to support multiple implementation strategies.

The Client provides a optimistic concurrency control using ETags. Callers that want to enforce OCC should provide the ETag value in the options when calling Save or Delete.

The Client may return the errors ErrNotFound, ErrInvalid, and ErrConcurrency.

- Callers should handle ErrNotFound on Get, Save, and Delete operations. - Callers should handle ErrConcurrency when using ETags. - Callers should not handle ErrInvalid as it represents a programming error.

When using ETags, the Save or Delete operation will fail with ErrConcurrency (rather than ErrNotFound) if the underlying resource has been deleted.

type DatabaseOptions

type DatabaseOptions struct {
	// PaginationToken represents pagination token such as continuation token.
	PaginationToken string

	// MaxQueryItemCount represents max items in query result.
	MaxQueryItemCount int

	// ETag represents the entity tag for optimistic consistency control.
	ETag ETag
}

DatabaseOptions represents the configurations of the underlying database APIs.

func NewDeleteConfig

func NewDeleteConfig(opts ...DeleteOptions) DatabaseOptions

NewDeleteConfig applies the given DeleteOptions to a StoreConfig and returns the resulting StoreConfig for Delete().

func NewQueryConfig

func NewQueryConfig(opts ...QueryOptions) DatabaseOptions

NewQueryConfig applies a set of QueryOptions to a StoreConfig and returns the modified StoreConfig for Query().

func NewSaveConfig

func NewSaveConfig(opts ...SaveOptions) DatabaseOptions

NewSaveConfig applies a set of SaveOptions to a StoreConfig and returns the modified StoreConfig for Save().

type DeleteOptions

type DeleteOptions interface {
	ApplyDeleteOption(DatabaseOptions) DatabaseOptions
	// contains filtered or unexported methods
}

DeleteOptions applies an option to Delete().

type ETag

type ETag = string

type ErrConcurrency

type ErrConcurrency struct {
}

func (*ErrConcurrency) Error

func (e *ErrConcurrency) Error() string

Error returns the error message for ErrConcurrency error.

func (*ErrConcurrency) Is

func (e *ErrConcurrency) Is(target error) bool

Is checks if the target error is an instance of ErrConcurrency.

type ErrInvalid

type ErrInvalid struct {
	Message string
}

func (*ErrInvalid) Error

func (e *ErrInvalid) Error() string

Error returns a string representation of the error.

func (*ErrInvalid) Is

func (e *ErrInvalid) Is(target error) bool

Is checks if the target error is of type ErrInvalid and if the message of the target error is equal to the message of the ErrInvalid instance or is an empty string.

type ErrNotFound

type ErrNotFound struct {
	// ID of the resource that was not found
	ID string
}

func (*ErrNotFound) Error

func (e *ErrNotFound) Error() string

Error returns a string describing the resource not found error for the given ID.

func (*ErrNotFound) Is

func (e *ErrNotFound) Is(target error) bool

Is checks if the target error is an instance of ErrNotFound and returns a boolean.

type GetOptions

type GetOptions interface {
	// contains filtered or unexported methods
}

GetOptions applies an option to Get().

type Metadata

type Metadata struct {
	ID   string
	ETag ETag
}

type MockClient

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

MockClient is a mock of Client interface.

func NewMockClient

func NewMockClient(ctrl *gomock.Controller) *MockClient

NewMockClient creates a new mock instance.

func (*MockClient) Delete

func (m *MockClient) Delete(arg0 context.Context, arg1 string, arg2 ...DeleteOptions) error

Delete mocks base method.

func (*MockClient) EXPECT

func (m *MockClient) EXPECT() *MockClientMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockClient) Get

func (m *MockClient) Get(arg0 context.Context, arg1 string, arg2 ...GetOptions) (*Object, error)

Get mocks base method.

func (*MockClient) Query

func (m *MockClient) Query(arg0 context.Context, arg1 Query, arg2 ...QueryOptions) (*ObjectQueryResult, error)

Query mocks base method.

func (*MockClient) Save

func (m *MockClient) Save(arg0 context.Context, arg1 *Object, arg2 ...SaveOptions) error

Save mocks base method.

type MockClientDeleteCall

type MockClientDeleteCall struct {
	*gomock.Call
}

MockClientDeleteCall wrap *gomock.Call

func (*MockClientDeleteCall) Do

Do rewrite *gomock.Call.Do

func (*MockClientDeleteCall) DoAndReturn

DoAndReturn rewrite *gomock.Call.DoAndReturn

func (*MockClientDeleteCall) Return

Return rewrite *gomock.Call.Return

type MockClientGetCall

type MockClientGetCall struct {
	*gomock.Call
}

MockClientGetCall wrap *gomock.Call

func (*MockClientGetCall) Do

Do rewrite *gomock.Call.Do

func (*MockClientGetCall) DoAndReturn

func (c *MockClientGetCall) DoAndReturn(f func(context.Context, string, ...GetOptions) (*Object, error)) *MockClientGetCall

DoAndReturn rewrite *gomock.Call.DoAndReturn

func (*MockClientGetCall) Return

func (c *MockClientGetCall) Return(arg0 *Object, arg1 error) *MockClientGetCall

Return rewrite *gomock.Call.Return

type MockClientMockRecorder

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

MockClientMockRecorder is the mock recorder for MockClient.

func (*MockClientMockRecorder) Delete

func (mr *MockClientMockRecorder) Delete(arg0, arg1 any, arg2 ...any) *MockClientDeleteCall

Delete indicates an expected call of Delete.

func (*MockClientMockRecorder) Get

func (mr *MockClientMockRecorder) Get(arg0, arg1 any, arg2 ...any) *MockClientGetCall

Get indicates an expected call of Get.

func (*MockClientMockRecorder) Query

func (mr *MockClientMockRecorder) Query(arg0, arg1 any, arg2 ...any) *MockClientQueryCall

Query indicates an expected call of Query.

func (*MockClientMockRecorder) Save

func (mr *MockClientMockRecorder) Save(arg0, arg1 any, arg2 ...any) *MockClientSaveCall

Save indicates an expected call of Save.

type MockClientQueryCall

type MockClientQueryCall struct {
	*gomock.Call
}

MockClientQueryCall wrap *gomock.Call

func (*MockClientQueryCall) Do

Do rewrite *gomock.Call.Do

func (*MockClientQueryCall) DoAndReturn

DoAndReturn rewrite *gomock.Call.DoAndReturn

func (*MockClientQueryCall) Return

Return rewrite *gomock.Call.Return

type MockClientSaveCall

type MockClientSaveCall struct {
	*gomock.Call
}

MockClientSaveCall wrap *gomock.Call

func (*MockClientSaveCall) Do

Do rewrite *gomock.Call.Do

func (*MockClientSaveCall) DoAndReturn

DoAndReturn rewrite *gomock.Call.DoAndReturn

func (*MockClientSaveCall) Return

func (c *MockClientSaveCall) Return(arg0 error) *MockClientSaveCall

Return rewrite *gomock.Call.Return

type MutatingOptions

type MutatingOptions interface {
	SaveOptions
	DeleteOptions
}

MutatingOptions applies an option to Delete() or Save().

func WithETag

func WithETag(etag ETag) MutatingOptions

WithETag sets the ETag field in the StoreConfig struct.

type Object

type Object struct {
	Metadata

	// Data is the payload of the object. It will be marshaled to and from JSON for storage.
	Data any
}

func (*Object) As

func (o *Object) As(out any) error

As decodes the Data field of the Object instance into the out parameter.

func (*Object) DeepCopy

func (o *Object) DeepCopy() (*Object, error)

DeepCopy creates a deep copy of the Object instance.

func (Object) MatchesFilters

func (o Object) MatchesFilters(filters []QueryFilter) (bool, error)

MatchesFilters checks if the object's data matches the given filters and returns a boolean and an error.

type ObjectQueryResult

type ObjectQueryResult struct {
	// PaginationToken represents the token for pagination, such as continuation token.
	PaginationToken string
	// Items represents the list of documents.
	Items []Object
}

ObjectQueryResult represents the result of Query().

type Query

type Query struct {
	// Scope sets the root scope of the query. This will be the fully-qualified root scope. This can be a
	// UCP scope ('/planes/...') or an ARM scope as long as the data-store is self-consistent.
	//
	// Example:
	//	/planes/radius/local/resourceGroups/cool-group/
	RootScope string

	// ScopeRecursive determines whether the root scope is applied recursively.
	//
	// Example: If 'true' the following value of Scope would match all of the provided root scopes.
	//	/planes/radius/local/ ->
	//		/planes/radius/local/
	//		/planes/radius/local/resourceGroups/cool-group
	//		/planes/radius/local/resourceGroups/cool-group2
	ScopeRecursive bool

	// ResourceType is the optional resource type used to filter the query. ResourceType must be a fully-qualified
	// type if it is provided.
	//
	// Example:
	//	Applications.Core/applications
	ResourceType string

	// RoutingScopePrefix is the optional routing scope used to filter the query. RoutingScopePrefix should be the prefix
	// of the desired resources (types and names). RoutingScopePrefix should have a resource name as its last segment
	// not a type.
	//
	// Example:
	//	/Applications.Core/applications/my-app/
	RoutingScopePrefix string

	// IsScopeQuery is used to determine whether to query scopes (true) or resources (false).
	// Example: To query all "plane"
	// 	set RootScope to /planes and both ScopeRecursive and IsScopeQuery to True.
	// If ScopeQuery is False, we would be querying for resources that match RootScope and other optional
	// query field values.
	// Example: To query all resources in a radius local plane scope
	// 	set RootScope to /planes/radius/local and ScopeRecursive = True and IsScopeQuery to False.
	IsScopeQuery bool

	// Filters is an query filter to filter the specific property value.
	Filters []QueryFilter
}

Query specifies the structure of a query. RootScope and ResourceType are required and other fields are optional.

func (Query) Validate

func (q Query) Validate() error

Validate validates the Query.

type QueryFilter

type QueryFilter struct {
	// Field specifies the property name to filter.
	//
	// Field can be a simple property name of a '.' separated property path.
	// Examples:
	//	- "location"
	//	- "properties.application"
	Field string

	// Value specifies the value to filter. The value must be a string and will be
	// compared case-insentively with the property value.
	Value string
}

QueryFilter is the filter which filters property in resource entity.

func (QueryFilter) Validate

func (f QueryFilter) Validate() error

Validate validates the QueryFilter.

type QueryOptions

type QueryOptions interface {
	ApplyQueryOption(DatabaseOptions) DatabaseOptions
	// contains filtered or unexported methods
}

QueryOptions applies an option to Query().

func WithMaxQueryItemCount

func WithMaxQueryItemCount(maxcnt int) QueryOptions

WithMaxQueryItemCount creates a QueryOptions instance that sets the maximum number of items in query result.

func WithPaginationToken

func WithPaginationToken(token string) QueryOptions

WithPaginationToken sets pagination token for Query().

type SaveOptions

type SaveOptions interface {
	ApplySaveOption(DatabaseOptions) DatabaseOptions
	// contains filtered or unexported methods
}

SaveOptions applies an option to Save().

Directories

Path Synopsis
Package apiserverstore stores resources using the Kubernetes API Server - using CRDs as a key-value store.
Package apiserverstore stores resources using the Kubernetes API Server - using CRDs as a key-value store.
api/ucp.dev/v1alpha1
Package v1alpha1 contains API Schema definitions for the ucp v1alpha1 API group +kubebuilder:object:generate=true +groupName=ucp.dev
Package v1alpha1 contains API Schema definitions for the ucp v1alpha1 API group +kubebuilder:object:generate=true +groupName=ucp.dev
storeutil contains utility functions for implementing database backends.
storeutil contains utility functions for implementing database backends.
inmemory contains an implementation of the Radius data store interface that stores data in memory.
inmemory contains an implementation of the Radius data store interface that stores data in memory.

Jump to

Keyboard shortcuts

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