Class JpaBatchingQueryUtils
java.lang.Object
com.broadleafcommerce.catalog.provider.jpa.repository.support.JpaBatchingQueryUtils
Common utilities to fetch query results in batches.
- Since:
- CatalogServices 2.1.5, CatalogServices 2.2.1
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classDescribes details that are provided to the operation responsible for fetching a batch of results from the datastore. -
Method Summary
Modifier and TypeMethodDescriptionstatic <D> List<D>queryResultListInBatches(int batchSize, Function<JpaBatchingQueryUtils.BatchRequestConfig, List<D>> batchResultSupplier, Function<D, String> idExtractor) In some repository methods, the expectation is to return a list of all matching results based on certain filtration criteria.
-
Method Details
-
queryResultListInBatches
public static <D> List<D> queryResultListInBatches(int batchSize, Function<JpaBatchingQueryUtils.BatchRequestConfig, List<D>> batchResultSupplier, Function<D, String> idExtractor) In some repository methods, the expectation is to return a list of all matching results based on certain filtration criteria. With such a setup, it is possible that the result size is very large.
This can be troublesome particularly when the domain being queried has been extended by clients. This is because Hibernate performs a query to join the base and extended domains, and uses an 'IN' criteria on record ID to do so. When the result size is large, this 'IN' criteria can exceed query size limits.
This method can be leveraged by repository method implementations to support transparently collecting the results in batches to avoid that error before returning the full result set to the caller.
- Type Parameters:
D- the domain type being returned- Parameters:
batchSize- the size to limit each internal batch of results to when queryingbatchResultSupplier- this should be a function that actually executes the appropriate query to fetch a batch of results. Note that batching should not be done via simple pagination (by offset), since there is a chance that inserts/deletes against the DB between page requests result in records being missed or the same record appearing in multiple batches. Instead, this function should sort results by ID, and leverage the last ID fetched in the previous batch and use it as part of the query criteria. This relies on IDs being ULIDs (new inserts are time-ordered last). This approach ensures we don't double-fetch or miss anything.idExtractor- a function that can be used to extract the ID from a fetched record. Used to track the last-fetched ID of a batch.- Returns:
- the complete list of all gathered results
-