persistence

package
v0.0.1-3 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2024 License: MIT Imports: 26 Imported by: 4

Documentation

Index

Constants

View Source
const ConfigParamPath = "path"
View Source
const IdentifiableMemoryPersistenceConfigParamOptionsMaxPageSize = "options.max_page_size"

Variables

This section is empty.

Functions

func CloneObject

func CloneObject(item any, proto reflect.Type) any

CloneObject is clones object function

Parameters:
	- item any an object to clone
Return any copy of input item

func CloneObjectForResult

func CloneObjectForResult(src any, proto reflect.Type) any

CloneObjectForResult is clones object for result function

Parameters:
	- item any an object to clone
	- proto reflect.Type of returned value, need for detect object or pointer returned type
Returns: any copy of input item

func CompareValues

func CompareValues(value1 any, value2 any) bool

CompareValues are ompares two values

Parameters:
	- value1 any an object one for compare
	- value2 any an object two for compare
Returns: bool true if value1 equal value2 and false otherwise

func FromIds

func FromIds(ids []string) []any

FromIds method convert ids string array to array of any object

Parameters:
	- ids - []string array of ids
Returns: []any array of ids

func GenerateObjectId

func GenerateObjectId(item *any)

GenerateObjectId is generates a new id value when it's empty

Parameters:
	- item *any a pointer on object to set id property
Results: saved in input object

func GetObjectId

func GetObjectId(item any) any

GetObjectId value

Parameters:
	- item any an object to read property from.
Returns: any the property value or nil if property doesn't exist or introspection failed.

func GetProperty

func GetProperty(obj any, name string) any

GetProperty value of object property specified by its name.

Parameters:
	- obj any an object to read property from.
	- name string a name of the property to get.
Returns: any the property value or null if property doesn't exist or introspection failed.

func SetObjectId

func SetObjectId(item *any, id any)

SetObjectId is set object Id value

Parameters:
	- item *any a pointer on object to set id property
	- id any id value for set
Results: saved in input object

func SetProperty

func SetProperty(obj any, name string, value any)

SetProperty value of object property specified by its name. If the property does not exist or introspection fails this method doesn't do anything and doesn't any throw errors.

Parameters:
	- obj any an object to write property to.
	- name string a name of the property to set.
	- value any a new value for the property to set.

func ToPublicArray

func ToPublicArray(values []any) []map[string]any

ToPublicArray method convert array of any object to array of map[string]any

Parameters:
	- value []any input object to convert
Returns: []map[string]any converted map array

func ToPublicMap

func ToPublicMap(value any) map[string]any

ToPublicMap method convert any object to map[string]any

Parameters:
	- value any input object to convert
Returns: map[string]any converted object to map

Types

type FilePersistence

type FilePersistence[T cdata.ICloneable[T]] struct {
	*MemoryPersistence[T]
	Persister *JsonFilePersister[T]
}

FilePersistence is an abstract persistence component that stores data in flat files and caches them in memory.

FilePersistence is the most basic persistence component that is only able to store data items of any type. Specific CRUD operations over the data items must be implemented in child structs by accessing fp._items property and calling Save method.

see MemoryPersistence
see JsonFilePersister

Configuration parameters:
	- path to the file where data is stored
References:
	- *:logger:*:*:1.0  (optional) ILogger components to pass log messages
Typed params:
	- T cdata.ICloneable[T] any type that implemented
		ICloneable interface of getting element

Example:
	type MyJsonFilePersistence struct {
		*FilePersistence[*MyData]
	}

	func NewMyJsonFilePersistence(path string) *MyJsonFilePersistence {
		return &MyJsonFilePersistence{
			FilePersistence: NewFilePersistence(NewJsonFilePersister[*MyData](path)),
		}
	}

	func (c *MyJsonFilePersistence) GetByName(ctx context.Context,
		name string) (*MyData, error) {
		for _, v := range c.Items {
			if v.Name == name {
				return v, nil
			}
		}

		var defaultValue *MyData
		return defaultValue, nil
	}

	func (c *MyData) Clone() *MyData {
		return &MyData{Id: c.Id, Name: c.Name}
	}

	type MyData struct {
		Id   string
		Name string
	}

