Stop passing ReviewDb, etc. to ChangeData methods

Many methods that are intended for lazy initialization take a
ReviewDb, and some take other helper objects such as
GitRepositoryManager. This achieves the intended lazy-loading behavior
but requires a lot of injection of dependencies into callers, and may
become untenable as lazy loading of ChangeData requires more helpers.

Instead, since after all this is Java, construct ChangeData with a
Factory. This is subtle because it is used from a lot of places that
need control over which ReviewDb handle to pass in, so the factory
methods still need to take a db. ChangeData objects are still intended
(i.e. safe) for use only by one thread, so having a single ReviewDb
instance stored for the lifetime of the ChangeData should be fine. We
just need to be careful about which we pass in, particularly in a
place like ChangeIndexer.

As a side effect, clean up some other injection, particularly in the
predicate class hierarchy.

Change-Id: I52e1eb2a76788c12dd95767e89095ab80df7e1cc
This commit is contained in:
Dave Borowitz
2013-12-20 11:38:13 -08:00
parent 24b40a9592
commit 7547233449
67 changed files with 470 additions and 505 deletions

View File

@@ -25,6 +25,7 @@ import com.google.common.collect.Sets;
import com.google.gerrit.extensions.events.LifecycleListener;
import com.google.gerrit.lucene.QueryBuilder;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.config.SitePaths;
import com.google.gerrit.server.index.ChangeField;
@@ -42,6 +43,7 @@ import com.google.gerrit.server.query.change.ChangeDataSource;
import com.google.gerrit.server.query.change.ChangeQueryBuilder;
import com.google.gwtorm.server.OrmException;
import com.google.gwtorm.server.ResultSet;
import com.google.inject.Provider;
import org.apache.lucene.search.Query;
import org.apache.solr.client.solrj.SolrQuery;
@@ -69,6 +71,8 @@ class SolrChangeIndex implements ChangeIndex, LifecycleListener {
public static final String CHANGES_CLOSED = "changes_closed";
private static final String ID_FIELD = ChangeField.LEGACY_ID.getName();
private final Provider<ReviewDb> db;
private final ChangeData.Factory changeDataFactory;
private final FillArgs fillArgs;
private final SitePaths sitePaths;
private final IndexCollection indexes;
@@ -78,11 +82,15 @@ class SolrChangeIndex implements ChangeIndex, LifecycleListener {
SolrChangeIndex(
@GerritServerConfig Config cfg,
Provider<ReviewDb> db,
ChangeData.Factory changeDataFactory,
FillArgs fillArgs,
SitePaths sitePaths,
IndexCollection indexes,
Schema<ChangeData> schema,
String base) throws IOException {
this.db = db;
this.changeDataFactory = changeDataFactory;
this.fillArgs = fillArgs;
this.sitePaths = sitePaths;
this.indexes = indexes;
@@ -256,7 +264,8 @@ class SolrChangeIndex implements ChangeIndex, LifecycleListener {
List<ChangeData> result = Lists.newArrayListWithCapacity(docs.size());
for (SolrDocument doc : docs) {
Integer v = (Integer) doc.getFieldValue(ID_FIELD);
result.add(new ChangeData(new Change.Id(v.intValue())));
result.add(
changeDataFactory.create(db.get(), new Change.Id(v.intValue())));
}
final List<ChangeData> r = Collections.unmodifiableList(result);

View File

@@ -15,6 +15,7 @@
package com.google.gerrit.solr;
import com.google.gerrit.lifecycle.LifecycleModule;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.config.SitePaths;
import com.google.gerrit.server.index.ChangeIndex;
@@ -22,6 +23,8 @@ import com.google.gerrit.server.index.ChangeSchemas;
import com.google.gerrit.server.index.FieldDef.FillArgs;
import com.google.gerrit.server.index.IndexCollection;
import com.google.gerrit.server.index.IndexModule;
import com.google.gerrit.server.query.change.ChangeData;
import com.google.inject.Provider;
import com.google.inject.Provides;
import com.google.inject.Singleton;
@@ -57,10 +60,12 @@ public class SolrIndexModule extends LifecycleModule {
@Provides
@Singleton
public SolrChangeIndex getChangeIndex(@GerritServerConfig Config cfg,
Provider<ReviewDb> db,
ChangeData.Factory changeDataFactory,
SitePaths sitePaths,
IndexCollection indexes,
FillArgs fillArgs) throws IOException {
return new SolrChangeIndex(cfg, fillArgs, sitePaths, indexes,
ChangeSchemas.getLatest(), base);
return new SolrChangeIndex(cfg, db, changeDataFactory, fillArgs, sitePaths,
indexes, ChangeSchemas.getLatest(), base);
}
}