mirror of
https://github.com/google/nomulus
synced 2026-01-03 03:35:42 +00:00
Add stream to the console dum download (#2783)
This commit is contained in:
@@ -14,7 +14,6 @@
|
|||||||
|
|
||||||
package google.registry.ui.server.console;
|
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.persistence.transaction.TransactionManagerFactory.tm;
|
||||||
import static google.registry.request.Action.Method.GET;
|
import static google.registry.request.Action.Method.GET;
|
||||||
import static org.joda.time.DateTimeZone.UTC;
|
import static org.joda.time.DateTimeZone.UTC;
|
||||||
@@ -34,7 +33,6 @@ import google.registry.util.Clock;
|
|||||||
import jakarta.inject.Inject;
|
import jakarta.inject.Inject;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
|
||||||
import org.apache.commons.csv.CSVFormat;
|
import org.apache.commons.csv.CSVFormat;
|
||||||
import org.apache.commons.csv.CSVPrinter;
|
import org.apache.commons.csv.CSVPrinter;
|
||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
@@ -106,21 +104,27 @@ public class ConsoleDumDownloadAction extends ConsoleApiAction {
|
|||||||
private void writeCsv(CSVPrinter printer) throws IOException {
|
private void writeCsv(CSVPrinter printer) throws IOException {
|
||||||
String sql = SQL_TEMPLATE.replaceAll(":now", clock.nowUtc().toString());
|
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(
|
printer.printRecord(
|
||||||
ImmutableList.of("Domain Name", "Creation Time", "Expiration Time", "Domain Statuses"));
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user