Class MemoryPersistence<T>

java.lang.Object
org.pipservices3.data.persistence.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

public abstract class MemoryPersistence<T> extends Object implements org.pipservices3.commons.config.IConfigurable, org.pipservices3.commons.refer.IReferenceable, org.pipservices3.commons.run.IOpenable, org.pipservices3.commons.run.ICleanable
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 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 Details

    • _type

      protected Class<?> _type
    • _typeName

      protected String _typeName
    • _logger

      protected org.pipservices3.components.log.CompositeLogger _logger
    • _items

      protected List<T> _items
    • _loader

      protected ILoader<T> _loader
    • _saver

      protected ISaver<T> _saver
    • _opened

      protected boolean _opened
    • _maxPageSize

      protected int _maxPageSize
    • _lock

      protected final Object _lock
  • Constructor Details

    • MemoryPersistence

      protected MemoryPersistence(Class<T> type)
      Creates a new instance of the persistence.
      Parameters:
      type - the class type
    • MemoryPersistence

      protected MemoryPersistence(Class<T> type, ILoader<T> loader, ISaver<T> saver)
      Creates a new instance of the persistence.
      Parameters:
      type - the class type
      loader - (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 interface org.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 interface org.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 interface org.pipservices3.commons.run.IOpenable
      Returns:
      true if the component has been opened and false otherwise.
    • open

      public void open(String correlationId) throws org.pipservices3.commons.errors.ApplicationException
      Opens the component.
      Specified by:
      open in interface org.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

      public void close(String correlationId) throws org.pipservices3.commons.errors.ApplicationException
      Closes component and frees used resources.
      Specified by:
      close in interface org.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

      public void save(String correlationId) throws org.pipservices3.commons.errors.ApplicationException
      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

      public void clear(String correlationId) throws org.pipservices3.commons.errors.ApplicationException
      Clears component state.
      Specified by:
      clear in interface org.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 items
      paging - (optional) paging parameters
      sort - (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.ApplicationException
      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 items
      paging - (optional) paging parameters
      sort - (optional) sorting parameters
      select - (optional) projection parameters (not used yet)
      Returns:
      a data page of result by filter.
      Throws:
      org.pipservices3.commons.errors.ApplicationException - when error occured.
    • getCountByFilter

      protected int getCountByFilter(String correlationId, Predicate<T> filter)
      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

      protected List<T> 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.

      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 items
      sort - (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.ApplicationException
      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 items
      sort - (optional) sorting parameters
      select - (optional) projection parameters (not used yet)
      Returns:
      a data list of results by filter.
      Throws:
      org.pipservices3.commons.errors.ApplicationException - when error occured.
    • getOneRandom

      protected T getOneRandom(String correlationId, Predicate<T> filter)
      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.