pip_services3_data.persistence.IdentifiableMemoryPersistence module

pip_services3_data.persistence.IdentifiableMemoryPersistence

Identifiable memory persistence implementation

copyright

Conceptual Vision Consulting LLC 2018-2019, see AUTHORS for more details.

license

MIT, see LICENSE for more details.

class pip_services3_data.persistence.IdentifiableMemoryPersistence.IdentifiableMemoryPersistence(loader: pip_services3_data.ILoader.ILoader = None, saver: pip_services3_data.ISaver.ISaver = None)

Bases: pip_services3_data.persistence.MemoryPersistence.MemoryPersistence, pip_services3_data.IWriter.IWriter, pip_services3_data.IGetter.IGetter, pip_services3_data.ISetter.ISetter, pip_services3_commons.data.IIdentifiable.IIdentifiable

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 get_page_by_filter(), get_list_by_filter() or delete_by_filter() 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

Example:

class MyMemoryPersistence(IdentifiableMemoryPersistence):

    def get_page_by_filter(self, correlationId, filter, paging):
        super().get_page_by_filter(correlationId, filter, paging, None)

    persistence = MyMemoryPersistence("./data/data.json")

    item = persistence.create("123", MyData("1", "ABC"))

    mydata = persistence.get_page_by_filter("123", FilterParams.from_tuples("name", "ABC"), None, None)
    print str(mydata.get_data())

    persistence.delete_by_id("123", "1")
create(correlation_id: Optional[str], item: T) → T

Creates a data item.

Parameters
  • correlation_id – (optional) transaction id to trace execution through call chain.

  • item – an item to be created.

Returns

a created item

delete_by_id(correlation_id: Optional[str], id: Any) → T

Deleted a data item by it’s unique id.

Parameters
  • correlation_id – (optional) transaction id to trace execution through call chain.

  • id – an id of the item to be deleted

Returns

a deleted item.

delete_by_ids(correlation_id: Optional[str], ids: List[Any])

Deletes multiple data items by their unique ids.

Parameters
  • correlation_id – (optional) transaction id to trace execution through call chain.

  • ids – ids of data items to be deleted.

get_list_by_ids(correlation_id: Optional[str], ids: List[Any]) → List[T]

Gets a list of data items retrieved by given unique ids.

Parameters
  • correlation_id – (optional) transaction id to trace execution through call chain.

  • ids – ids of data items to be retrieved

Returns

a data list of results by ids.

get_one_by_id(correlation_id: Optional[str], id: Any) → T

Gets a data item by its unique id.

Parameters
  • correlation_id – (optional) transaction id to trace execution through call chain.

  • id – an id of data item to be retrieved.

Returns

data item by id.

id: Any
set(correlation_id: Optional[str], item: T) → T

Sets a data item. If the data item exists it updates it, otherwise it create a new data item.

Parameters
  • correlation_id – (optional) transaction id to trace execution through call chain.

  • item – an item to be set.

Returns

an updated item

update(correlation_id: Optional[str], new_item: T) → T

Updates a data item.

Parameters
  • correlation_id – (optional) transaction id to trace execution through call chain.

  • new_item – an item to be updated.

Returns

an updated item.

update_partially(correlation_id: Optional[str], id: Any, data: pip_services3_commons.data.AnyValueMap.AnyValueMap) → T

Updates only few selected fields in a data item.

Parameters
  • correlation_id – (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

an updated item.