Extends: MemoryPersistence
Implements: IConfigurable

func NewFilePersistence

func NewFilePersistence[T cdata.ICloneable[T]](persister *JsonFilePersister[T]) *FilePersistence[T]

NewFilePersistence creates a new instance of the persistence.

Parameters:
	- persister (optional) a persister component that loads and saves data from/to flat file.
Typed params:
	- T cdata.ICloneable[T] any type that implemented
		ICloneable interface of getting element

Returns: *FilePersistence[T] pointer on new FilePersistence instance

func (*FilePersistence[T]) Configure

func (c *FilePersistence[T]) Configure(ctx context.Context, conf *config.ConfigParams)

Configure configures component by passing configuration parameters.

Parameters:
	- ctx context.Context
	- config configuration parameters to be set.

type IdentifiableFilePersistence

type IdentifiableFilePersistence[T any, K any] struct {
	*IdentifiableMemoryPersistence[T, K]
	Persister *JsonFilePersister[T]
}

IdentifiableFilePersistence is an abstract persistence component that stores data in flat files and implements a number of CRUD operations over data items with unique ids. The data items must implement IDataObject interface

In basic scenarios child classes shall only override GetPageByFilter, GetListByFilter or DeleteByFilter operations with specific filter function. All other operations can be used out of the box.

In complex scenarios child classes can implement additional operations by accessing cached items via IdentifiableFilePersistence._items property and calling Save method on updates.

Important:
	- this component is a thread save!
	- the data items must implement IDataObject interface

see JsonFilePersister
see MemoryPersistence

Configuration parameters:
	- path: path to the file where data is stored
	- options:
	- max_page_size: Maximum number of items returned in a single page (default: 100)

References:
	- *:logger:*:*:1.0 (optional)  ILogger components to pass log messages
Typed params:
	- T cdata.IDataObject[T, K] any type that implemented
		IDataObject interface of getting element
	- K any type if id (key)

Example:
	type MyFilePersistence struct {
		*IdentifiableFilePersistence[*MyData, string]
	}

	func NewMyFilePersistence(path string) (mfp *MyFilePersistence) {
		mfp = &MyFilePersistence{}
		mfp.IdentifiableFilePersistence = NewIdentifiableFilePersistence[*MyData, string](NewJsonFilePersister[*MyData](path))
		return mfp
	}

	func (c *MyFilePersistence) composeFilter(filter cdata.FilterParams) func(item *MyData) bool {
		if &filter == nil {
			filter = NewFilterParams()
		}
		name, _ := filter.GetAsNullableString("name")
		return func(item *MyData) bool {
			if name != "" && item.Name != name {
				return false
			}
			return true
		}
	}

	func (c *MyFilePersistence) GetPageByFilter(ctx context.Context,
		filter FilterParams, paging PagingParams) (page cdata.DataPage[MyData], err error) {
		return c.GetPageByFilter(ctx, c.composeFilter(filter), paging, nil, nil)
	}

	func (c *MyData) Clone() *MyData {
		return &MyData{Id: c.Id, Name: c.Name}
	}

	type MyData struct {
		Id   string
		Name string
	}

	persistence := NewMyFilePersistence("./data/data.json")
	_, err := persistence.Create(context.Background(), "123", &MyData{Id: "1", Name: "ABC"})
	if err != nil {
		panic(err)
	}
	page, err := persistence.GetPageByFilter(context.Background(), "123", *NewFilterParamsFromTuples("Name", "ABC"), nil)
	if err != nil {
		panic("Error")
	}
	data := page.Data
	fmt.Println(data) // Result: { Id: "1", Name: "ABC" )

func NewIdentifiableFilePersistence

func NewIdentifiableFilePersistence[T any, K any](persister *JsonFilePersister[T]) *IdentifiableFilePersistence[T, K]

NewIdentifiableFilePersistence creates a new instance of the persistence.

Typed params:
	- T cdata.IDataObject[T, K] any type that implemented
		IDataObject interface of getting element
	- K any type if id (key)
Parameters:
	- persister (optional) a persister component that loads and saves data from/to flat file.
