MemoryPersistence<T>

Persistence component that stores data in memory.

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.

public MemoryPersistence(Class type, 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

_logger

Logger.

protected CompositeLogger _logger = new CompositeLogger()

_items

Items to load/save.

protected List _items = new ArrayList<>()

_loader

Loader.

protected ILoader _loader

_saver

Saver.

protected ISaver _saver

_opened

Boolean that indicates whether the compent is open or not.

protected boolean _opened = false

_maxPageSize

Maximum amount of items per page.

protected int _maxPageSize = 100

Instance methods

clear

Clears the component’s state.

public void 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.

public void configure(ConfigParams config) throws ConfigException

  • config: ConfigParams - configuration parameters to be set.

create

Creates a data item.

public T 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.

protected void deleteByFilter(IContext context, Predicate filter) 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.

protected int getListByFilter(IContext context, Predicate filter) 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.

protected List getListByFilter(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.

protected List getListByFilter(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.

protected T getOneRandom(IContext context, Predicate filter)

  • 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 getPageByFilter(IContext context, Predicate filter, PagingParams paging, Comparator sort)

  • 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.

public boolean isOpen()

  • returns: boolean - True if the component is open and False otherwise.

load

Loads items.

protected void load(IContext context) throws ApplicationException

  • context: IContext - (optional) a context to trace execution through a call chain.

open

Opens the component.

public void 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.

public void 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.

public void setReferences(IReferences references)

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" }
  }

See also