1
0
mirror of https://github.com/google/nomulus synced 2025-12-23 06:15:42 +00:00

Add stream to the console dum download (#2783)

This commit is contained in:
Pavlo Tkach
2025-07-16 14:56:20 -04:00
committed by GitHub
parent 514d24ed67
commit c5644d5c8b

View File

@@ -14,7 +14,6 @@
package google.registry.ui.server.console;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.request.Action.Method.GET;
import static org.joda.time.DateTimeZone.UTC;
@@ -34,7 +33,6 @@ import google.registry.util.Clock;
import jakarta.inject.Inject;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.joda.time.DateTime;
@@ -106,21 +104,27 @@ public class ConsoleDumDownloadAction extends ConsoleApiAction {
private void writeCsv(CSVPrinter printer) throws IOException {
String sql = SQL_TEMPLATE.replaceAll(":now", clock.nowUtc().toString());
// We deliberately don't want to use ImmutableList.copyOf because underlying list may contain
// large amount of records and that will degrade performance.
List<String> queryResult =
tm().transact(
() ->
tm().getEntityManager()
.createNativeQuery(sql)
.setParameter("registrarId", registrarId)
.setHint("org.hibernate.fetchSize", 1000)
.getResultList());
ImmutableList<String[]> formattedRecords =
queryResult.stream().map(r -> r.split(",")).collect(toImmutableList());
printer.printRecord(
ImmutableList.of("Domain Name", "Creation Time", "Expiration Time", "Domain Statuses"));
printer.printRecords(formattedRecords);
tm().transact(
() -> {
try (var resultStream =
tm().getEntityManager()
.createNativeQuery(sql, String.class)
.setParameter("registrarId", registrarId)
.setHint("org.hibernate.fetchSize", 1000)
.getResultStream()) {
resultStream.forEach(
row -> {
try {
printer.printRecord((Object[]) ((String) row).split(","));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
});
}
}