Class IdentifiableMemoryPersistence<T extends org.pipservices3.commons.data.IIdentifiable<K>,K>

java.lang.Object
org.pipservices3.data.persistence.MemoryPersistence<T>
org.pipservices3.data.persistence.IdentifiableMemoryPersistence<T,K>
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, IGetter<T,K>, IPartialUpdater<T,K>, ISetter<T>, IWriter<T,K>
Direct Known Subclasses:
IdentifiableFilePersistence

public class IdentifiableMemoryPersistence<T extends org.pipservices3.commons.data.IIdentifiable<K>,K> extends MemoryPersistence<T> implements org.pipservices3.commons.config.IConfigurable, IWriter<T,K>, IGetter<T,K>, ISetter<T>, IPartialUpdater<T,K>
Abstract persistence component that stores data in memory and implements a number of CRUD operations over data items with unique ids. The data items must implement IIdentifiable 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 this._items property and calling save() method on updates.

### 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

### Examples ###

 
 class MyMemoryPersistence extends IdentifiableMemoryPersistence<MyData, String> {

   private Predicate<MyData> composeFilter(FilterParams filter) {
       filter = filter != null ? filter : new FilterParams();
       String name = filter.getAsNullableString("name");
       return (item) -> {
           if (name != null && item.name != name)
               return false;
           return true;
       };
   }

   public DataPage<MyData> getPageByFilter(String correlationId, FilterParams filter, PagingParams paging) {
       super.getPageByFilter(correlationId, this.composeFilter(filter), paging, null, null);
   }

 }

 MyMemoryPersistence persistence = new MyMemoryPersistence(MyData.class);

 MyData item = persistence.create("123", new MyData("1", "ABC"));
 DataPage<MyData> mydata = persistence.getPageByFilter(
         "123",
         FilterParams.fromTuples("name", "ABC"),
         null, null, null);
 System.out.println(mydata.getData().toString());          // Result: { id: "1", name: "ABC" }
 persistence.deleteById("123", "1");
 ...
 
 
See Also:
  • Constructor Details

    • IdentifiableMemoryPersistence

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

      protected IdentifiableMemoryPersistence(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

    • findOne

      protected T findOne(K id)
      Finds one element by id.
      Parameters:
      id - an id of data item.
      Returns:
      data item.
    • findAll

      protected List<T> findAll(K[] ids)
      Finds all elements by ids.
      Parameters:
      ids - ids of data items.
      Returns:
      data list of items.
    • getOneById

      public T getOneById(String correlationId, K id) throws org.pipservices3.commons.errors.ApplicationException
      Gets a data item by its unique id.
      Specified by:
      getOneById in interface IGetter<T extends org.pipservices3.commons.data.IIdentifiable<K>,K>
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      id - an id of data item to be retrieved.
      Returns:
      data item.
      Throws:
      org.pipservices3.commons.errors.ApplicationException - when error occured.
    • getListByIds

      public List<T> getListByIds(String correlationId, K[] ids) throws org.pipservices3.commons.errors.ApplicationException
      Gets a list of data items retrieved by given unique ids.
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      ids - ids of data items to be retrieved
      Returns:
      a data list.
      Throws:
      org.pipservices3.commons.errors.ApplicationException - when error occured.
    • getListByIds

      public List<T> getListByIds(String correlationId, List<K> ids) throws org.pipservices3.commons.errors.ApplicationException
      Gets a list of data items retrieved by given unique ids.
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      ids - ids of data items to be retrieved
      Returns:
      a data list.
      Throws:
      org.pipservices3.commons.errors.ApplicationException - when error occured.
    • create

      public T create(String correlationId, T item) throws org.pipservices3.commons.errors.ApplicationException
      Creates a data item.
      Specified by:
      create in interface IWriter<T extends org.pipservices3.commons.data.IIdentifiable<K>,K>
      Overrides:
      create in class MemoryPersistence<T extends org.pipservices3.commons.data.IIdentifiable<K>>
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      item - an item to be created.
      Returns:
      created item.
      Throws:
      org.pipservices3.commons.errors.ApplicationException - when error occured.
    • update

      public T update(String correlationId, T newItem) throws org.pipservices3.commons.errors.ApplicationException
      Updates a data item.
      Specified by:
      update in interface IWriter<T extends org.pipservices3.commons.data.IIdentifiable<K>,K>
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      newItem - an item to be updated.
      Returns:
      updated item.
      Throws:
      org.pipservices3.commons.errors.ApplicationException - when error occured.
    • set

      public T set(String correlationId, T newItem) throws org.pipservices3.commons.errors.ApplicationException
      Sets a data item. If the data item exists it updates it, otherwise it create a new data item.
      Specified by:
      set in interface ISetter<T extends org.pipservices3.commons.data.IIdentifiable<K>>
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      newItem - a item to be set.
      Returns:
      updated item.
      Throws:
      org.pipservices3.commons.errors.ApplicationException - when error occured.
    • deleteById

      public T deleteById(String correlationId, K id) throws org.pipservices3.commons.errors.ApplicationException
      Deleted a data item by it's unique id.
      Specified by:
      deleteById in interface IWriter<T extends org.pipservices3.commons.data.IIdentifiable<K>,K>
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      id - an id of the item to be deleted
      Returns:
      deleted item.
      Throws:
      org.pipservices3.commons.errors.ApplicationException - when error occured.
    • deleteByIds

      public void deleteByIds(String correlationId, K[] ids) throws org.pipservices3.commons.errors.ApplicationException
      Deletes multiple data items by their unique ids.
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      ids - ids of data items to be deleted.
      Throws:
      org.pipservices3.commons.errors.ApplicationException - when error occured.
    • updatePartially

      public T updatePartially(String correlationId, K id, org.pipservices3.commons.data.AnyValueMap data) throws org.pipservices3.commons.errors.ApplicationException
      Updates only few selected fields in a data item.
      Specified by:
      updatePartially in interface IPartialUpdater<T extends org.pipservices3.commons.data.IIdentifiable<K>,K>
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      id - an id of data item to be updated.
      data - a map with fields to be updated.
      Returns:
      updated item.
      Throws:
      org.pipservices3.commons.errors.ApplicationException