Module storm
Package st.orm.spi

Class ProjectionRepositoryImpl<P extends Record & Projection<ID>,ID>

java.lang.Object
st.orm.spi.ProjectionRepositoryImpl<P,ID>
Type Parameters:
P - the type of projection managed by this repository.
ID - the type of the primary key of the projection, or Void if the projection has no primary key.
All Implemented Interfaces:
ProjectionRepository<P,ID>, Repository

public final class ProjectionRepositoryImpl<P extends Record & Projection<ID>,ID> extends Object implements ProjectionRepository<P,ID>
Default implementation of ProjectionRepository.
  • Field Details

    • ormTemplate

      protected final ORMTemplate ormTemplate
    • model

      protected final Model<P extends Record & Projection<ID>,ID> model
    • defaultSliceSize

      protected final int defaultSliceSize
  • Constructor Details

    • ProjectionRepositoryImpl

      public ProjectionRepositoryImpl(@Nonnull ORMTemplate ormTemplate, @Nonnull Model<P,ID> model)
  • Method Details

    • ref

      public Ref<P> ref(@Nonnull P projection, @Nonnull ID id)
      Creates a new ref projection instance with the specified projection.
      Specified by:
      ref in interface ProjectionRepository<P extends Record & Projection<ID>,ID>
      Parameters:
      projection - the projection.
      Returns:
      a ref projection instance.
    • toStream

      protected static <X> Stream<X> toStream(@Nonnull Iterable<X> iterable)
      Converts an iterable collection to a stream.
      Type Parameters:
      X - the type of elements in the iterable collection.
      Parameters:
      iterable - the iterable collection to convert.
      Returns:
      a stream representing the elements of the iterable collection.
    • orm

      public ORMTemplate orm()
      Provides access to the underlying ORM template.
      Specified by:
      orm in interface Repository
      Returns:
      the ORM template.
    • model

      public Model<P,ID> model()
      Returns the model associated with this repository.
      Returns:
      the model.
    • ref

      public Ref<P> ref(@Nonnull ID id)
      Creates a new ref entity instance with the specified primary key.

      This method creates a lightweight reference that encapsulates only the primary key of an entity, without loading the full entity data into memory. The complete record can be fetched on demand by invoking Ref.fetch(), which will trigger a separate database call.

      Parameters:
      id - the primary key of the entity.
      Returns:
      a ref entity instance containing only the primary key.
      Since:
      1.3
    • select

      public QueryBuilder<P,P,ID> select()
      Creates a new query builder for the entity type managed by this repository.
      Returns:
      a new query builder for the entity type.
    • selectCount

      public QueryBuilder<P,Long,ID> selectCount()
      Creates a new query builder for the entity type managed by this repository.
      Returns:
      a new query builder for the entity type.
    • select

      public <R> QueryBuilder<P,R,ID> select(@Nonnull Class<R> selectType)
      Creates a new query builder for the custom selectType.
      Type Parameters:
      R - the result type of the query.
      Parameters:
      selectType - the result type of the query.
      Returns:
      a new query builder for the custom selectType.
    • select

      public <R> QueryBuilder<P,R,ID> select(@Nonnull Class<R> selectType, @Nonnull StringTemplatePREVIEW template)
      Creates a new query builder for the custom selectType and custom template for the select clause.
      Type Parameters:
      R - the result type of the query.
      Parameters:
      selectType - the result type of the query.
      template - the custom template for the select clause.
      Returns:
      a new query builder for the custom selectType.
    • count

      public long count()
      Returns the number of entities in the database of the entity type supported by this repository.
      Returns:
      the total number of entities in the database as a long value.
      Throws:
      PersistenceException - if the count operation fails due to underlying database issues, such as connectivity.
    • existsById

      public boolean existsById(@Nonnull ID id)
      Checks if an entity with the specified primary key exists in the database.

      This method determines the presence of an entity by checking if the count of entities with the given primary key is greater than zero. It leverages the selectCount method, which performs a count operation on the database.

      Parameters:
      id - the primary key of the entity to check for existence.
      Returns:
      true if an entity with the specified primary key exists, false otherwise.
      Throws:
      PersistenceException - if there is an underlying database issue during the count operation.
    • existsByRef

      public boolean existsByRef(@Nonnull Ref<P> ref)
      Checks if an entity with the specified primary key exists in the database.

      This method determines the presence of an entity by checking if the count of entities with the given primary key is greater than zero. It leverages the selectCount method, which performs a count operation on the database.

      Parameters:
      ref - the primary key of the entity to check for existence.
      Returns:
      true if an entity with the specified primary key exists, false otherwise.
      Throws:
      PersistenceException - if there is an underlying database issue during the count operation.
    • findById

      public Optional<P> findById(@Nonnull ID id)
      Retrieves an entity based on its primary key.

      This method performs a lookup in the database, returning the corresponding entity if it exists.

      Parameters:
      id - the primary key of the entity to retrieve.
      Returns:
      the entity associated with the provided primary key. The returned entity encapsulates all relevant data as mapped by the entity model.
      Throws:
      PersistenceException - if the retrieval operation fails due to underlying database issues, such as connectivity problems or query execution errors.
    • findByRef

      public Optional<P> findByRef(@Nonnull Ref<P> ref)
      Retrieves an entity based on its primary key, expressed by a ref.

      This method performs a lookup in the database, returning the corresponding entity if it exists.

      Parameters:
      ref - the ref to match.
      Returns:
      the entity associated with the provided primary key. The returned entity encapsulates all relevant data as mapped by the entity model.
      Throws:
      PersistenceException - if the retrieval operation fails due to underlying database issues, such as connectivity problems or query execution errors.
    • getById

      public P getById(@Nonnull ID id)
      Retrieves an entity based on its primary key.

      This method performs a lookup in the database, returning the corresponding entity if it exists.

      Parameters:
      id - the primary key of the entity to retrieve.
      Returns:
      the entity associated with the provided primary key. The returned entity encapsulates all relevant data as mapped by the entity model.
      Throws:
      NoResultException - if no entity is found matching the given primary key, indicating that there's no corresponding data in the database.
      PersistenceException - if the retrieval operation fails due to underlying database issues, such as connectivity problems or query execution errors.
    • getByRef

      public P getByRef(@Nonnull Ref<P> ref)
      Retrieves an entity based on its primary key, expressed by a ref.

      This method performs a lookup in the database, returning the corresponding entity if it exists.

      Parameters:
      ref - the ref to match.
      Returns:
      the entity associated with the provided primary key. The returned entity encapsulates all relevant data as mapped by the entity model.
      Throws:
      NoResultException - if no entity is found matching the given primary key, indicating that there's no corresponding data in the database.
      PersistenceException - if the retrieval operation fails due to underlying database issues, such as connectivity problems or query execution errors.
    • findAll

      public List<P> findAll()
      Returns a list of all entities of the type supported by this repository. Each element in the list represents an entity in the database, encapsulating all relevant data as mapped by the entity model.

      Please note: loading all entities into memory at once can be very memory-intensive if your table is large.

      Returns:
      a stream of all entities of the type supported by this repository.
      Throws:
      PersistenceException - if the selection operation fails due to underlying database issues, such as connectivity.
    • findAllById

      public List<P> findAllById(@Nonnull Iterable<ID> ids)
      Retrieves a list of entities based on their primary keys.

      This method retrieves entities matching the provided IDs in batches, consolidating them into a single list. The batch-based retrieval minimizes database overhead, allowing efficient handling of larger collections of IDs. Note that the order of entities in the returned list is not guaranteed to match the order of IDs in the input collection, as the database may not preserve insertion order during retrieval.

      Parameters:
      ids - the primary keys of the entities to retrieve, represented as an iterable collection.
      Returns:
      a list of entities corresponding to the provided primary keys. Entities are returned without any guarantee of order alignment with the input list. If an ID does not correspond to any entity in the database, no corresponding entity will be included in the returned list.
      Throws:
      PersistenceException - if the selection operation fails due to database issues, such as connectivity problems or invalid input parameters.
    • findAllByRef

      public List<P> findAllByRef(@Nonnull Iterable<Ref<P>> refs)
      Retrieves a list of entities based on their primary keys.

      This method retrieves entities matching the provided IDs in batches, consolidating them into a single list. The batch-based retrieval minimizes database overhead, allowing efficient handling of larger collections of IDs. Note that the order of entities in the returned list is not guaranteed to match the order of IDs in the input collection, as the database may not preserve insertion order during retrieval.

      Parameters:
      refs - the primary keys of the entities to retrieve, represented as an iterable collection.
      Returns:
      a list of entities corresponding to the provided primary keys. Entities are returned without any guarantee of order alignment with the input list. If an ID does not correspond to any entity in the database, no corresponding entity will be included in the returned list.
      Throws:
      PersistenceException - if the selection operation fails due to database issues, such as connectivity problems or invalid input parameters.
    • selectAll

      public Stream<P> selectAll()
      Returns a stream of all entities of the type supported by this repository. Each element in the stream represents an entity in the database, encapsulating all relevant data as mapped by the entity model.

      The resulting stream is lazily loaded, meaning that the entities are only retrieved from the database as they are consumed by the stream. This approach is efficient and minimizes the memory footprint, especially when dealing with large volumes of entities.

      Note that calling this method does trigger the execution of the underlying query, so it should only be invoked when the query is intended to run. Since the stream holds resources open while in use, it must be closed after usage to prevent resource leaks. As the stream is AutoCloseable, it is recommended to use it within a try-with-resources block.

      Returns:
      a stream of all entities of the type supported by this repository.
      Throws:
      PersistenceException - if the selection operation fails due to underlying database issues, such as connectivity.
    • selectAllById

      public Stream<P> selectAllById(@Nonnull Stream<ID> ids)
      Retrieves a stream of entities based on their primary keys.

      This method executes queries in batches, depending on the number of primary keys in the specified ids stream. This optimization aims to reduce the overhead of executing multiple queries and efficiently retrieve entities. The batching strategy enhances performance, particularly when dealing with large sets of primary keys.

      The resulting stream is lazily loaded, meaning that the entities are only retrieved from the database as they are consumed by the stream. This approach is efficient and minimizes the memory footprint, especially when dealing with large volumes of entities.

      Note that calling this method does trigger the execution of the underlying query, so it should only be invoked when the query is intended to run. Since the stream holds resources open while in use, it must be closed after usage to prevent resource leaks. As the stream is AutoCloseable, it is recommended to use it within a try-with-resources block.

      Parameters:
      ids - a stream of entity IDs to retrieve from the repository.
      Returns:
      a stream of entities corresponding to the provided primary keys. The order of entities in the stream is not guaranteed to match the order of ids in the input stream. If an id does not correspond to any entity in the database, it will simply be skipped, and no corresponding entity will be included in the returned stream. If the same entity is requested multiple times, it may be included in the stream multiple times if it is part of a separate batch.
      Throws:
      PersistenceException - if the selection operation fails due to underlying database issues, such as connectivity.
    • selectAllByRef

      public Stream<P> selectAllByRef(@Nonnull Stream<Ref<P>> refs)
      Retrieves a stream of entities based on their primary keys.

      This method executes queries in batches, depending on the number of primary keys in the specified ids stream. This optimization aims to reduce the overhead of executing multiple queries and efficiently retrieve entities. The batching strategy enhances performance, particularly when dealing with large sets of primary keys.

      The resulting stream is lazily loaded, meaning that the entities are only retrieved from the database as they are consumed by the stream. This approach is efficient and minimizes the memory footprint, especially when dealing with large volumes of entities.

      Note that calling this method does trigger the execution of the underlying query, so it should only be invoked when the query is intended to run. Since the stream holds resources open while in use, it must be closed after usage to prevent resource leaks. As the stream is AutoCloseable, it is recommended to use it within a try-with-resources block.

      Parameters:
      refs - a stream of refs to retrieve from the repository.
      Returns:
      a stream of entities corresponding to the provided primary keys. The order of entities in the stream is not guaranteed to match the order of ids in the input stream. If an id does not correspond to any entity in the database, it will simply be skipped, and no corresponding entity will be included in the returned stream. If the same entity is requested multiple times, it may be included in the stream multiple times if it is part of a separate batch.
      Throws:
      PersistenceException - if the selection operation fails due to underlying database issues, such as connectivity.
    • selectAllById

      public Stream<P> selectAllById(@Nonnull Stream<ID> ids, int batchSize)
      Retrieves a stream of entities based on their primary keys.

      This method executes queries in batches, with the batch size determined by the provided parameter. This optimization aims to reduce the overhead of executing multiple queries and efficiently retrieve entities. The batching strategy enhances performance, particularly when dealing with large sets of primary keys.

      The resulting stream is lazily loaded, meaning that the entities are only retrieved from the database as they are consumed by the stream. This approach is efficient and minimizes the memory footprint, especially when dealing with large volumes of entities.

      Note that calling this method does trigger the execution of the underlying query, so it should only be invoked when the query is intended to run. Since the stream holds resources open while in use, it must be closed after usage to prevent resource leaks. As the stream is AutoCloseable, it is recommended to use it within a try-with-resources block.

      Parameters:
      ids - a stream of entity IDs to retrieve from the repository.
      batchSize - the number of primary keys to include in each batch. This parameter determines the size of the batches used to execute the selection operation. A larger batch size can improve performance, especially when dealing with large sets of primary keys.
      Returns:
      a stream of entities corresponding to the provided primary keys. The order of entities in the stream is not guaranteed to match the order of ids in the input stream. If an id does not correspond to any entity in the database, it will simply be skipped, and no corresponding entity will be included in the returned stream. If the same entity is requested multiple times, it may be included in the stream multiple times if it is part of a separate batch.
      Throws:
      PersistenceException - if the selection operation fails due to underlying database issues, such as connectivity.
    • selectAllByRef

      public Stream<P> selectAllByRef(@Nonnull Stream<Ref<P>> refs, int batchSize)
      Retrieves a stream of entities based on their primary keys.

      This method executes queries in batches, with the batch size determined by the provided parameter. This optimization aims to reduce the overhead of executing multiple queries and efficiently retrieve entities. The batching strategy enhances performance, particularly when dealing with large sets of primary keys.

      The resulting stream is lazily loaded, meaning that the entities are only retrieved from the database as they are consumed by the stream. This approach is efficient and minimizes the memory footprint, especially when dealing with large volumes of entities.

      Note that calling this method does trigger the execution of the underlying query, so it should only be invoked when the query is intended to run. Since the stream holds resources open while in use, it must be closed after usage to prevent resource leaks. As the stream is AutoCloseable, it is recommended to use it within a try-with-resources block.

      Parameters:
      refs - a stream of refs to retrieve from the repository.
      batchSize - the number of primary keys to include in each batch. This parameter determines the size of the batches used to execute the selection operation. A larger batch size can improve performance, especially when dealing with large sets of primary keys.
      Returns:
      a stream of entities corresponding to the provided primary keys. The order of entities in the stream is not guaranteed to match the order of ids in the input stream. If an id does not correspond to any entity in the database, it will simply be skipped, and no corresponding entity will be included in the returned stream. If the same entity is requested multiple times, it may be included in the stream multiple times if it is part of a separate batch.
      Throws:
      PersistenceException - if the selection operation fails due to underlying database issues, such as connectivity.
    • countById

      public long countById(@Nonnull Stream<ID> ids)
      Counts the number of entities identified by the provided stream of IDs using the default batch size.

      This method calculates the total number of entities that match the provided primary keys. The counting is performed in batches, which helps optimize performance and manage database load when dealing with large sets of IDs.

      Parameters:
      ids - a stream of IDs for which to count matching entities.
      Returns:
      the total count of entities matching the provided IDs.
      Throws:
      PersistenceException - if there is an error during the counting operation, such as connectivity issues.
    • countById

      public long countById(@Nonnull Stream<ID> ids, int batchSize)
      Counts the number of entities identified by the provided stream of IDs, with the counting process divided into batches of the specified size.

      This method performs the counting operation in batches, specified by the batchSize parameter. This batching approach is particularly useful for efficiently handling large volumes of IDs, reducing the overhead on the database and improving performance.

      Parameters:
      ids - a stream of IDs for which to count matching entities.
      batchSize - the size of the batches to use for the counting operation. A larger batch size can improve performance but may also increase the load on the database.
      Returns:
      the total count of entities matching the provided IDs.
      Throws:
      PersistenceException - if there is an error during the counting operation, such as connectivity issues.
    • countByRef

      public long countByRef(@Nonnull Stream<Ref<P>> refs)
      Counts the number of entities identified by the provided stream of refs using the default batch size.

      This method calculates the total number of entities that match the provided primary keys. The counting is performed in batches, which helps optimize performance and manage database load when dealing with large sets of IDs.

      Parameters:
      refs - a stream of refs for which to count matching entities.
      Returns:
      the total count of entities matching the provided IDs.
      Throws:
      PersistenceException - if there is an error during the counting operation, such as connectivity issues.
    • countByRef

      public long countByRef(@Nonnull Stream<Ref<P>> refs, int batchSize)
      Counts the number of entities identified by the provided stream of refs, with the counting process divided into batches of the specified size.

      This method performs the counting operation in batches, specified by the batchSize parameter. This batching approach is particularly useful for efficiently handling large volumes of IDs, reducing the overhead on the database and improving performance.

      Parameters:
      refs - a stream of IDs for which to count matching entities.
      batchSize - the size of the batches to use for the counting operation. A larger batch size can improve performance but may also increase the load on the database.
      Returns:
      the total count of entities matching the provided IDs.
      Throws:
      PersistenceException - if there is an error during the counting operation, such as connectivity issues.