diff --git a/java/com/google/gerrit/acceptance/GerritServer.java b/java/com/google/gerrit/acceptance/GerritServer.java index 6f54e84bf9..25315b6c96 100644 --- a/java/com/google/gerrit/acceptance/GerritServer.java +++ b/java/com/google/gerrit/acceptance/GerritServer.java @@ -39,6 +39,7 @@ import com.google.gerrit.server.config.GerritRuntime; import com.google.gerrit.server.config.GerritServerConfig; import com.google.gerrit.server.config.SitePath; import com.google.gerrit.server.git.receive.AsyncReceiveCommits; +import com.google.gerrit.server.schema.JdbcAccountPatchReviewStore; import com.google.gerrit.server.ssh.NoSshModule; import com.google.gerrit.server.util.SocketUtil; import com.google.gerrit.server.util.SystemLog; @@ -388,6 +389,8 @@ public class GerritServer implements AutoCloseable { cfg.setBoolean("sshd", null, "requestLog", false); cfg.setBoolean("index", "lucene", "testInmemory", true); cfg.setString("gitweb", null, "cgi", ""); + cfg.setString( + "accountPatchReviewDb", null, "url", JdbcAccountPatchReviewStore.TEST_IN_MEMORY_URL); daemon.setEnableHttpd(desc.httpd()); daemon.setLuceneModule(LuceneIndexModule.singleVersionAllLatest(0, isSlave(baseConfig))); daemon.setDatabaseForTesting( diff --git a/java/com/google/gerrit/elasticsearch/AbstractElasticIndex.java b/java/com/google/gerrit/elasticsearch/AbstractElasticIndex.java index 0379423ab1..ef4ef40ab3 100644 --- a/java/com/google/gerrit/elasticsearch/AbstractElasticIndex.java +++ b/java/com/google/gerrit/elasticsearch/AbstractElasticIndex.java @@ -20,6 +20,7 @@ import static com.google.gson.FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES; import static java.nio.charset.StandardCharsets.UTF_8; import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ListMultimap; import com.google.common.collect.Lists; @@ -38,8 +39,10 @@ import com.google.gerrit.index.QueryOptions; import com.google.gerrit.index.Schema; import com.google.gerrit.index.query.DataSource; import com.google.gerrit.index.query.FieldBundle; +import com.google.gerrit.index.query.ListResultSet; import com.google.gerrit.index.query.Predicate; import com.google.gerrit.index.query.QueryParseException; +import com.google.gerrit.index.query.ResultSet; import com.google.gerrit.proto.Protos; import com.google.gerrit.reviewdb.converter.ProtoConverter; import com.google.gerrit.server.config.SitePaths; @@ -51,7 +54,6 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gwtorm.server.OrmException; -import com.google.gwtorm.server.ResultSet; import com.google.protobuf.MessageLite; import java.io.IOException; import java.io.InputStream; @@ -62,7 +64,6 @@ import java.net.URLEncoder; import java.sql.Timestamp; import java.util.Collections; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @@ -385,7 +386,6 @@ abstract class AbstractElasticIndex implements Index { private ResultSet readImpl(Function mapper) throws OrmException { try { - List results = Collections.emptyList(); String uri = getURI(index, SEARCH); Response response = performRequest(HttpPost.METHOD_NAME, uri, search, Collections.emptyMap()); @@ -396,34 +396,19 @@ abstract class AbstractElasticIndex implements Index { new JsonParser().parse(content).getAsJsonObject().getAsJsonObject("hits"); if (obj.get("hits") != null) { JsonArray json = obj.getAsJsonArray("hits"); - results = Lists.newArrayListWithCapacity(json.size()); + ImmutableList.Builder results = ImmutableList.builderWithExpectedSize(json.size()); for (int i = 0; i < json.size(); i++) { T mapperResult = mapper.apply(json.get(i).getAsJsonObject()); if (mapperResult != null) { results.add(mapperResult); } } + return new ListResultSet<>(results.build()); } } else { logger.atSevere().log(statusLine.getReasonPhrase()); } - final List r = Collections.unmodifiableList(results); - return new ResultSet() { - @Override - public Iterator iterator() { - return r.iterator(); - } - - @Override - public List toList() { - return r; - } - - @Override - public void close() { - // Do nothing. - } - }; + return new ListResultSet<>(ImmutableList.of()); } catch (IOException e) { throw new OrmException(e); } diff --git a/java/com/google/gerrit/index/Index.java b/java/com/google/gerrit/index/Index.java index 2d7e31e182..f60c08fe6f 100644 --- a/java/com/google/gerrit/index/Index.java +++ b/java/com/google/gerrit/index/Index.java @@ -14,6 +14,7 @@ package com.google.gerrit.index; +import com.google.common.collect.ImmutableList; import com.google.gerrit.index.query.DataSource; import com.google.gerrit.index.query.FieldBundle; import com.google.gerrit.index.query.IndexPredicate; @@ -21,7 +22,6 @@ import com.google.gerrit.index.query.Predicate; import com.google.gerrit.index.query.QueryParseException; import com.google.gwtorm.server.OrmException; import java.io.IOException; -import java.util.List; import java.util.Optional; /** @@ -95,7 +95,7 @@ public interface Index { */ default Optional get(K key, QueryOptions opts) throws IOException { opts = opts.withStart(0).withLimit(2); - List results; + ImmutableList results; try { results = getSource(keyPredicate(key), opts).read().toList(); } catch (QueryParseException e) { @@ -120,7 +120,7 @@ public interface Index { */ default Optional getRaw(K key, QueryOptions opts) throws IOException { opts = opts.withStart(0).withLimit(2); - List results; + ImmutableList results; try { results = getSource(keyPredicate(key), opts).readRaw().toList(); } catch (QueryParseException e) { diff --git a/java/com/google/gerrit/index/project/IndexedProjectQuery.java b/java/com/google/gerrit/index/project/IndexedProjectQuery.java index 4409ccbc34..383ba1c75d 100644 --- a/java/com/google/gerrit/index/project/IndexedProjectQuery.java +++ b/java/com/google/gerrit/index/project/IndexedProjectQuery.java @@ -15,9 +15,9 @@ package com.google.gerrit.index.project; import com.google.gerrit.index.Index; -import com.google.gerrit.index.IndexedQuery; import com.google.gerrit.index.QueryOptions; import com.google.gerrit.index.query.DataSource; +import com.google.gerrit.index.query.IndexedQuery; import com.google.gerrit.index.query.Predicate; import com.google.gerrit.index.query.QueryParseException; import com.google.gerrit.reviewdb.client.Project; diff --git a/java/com/google/gerrit/index/query/AndSource.java b/java/com/google/gerrit/index/query/AndSource.java index d1e1c30b09..649dc32a02 100644 --- a/java/com/google/gerrit/index/query/AndSource.java +++ b/java/com/google/gerrit/index/query/AndSource.java @@ -21,10 +21,8 @@ import com.google.common.base.Throwables; import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; -import com.google.gwtorm.server.ListResultSet; import com.google.gwtorm.server.OrmException; import com.google.gwtorm.server.OrmRuntimeException; -import com.google.gwtorm.server.ResultSet; import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; diff --git a/java/com/google/gerrit/index/query/DataSource.java b/java/com/google/gerrit/index/query/DataSource.java index 88cc0e3c0e..a82337f1b4 100644 --- a/java/com/google/gerrit/index/query/DataSource.java +++ b/java/com/google/gerrit/index/query/DataSource.java @@ -15,7 +15,6 @@ package com.google.gerrit.index.query; import com.google.gwtorm.server.OrmException; -import com.google.gwtorm.server.ResultSet; public interface DataSource { /** @return an estimate of the number of results from {@link #read()}. */ diff --git a/java/com/google/gerrit/index/IndexedQuery.java b/java/com/google/gerrit/index/query/IndexedQuery.java similarity index 90% rename from java/com/google/gerrit/index/IndexedQuery.java rename to java/com/google/gerrit/index/query/IndexedQuery.java index 143cc26a0e..8df46a7b30 100644 --- a/java/com/google/gerrit/index/IndexedQuery.java +++ b/java/com/google/gerrit/index/query/IndexedQuery.java @@ -12,18 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -package com.google.gerrit.index; +package com.google.gerrit.index.query; import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableList; -import com.google.gerrit.index.query.DataSource; -import com.google.gerrit.index.query.FieldBundle; -import com.google.gerrit.index.query.IndexPredicate; -import com.google.gerrit.index.query.Paginated; -import com.google.gerrit.index.query.Predicate; -import com.google.gerrit.index.query.QueryParseException; +import com.google.gerrit.index.Index; +import com.google.gerrit.index.QueryOptions; import com.google.gwtorm.server.OrmException; -import com.google.gwtorm.server.ResultSet; import java.util.Collection; import java.util.List; diff --git a/java/com/google/gerrit/index/query/ListResultSet.java b/java/com/google/gerrit/index/query/ListResultSet.java new file mode 100644 index 0000000000..4cf48c8f1b --- /dev/null +++ b/java/com/google/gerrit/index/query/ListResultSet.java @@ -0,0 +1,47 @@ +// Copyright 2008 Google Inc. +// +// 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.index.query; + +import com.google.common.collect.ImmutableList; +import java.util.Iterator; +import java.util.List; + +public class ListResultSet implements ResultSet { + private ImmutableList items; + + public ListResultSet(List r) { + items = ImmutableList.copyOf(r); + } + + @Override + public Iterator iterator() { + return toList().iterator(); + } + + @Override + public ImmutableList toList() { + if (items == null) { + throw new IllegalStateException("Results already obtained"); + } + ImmutableList r = items; + items = null; + return r; + } + + @Override + public void close() { + items = null; + } +} diff --git a/java/com/google/gerrit/index/query/Paginated.java b/java/com/google/gerrit/index/query/Paginated.java index 20f65dcf06..c11d8c7440 100644 --- a/java/com/google/gerrit/index/query/Paginated.java +++ b/java/com/google/gerrit/index/query/Paginated.java @@ -16,7 +16,6 @@ package com.google.gerrit.index.query; import com.google.gerrit.index.QueryOptions; import com.google.gwtorm.server.OrmException; -import com.google.gwtorm.server.ResultSet; public interface Paginated { QueryOptions getOptions(); diff --git a/java/com/google/gerrit/index/query/QueryProcessor.java b/java/com/google/gerrit/index/query/QueryProcessor.java index 081575b631..6f10750955 100644 --- a/java/com/google/gerrit/index/query/QueryProcessor.java +++ b/java/com/google/gerrit/index/query/QueryProcessor.java @@ -30,7 +30,6 @@ import com.google.gerrit.index.Index; import com.google.gerrit.index.IndexCollection; import com.google.gerrit.index.IndexConfig; import com.google.gerrit.index.IndexRewriter; -import com.google.gerrit.index.IndexedQuery; import com.google.gerrit.index.QueryOptions; import com.google.gerrit.index.SchemaDefinitions; import com.google.gerrit.metrics.Description; @@ -40,7 +39,6 @@ import com.google.gerrit.metrics.Timer1; import com.google.gerrit.server.logging.CallerFinder; import com.google.gwtorm.server.OrmException; import com.google.gwtorm.server.OrmRuntimeException; -import com.google.gwtorm.server.ResultSet; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -268,7 +266,7 @@ public abstract class QueryProcessor { out = new ArrayList<>(cnt); for (int i = 0; i < cnt; i++) { - List matchesList = matches.get(i).toList(); + ImmutableList matchesList = matches.get(i).toList(); logger.atFine().log( "Matches[%d]:\n%s", i, lazy(() -> matchesList.stream().map(this::formatForLogging).collect(toSet()))); diff --git a/java/com/google/gerrit/index/query/QueryResult.java b/java/com/google/gerrit/index/query/QueryResult.java index 341e2b60f7..33fcef0364 100644 --- a/java/com/google/gerrit/index/query/QueryResult.java +++ b/java/com/google/gerrit/index/query/QueryResult.java @@ -15,6 +15,7 @@ package com.google.gerrit.index.query; import com.google.auto.value.AutoValue; +import com.google.common.collect.ImmutableList; import com.google.gerrit.common.Nullable; import java.util.List; @@ -22,15 +23,15 @@ import java.util.List; @AutoValue public abstract class QueryResult { public static QueryResult create( - @Nullable String query, Predicate predicate, int limit, List entites) { + @Nullable String query, Predicate predicate, int limit, List entities) { boolean more; - if (entites.size() > limit) { + if (entities.size() > limit) { more = true; - entites = entites.subList(0, limit); + entities = entities.subList(0, limit); } else { more = false; } - return new AutoValue_QueryResult<>(query, predicate, entites, more); + return new AutoValue_QueryResult<>(query, predicate, ImmutableList.copyOf(entities), more); } /** @return the original query string, or null if the query was created programmatically. */ @@ -41,7 +42,7 @@ public abstract class QueryResult { public abstract Predicate predicate(); /** @return the query results. */ - public abstract List entities(); + public abstract ImmutableList entities(); /** * @return whether the query could be retried with a higher start/limit to produce more results. diff --git a/java/com/google/gerrit/index/query/ResultSet.java b/java/com/google/gerrit/index/query/ResultSet.java new file mode 100644 index 0000000000..65fcd4593c --- /dev/null +++ b/java/com/google/gerrit/index/query/ResultSet.java @@ -0,0 +1,52 @@ +// Copyright 2008 Google Inc. +// +// 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.index.query; + +import com.google.common.collect.ImmutableList; +import java.util.Iterator; + +/** + * Result from any data store query function. + * + * @param type of entity being returned by the query. + */ +public interface ResultSet extends Iterable { + /** + * Obtain an iterator to loop through the results. + * + *

The iterator can be obtained only once. When the iterator completes ( hasNext() + * returns false) {@link #close()} will be automatically called. + */ + @Override + Iterator iterator(); + + /** + * Materialize all results as a single list. + * + *

Prior to returning {@link #close()} is invoked. This method must not be combined with {@link + * #iterator()} on the same instance. + * + * @return immutable list of the complete results. + */ + ImmutableList toList(); + + /** + * Close the result, discarding any further results. + * + *

This method may be invoked more than once. Its main use is to stop obtaining results before + * the iterator has finished. + */ + void close(); +} diff --git a/java/com/google/gerrit/lucene/AbstractLuceneIndex.java b/java/com/google/gerrit/lucene/AbstractLuceneIndex.java index 40acf80744..d9ca76ddd0 100644 --- a/java/com/google/gerrit/lucene/AbstractLuceneIndex.java +++ b/java/com/google/gerrit/lucene/AbstractLuceneIndex.java @@ -20,6 +20,7 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS; import com.google.common.base.Joiner; import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ListMultimap; import com.google.common.collect.Sets; import com.google.common.flogger.FluentLogger; @@ -37,18 +38,15 @@ import com.google.gerrit.index.Schema; import com.google.gerrit.index.Schema.Values; import com.google.gerrit.index.query.DataSource; import com.google.gerrit.index.query.FieldBundle; +import com.google.gerrit.index.query.ListResultSet; +import com.google.gerrit.index.query.ResultSet; import com.google.gerrit.server.config.SitePaths; import com.google.gerrit.server.index.IndexUtils; import com.google.gerrit.server.logging.LoggingContextAwareExecutorService; import com.google.gerrit.server.logging.LoggingContextAwareScheduledExecutorService; import com.google.gwtorm.server.OrmException; -import com.google.gwtorm.server.ResultSet; import java.io.IOException; import java.sql.Timestamp; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.Callable; @@ -494,32 +492,16 @@ public abstract class AbstractLuceneIndex implements Index { searcher = acquire(); int realLimit = opts.start() + opts.limit(); TopFieldDocs docs = searcher.search(query, realLimit, sort); - List result = new ArrayList<>(docs.scoreDocs.length); + ImmutableList.Builder b = ImmutableList.builderWithExpectedSize(docs.scoreDocs.length); for (int i = opts.start(); i < docs.scoreDocs.length; i++) { ScoreDoc sd = docs.scoreDocs[i]; Document doc = searcher.doc(sd.doc, opts.fields()); T mapperResult = mapper.apply(doc); if (mapperResult != null) { - result.add(mapperResult); + b.add(mapperResult); } } - final List r = Collections.unmodifiableList(result); - return new ResultSet() { - @Override - public Iterator iterator() { - return r.iterator(); - } - - @Override - public List toList() { - return r; - } - - @Override - public void close() { - // Do nothing. - } - }; + return new ListResultSet<>(b.build()); } catch (IOException e) { throw new OrmException(e); } finally { diff --git a/java/com/google/gerrit/lucene/LuceneChangeIndex.java b/java/com/google/gerrit/lucene/LuceneChangeIndex.java index 938665692d..a3394c3fa6 100644 --- a/java/com/google/gerrit/lucene/LuceneChangeIndex.java +++ b/java/com/google/gerrit/lucene/LuceneChangeIndex.java @@ -28,6 +28,7 @@ import com.google.common.base.Function; import com.google.common.base.Throwables; import com.google.common.collect.Collections2; import com.google.common.collect.FluentIterable; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.google.common.collect.ListMultimap; import com.google.common.collect.MultimapBuilder; @@ -40,6 +41,7 @@ import com.google.gerrit.index.Schema; import com.google.gerrit.index.query.FieldBundle; import com.google.gerrit.index.query.Predicate; import com.google.gerrit.index.query.QueryParseException; +import com.google.gerrit.index.query.ResultSet; import com.google.gerrit.proto.Protos; import com.google.gerrit.reviewdb.client.Account; import com.google.gerrit.reviewdb.client.Change; @@ -62,7 +64,6 @@ import com.google.gerrit.server.query.change.ChangeData; import com.google.gerrit.server.query.change.ChangeDataSource; import com.google.gwtorm.server.OrmException; import com.google.gwtorm.server.OrmRuntimeException; -import com.google.gwtorm.server.ResultSet; import com.google.inject.Inject; import com.google.inject.assistedinject.Assisted; import com.google.protobuf.MessageLite; @@ -333,7 +334,8 @@ public class LuceneChangeIndex implements ChangeIndex { } catch (IOException e) { throw new OrmException(e); } - List fieldBundles = documents.stream().map(rawDocumentMapper).collect(toList()); + ImmutableList fieldBundles = + documents.stream().map(rawDocumentMapper).collect(toImmutableList()); return new ResultSet() { @Override public Iterator iterator() { @@ -341,7 +343,7 @@ public class LuceneChangeIndex implements ChangeIndex { } @Override - public List toList() { + public ImmutableList toList() { return fieldBundles; } @@ -401,15 +403,16 @@ public class LuceneChangeIndex implements ChangeIndex { } @Override - public List toList() { + public ImmutableList toList() { try { List docs = future.get(); - List result = new ArrayList<>(docs.size()); + ImmutableList.Builder result = + ImmutableList.builderWithExpectedSize(docs.size()); String idFieldName = LEGACY_ID.getName(); for (Document doc : docs) { result.add(toChangeData(fields(doc, fields), fields, idFieldName)); } - return result; + return result.build(); } catch (InterruptedException e) { close(); throw new OrmRuntimeException(e); diff --git a/java/com/google/gerrit/pgm/Daemon.java b/java/com/google/gerrit/pgm/Daemon.java index e7e8ea359d..01c7685d4b 100644 --- a/java/com/google/gerrit/pgm/Daemon.java +++ b/java/com/google/gerrit/pgm/Daemon.java @@ -90,7 +90,6 @@ import com.google.gerrit.server.plugins.PluginGuiceEnvironment; import com.google.gerrit.server.plugins.PluginModule; import com.google.gerrit.server.project.DefaultProjectNameLockManager; import com.google.gerrit.server.restapi.RestApiModule; -import com.google.gerrit.server.schema.InMemoryAccountPatchReviewStore; import com.google.gerrit.server.schema.JdbcAccountPatchReviewStore; import com.google.gerrit.server.schema.NoteDbSchemaVersionCheck; import com.google.gerrit.server.securestore.DefaultSecureStore; @@ -404,10 +403,7 @@ public class Daemon extends SiteProgram { modules.add(new WorkQueue.Module()); modules.add(new StreamEventsApiListener.Module()); modules.add(new EventBroker.Module()); - modules.add( - inMemoryTest - ? new InMemoryAccountPatchReviewStore.Module() - : new JdbcAccountPatchReviewStore.Module(config)); + modules.add(new JdbcAccountPatchReviewStore.Module(config)); modules.add(new SysExecutorModule()); modules.add(new DiffExecutorModule()); modules.add(new MimeUtil2Module()); diff --git a/java/com/google/gerrit/server/index/account/IndexedAccountQuery.java b/java/com/google/gerrit/server/index/account/IndexedAccountQuery.java index 644f1ebae3..18f44f08af 100644 --- a/java/com/google/gerrit/server/index/account/IndexedAccountQuery.java +++ b/java/com/google/gerrit/server/index/account/IndexedAccountQuery.java @@ -17,9 +17,9 @@ package com.google.gerrit.server.index.account; import static com.google.common.base.Preconditions.checkState; import com.google.gerrit.index.Index; -import com.google.gerrit.index.IndexedQuery; import com.google.gerrit.index.QueryOptions; import com.google.gerrit.index.query.DataSource; +import com.google.gerrit.index.query.IndexedQuery; import com.google.gerrit.index.query.Matchable; import com.google.gerrit.index.query.Predicate; import com.google.gerrit.index.query.QueryParseException; diff --git a/java/com/google/gerrit/server/index/change/IndexedChangeQuery.java b/java/com/google/gerrit/server/index/change/IndexedChangeQuery.java index 66f8df2534..38ea730b51 100644 --- a/java/com/google/gerrit/server/index/change/IndexedChangeQuery.java +++ b/java/com/google/gerrit/server/index/change/IndexedChangeQuery.java @@ -19,25 +19,25 @@ import static com.google.gerrit.server.index.change.ChangeField.CHANGE; import static com.google.gerrit.server.index.change.ChangeField.PROJECT; import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.gerrit.index.IndexConfig; -import com.google.gerrit.index.IndexedQuery; import com.google.gerrit.index.QueryOptions; import com.google.gerrit.index.query.DataSource; import com.google.gerrit.index.query.IndexPredicate; +import com.google.gerrit.index.query.IndexedQuery; import com.google.gerrit.index.query.Matchable; import com.google.gerrit.index.query.Predicate; import com.google.gerrit.index.query.QueryParseException; +import com.google.gerrit.index.query.ResultSet; import com.google.gerrit.reviewdb.client.Change; import com.google.gerrit.server.query.change.ChangeData; import com.google.gerrit.server.query.change.ChangeDataSource; import com.google.gwtorm.server.OrmException; -import com.google.gwtorm.server.ResultSet; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; -import java.util.List; import java.util.Map; import java.util.Set; @@ -98,8 +98,8 @@ public class IndexedChangeQuery extends IndexedQuery } @Override - public List toList() { - List r = rs.toList(); + public ImmutableList toList() { + ImmutableList r = rs.toList(); for (ChangeData cd : r) { fromSource.put(cd, currSource); } diff --git a/java/com/google/gerrit/server/index/group/IndexedGroupQuery.java b/java/com/google/gerrit/server/index/group/IndexedGroupQuery.java index 79f25c0861..32393b0687 100644 --- a/java/com/google/gerrit/server/index/group/IndexedGroupQuery.java +++ b/java/com/google/gerrit/server/index/group/IndexedGroupQuery.java @@ -16,9 +16,9 @@ package com.google.gerrit.server.index.group; import com.google.gerrit.index.Index; import com.google.gerrit.index.IndexConfig; -import com.google.gerrit.index.IndexedQuery; import com.google.gerrit.index.QueryOptions; import com.google.gerrit.index.query.DataSource; +import com.google.gerrit.index.query.IndexedQuery; import com.google.gerrit.index.query.Predicate; import com.google.gerrit.index.query.QueryParseException; import com.google.gerrit.reviewdb.client.AccountGroup; diff --git a/java/com/google/gerrit/server/query/change/OrSource.java b/java/com/google/gerrit/server/query/change/OrSource.java index b3e1c271ef..dcbf5170ab 100644 --- a/java/com/google/gerrit/server/query/change/OrSource.java +++ b/java/com/google/gerrit/server/query/change/OrSource.java @@ -15,12 +15,12 @@ package com.google.gerrit.server.query.change; import com.google.gerrit.index.query.FieldBundle; +import com.google.gerrit.index.query.ListResultSet; import com.google.gerrit.index.query.OrPredicate; import com.google.gerrit.index.query.Predicate; +import com.google.gerrit.index.query.ResultSet; import com.google.gerrit.reviewdb.client.Change; -import com.google.gwtorm.server.ListResultSet; import com.google.gwtorm.server.OrmException; -import com.google.gwtorm.server.ResultSet; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; diff --git a/java/com/google/gerrit/server/restapi/change/ReviewersUtil.java b/java/com/google/gerrit/server/restapi/change/ReviewersUtil.java index 854125c933..75710c46fb 100644 --- a/java/com/google/gerrit/server/restapi/change/ReviewersUtil.java +++ b/java/com/google/gerrit/server/restapi/change/ReviewersUtil.java @@ -34,6 +34,7 @@ import com.google.gerrit.index.QueryOptions; import com.google.gerrit.index.query.FieldBundle; import com.google.gerrit.index.query.Predicate; import com.google.gerrit.index.query.QueryParseException; +import com.google.gerrit.index.query.ResultSet; import com.google.gerrit.metrics.Description; import com.google.gerrit.metrics.Description.Units; import com.google.gerrit.metrics.MetricMaker; @@ -57,7 +58,6 @@ import com.google.gerrit.server.project.ProjectState; import com.google.gerrit.server.query.account.AccountPredicates; import com.google.gerrit.server.query.account.AccountQueryBuilder; import com.google.gwtorm.server.OrmException; -import com.google.gwtorm.server.ResultSet; import com.google.inject.Inject; import com.google.inject.Provider; import com.google.inject.Singleton; diff --git a/java/com/google/gerrit/server/schema/InMemoryAccountPatchReviewStore.java b/java/com/google/gerrit/server/schema/InMemoryAccountPatchReviewStore.java deleted file mode 100644 index 35e81b22dc..0000000000 --- a/java/com/google/gerrit/server/schema/InMemoryAccountPatchReviewStore.java +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright (C) 2017 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.schema; - -import com.google.common.annotations.VisibleForTesting; -import com.google.gerrit.extensions.registration.DynamicItem; -import com.google.gerrit.lifecycle.LifecycleModule; -import com.google.gerrit.server.change.AccountPatchReviewStore; -import com.google.gwtorm.jdbc.SimpleDataSource; -import java.sql.SQLException; -import java.util.Properties; -import javax.sql.DataSource; - -public class InMemoryAccountPatchReviewStore extends JdbcAccountPatchReviewStore { - @VisibleForTesting - public static class Module extends LifecycleModule { - @Override - protected void configure() { - InMemoryAccountPatchReviewStore inMemoryStore = new InMemoryAccountPatchReviewStore(); - DynamicItem.bind(binder(), AccountPatchReviewStore.class).toInstance(inMemoryStore); - listener().toInstance(inMemoryStore); - } - } - - /** - * Creates an in-memory H2 database to store the reviewed flags. This should be used for tests - * only. - */ - @VisibleForTesting - private InMemoryAccountPatchReviewStore() { - super(newDataSource()); - } - - private static synchronized DataSource newDataSource() { - final Properties p = new Properties(); - p.setProperty("driver", "org.h2.Driver"); - // DB_CLOSE_DELAY=-1: By default the content of an in-memory H2 database is lost at the moment - // the last connection is closed. This option keeps the content as long as the vm lives. - p.setProperty("url", "jdbc:h2:mem:account_patch_reviews;DB_CLOSE_DELAY=-1"); - try { - return new SimpleDataSource(p); - } catch (SQLException e) { - throw new RuntimeException("Unable to create test datasource", e); - } - } -} diff --git a/java/com/google/gerrit/server/schema/JdbcAccountPatchReviewStore.java b/java/com/google/gerrit/server/schema/JdbcAccountPatchReviewStore.java index 1ef69db8ed..fa4de40baa 100644 --- a/java/com/google/gerrit/server/schema/JdbcAccountPatchReviewStore.java +++ b/java/com/google/gerrit/server/schema/JdbcAccountPatchReviewStore.java @@ -17,6 +17,7 @@ package com.google.gerrit.server.schema; import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.SECONDS; +import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableSet; import com.google.common.flogger.FluentLogger; import com.google.common.primitives.Ints; @@ -48,6 +49,12 @@ public abstract class JdbcAccountPatchReviewStore implements AccountPatchReviewStore, LifecycleListener { private static final FluentLogger logger = FluentLogger.forEnclosingClass(); + // DB_CLOSE_DELAY=-1: By default the content of an in-memory H2 database is lost at the moment the + // last connection is closed. This option keeps the content as long as the VM lives. + @VisibleForTesting + public static final String TEST_IN_MEMORY_URL = + "jdbc:h2:mem:account_patch_reviews;DB_CLOSE_DELAY=-1"; + private static final String ACCOUNT_PATCH_REVIEW_DB = "accountPatchReviewDb"; private static final String H2_DB = "h2"; private static final String MARIADB = "mariadb"; @@ -109,10 +116,6 @@ public abstract class JdbcAccountPatchReviewStore this.ds = createDataSource(cfg, sitePaths, threadSettingsConfig); } - protected JdbcAccountPatchReviewStore(DataSource ds) { - this.ds = ds; - } - private static String getUrl(@GerritServerConfig Config cfg, SitePaths sitePaths) { String url = cfg.getString(ACCOUNT_PATCH_REVIEW_DB, null, URL); if (url == null) { diff --git a/java/com/google/gerrit/testing/InMemoryModule.java b/java/com/google/gerrit/testing/InMemoryModule.java index 681bbf3420..c8cea6f24f 100644 --- a/java/com/google/gerrit/testing/InMemoryModule.java +++ b/java/com/google/gerrit/testing/InMemoryModule.java @@ -77,7 +77,7 @@ import com.google.gerrit.server.permissions.DefaultPermissionBackendModule; import com.google.gerrit.server.plugins.ServerInformationImpl; import com.google.gerrit.server.project.DefaultProjectNameLockManager; import com.google.gerrit.server.restapi.RestApiModule; -import com.google.gerrit.server.schema.InMemoryAccountPatchReviewStore; +import com.google.gerrit.server.schema.JdbcAccountPatchReviewStore; import com.google.gerrit.server.schema.SchemaCreator; import com.google.gerrit.server.schema.SchemaCreatorImpl; import com.google.gerrit.server.securestore.DefaultSecureStore; @@ -112,6 +112,8 @@ public class InMemoryModule extends FactoryModule { } public static void setDefaults(Config cfg) { + cfg.setString( + "accountPatchReviewDb", null, "url", JdbcAccountPatchReviewStore.TEST_IN_MEMORY_URL); cfg.setEnum("auth", null, "type", AuthType.DEVELOPMENT_BECOME_ANY_ACCOUNT); cfg.setString("gerrit", null, "allProjects", "Test-Projects"); cfg.setString("gerrit", null, "basePath", "git"); @@ -215,7 +217,6 @@ public class InMemoryModule extends FactoryModule { install(new FakeEmailSender.Module()); install(new SignedTokenEmailTokenVerifier.Module()); install(new GpgModule(cfg)); - install(new InMemoryAccountPatchReviewStore.Module()); install(new LocalMergeSuperSetComputation.Module()); bind(AllAccountsIndexer.class).toProvider(Providers.of(null)); diff --git a/javatests/com/google/gerrit/server/index/change/FakeChangeIndex.java b/javatests/com/google/gerrit/server/index/change/FakeChangeIndex.java index d4ecb6df39..faee23311c 100644 --- a/javatests/com/google/gerrit/server/index/change/FakeChangeIndex.java +++ b/javatests/com/google/gerrit/server/index/change/FakeChangeIndex.java @@ -21,11 +21,11 @@ import com.google.gerrit.index.Schema; import com.google.gerrit.index.query.FieldBundle; import com.google.gerrit.index.query.Predicate; import com.google.gerrit.index.query.QueryParseException; +import com.google.gerrit.index.query.ResultSet; import com.google.gerrit.reviewdb.client.Change; import com.google.gerrit.server.query.change.ChangeData; import com.google.gerrit.server.query.change.ChangeDataSource; import com.google.gwtorm.server.OrmException; -import com.google.gwtorm.server.ResultSet; import org.junit.Ignore; @Ignore