Returns: *IdentifiableFilePersistence pointer on new IdentifiableFilePersistence

func (*IdentifiableFilePersistence[T, K]) Configure

func (c *IdentifiableFilePersistence[T, K]) Configure(ctx context.Context, config *config.ConfigParams)

Configure component by passing configuration parameters.

Parameters:
	- ctx context.Context
	- config *config.ConfigParams configuration parameters to be set.

type IdentifiableMemoryPersistence

type IdentifiableMemoryPersistence[T any, K any] struct {
	*MemoryPersistence[T]
	Mtx sync.RWMutex
}

IdentifiableMemoryPersistence Abstract persistence component that stores data in memory and implements a number of CRUD operations over data items with unique ids.

In basic scenarios' child structs shall only override GetPageByFilter, GetListByFilter or DeleteByFilter operations with specific filter function. All other operations can be used out of the box.

In complex scenarios' child structs can implement additional operations by accessing cached items via c.Items property and calling Save method on updates.

Important:
	- this component is a thread save!
	- the data items must implement IDataObject interface

see MemoryPersistence

Configuration parameters:
	- options
	- max_page_size maximum number of items returned in a single page (default: 100)
References:
	- *:logger:*:*:1.0 (optional) ILogger components to pass log messages
Typed params:
	- T cdata.IDataObject[T, K] any type that implemented
		IDataObject interface of getting element
	- K any type if id (key)
Examples:
	type MyMemoryPersistence struct {
		*IdentifiableMemoryPersistence[*MyData, string]
	}

	func NewMyMemoryPersistence() *MyMemoryPersistence {
		return &MyMemoryPersistence{IdentifiableMemoryPersistence: NewIdentifiableMemoryPersistence[*MyData, string]()}
	}
	func (c *MyMemoryPersistence) composeFilter(filter cdata.FilterParams) func(item *MyData) bool {
		name, _ := filter.GetAsNullableString("Name")
		return func(item *MyData) bool {
			if name != "" && item.Name != name {
				return false
			}
			return true
		}
	}

	func (c *MyMemoryPersistence) GetPageByFilter(ctx context.Context,
		filter FilterParams, paging PagingParams) (page cdata.DataPage[*MyData], err error) {
		return c.GetPageByFilter(ctx, c.composeFilter(filter), paging, nil, nil)
	}

	func f() {
		persistence := NewMyMemoryPersistence()

		item, err := persistence.Create(context.Background(), "123", &MyData{Id: "1", Name: "ABC"})
		// ...
		page, err := persistence.GetPageByFilter(context.Background(), *NewFilterParamsFromTuples("Name", "ABC"), nil)
		if err != nil {
			panic("Error can't get data")
		}
		data := page.Data
		fmt.Println(data) // Result: { Id: "1", Name: "ABC" }
		item, err = persistence.DeleteById(context.Background(), "123", "1")
		// ...
	}

	func (c *MyData) Clone() *MyData {
		return &MyData{Id: c.Id, Name: c.Name}
	}

	type MyData struct {
		Id   string
		Name string
	}

Extends: MemoryPersistence
Implements: IConfigurable, IWriter, IGetter, ISetter

func NewIdentifiableMemoryPersistence

func NewIdentifiableMemoryPersistence[T any, K any]() (c *IdentifiableMemoryPersistence[T, K])

NewIdentifiableMemoryPersistence creates a new empty instance of the persistence.

Typed params:
	- T cdata.IDataObject[T, K] any type that implemented
		IDataObject interface of getting element
	- K any type if id (key)

Returns: *IdentifiableMemoryPersistence created empty IdentifiableMemoryPersistence

func (*IdentifiableMemoryPersistence[T, K]) Configure

func (c *IdentifiableMemoryPersistence[T, K]) Configure(ctx context.Context, config *config.ConfigParams)

Configure component by passing configuration parameters.

Parameters:
	- ctx context.Context	operation context
	- config *config.ConfigParams configuration parameters to be set.

func (*IdentifiableMemoryPersistence[T, K]) Create

func (c *IdentifiableMemoryPersistence[T, K]) Create(ctx context.Context, item T) (T, error)

Create a data item.

