Extract logic for batching/looping dataset documents.