Class MemoryPersistence<T>
- All Implemented Interfaces:
org.pipservices3.commons.config.IConfigurable
,org.pipservices3.commons.refer.IReferenceable
,org.pipservices3.commons.run.ICleanable
,org.pipservices3.commons.run.IClosable
,org.pipservices3.commons.run.IOpenable
- Direct Known Subclasses:
FilePersistence
,IdentifiableMemoryPersistence
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 this._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 class for file and other types of persistence components that cache all data in memory.
### References ###
- *:logger:*:*:1.0 (optional) ILogger components to pass log messages
### Example ###
class MyMemoryPersistence extends MemoryPersistence {
public MyData getByName(String correlationId, String name) {
MyData item = find(name); // search method
...
return item;
});
public MyData set(String correlatonId, MyData item) {
this._items = filter(); // filter method
...
this._items.add(item);
this.save(correlationId);
}
}
MyMemoryPersistence persistence = new MyMemoryPersistence();
persistence.set("123", new MyData("ABC"));
System.out.println(persistence.getByName("123", "ABC")).toString(); // Result: { name: "ABC" }
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
MemoryPersistence
(Class<T> type) Creates a new instance of the persistence.protected
Creates a new instance of the persistence. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Clears component state.void
Closes component and frees used resources.void
configure
(org.pipservices3.commons.config.ConfigParams config) Configures component by passing configuration parameters.Creates a data item.void
deleteByFilter
(String correlationId, Predicate<T> filter) Deletes data items that match to a given filter.protected int
getCountByFilter
(String correlationId, Predicate<T> filter) Gets a number of items retrieved by a given filter.getListByFilter
(String correlationId, Predicate<T> filter, Comparator<T> sort) Gets a list of data items retrieved by a given filter and sorted according to sort parameters.getListByFilter
(String correlationId, Predicate<T> filter, Comparator<T> sort, Function<T, T> select) Gets a list of data items retrieved by a given filter and sorted according to sort parameters.protected T
getOneRandom
(String correlationId, Predicate<T> filter) Gets a random item from items that match to a given filter.protected org.pipservices3.commons.data.DataPage<T>
getPageByFilter
(String correlationId, Predicate<T> filter, org.pipservices3.commons.data.PagingParams paging, Comparator<T> sort) Gets a page of data items retrieved by a given filter and sorted according to sort parameters.protected org.pipservices3.commons.data.DataPage<T>
getPageByFilter
(String correlationId, Predicate<T> filter, org.pipservices3.commons.data.PagingParams paging, Comparator<T> sort, Function<T, T> select) Gets a page of data items retrieved by a given filter and sorted according to sort parameters.boolean
isOpen()
Checks if the component is opened.void
Opens the component.void
Saves items to external data source using configured saver component.void
setReferences
(org.pipservices3.commons.refer.IReferences references) Sets references to dependent components.
-
Field Details
-
_type
-
_typeName
-
_logger
protected org.pipservices3.components.log.CompositeLogger _logger -
_items
-
_loader
-
_saver
-
_opened
protected boolean _opened -
_maxPageSize
protected int _maxPageSize -
_lock
-
-
Constructor Details
-
MemoryPersistence
Creates a new instance of the persistence.- Parameters:
type
- the class type
-
MemoryPersistence
Creates a new instance of the persistence.- Parameters:
type
- the class typeloader
- (optional) a loader to load items from external datasource.saver
- (optional) a saver to save items to external datasource.
-
-
Method Details
-
configure
public void configure(org.pipservices3.commons.config.ConfigParams config) throws org.pipservices3.commons.errors.ConfigException Configures component by passing configuration parameters.- Specified by:
configure
in interfaceorg.pipservices3.commons.config.IConfigurable
- Parameters:
config
- configuration parameters to be set.- Throws:
org.pipservices3.commons.errors.ConfigException
-
setReferences
public void setReferences(org.pipservices3.commons.refer.IReferences references) throws org.pipservices3.commons.refer.ReferenceException Sets references to dependent components.- Specified by:
setReferences
in interfaceorg.pipservices3.commons.refer.IReferenceable
- Parameters:
references
- references to locate the component dependencies.- Throws:
org.pipservices3.commons.refer.ReferenceException
- when no found references.
-
isOpen
public boolean isOpen()Checks if the component is opened.- Specified by:
isOpen
in interfaceorg.pipservices3.commons.run.IOpenable
- Returns:
- true if the component has been opened and false otherwise.
-
open
Opens the component.- Specified by:
open
in interfaceorg.pipservices3.commons.run.IOpenable
- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.- Throws:
org.pipservices3.commons.errors.ApplicationException
- when error occured.
-
close
Closes component and frees used resources.- Specified by:
close
in interfaceorg.pipservices3.commons.run.IClosable
- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.- Throws:
org.pipservices3.commons.errors.ApplicationException
- when error occured.
-
save
Saves items to external data source using configured saver component.- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.- Throws:
org.pipservices3.commons.errors.ApplicationException
- when error occured.
-
clear
Clears component state.- Specified by:
clear
in interfaceorg.pipservices3.commons.run.ICleanable
- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.- Throws:
org.pipservices3.commons.errors.ApplicationException
- when error occured.
-
getPageByFilter
protected org.pipservices3.commons.data.DataPage<T> getPageByFilter(String correlationId, Predicate<T> filter, org.pipservices3.commons.data.PagingParams paging, Comparator<T> sort) Gets a page of data items retrieved by a given filter and sorted according to sort parameters.This method shall be called by a public getPageByFilter method from child class that receives FilterParams and converts them into a filter function.
- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.filter
- (optional) a filter function to filter itemspaging
- (optional) paging parameterssort
- (optional) sorting parameters
-
getPageByFilter
protected org.pipservices3.commons.data.DataPage<T> getPageByFilter(String correlationId, Predicate<T> filter, org.pipservices3.commons.data.PagingParams paging, Comparator<T> sort, Function<T, T> select) throws org.pipservices3.commons.errors.ApplicationExceptionGets a page of data items retrieved by a given filter and sorted according to sort parameters.This method shall be called by a public getPageByFilter method from child class that receives FilterParams and converts them into a filter function.
- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.filter
- (optional) a filter function to filter itemspaging
- (optional) paging parameterssort
- (optional) sorting parametersselect
- (optional) projection parameters (not used yet)- Returns:
- a data page of result by filter.
- Throws:
org.pipservices3.commons.errors.ApplicationException
- when error occured.
-
getCountByFilter
Gets a number of items retrieved by a given filter.This method shall be called by a public getCountByFilter method from child class that receives FilterParams and converts them into a filter function.
- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.filter
- (optional) a filter function to filter items
-
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 public getListByFilter method from child class that receives FilterParams and converts them into a filter function.
- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.filter
- (optional) a filter function to filter itemssort
- (optional) sorting parameters- Returns:
- a data list of results by filter.
-
getListByFilter
protected List<T> getListByFilter(String correlationId, Predicate<T> filter, Comparator<T> sort, Function<T, T> select) throws org.pipservices3.commons.errors.ApplicationExceptionGets a list of data items retrieved by a given filter and sorted according to sort parameters.This method shall be called by a public getListByFilter method from child class that receives FilterParams and converts them into a filter function.
- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.filter
- (optional) a filter function to filter itemssort
- (optional) sorting parametersselect
- (optional) projection parameters (not used yet)- Returns:
- a data list of results by filter.
- Throws:
org.pipservices3.commons.errors.ApplicationException
- when error occured.
-
getOneRandom
Gets a random item from items that match to a given filter.This method shall be called by a public getOneRandom method from child class that receives FilterParams and converts them into a filter function.
- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.filter
- (optional) a filter function to filter items.
-
create
public T create(String correlationId, T item) throws IOException, org.pipservices3.commons.errors.ApplicationException Creates a data item.- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.item
- an item to be created.- Throws:
IOException
org.pipservices3.commons.errors.ApplicationException
-
deleteByFilter
public void deleteByFilter(String correlationId, Predicate<T> filter) throws org.pipservices3.commons.errors.ApplicationException Deletes data items that match to a given filter.This method shall be called by a public deleteByFilter method from child class that receives FilterParams and converts them into a filter function.
- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.filter
- (optional) a filter function to filter items.- Throws:
org.pipservices3.commons.errors.ApplicationException
- when error occured.
-