Parameters:
	- ctx context.Context execution context to trace execution through call chain.
	- item T an item to be created.
Returns: T, error created item or error.

func (*IdentifiableMemoryPersistence[T, K]) DeleteById

func (c *IdentifiableMemoryPersistence[T, K]) DeleteById(ctx context.Context, id K) (T, error)

DeleteById a data item by it's unique id.

Parameters:
	- ctx context.Context execution context to trace execution through call chain.
	- id K an id of the item to be deleted
Returns: T, error deleted item or error.

func (*IdentifiableMemoryPersistence[T, K]) DeleteByIds

func (c *IdentifiableMemoryPersistence[T, K]) DeleteByIds(ctx context.Context, ids []K) error

DeleteByIds multiple data items by their unique ids.

Parameters:
	- ctx context.Context execution context to trace execution through call chain.
	- ids []K ids of data items to be deleted.
Returns: error or null for success.

func (*IdentifiableMemoryPersistence[T, K]) GetIndexById

func (c *IdentifiableMemoryPersistence[T, K]) GetIndexById(id K) int

GetIndexById get index by "Id" field

Parameters:
	- id K id parameter of data struct
Returns: index number

func (*IdentifiableMemoryPersistence[T, K]) GetListByIds

func (c *IdentifiableMemoryPersistence[T, K]) GetListByIds(ctx context.Context,
	ids []K) ([]T, error)

GetListByIds gets a list of data items retrieved by given unique ids.

Parameters:
	- ctx context.Context execution context to trace execution through call chain.
	- ids []K ids of data items to be retrieved
Returns: []T, error data list or error.

func (*IdentifiableMemoryPersistence[T, K]) GetOneById

func (c *IdentifiableMemoryPersistence[T, K]) GetOneById(ctx context.Context, id K) (T, error)

GetOneById gets a data item by its unique id.

Parameters:
	- ctx context.Context execution context to trace execution through call chain.
	- id K an id of data item to be retrieved.

Returns: T, error data item or error.

func (*IdentifiableMemoryPersistence[T, K]) Set

func (c *IdentifiableMemoryPersistence[T, K]) Set(ctx context.Context, item T) (T, error)

Set a data item. If the data item exists it updates it, otherwise it creates a new data item.

Parameters:
	- ctx context.Context execution context to trace execution through call chain.
	- item T a item to be set.

Returns: T, error updated item or error.

func (*IdentifiableMemoryPersistence[T, K]) Update

func (c *IdentifiableMemoryPersistence[T, K]) Update(ctx context.Context, item T) (T, error)

Update a data item.

Parameters:
	- ctx context.Context execution context to trace execution through call chain.
	- item T an item to be updated.

Returns: T, error updated item or error.

func (*IdentifiableMemoryPersistence[T, K]) UpdatePartially

func (c *IdentifiableMemoryPersistence[T, K]) UpdatePartially(ctx context.Context,
	id K, data cdata.AnyValueMap) (T, error)

UpdatePartially only few selected fields in a data item.

Parameters:
	- ctx context.Context execution context to trace execution through call chain.
	- id K an id of data item to be updated.
	- data  cdata.AnyValueMap a map with fields to be updated.

Returns: T, error updated item or error.

type JsonFilePersister

type JsonFilePersister[T any] struct {
	// contains filtered or unexported fields
}

JsonFilePersister is a persistence component that loads and saves data from/to flat file. It is used by FilePersistence, but can be useful on its own.

Important: this component is not thread save!
Configuration parameters:
	- path to the file where data is stored
Typed params:
	- T any type
Example:
	persister := NewJsonFilePersister[MyData]("./data/data.json")
	err := persister.Save(context.Background(), "123", []string{"A", "B", "C"})
	if err == nil {
		items, err := persister.Load("123")
		if err == nil {
			fmt.Println(items) // Result: ["A", "B", "C"]
		}
	}
Implements: ILoader, ISaver, IConfigurable

func NewJsonFilePersister

func NewJsonFilePersister[T any](path string) *JsonFilePersister[T]

NewJsonFilePersister creates a new instance of the persistence.

Typed params:
	- T any type
