Hi all,
I want to share my discovery of the day as this is probably useful to others.
In an app that need to process records sometime in huge batches I had some trouble getting my loop working correctly using some manual batching code du to some rules that may postpone some record processing.
Today, I found ERXFetchSpecificationBatchIterator, a helper class to do batch fetches that work and is isolated from external changes to the data during the process. This class fetch all the primary keys and iterate through them. It also allows to change the EOEditingContext during the process to optimise the garbage collection. It try to replicate a database cursor in EOF by fetching real EO by batch using their PK. Only the PK list and current batch of EO is kept in memory.
Very simple to use, it needs a fetch specification, an EOEditingContext and a batch size like this:
ERXSortOrderings sortOrderings = IsaacActivityRecord.DATE.ascs();
ERXFetchSpecification<IsaacActivityRecord> fs = IsaacActivityRecord.fetchSpec();
fs.setQualifier(IsaacActivityRecord.PROCESSED_DATE.isNull());
fs.setSortOrderings(sortOrderings);
ERXFetchSpecificationBatchIterator<IsaacActivityRecord> batchFs = new ERXFetchSpecificationBatchIterator<>(fs);
batchFs.setEditingContext(ec);
batchFs.setBatchSize(10000);
int nbActivities = 0;
while (batchFs.hasMoreElements()) {
IsaacActivityRecord activityRecord = batchFs.next();
if (++nbActivities % 500 == 0) { // Save every 500 records
ec.saveChanges();
}
}
ec.saveChanges();
Samuel