private Page<NamedBlobRecord> mergePageResults(Page<NamedBlobRecord> aggregatedPage,
        Page<NamedBlobRecord> currentPage, String accountName, String containerName, String blobNamePrefix, int maxKey,
        boolean groupDirectories) {

      // Start with a copy of the current aggregated entries.
      List<NamedBlobRecord> accumulator = new ArrayList<>(aggregatedPage.getEntries());
      String newToken = null;
      List<NamedBlobRecord> entries = currentPage.getEntries();

      for (NamedBlobRecord record : entries) {
        NamedBlobRecord recordToAdd = record;
        if (groupDirectories) {
          String directory = extractDirectory(record.getBlobName(), blobNamePrefix);
          if (directory != null) {
            // Skip duplicate directory records.
            if (accumulator.stream()
                .filter(NamedBlobRecord::isDirectory)
                .anyMatch(existing -> existing.getBlobName().equals(directory))) {
              continue;
            }
            recordToAdd =
                new NamedBlobRecord(accountName, containerName, directory, null, Utils.Infinite_Time, 0, 0, 0, true);
          }
        }
        // If adding this record would reach maxKey, capture its blobName as newToken and stop processing.
        if (accumulator.size() == maxKey) {
          newToken = record.getBlobName();
          break;
        }
        accumulator.add(recordToAdd);
      }
      // Determine the next page token: use newToken if set, else use the token from the underlying currentPage.
      String tokenToUse = (newToken != null) ? newToken : currentPage.getNextPageToken();
      return new Page<>(accumulator, tokenToUse);
    }
Merges the current page of results into the aggregated Page.
<p>
This method iterates over the entries of the provided {@code currentPage}. If {@code groupDirectories} is true,
each entry is examined via {@link #extractDirectory(String, String)}. If a directory is detected, a new directory
record is constructed and added to the aggregated results only if not already present. If adding an entry would
cause the aggregated results to reach {@code maxKey} entries, the blobName of that entry is captured as the new
page token, and processing of the current page stops.
</p>

@param aggregatedPage   the aggregated Page so far.
@param currentPage      the Page returned from the underlying list call.
@param accountName      the account name.
@param containerName    the container name.
@param blobNamePrefix   the blob name prefix.
@param maxKey           the maximum number of entries to accumulate.
@param groupDirectories flag indicating whether to group directories.
@return a new immutable Page whose entries are the merged entries (accumulator) and whose nextPageToken is
        either the blobName of the first unprocessed record or the underlying currentPage's token.
ambry-frontend/src/main/java/com/github/ambry/frontend/NamedBlobListHandler.java