Move common intra-Gerrit change queries to their own class
This is intended to replace most or all of the primary database index queries in ChangeAccess. Change-Id: I14fad0a46090b9fa3ef125c400c1d0a47d36381d
This commit is contained in:
@@ -111,7 +111,7 @@ import com.google.gerrit.server.project.ProjectControl;
|
||||
import com.google.gerrit.server.project.ProjectState;
|
||||
import com.google.gerrit.server.project.RefControl;
|
||||
import com.google.gerrit.server.query.change.ChangeData;
|
||||
import com.google.gerrit.server.query.change.QueryProcessor;
|
||||
import com.google.gerrit.server.query.change.InternalChangeQuery;
|
||||
import com.google.gerrit.server.ssh.SshInfo;
|
||||
import com.google.gerrit.server.util.LabelVote;
|
||||
import com.google.gerrit.server.util.MagicBranch;
|
||||
@@ -337,7 +337,7 @@ public class ReceiveCommits {
|
||||
|
||||
@Inject
|
||||
ReceiveCommits(final ReviewDb db,
|
||||
final Provider<QueryProcessor> queryProcessor,
|
||||
final Provider<InternalChangeQuery> queryProvider,
|
||||
final SchemaFactory<ReviewDb> schemaFactory,
|
||||
final ChangeData.Factory changeDataFactory,
|
||||
final ChangeUpdate.Factory updateFactory,
|
||||
@@ -474,7 +474,7 @@ public class ReceiveCommits {
|
||||
});
|
||||
advHooks.add(rp.getAdvertiseRefsHook());
|
||||
advHooks.add(new ReceiveCommitsAdvertiseRefsHook(
|
||||
db, queryProcessor, projectControl.getProject().getNameKey()));
|
||||
db, queryProvider, projectControl.getProject().getNameKey()));
|
||||
rp.setAdvertiseRefsHook(AdvertiseRefsHookChain.newChain(advHooks));
|
||||
}
|
||||
|
||||
|
||||
@@ -22,11 +22,8 @@ import com.google.gerrit.reviewdb.client.PatchSet;
|
||||
import com.google.gerrit.reviewdb.client.Project;
|
||||
import com.google.gerrit.reviewdb.client.RefNames;
|
||||
import com.google.gerrit.reviewdb.server.ReviewDb;
|
||||
import com.google.gerrit.server.query.Predicate;
|
||||
import com.google.gerrit.server.query.QueryParseException;
|
||||
import com.google.gerrit.server.query.change.ChangeData;
|
||||
import com.google.gerrit.server.query.change.ChangeQueryBuilder;
|
||||
import com.google.gerrit.server.query.change.QueryProcessor;
|
||||
import com.google.gerrit.server.query.change.InternalChangeQuery;
|
||||
import com.google.gerrit.server.util.MagicBranch;
|
||||
import com.google.gwtorm.server.OrmException;
|
||||
import com.google.inject.Provider;
|
||||
@@ -44,7 +41,6 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -54,14 +50,14 @@ public class ReceiveCommitsAdvertiseRefsHook implements AdvertiseRefsHook {
|
||||
.getLogger(ReceiveCommitsAdvertiseRefsHook.class);
|
||||
|
||||
private final ReviewDb db;
|
||||
private final Provider<QueryProcessor> queryProcessor;
|
||||
private final Provider<InternalChangeQuery> queryProvider;
|
||||
private final Project.NameKey projectName;
|
||||
|
||||
public ReceiveCommitsAdvertiseRefsHook(ReviewDb db,
|
||||
Provider<QueryProcessor> queryProcessor,
|
||||
Provider<InternalChangeQuery> queryProvider,
|
||||
Project.NameKey projectName) {
|
||||
this.db = db;
|
||||
this.queryProcessor = queryProcessor;
|
||||
this.queryProvider = queryProvider;
|
||||
this.projectName = projectName;
|
||||
}
|
||||
|
||||
@@ -105,7 +101,8 @@ public class ReceiveCommitsAdvertiseRefsHook implements AdvertiseRefsHook {
|
||||
final int limit = 32;
|
||||
try {
|
||||
Set<PatchSet.Id> toGet = Sets.newHashSetWithExpectedSize(limit);
|
||||
for (ChangeData cd : queryRecentChanges(limit)) {
|
||||
for (ChangeData cd :
|
||||
queryProvider.get().setLimit(limit).byProjectOpen(projectName)) {
|
||||
PatchSet.Id id = cd.change().currentPatchSetId();
|
||||
if (id != null) {
|
||||
toGet.add(id);
|
||||
@@ -174,20 +171,6 @@ public class ReceiveCommitsAdvertiseRefsHook implements AdvertiseRefsHook {
|
||||
return toInclude;
|
||||
}
|
||||
|
||||
private List<ChangeData> queryRecentChanges(int limit)
|
||||
throws OrmException {
|
||||
QueryProcessor qp = queryProcessor.get();
|
||||
qp.setLimit(limit);
|
||||
ChangeQueryBuilder qb = qp.getQueryBuilder();
|
||||
Predicate<ChangeData> p =
|
||||
Predicate.and(qb.project(projectName.get()), qb.status_open());
|
||||
try {
|
||||
return qp.queryChanges(p).changes();
|
||||
} catch (QueryParseException e) {
|
||||
throw new OrmException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean skip(String name) {
|
||||
return name.startsWith(RefNames.REFS_CHANGES)
|
||||
|| name.startsWith(RefNames.REFS_CACHE_AUTOMERGE)
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
// Copyright (C) 2014 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.server.query.change;
|
||||
|
||||
import static com.google.gerrit.server.query.Predicate.and;
|
||||
|
||||
import com.google.gerrit.reviewdb.client.Project;
|
||||
import com.google.gerrit.server.query.Predicate;
|
||||
import com.google.gerrit.server.query.QueryParseException;
|
||||
import com.google.gwtorm.server.OrmException;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** Execute a single query over changes, for use by Gerrit internals. */
|
||||
public class InternalChangeQuery {
|
||||
private final QueryProcessor qp;
|
||||
private final ChangeQueryBuilder qb;
|
||||
|
||||
@Inject
|
||||
InternalChangeQuery(QueryProcessor queryProcessor) {
|
||||
qp = queryProcessor;
|
||||
qb = qp.getQueryBuilder();
|
||||
}
|
||||
|
||||
public InternalChangeQuery setLimit(int n) {
|
||||
qp.setLimit(n);
|
||||
return this;
|
||||
}
|
||||
|
||||
public List<ChangeData> byProjectOpen(Project.NameKey project)
|
||||
throws OrmException {
|
||||
return query(and(qb.project(project.get()), qb.status_open()));
|
||||
}
|
||||
|
||||
private List<ChangeData> query(Predicate<ChangeData> p) throws OrmException {
|
||||
try {
|
||||
return qp.queryChanges(p).changes();
|
||||
} catch (QueryParseException e) {
|
||||
throw new OrmException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -53,12 +53,14 @@ public class QueryProcessor {
|
||||
return queryBuilder;
|
||||
}
|
||||
|
||||
public void setLimit(int n) {
|
||||
public QueryProcessor setLimit(int n) {
|
||||
limitFromCaller = n;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void setStart(int n) {
|
||||
public QueryProcessor setStart(int n) {
|
||||
start = n;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user