Here is a brief illustration of current eHoldings export job flow:
The main problem is high RAM usage because of all job info aggregation on final step in order to prepare it to final format and write to file in a scope of single step.
This is needed mainly to define number of columns for notes in result file so we need to query all titles for package first to define maximum amount of notes that titles have.
Brief illustration of proposed solution
Create db schemas for titles, packages:
TEXT field type is used because we don't need any operations with this fields, they'll just keep values and data that is going to be queried by id or by "seek" method (using id).
Two separate tables are used in order to be able to paginate on data without having additional filtering (f.e. enum for package/title entity type).
JOB_EXECUTION_ID is added in order to query only data related to current job and cleanup afterwards.
Create repositories for new tables
Create EholdingsPackageRepository along with entity and mapper to "org.folio.dew.domain.dto.eholdings.EHoldingsPackage". There will be a need to save single record, read single record by id and remove all by job execution id.
Create EholdingsResourceRepository along with entity and mapper to "org.folio.dew.domain.dto.eholdings.EHoldingsResource". There will be a need to save batch of records, read batch of records using "seek method" (passing e_holdings_resource id) and remove all by job execution id.
Move data reading logic to some 'Reader'.
Add 'Writer' that will save package data to database. "packageMaxNotes" still must be written to job context.
Rework getEholdings step
Remove "stepExecutionListener" that writes resources to context.
Implement custom "writer" that writes resources to database. Replace "listItemWriter" with newly created one. "titleMaxNotes" still must be written to job context.
Rework saveEholdings step
Data should be processed in chunks (same as for "getEholdings" step).
Implement abstract reader for "seek method" (see "CsvItemReader" for reference, use last object from previous batch instead of "offset").
Implement reader that extends newly created abstract reader for seek method. This reader should query data from database. "e_holdings_resource" id should be used for seek.
Package data should be queried from db once on "beforeStep" in reader and saved to class field ("EHoldingsExportConfig" could be passed to cunstructor and package id retrieved from "recordId").
Reader should return object mapped to "EHoldingsResourceExportFormat".
Implement database "cleanup"
Add tasklet that removes all records from "EholdingsPackageRepository" by "job_execution_id" and from "EholdingsResourceRepository" by "job_execution_id".
Add tasklet on "next" to the end of "getEHoldingsJob" bean in "EHoldingsJobConfig".
Add "info" level logs for each step start/finish.
Add "trace" level logs about each operation start/finish (on every chunk).