private List<DatasetVersionRecord> executeListAllValidVersionsOrderedByLastModifiedTimeStatement(
      PreparedStatement statement, int accountId, int containerId, String datasetName, int retentionCount,
      Dataset.VersionSchema versionSchema) throws SQLException {
    ResultSet resultSet = null;
    List<DatasetVersionRecord> datasetVersionRecordList = new ArrayList<>();
    try {
      statement.setInt(1, accountId);
      statement.setInt(2, containerId);
      statement.setString(3, datasetName);
      statement.setInt(4, DatasetVersionState.READY.ordinal());
      statement.setInt(5, retentionCount);
      resultSet = statement.executeQuery();
      while (resultSet.next()) {
        long versionValue = resultSet.getLong(VERSION);
        String renameFrom = resultSet.getString(RENAME_FROM);
        Timestamp deletionTime = resultSet.getTimestamp(DELETED_TS);
        String version = convertVersionValueToVersion(versionValue, versionSchema);
        datasetVersionRecordList.add(
            new DatasetVersionRecord(accountId, containerId, datasetName, version, timestampToMs(deletionTime),
                renameFrom));
      }
      return datasetVersionRecordList;
    } finally {
      closeQuietly(resultSet);
    }
  }
Execute the listAllValidVersionsOrderedByLastModifiedTimeStatement and fileter all versions out of retentionCount.
@param statement the listVersionByModifiedTimeSql statement.
@param accountId the id for the parent account.
@param containerId the id of the container.
@param datasetName the name of the dataset.
@param retentionCount the retention count of dataset.
@param versionSchema the {@link com.github.ambry.account.Dataset.VersionSchema} of the datset.
@return a list of versions from a dataset which has not expired and out of retentionCount by checking the last modified time.
@throws SQLException
ambry-account/src/main/java/com/github/ambry/account/mysql/DatasetDao.java