ListProjects: print projects list using secondary index

Render the list of projects to OutputStream leveraging the
projects secondary index instead of relying on the in-memory cache.

Change-Id: I864e1c3cd63b206c0f9dc76bbaa6de99ddfdc0d4
This commit is contained in:
Luca Milanesio 2019-02-01 21:55:20 +01:00 committed by David Pursehouse
parent 5017ba50ce
commit e3efc1f715
3 changed files with 46 additions and 11 deletions

View File

@ -313,7 +313,7 @@ public class ListProjects implements RestReadView<TopLevelResource> {
throws BadRequestException, PermissionBackendException {
if (format == OutputFormat.TEXT) {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
display(buf);
displayToStream(buf);
return BinaryResult.create(buf.toByteArray())
.setContentType("text/plain")
.setCharacterEncoding(UTF_8);
@ -340,6 +340,7 @@ public class ListProjects implements RestReadView<TopLevelResource> {
&& isNullOrEmpty(matchSubstring) // TODO: see Issue 10446
&& type == FilterType.ALL
&& showBranch.isEmpty()
&& !showTree
? Optional.of(stateToQuery())
: Optional.empty();
}
@ -379,7 +380,47 @@ public class ListProjects implements RestReadView<TopLevelResource> {
return p;
}
public SortedMap<String, ProjectInfo> display(@Nullable OutputStream displayOutputStream)
private void printQueryResults(String query, PrintWriter out) throws BadRequestException {
try {
if (format.isJson()) {
format.newGson().toJson(applyAsQuery(query), out);
} else {
newProjectsNamesStream(query).forEach(out::println);
}
out.flush();
} catch (OrmException | MethodNotAllowedException e) {
logger.atWarning().withCause(e).log(
"Internal error while processing the query '{}' request", query);
throw new BadRequestException("Internal error while processing the query request");
}
}
private Stream<String> newProjectsNamesStream(String query)
throws OrmException, MethodNotAllowedException, BadRequestException {
Stream<String> projects =
queryProjectsProvider.get().withQuery(query).apply().stream().map(p -> p.name).skip(start);
if (limit > 0) {
projects = projects.limit(limit);
}
return projects;
}
public void displayToStream(OutputStream displayOutputStream)
throws BadRequestException, PermissionBackendException {
PrintWriter stdout =
new PrintWriter(new BufferedWriter(new OutputStreamWriter(displayOutputStream, UTF_8)));
Optional<String> projectsQuery = expressAsProjectsQuery();
if (projectsQuery.isPresent()) {
printQueryResults(projectsQuery.get(), stdout);
} else {
display(stdout);
}
}
@Nullable
public SortedMap<String, ProjectInfo> display(@Nullable PrintWriter stdout)
throws BadRequestException, PermissionBackendException {
if (all && state != null) {
throw new BadRequestException("'all' and 'state' may not be used together");
@ -394,12 +435,6 @@ public class ListProjects implements RestReadView<TopLevelResource> {
}
}
PrintWriter stdout = null;
if (displayOutputStream != null) {
stdout =
new PrintWriter(new BufferedWriter(new OutputStreamWriter(displayOutputStream, UTF_8)));
}
if (type == FilterType.PARENT_CANDIDATES) {
// Historically, PARENT_CANDIDATES implied showDescription.
showDescription = true;

View File

@ -41,6 +41,6 @@ public class ListProjectsCommand extends SshCommand {
throw die("--tree and --description options are not compatible.");
}
}
impl.display(out);
impl.displayToStream(out);
}
}

View File

@ -126,7 +126,7 @@ public class ListProjectsIT extends AbstractDaemonTest {
try (ByteArrayOutputStream displayOut = new ByteArrayOutputStream()) {
listProjects.setStart(numInitialProjects);
listProjects.display(displayOut);
listProjects.displayToStream(displayOut);
List<String> lines =
Splitter.on("\n").omitEmptyStrings().splitToList(new String(displayOut.toByteArray()));
@ -156,7 +156,7 @@ public class ListProjectsIT extends AbstractDaemonTest {
listProjects.setStart(numInitialProjects);
listProjects.setFormat(jsonFormat);
listProjects.display(displayOut);
listProjects.displayToStream(displayOut);
String projectsJsonOutput = new String(displayOut.toByteArray());