add queryChanges method to QueryProcessor

This change refactors some of the logic in the `query` method of
Query processor into a `queryChanges` method.  This is useful for
client code that simply wants to read the results instead of writing
a JSON or otherwise formatted list of data.

Specifically, this change is in preparation for a revamped review
command that will accept a query string instead of simply a change
identifier.

Change-Id: I2798a9ca793ce778e5cc5c1874b22affae741050
This commit is contained in:
Conley Owens 2012-02-23 10:49:37 -08:00
parent 7f7eadff20
commit 49fe6e31b8
1 changed files with 46 additions and 40 deletions

View File

@ -137,6 +137,51 @@ public class QueryProcessor {
this.outputFormat = fmt;
}
public List<ChangeData> queryChanges(final String queryString)
throws OrmException, QueryParseException {
final Predicate<ChangeData> visibleToMe = queryBuilder.is_visible();
Predicate<ChangeData> s = compileQuery(queryString, visibleToMe);
List<ChangeData> results = new ArrayList<ChangeData>();
HashSet<Change.Id> want = new HashSet<Change.Id>();
for (ChangeData d : ((ChangeDataSource) s).read()) {
if (d.hasChange()) {
// Checking visibleToMe here should be unnecessary, the
// query should have already performed it. But we don't
// want to trust the query rewriter that much yet.
//
if (visibleToMe.match(d)) {
results.add(d);
}
} else {
want.add(d.getId());
}
}
if (!want.isEmpty()) {
for (Change c : db.get().changes().get(want)) {
ChangeData d = new ChangeData(c);
if (visibleToMe.match(d)) {
results.add(d);
}
}
}
Collections.sort(results, new Comparator<ChangeData>() {
@Override
public int compare(ChangeData a, ChangeData b) {
return b.getChange().getSortKey().compareTo(
a.getChange().getSortKey());
}
});
int limit = limit(s);
if (limit < results.size()) {
results = results.subList(0, limit);
}
return results;
}
public void query(String queryString) throws IOException {
out = new PrintWriter( //
new BufferedWriter( //
@ -153,46 +198,7 @@ public class QueryProcessor {
final QueryStats stats = new QueryStats();
stats.runTimeMilliseconds = System.currentTimeMillis();
final Predicate<ChangeData> visibleToMe = queryBuilder.is_visible();
Predicate<ChangeData> s = compileQuery(queryString, visibleToMe);
List<ChangeData> results = new ArrayList<ChangeData>();
HashSet<Change.Id> want = new HashSet<Change.Id>();
for (ChangeData d : ((ChangeDataSource) s).read()) {
if (d.hasChange()) {
// Checking visibleToMe here should be unnecessary, the
// query should have already performed it. But we don't
// want to trust the query rewriter that much yet.
//
if (visibleToMe.match(d)) {
results.add(d);
}
} else {
want.add(d.getId());
}
}
if (!want.isEmpty()) {
for (Change c : db.get().changes().get(want)) {
ChangeData d = new ChangeData(c);
if (visibleToMe.match(d)) {
results.add(d);
}
}
}
Collections.sort(results, new Comparator<ChangeData>() {
@Override
public int compare(ChangeData a, ChangeData b) {
return b.getChange().getSortKey().compareTo(
a.getChange().getSortKey());
}
});
int limit = limit(s);
if (limit < results.size()) {
results = results.subList(0, limit);
}
List<ChangeData> results = queryChanges(queryString);
for (ChangeData d : results) {
ChangeAttribute c = eventFactory.asChangeAttribute(d.getChange());
eventFactory.extend(c, d.getChange());