I
- the type of entity identifiersE
- the type of stored entitiesS
- the type of entity state messagespublic abstract class RecordBasedRepository<I,E extends Entity<I,S>,S extends io.spine.base.EntityState> extends Repository<I,E>
Such a repository is backed by RecordStorage
.
Entity states are stored as EntityRecord
s.
Modifier | Constructor and Description |
---|---|
protected |
RecordBasedRepository()
Creates a new instance.
|
Modifier and Type | Method and Description |
---|---|
<T extends TransactionalEntity<I,S,?>> |
applyMigration(I id,
Migration<I,T,S> migration)
Applies a given
Migration to an entity with the given ID. |
<T extends TransactionalEntity<I,S,?>> |
applyMigration(java.util.Set<I> ids,
Migration<I,T,S> migration)
Applies a
Migration to several entities in batch. |
E |
create(I id)
Create a new entity instance with its default state.
|
protected RecordStorage<I> |
createStorage()
Creates the storage for this repository.
|
protected abstract EntityFactory<E> |
entityFactory()
Obtains
EntityFactory associated with this repository. |
java.util.Optional<E> |
find(I id)
Finds an entity with the passed ID.
|
java.util.Iterator<E> |
find(io.spine.client.TargetFilters filters,
io.spine.client.ResponseFormat format)
Finds the entities passing the given filters and applies the given
FieldMask
to the results. |
java.util.Optional<E> |
findActive(I id)
Finds an entity with the passed ID even if the entity is
active.
|
protected E |
findOrCreate(I id)
Loads an entity by the passed ID or creates a new one, if the entity was not found.
|
java.util.Iterator<E> |
iterator(java.util.function.Predicate<E> filter)
Returns an iterator over the entities managed by the repository that match the passed filter.
|
java.util.Iterator<E> |
loadAll(java.lang.Iterable<I> ids,
com.google.protobuf.FieldMask fieldMask)
Loads all the entities in this repository with IDs,
contained within the passed
ids values. |
java.util.Iterator<E> |
loadAll(io.spine.client.ResponseFormat format) |
protected RecordStorage<I> |
recordStorage()
Ensures that the repository has the storage.
|
protected abstract StorageConverter<I,E,S> |
storageConverter()
Obtains
StorageConverter associated with this repository. |
void |
store(java.util.Collection<E> entities)
Stores Entities in bulk.
|
void |
store(E entity)
Stores the passed object.
|
protected E |
toEntity(io.spine.server.entity.EntityRecord record)
Converts the passed record into an entity.
|
checkStorage, close, context, entityClass, entityStateType, eventFilter, hasContext, idClass, index, isOpen, isRegistered, onRegistered, open, storage, storageAssigned
protected abstract EntityFactory<E> entityFactory()
EntityFactory
associated with this repository.protected abstract StorageConverter<I,E,S> storageConverter()
StorageConverter
associated with this repository.protected RecordStorage<I> recordStorage()
java.lang.IllegalStateException
- if the storage is null@OverridingMethodsMustInvokeSuper public E create(I id)
Repository
public void store(E entity)
Repository
Note: The storage must be assigned before calling this method.
public java.util.Iterator<E> iterator(java.util.function.Predicate<E> filter)
Repository
The returned iterator does not support removal.
Iteration through entities is performed by loading them one by one.
@Experimental public final <T extends TransactionalEntity<I,S,?>> void applyMigration(I id, Migration<I,T,S> migration)
Migration
to an entity with the given ID.
The operation is performed in three steps:
This operation is only supported for entities that are transactional.
java.lang.IllegalArgumentException
- if the entity with the given ID is not found in the repositoryjava.lang.IllegalStateException
- if the repository manages a non-transactional entity typeMigration
,
the batch version of the method
@Experimental public final <T extends TransactionalEntity<I,S,?>> void applyMigration(java.util.Set<I> ids, Migration<I,T,S> migration)
Migration
to several entities in batch.
The operation is performed in three steps:
Migration
back to the repository.
This operation is only supported for entities that are transactional.
java.lang.IllegalStateException
- if the repository manages a non-transactional entity typeMigration
protected RecordStorage<I> createStorage()
Repository
Default implementations use Repository.defaultStorageFactory()
invoking its method
which creates a storage compatible with the repository.
Overwrite this method for creating a custom implementation of Storage
.
createStorage
in class Repository<I,E extends Entity<I,S>>
public void store(java.util.Collection<E> entities)
Note: The storage must be assigned before calling this method.
entities
- the Entities to storepublic java.util.Optional<E> findActive(I id)
id
- the ID of the entity to findOptional.empty()
if there is no entity with such ID,
or the entity is not activeprotected E findOrCreate(I id)
An entity will be loaded whether its active or not.
The new entity is created if and only if there is no record with the corresponding ID.
id
- the ID of the entity to loadpublic java.util.Iterator<E> loadAll(io.spine.client.ResponseFormat format)
public java.util.Iterator<E> loadAll(java.lang.Iterable<I> ids, com.google.protobuf.FieldMask fieldMask)
ids
values.
Provides a convenience wrapper around multiple invocations of
RecordBasedRepository.find(Object)
. Descendants may optimize the execution of this
method, choosing the most suitable way for the particular storage engine used.
The result only contains those entities which IDs are contained inside
the passed ids
. The resulting collection is always returned
with no null
values.
The order of objects in the result is not guaranteed to be the same as the order of IDs passed as argument.
If the IDs contain duplicates, the result may also contain duplicates depending on a particular implementation.
The resulting entity state must be valid in terms of (required)
,
(required_fields)
, and (goes).with
options after the mask is applied.
Otherwise, an InvalidEntityStateException
is thrown.
Note: The storage must be assigned before calling this method.
ids
- entity IDs to search forfieldMask
- the entity state fields to loadIterable
public java.util.Iterator<E> find(io.spine.client.TargetFilters filters, io.spine.client.ResponseFormat format)
FieldMask
to the results.
A number of elements to retrieve can be limited to a certain number. The order of
the resulting entities is specified by the OrderBy
.
Field mask is applied according to FieldMask specs.
The field paths in the entity column field filters are specified to contain a single path member - the name of the entity column.
The filtering process is delegated to the underlying RecordStorage
.
Note: The storage must be assigned before calling this method.
filters
- the entity filtersformat
- the expected format of the query responseEntityQuery
@OverridingMethodsMustInvokeSuper protected E toEntity(io.spine.server.entity.EntityRecord record)