Parameters: path string (optional) a path to the file where data is stored.

func (*JsonFilePersister[T]) Configure

func (c *JsonFilePersister[T]) Configure(ctx context.Context, config *config.ConfigParams)

Configure component by passing configuration parameters.

Parameters:
	- ctx context.Context	operation context
	- config: ConfigParams configuration parameters to be set.

func (*JsonFilePersister[T]) Load

func (c *JsonFilePersister[T]) Load(ctx context.Context) ([]T, error)

Load data items from external JSON file.

Parameters:
	- ctx context.Context execution context to trace execution through call chain.

Returns: []T, error loaded items or error.

func (*JsonFilePersister[T]) Path

func (c *JsonFilePersister[T]) Path() string

Path gets the file path where data is stored.

Returns: the file path where data is stored.

func (*JsonFilePersister[T]) Save

func (c *JsonFilePersister[T]) Save(ctx context.Context, items []T) error

Save given data items to external JSON file.

	Parameters:
		- ctx context.Context execution context to trace execution through call chain.
		- items []T list of data items to save
 Returns: error or nil for success.

func (*JsonFilePersister[T]) SetPath

func (c *JsonFilePersister[T]) SetPath(value string)

SetPath the file path where data is stored.

Parameters:
	- value string the file path where data is stored.

type MemoryPersistence

type MemoryPersistence[T any] struct {
	Logger *log.CompositeLogger
	Items  []T
	Loader read.ILoader[T]
	Saver  write.ISaver[T]
	Mtx    sync.RWMutex

	MaxPageSize int
	// contains filtered or unexported fields
}

MemoryPersistence abstract persistence component that stores data in memory.

This is the most basic persistence component that is only
able to store data items of any type. Specific CRUD operations
over the data items must be implemented in child struct by
accessing Items property and calling Save method.

The component supports loading and saving items from another data source.
That allows to use it as a base struct for file and other types
of persistence components that cache all data in memory.

Important:
	- this component is a thread save!
	- if data object will implement ICloneable interface, it rises speed of execution
References:
	*:logger:*:*:1.0    ILogger components to pass log messages
Typed params:
	- T cdata.ICloneable[T] any type that implemented
		ICloneable interface of getting element
Example:
	type MyMemoryPersistence struct {
		*MemoryPersistence[MyData]
	}

	func (c *MyMemoryPersistence) GetByName(ctx context.Context,
		name string) (MyData, error) {
		for _, v := range c.Items {
			if v.Name == name {
				return v
			}
		}
		var defaultValue T
		return defaultValue, nil
	}

Implements: IReferenceable, IOpenable, ICleanable

func NewMemoryPersistence

func NewMemoryPersistence[T any]() *MemoryPersistence[T]

NewMemoryPersistence creates a new instance of the MemoryPersistence

Typed params:
	- T cdata.ICloneable[T] any type that implemented
		ICloneable interface of getting element
Return *MemoryPersistence[T]

func (*MemoryPersistence[T]) Clear

func (c *MemoryPersistence[T]) Clear(ctx context.Context) error

Clear component state.

Parameters:
	- ctx context.Context execution context to trace execution through call chain.
Returns: error or null no errors occurred.

func (*MemoryPersistence[T]) Close

func (c *MemoryPersistence[T]) Close(ctx context.Context) error

Close component and frees used resources.

Parameters:
	- ctx context.Context execution context to trace execution through call chain.
Returns: error or null no errors occurred.

func (*MemoryPersistence[T]) Create

func (c *MemoryPersistence[T]) Create(ctx context.Context, item T) (T, error)

Create a data item.

Parameters:
	- ctx context.Context execution context to trace execution through call chain.
	- item T an item to be created.
Returns: T, error created item or error.

func (*MemoryPersistence[T]) DeleteByFilter

func (c *MemoryPersistence[T]) DeleteByFilter(ctx context.Context,
	filterFunc func(T) bool) error

DeleteByFilter data items that match to a given filter. this method shall be called by a func (c* IdentifiableMemoryPersistence) DeleteByFilter method from child struct that receives FilterParams and converts them into a filter function.

