Implements: IConfigurable, IReferenceable, IOpenable, ICleanable
Description
The MemoryPersistence class allows you to create persistence components that store data in memory.
Important points
- 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 classes by accessing the this._items property and calling the save method.
- The component supports loading and saving items from another data source. This allows to use it as a base class for file and other types of persistence components that cache all data in memory.
Configuration parameters
options:
- max_page_size: maximum number of items returned in a single page (default: 100)
References
- *:logger:*:*:1.0 - (optional) (../../../components/log/ilogger) components to pass log messages
Constructors
Creates a new instance of the memory persistence component.
publicMemoryPersistence(Classtype, ILoader loader, ISaver saver)
- loader: ILoader
- (optional) loader used to load items from an external datasource. - saver: ISaver
- (optional) saver used to save items to an external datasource.
Fields
Instance methods
clear
Clears the component’s state.
publicvoid clear(IContextt context) throws ApplicationException
- context: IContext - (optional) a context to trace execution through a call chain.
configure
Configures the component by passing its configuration parameters.
publicvoid configure(ConfigParams config) throws ConfigException
- config: ConfigParams - configuration parameters to be set.
create
Creates a data item.
publicT create(IContext context, T item) throws IOException, ApplicationException
- context: IContext - (optional) a context to trace execution through a call chain.
- item: T - item to be created.
- returns: T - created item
deleteByFilter
Deletes data items that match to a given filter. This method shall be called by a public deleteByFilter method from a child class that receives FilterParams and converts them into a filter function.
protectedvoid deleteByFilter(IContext context, Predicatefilter) throws ApplicationException
- context: IContext - (optional) a context to trace execution through a call chain.
- filter: Predicate
- (optional) filter function used to filter items.
getCountByFilter
Gets the number of items retrieved by a given filter.
This method shall be called by a public getCountByFilter method from a child class that receives FilterParams and converts them into a filter function.
protectedint getListByFilter(IContext context, Predicatefilter) throws ApplicationException
- context: IContext - (optional) a context to trace execution through a call chain.
- filter: Predicate
- (optional) a filter function to filter items - returns: int - number of data items that satisfy the filter.
getListByFilter
Gets a list of data items retrieved by a given filter and sorted according to sorting parameters.
This method shall be called by a public getListByFilter method from a child class that receives FilterParams and converts them into a filter function.
protectedListgetListByFilter(context: IContext, Predicate filter, Comparator sort)
- context: IContext - (optional) a context to trace execution through a call chain.
- filter: Predicate
- (optional) filter function used to filter items - sort: Comparator
- (optional) sorting parameters - returns: List
- data list of filtered results.
getListByFilter
Gets a list of data items retrieved by a given filter and sorted according to sorting parameters.
This method shall be called by a public getListByFilter method from a child class that receives FilterParams and converts them into a filter function.
protectedListgetListByFilter(context: IContext, Predicate filter, Comparator sort, Function<T, T> select)
- context: IContext - (optional) a context to trace execution through a call chain.
- filter: Predicate
- (optional) filter function used to filter items - sort: Comparator
- (optional) sorting parameters - select: Function<T, T> - (optional) projection parameters (not used yet)
- returns: List
- data list of filtered results.
getOneRandom
Gets a random item from items that match to a given filter.
This method shall be called by a public getOneRandom method from a child class that receives FilterParams and converts them into a filter function.
protectedT getOneRandom(IContext context, Predicatefilter)
- context: IContext - (optional) a context to trace execution through a call chain.
- filter: Predicate
- (optional) a filter function to filter items. - returns: T - random item.
getPageByFilter
Gets a page of data items retrieved by a given filter and sorted according to sorting parameters.
This method shall be called by a public getPageByFilter method from a child class that receives FilterParams and converts them into a filter function.
DataPage
- context: IContext - (optional) a context to trace execution through a call chain.
- filter: Predicate
- filter function used to filter items - paging: PagingParams - (optional) paging parameters
- sort: Comparator
- (optional) sorting parameters - returns: DataPage
- data page with filterd results.
isOpen
Checks if the component is open.
publicboolean isOpen()
- returns: boolean - True if the component is open and False otherwise.
load
Loads items.
protectedvoid load(IContext context) throws ApplicationException
- context: IContext - (optional) a context to trace execution through a call chain.
open
Opens the component.
publicvoid open(context: IContext)
- context: IContext - (optional) a context to trace execution through a call chain.
save
Saves items to an external data source using a configured saver component.
publicvoid save(IContext context) throws ApplicationException
- context: IContext - (optional) a context to trace execution through a call chain.
setReferences
Sets the component’s references. References must match configured dependencies.
publicvoid setReferences(IReferences references)
- references: IReferences - references to set.
Examples
{@code
class MyMemoryPersistence extends MemoryPersistence {
public MyData getByName(IContext context, String name) {
MyData item = find(name); // search method
...
return item;
});
public MyData set(IContext context, MyData item) {
this._items = filter(); // filter method
...
this._items.add(item);
this.save(context);
}
}
MyMemoryPersistence persistence = new MyMemoryPersistence();
persistence.set("123", new MyData("ABC"));
System.out.println(persistence.getByName("123", "ABC")).toString(); // Result: { name: "ABC" }
}