Class IdentifiableFilePersistence<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>
org.pipservices3.data.persistence.IdentifiableFilePersistence<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>
public class IdentifiableFilePersistence<T extends org.pipservices3.commons.data.IIdentifiable<K>,K>
extends IdentifiableMemoryPersistence<T,K>
Abstract persistence component that stores data in flat files
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 ###
- path: path to the file where data is stored
- 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 MyFilePersistence extends IdentifiableFilePersistence<MyData, String> {
public MyFilePersistence(String path) {
super(MyData.class, new JsonPersister(path));
}
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);
}
}
MyFilePersistence persistence = new MyFilePersistence("./data/data.json");
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(page.getData().toString()); // Result: { id: "1", name: "ABC" }
persistence.deleteById("123", "1");
...
- See Also:
-
Field Summary
FieldsFields inherited from class org.pipservices3.data.persistence.MemoryPersistence
_items, _loader, _lock, _logger, _maxPageSize, _opened, _saver, _type, _typeName -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedIdentifiableFilePersistence(Class<T> type) Creates a new instance of the persistence.protectedIdentifiableFilePersistence(Class<T> type, JsonFilePersister<T> persister) Creates a new instance of the persistence. -
Method Summary
Modifier and TypeMethodDescriptionvoidconfigure(org.pipservices3.commons.config.ConfigParams config) Configures component by passing configuration parameters.Methods inherited from class org.pipservices3.data.persistence.IdentifiableMemoryPersistence
create, deleteById, deleteByIds, findAll, findOne, getListByIds, getListByIds, getOneById, set, update, updatePartiallyMethods inherited from class org.pipservices3.data.persistence.MemoryPersistence
clear, close, deleteByFilter, getCountByFilter, getListByFilter, getListByFilter, getOneRandom, getPageByFilter, getPageByFilter, isOpen, open, save, setReferences
-
Field Details
-
_persister
-
-
Constructor Details
-
IdentifiableFilePersistence
Creates a new instance of the persistence.- Parameters:
type- the class type
-
IdentifiableFilePersistence
Creates a new instance of the persistence.- Parameters:
type- the class typepersister- (optional) a persister component that loads and saves data from/to flat file.
-
-
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:
configurein interfaceorg.pipservices3.commons.config.IConfigurable- Overrides:
configurein classMemoryPersistence<T extends org.pipservices3.commons.data.IIdentifiable<K>>- Parameters:
config- configuration parameters to be set.- Throws:
org.pipservices3.commons.errors.ConfigException
-