Parameters:
	- ctx context.Context execution context to trace execution through call chain.
	- filter  filter func(T) bool (optional) a filter function to filter items.
Returns: error or nil for success.

func (*MemoryPersistence[T]) GetCountByFilter

func (c *MemoryPersistence[T]) GetCountByFilter(ctx context.Context,
	filterFunc func(T) bool) (int64, error)

GetCountByFilter gets a count of data items retrieved by a given filter. this method shall be called by a func (imp* IdentifiableMemoryPersistence) getCountByFilter method from child struct that receives FilterParams and converts them into a filter function.

Parameters:
	- ctx context.Context execution context to trace execution through call chain.
	- filter func(T) bool (optional) a filter function to filter items
Return int64, error data count or error.

func (*MemoryPersistence[T]) GetListByFilter

func (c *MemoryPersistence[T]) GetListByFilter(ctx context.Context,
	filterFunc func(T) bool,
	sortFunc func(T, T) bool,
	selectFunc func(T) T) ([]T, error)

GetListByFilter gets a list of data items retrieved by a given filter and sorted according to sort parameters. This method shall be called by a func (c * IdentifiableMemoryPersistence) GetListByFilter method from child struct that receives FilterParams and converts them into a filter function.

Parameters:
	- ctx context.Context execution context to trace execution through call chain.
	- filter func(T) bool (optional) a filter function to filter items
	- sortFunc func(a, b T) bool (optional) sorting compare function
		func Less (a, b T) bool  see sort.Interface Less function
	- selectFunc func(in T) (out T) (optional) projection parameters
Returns: []T, error array of items and error

func (*MemoryPersistence[T]) GetOneRandom

func (c *MemoryPersistence[T]) GetOneRandom(ctx context.Context,
	filterFunc func(T) bool) (T, error)

GetOneRandom gets a random item from items that match to a given filter. This method shall be called by a func (c* IdentifiableMemoryPersistence) GetOneRandom method from child type that receives FilterParams and converts them into a filter function.

Parameters:
	- ctx context.Context execution context to trace execution through call chain.
	- filter func(T) bool (optional) a filter function to filter items.
Returns: T, error random item or error.

func (*MemoryPersistence[T]) GetPageByFilter

func (c *MemoryPersistence[T]) GetPageByFilter(ctx context.Context,
	filterFunc func(T) bool,
	paging cquery.PagingParams,
	sortFunc func(T, T) bool,
	selectFunc func(T) T) (cquery.DataPage[T], error)

GetPageByFilter gets a page of data items retrieved by a given filter and sorted according to sort parameters. method shall be called by a func (imp* IdentifiableMemoryPersistence) getPageByFilter method from child struct that receives FilterParams and converts them into a filter function.

Parameters:
	- ctx context.Context execution context to trace execution through call chain.
	- filter func(any) bool (optional) a filter function to filter items
	- paging cdata.PagingParams (optional) paging parameters
	- sortFunc func(a, b T) bool (optional) sorting compare function func Less (a, b T) bool
		see sort.Interface Less function
	- selectFunc func(in T}) (out interface{}) (optional) projection parameters
Return cdata.DataPage[T], error data page or error.

func (*MemoryPersistence[T]) IsOpen

func (c *MemoryPersistence[T]) IsOpen() bool

IsOpen checks if the component is opened.

Returns true if the component has been opened and false otherwise.

func (*MemoryPersistence[T]) Open

func (c *MemoryPersistence[T]) Open(ctx context.Context) error

Open the component.

Parameters:
	- ctx context.Context execution context to trace execution through call chain.
Returns: error or null no errors occurred.

func (*MemoryPersistence[T]) Save

func (c *MemoryPersistence[T]) Save(ctx context.Context) error

Save items to external data source using configured saver component.

Parameters:
	- ctx context.Context execution context to trace execution through call chain.
Returns: error or null for success.

func (*MemoryPersistence[T]) SetReferences

func (c *MemoryPersistence[T]) SetReferences(ctx context.Context, references refer.IReferences)

SetReferences references to dependent components.

Parameters:
	- ctx context.Context
	- references refer.IReferences references to locate the component dependencies.

Jump to

Keyboard shortcuts

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