Format all Java files with google-java-format

Having a standard tool for formatting saves reviewers' valuable time.
google-java-format is Google's standard formatter and is somewhat
inspired by gofmt[1]. This commit formats everything using
google-java-format version 1.2.

The downside of this one-off formatting is breaking blame. This can be
somewhat hacked around with a tool like git-hyper-blame[2], but it's
definitely not optimal until/unless this kind of feature makes its way
to git core.

Not in this change:
* Tool support, e.g. Eclipse. The command must be run manually [3].
* Documentation of best practice, e.g. new 100-column default.

[1] https://talks.golang.org/2015/gofmt-en.slide#3
[2] https://commondatastorage.googleapis.com/chrome-infra-docs/flat/depot_tools/docs/html/git-hyper-blame.html
[3] git ls-files | grep java$ | xargs google-java-format -i

Change-Id: Id5f3c6de95ce0b68b41f0a478b5c99a93675aaa3
Signed-off-by: David Pursehouse <dpursehouse@collab.net>
This commit is contained in:
Dave Borowitz
2016-11-13 09:56:32 -08:00
committed by David Pursehouse
parent 6723b6d0fa
commit 292fa154c1
2443 changed files with 54816 additions and 57825 deletions

View File

@@ -34,16 +34,6 @@ import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gwtorm.protobuf.ProtobufCodec;
import org.eclipse.jgit.lib.Config;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.JestResult;
import io.searchbox.client.config.HttpClientConfig;
@@ -53,10 +43,17 @@ import io.searchbox.core.Delete;
import io.searchbox.indices.CreateIndex;
import io.searchbox.indices.DeleteIndex;
import io.searchbox.indices.IndicesExists;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.eclipse.jgit.lib.Config;
import org.elasticsearch.common.xcontent.XContentBuilder;
abstract class AbstractElasticIndex<K, V> implements Index<K, V> {
protected static <T> List<T> decodeProtos(JsonObject doc, String fieldName,
ProtobufCodec<T> codec) {
protected static <T> List<T> decodeProtos(
JsonObject doc, String fieldName, ProtobufCodec<T> codec) {
JsonArray field = doc.getAsJsonArray(fieldName);
if (field == null) {
return null;
@@ -76,7 +73,8 @@ abstract class AbstractElasticIndex<K, V> implements Index<K, V> {
protected final Gson gson;
protected final ElasticQueryBuilder queryBuilder;
AbstractElasticIndex(@GerritServerConfig Config cfg,
AbstractElasticIndex(
@GerritServerConfig Config cfg,
FillArgs fillArgs,
SitePaths sitePaths,
Schema<V> schema,
@@ -84,17 +82,18 @@ abstract class AbstractElasticIndex<K, V> implements Index<K, V> {
this.fillArgs = fillArgs;
this.sitePaths = sitePaths;
this.schema = schema;
this.gson = new GsonBuilder()
.setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES).create();
this.gson = new GsonBuilder().setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES).create();
this.queryBuilder = new ElasticQueryBuilder();
String protocol = getRequiredConfigOption(cfg, "protocol");
String hostname = getRequiredConfigOption(cfg, "hostname");
String port = getRequiredConfigOption(cfg, "port");
this.indexName = String.format("%s%s%04d",
Strings.nullToEmpty(cfg.getString("index", null, "prefix")),
indexName,
schema.getVersion());
this.indexName =
String.format(
"%s%s%04d",
Strings.nullToEmpty(cfg.getString("index", null, "prefix")),
indexName,
schema.getVersion());
// By default Elasticsearch has a 1s delay before changes are available in
// the index. Setting refresh(true) on calls to the index makes the index
@@ -110,12 +109,12 @@ abstract class AbstractElasticIndex<K, V> implements Index<K, V> {
String url = buildUrl(protocol, hostname, port);
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(new HttpClientConfig
.Builder(url)
.multiThreaded(true)
.discoveryEnabled(!refresh)
.discoveryFrequency(1L, TimeUnit.MINUTES)
.build());
factory.setHttpClientConfig(
new HttpClientConfig.Builder(url)
.multiThreaded(true)
.discoveryEnabled(!refresh)
.discoveryFrequency(1L, TimeUnit.MINUTES)
.build());
client = (JestHttpClient) factory.getObject();
}
@@ -139,33 +138,30 @@ abstract class AbstractElasticIndex<K, V> implements Index<K, V> {
Bulk bulk = addActions(new Bulk.Builder(), c).refresh(refresh).build();
JestResult result = client.execute(bulk);
if (!result.isSucceeded()) {
throw new IOException(String.format(
"Failed to delete change %s in index %s: %s", c, indexName,
result.getErrorMessage()));
throw new IOException(
String.format(
"Failed to delete change %s in index %s: %s",
c, indexName, result.getErrorMessage()));
}
}
@Override
public void deleteAll() throws IOException {
// Delete the index, if it exists.
JestResult result = client.execute(
new IndicesExists.Builder(indexName).build());
JestResult result = client.execute(new IndicesExists.Builder(indexName).build());
if (result.isSucceeded()) {
result = client.execute(
new DeleteIndex.Builder(indexName).build());
result = client.execute(new DeleteIndex.Builder(indexName).build());
if (!result.isSucceeded()) {
throw new IOException(String.format(
"Failed to delete index %s: %s", indexName,
result.getErrorMessage()));
throw new IOException(
String.format("Failed to delete index %s: %s", indexName, result.getErrorMessage()));
}
}
// Recreate the index.
result = client.execute(
new CreateIndex.Builder(indexName).settings(getMappings()).build());
result = client.execute(new CreateIndex.Builder(indexName).settings(getMappings()).build());
if (!result.isSucceeded()) {
String error = String.format("Failed to create index %s: %s",
indexName, result.getErrorMessage());
String error =
String.format("Failed to create index %s: %s", indexName, result.getErrorMessage());
throw new IOException(error);
}
}
@@ -178,20 +174,13 @@ abstract class AbstractElasticIndex<K, V> implements Index<K, V> {
protected Delete delete(String type, K c) {
String id = c.toString();
return new Delete.Builder(id)
.index(indexName)
.type(type)
.build();
return new Delete.Builder(id).index(indexName).type(type).build();
}
protected io.searchbox.core.Index insert(String type, V v) throws IOException {
String id = getId(v);
String doc = toDoc(v);
return new io.searchbox.core.Index.Builder(doc)
.index(indexName)
.type(type)
.id(id)
.build();
return new io.searchbox.core.Index.Builder(doc).index(indexName).type(type).id(id).build();
}
private String toDoc(V v) throws IOException {
@@ -221,8 +210,13 @@ abstract class AbstractElasticIndex<K, V> implements Index<K, V> {
return new URL(protocol, hostname, Integer.parseInt(port), "").toString();
} catch (MalformedURLException | NumberFormatException e) {
throw new RuntimeException(
"Cannot build url to Elasticsearch from values: protocol=" + protocol
+ " hostname=" + hostname + " port=" + port, e);
"Cannot build url to Elasticsearch from values: protocol="
+ protocol
+ " hostname="
+ hostname
+ " port="
+ port,
e);
}
}
}

View File

@@ -41,28 +41,25 @@ import com.google.gwtorm.server.ResultSet;
import com.google.inject.Provider;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import org.eclipse.jgit.lib.Config;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import io.searchbox.client.JestResult;
import io.searchbox.core.Bulk;
import io.searchbox.core.Bulk.Builder;
import io.searchbox.core.Search;
import io.searchbox.core.search.sort.Sort;
import io.searchbox.core.search.sort.Sort.Sorting;
import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.eclipse.jgit.lib.Config;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ElasticAccountIndex extends
AbstractElasticIndex<Account.Id, AccountState> implements AccountIndex {
public class ElasticAccountIndex extends AbstractElasticIndex<Account.Id, AccountState>
implements AccountIndex {
static class AccountMapping {
MappingProperties accounts;
@@ -74,8 +71,7 @@ public class ElasticAccountIndex extends
static final String ACCOUNTS = "accounts";
static final String ACCOUNTS_PREFIX = ACCOUNTS + "_";
private static final Logger log =
LoggerFactory.getLogger(ElasticAccountIndex.class);
private static final Logger log = LoggerFactory.getLogger(ElasticAccountIndex.class);
private final AccountMapping mapping;
private final Provider<AccountCache> accountCache;
@@ -94,23 +90,25 @@ public class ElasticAccountIndex extends
@Override
public void replace(AccountState as) throws IOException {
Bulk bulk = new Bulk.Builder()
.defaultIndex(indexName)
.defaultType(ACCOUNTS)
.addAction(insert(ACCOUNTS, as))
.refresh(refresh)
.build();
Bulk bulk =
new Bulk.Builder()
.defaultIndex(indexName)
.defaultType(ACCOUNTS)
.addAction(insert(ACCOUNTS, as))
.refresh(refresh)
.build();
JestResult result = client.execute(bulk);
if (!result.isSucceeded()) {
throw new IOException(
String.format("Failed to replace account %s in index %s: %s",
String.format(
"Failed to replace account %s in index %s: %s",
as.getAccount().getId(), indexName, result.getErrorMessage()));
}
}
@Override
public DataSource<AccountState> getSource(Predicate<AccountState> p,
QueryOptions opts) throws QueryParseException {
public DataSource<AccountState> getSource(Predicate<AccountState> p, QueryOptions opts)
throws QueryParseException {
return new QuerySource(p, opts);
}
@@ -121,8 +119,7 @@ public class ElasticAccountIndex extends
@Override
protected String getMappings() {
ImmutableMap<String, AccountMapping> mappings =
ImmutableMap.of("mappings", mapping);
ImmutableMap<String, AccountMapping> mappings = ImmutableMap.of("mappings", mapping);
return gson.toJson(mappings);
}
@@ -135,24 +132,25 @@ public class ElasticAccountIndex extends
private final Search search;
private final Set<String> fields;
QuerySource(Predicate<AccountState> p, QueryOptions opts)
throws QueryParseException {
QuerySource(Predicate<AccountState> p, QueryOptions opts) throws QueryParseException {
QueryBuilder qb = queryBuilder.toQueryBuilder(p);
fields = IndexUtils.accountFields(opts);
SearchSourceBuilder searchSource = new SearchSourceBuilder()
.query(qb)
.from(opts.start())
.size(opts.limit())
.fields(Lists.newArrayList(fields));
SearchSourceBuilder searchSource =
new SearchSourceBuilder()
.query(qb)
.from(opts.start())
.size(opts.limit())
.fields(Lists.newArrayList(fields));
Sort sort = new Sort(AccountField.ID.getName(), Sorting.ASC);
sort.setIgnoreUnmapped();
search = new Search.Builder(searchSource.toString())
.addType(ACCOUNTS)
.addIndex(indexName)
.addSort(ImmutableList.of(sort))
.build();
search =
new Search.Builder(searchSource.toString())
.addType(ACCOUNTS)
.addIndex(indexName)
.addSort(ImmutableList.of(sort))
.build();
}
@Override
@@ -210,8 +208,7 @@ public class ElasticAccountIndex extends
source = json.getAsJsonObject().get("fields");
}
Account.Id id = new Account.Id(
source.getAsJsonObject().get(ID.getName()).getAsInt());
Account.Id id = new Account.Id(source.getAsJsonObject().get(ID.getName()).getAsInt());
// Use the AccountCache rather than depending on any stored fields in the
// document (of which there shouldn't be any). The most expensive part to
// compute anyway is the effective group IDs, and we don't have a good way

View File

@@ -57,7 +57,17 @@ import com.google.gwtorm.server.ResultSet;
import com.google.inject.Provider;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import io.searchbox.client.JestResult;
import io.searchbox.core.Bulk;
import io.searchbox.core.Bulk.Builder;
import io.searchbox.core.Search;
import io.searchbox.core.search.sort.Sort;
import io.searchbox.core.search.sort.Sort.Sorting;
import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.commons.codec.binary.Base64;
import org.eclipse.jgit.lib.Config;
import org.elasticsearch.index.query.QueryBuilder;
@@ -65,24 +75,10 @@ import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import io.searchbox.client.JestResult;
import io.searchbox.core.Bulk;
import io.searchbox.core.Bulk.Builder;
import io.searchbox.core.Search;
import io.searchbox.core.search.sort.Sort;
import io.searchbox.core.search.sort.Sort.Sorting;
/** Secondary index implementation using Elasticsearch. */
class ElasticChangeIndex extends AbstractElasticIndex<Change.Id, ChangeData>
implements ChangeIndex {
private static final Logger log =
LoggerFactory.getLogger(ElasticChangeIndex.class);
private static final Logger log = LoggerFactory.getLogger(ElasticChangeIndex.class);
static class ChangeMapping {
MappingProperties openChanges;
@@ -134,18 +130,20 @@ class ElasticChangeIndex extends AbstractElasticIndex<Change.Id, ChangeData>
throw new IOException(e);
}
Bulk bulk = new Bulk.Builder()
.defaultIndex(indexName)
.defaultType("changes")
.addAction(insert(insertIndex, cd))
.addAction(delete(deleteIndex, cd.getId()))
.refresh(refresh)
.build();
Bulk bulk =
new Bulk.Builder()
.defaultIndex(indexName)
.defaultType("changes")
.addAction(insert(insertIndex, cd))
.addAction(delete(deleteIndex, cd.getId()))
.refresh(refresh)
.build();
JestResult result = client.execute(bulk);
if (!result.isSucceeded()) {
throw new IOException(String.format(
"Failed to replace change %s in index %s: %s", cd.getId(), indexName,
result.getErrorMessage()));
throw new IOException(
String.format(
"Failed to replace change %s in index %s: %s",
cd.getId(), indexName, result.getErrorMessage()));
}
}
@@ -165,9 +163,7 @@ class ElasticChangeIndex extends AbstractElasticIndex<Change.Id, ChangeData>
@Override
protected Builder addActions(Builder builder, Id c) {
return builder
.addAction(delete(OPEN_CHANGES, c))
.addAction(delete(OPEN_CHANGES, c));
return builder.addAction(delete(OPEN_CHANGES, c)).addAction(delete(OPEN_CHANGES, c));
}
@Override
@@ -184,27 +180,30 @@ class ElasticChangeIndex extends AbstractElasticIndex<Change.Id, ChangeData>
private final Search search;
private final Set<String> fields;
QuerySource(List<String> types, Predicate<ChangeData> p,
QueryOptions opts) throws QueryParseException {
List<Sort> sorts = ImmutableList.of(
new Sort(ChangeField.UPDATED.getName(), Sorting.DESC),
new Sort(ChangeField.LEGACY_ID.getName(), Sorting.DESC));
QuerySource(List<String> types, Predicate<ChangeData> p, QueryOptions opts)
throws QueryParseException {
List<Sort> sorts =
ImmutableList.of(
new Sort(ChangeField.UPDATED.getName(), Sorting.DESC),
new Sort(ChangeField.LEGACY_ID.getName(), Sorting.DESC));
for (Sort sort : sorts) {
sort.setIgnoreUnmapped();
}
QueryBuilder qb = queryBuilder.toQueryBuilder(p);
fields = IndexUtils.changeFields(opts);
SearchSourceBuilder searchSource = new SearchSourceBuilder()
.query(qb)
.from(opts.start())
.size(opts.limit())
.fields(Lists.newArrayList(fields));
SearchSourceBuilder searchSource =
new SearchSourceBuilder()
.query(qb)
.from(opts.start())
.size(opts.limit())
.fields(Lists.newArrayList(fields));
search = new Search.Builder(searchSource.toString())
.addType(types)
.addSort(sorts)
.addIndex(indexName)
.build();
search =
new Search.Builder(searchSource.toString())
.addType(types)
.addSort(sorts)
.addIndex(indexName)
.build();
}
@Override
@@ -271,27 +270,26 @@ class ElasticChangeIndex extends AbstractElasticIndex<Change.Id, ChangeData>
if (c == null) {
int id = source.get(ChangeField.LEGACY_ID.getName()).getAsInt();
String projectName =
source.get(ChangeField.PROJECT.getName()).getAsString();
String projectName = source.get(ChangeField.PROJECT.getName()).getAsString();
if (projectName == null) {
return changeDataFactory.createOnlyWhenNoteDbDisabled(
db.get(), new Change.Id(id));
return changeDataFactory.createOnlyWhenNoteDbDisabled(db.get(), new Change.Id(id));
}
return changeDataFactory.create(
db.get(), new Project.NameKey(projectName), new Change.Id(id));
}
ChangeData cd = changeDataFactory.create(db.get(),
ChangeProtoField.CODEC.decode(Base64.decodeBase64(c.getAsString())));
ChangeData cd =
changeDataFactory.create(
db.get(), ChangeProtoField.CODEC.decode(Base64.decodeBase64(c.getAsString())));
// Patch sets.
cd.setPatchSets(decodeProtos(
source, ChangeField.PATCH_SET.getName(), PatchSetProtoField.CODEC));
cd.setPatchSets(
decodeProtos(source, ChangeField.PATCH_SET.getName(), PatchSetProtoField.CODEC));
// Approvals.
if (source.get(ChangeField.APPROVAL.getName()) != null) {
cd.setCurrentApprovals(decodeProtos(source,
ChangeField.APPROVAL.getName(), PatchSetApprovalProtoField.CODEC));
cd.setCurrentApprovals(
decodeProtos(source, ChangeField.APPROVAL.getName(), PatchSetApprovalProtoField.CODEC));
} else if (fields.contains(ChangeField.APPROVAL.getName())) {
cd.setCurrentApprovals(Collections.emptyList());
}
@@ -320,12 +318,10 @@ class ElasticChangeIndex extends AbstractElasticIndex<Change.Id, ChangeData>
// Reviewed-by.
if (source.get(ChangeField.REVIEWEDBY.getName()) != null) {
JsonArray reviewedBy =
source.get(ChangeField.REVIEWEDBY.getName()).getAsJsonArray();
JsonArray reviewedBy = source.get(ChangeField.REVIEWEDBY.getName()).getAsJsonArray();
if (reviewedBy.size() > 0) {
Set<Account.Id> accounts =
Sets.newHashSetWithExpectedSize(reviewedBy.size());
for (int i = 0; i < reviewedBy.size() ; i++) {
Set<Account.Id> accounts = Sets.newHashSetWithExpectedSize(reviewedBy.size());
for (int i = 0; i < reviewedBy.size(); i++) {
int aId = reviewedBy.get(i).getAsInt();
if (reviewedBy.size() == 1 && aId == ChangeField.NOT_REVIEWED) {
break;
@@ -340,41 +336,40 @@ class ElasticChangeIndex extends AbstractElasticIndex<Change.Id, ChangeData>
if (source.get(ChangeField.REVIEWER.getName()) != null) {
cd.setReviewers(
ChangeField.parseReviewerFieldValues(FluentIterable
.from(
source.get(ChangeField.REVIEWER.getName()).getAsJsonArray())
.transform(JsonElement::getAsString)));
ChangeField.parseReviewerFieldValues(
FluentIterable.from(source.get(ChangeField.REVIEWER.getName()).getAsJsonArray())
.transform(JsonElement::getAsString)));
} else if (fields.contains(ChangeField.REVIEWER.getName())) {
cd.setReviewers(ReviewerSet.empty());
}
decodeSubmitRecords(source,
decodeSubmitRecords(
source,
ChangeField.STORED_SUBMIT_RECORD_STRICT.getName(),
ChangeField.SUBMIT_RULE_OPTIONS_STRICT, cd);
decodeSubmitRecords(source,
ChangeField.SUBMIT_RULE_OPTIONS_STRICT,
cd);
decodeSubmitRecords(
source,
ChangeField.STORED_SUBMIT_RECORD_LENIENT.getName(),
ChangeField.SUBMIT_RULE_OPTIONS_LENIENT, cd);
ChangeField.SUBMIT_RULE_OPTIONS_LENIENT,
cd);
if (source.get(ChangeField.REF_STATE.getName()) != null) {
JsonArray refStates =
source.get(ChangeField.REF_STATE.getName()).getAsJsonArray();
cd.setRefStates(
Iterables.transform(
refStates, e -> Base64.decodeBase64(e.getAsString())));
JsonArray refStates = source.get(ChangeField.REF_STATE.getName()).getAsJsonArray();
cd.setRefStates(Iterables.transform(refStates, e -> Base64.decodeBase64(e.getAsString())));
}
if (source.get(ChangeField.REF_STATE_PATTERN.getName()) != null) {
JsonArray refStatePatterns = source.get(
ChangeField.REF_STATE_PATTERN.getName()).getAsJsonArray();
JsonArray refStatePatterns =
source.get(ChangeField.REF_STATE_PATTERN.getName()).getAsJsonArray();
cd.setRefStatePatterns(
Iterables.transform(
refStatePatterns, e -> Base64.decodeBase64(e.getAsString())));
Iterables.transform(refStatePatterns, e -> Base64.decodeBase64(e.getAsString())));
}
return cd;
}
private void decodeSubmitRecords(JsonObject doc, String fieldName,
SubmitRuleOptions opts, ChangeData out) {
private void decodeSubmitRecords(
JsonObject doc, String fieldName, SubmitRuleOptions opts, ChangeData out) {
JsonArray records = doc.getAsJsonArray(fieldName);
if (records == null) {
return;
@@ -383,7 +378,8 @@ class ElasticChangeIndex extends AbstractElasticIndex<Change.Id, ChangeData>
FluentIterable.from(records)
.transform(i -> new String(decodeBase64(i.toString()), UTF_8))
.toList(),
opts, out);
opts,
out);
}
}
}

View File

@@ -38,28 +38,24 @@ import com.google.gwtorm.server.ResultSet;
import com.google.inject.Provider;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import org.eclipse.jgit.lib.Config;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import io.searchbox.client.JestResult;
import io.searchbox.core.Bulk;
import io.searchbox.core.Bulk.Builder;
import io.searchbox.core.Search;
import io.searchbox.core.search.sort.Sort;
import io.searchbox.core.search.sort.Sort.Sorting;
import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.eclipse.jgit.lib.Config;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ElasticGroupIndex
extends AbstractElasticIndex<AccountGroup.UUID, AccountGroup>
public class ElasticGroupIndex extends AbstractElasticIndex<AccountGroup.UUID, AccountGroup>
implements GroupIndex {
static class GroupMapping {
MappingProperties groups;
@@ -72,8 +68,7 @@ public class ElasticGroupIndex
static final String GROUPS = "groups";
static final String GROUPS_PREFIX = GROUPS + "_";
private static final Logger log =
LoggerFactory.getLogger(ElasticGroupIndex.class);
private static final Logger log = LoggerFactory.getLogger(ElasticGroupIndex.class);
private final GroupMapping mapping;
private final Provider<GroupCache> groupCache;
@@ -92,23 +87,25 @@ public class ElasticGroupIndex
@Override
public void replace(AccountGroup group) throws IOException {
Bulk bulk = new Bulk.Builder()
.defaultIndex(indexName)
.defaultType(GROUPS)
.addAction(insert(GROUPS, group))
.refresh(refresh)
.build();
Bulk bulk =
new Bulk.Builder()
.defaultIndex(indexName)
.defaultType(GROUPS)
.addAction(insert(GROUPS, group))
.refresh(refresh)
.build();
JestResult result = client.execute(bulk);
if (!result.isSucceeded()) {
throw new IOException(
String.format("Failed to replace group %s in index %s: %s",
String.format(
"Failed to replace group %s in index %s: %s",
group.getGroupUUID().get(), indexName, result.getErrorMessage()));
}
}
@Override
public DataSource<AccountGroup> getSource(Predicate<AccountGroup> p,
QueryOptions opts) throws QueryParseException {
public DataSource<AccountGroup> getSource(Predicate<AccountGroup> p, QueryOptions opts)
throws QueryParseException {
return new QuerySource(p, opts);
}
@@ -119,8 +116,7 @@ public class ElasticGroupIndex
@Override
protected String getMappings() {
ImmutableMap<String, GroupMapping> mappings =
ImmutableMap.of("mappings", mapping);
ImmutableMap<String, GroupMapping> mappings = ImmutableMap.of("mappings", mapping);
return gson.toJson(mappings);
}
@@ -133,24 +129,25 @@ public class ElasticGroupIndex
private final Search search;
private final Set<String> fields;
QuerySource(Predicate<AccountGroup> p, QueryOptions opts)
throws QueryParseException {
QuerySource(Predicate<AccountGroup> p, QueryOptions opts) throws QueryParseException {
QueryBuilder qb = queryBuilder.toQueryBuilder(p);
fields = IndexUtils.groupFields(opts);
SearchSourceBuilder searchSource = new SearchSourceBuilder()
.query(qb)
.from(opts.start())
.size(opts.limit())
.fields(Lists.newArrayList(fields));
SearchSourceBuilder searchSource =
new SearchSourceBuilder()
.query(qb)
.from(opts.start())
.size(opts.limit())
.fields(Lists.newArrayList(fields));
Sort sort = new Sort(GroupField.UUID.getName(), Sorting.ASC);
sort.setIgnoreUnmapped();
search = new Search.Builder(searchSource.toString())
.addType(GROUPS)
.addIndex(indexName)
.addSort(ImmutableList.of(sort))
.build();
search =
new Search.Builder(searchSource.toString())
.addType(GROUPS)
.addIndex(indexName)
.addSort(ImmutableList.of(sort))
.build();
}
@Override
@@ -208,8 +205,9 @@ public class ElasticGroupIndex
source = json.getAsJsonObject().get("fields");
}
AccountGroup.UUID uuid = new AccountGroup.UUID(
source.getAsJsonObject().get(GroupField.UUID.getName()).getAsString());
AccountGroup.UUID uuid =
new AccountGroup.UUID(
source.getAsJsonObject().get(GroupField.UUID.getName()).getAsString());
// Use the GroupCache rather than depending on any stored fields in the
// document (of which there shouldn't be any).
return groupCache.get().get(uuid);

View File

@@ -25,10 +25,8 @@ import com.google.gerrit.server.index.group.GroupIndex;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.assistedinject.FactoryModuleBuilder;
import org.eclipse.jgit.lib.Config;
import java.util.Map;
import org.eclipse.jgit.lib.Config;
public class ElasticIndexModule extends LifecycleModule {
private final int threads;
@@ -52,16 +50,16 @@ public class ElasticIndexModule extends LifecycleModule {
protected void configure() {
install(
new FactoryModuleBuilder()
.implement(AccountIndex.class, ElasticAccountIndex.class)
.build(AccountIndex.Factory.class));
.implement(AccountIndex.class, ElasticAccountIndex.class)
.build(AccountIndex.Factory.class));
install(
new FactoryModuleBuilder()
.implement(ChangeIndex.class, ElasticChangeIndex.class)
.build(ChangeIndex.Factory.class));
install(
new FactoryModuleBuilder()
.implement(GroupIndex.class, ElasticGroupIndex.class)
.build(GroupIndex.Factory.class));
.implement(GroupIndex.class, ElasticGroupIndex.class)
.build(GroupIndex.Factory.class));
install(new IndexModule(threads));
install(new SingleVersionModule(singleVersions));

View File

@@ -18,7 +18,6 @@ import com.google.common.collect.ImmutableMap;
import com.google.gerrit.server.index.FieldDef;
import com.google.gerrit.server.index.FieldType;
import com.google.gerrit.server.index.Schema;
import java.util.Map;
class ElasticMapping {
@@ -40,8 +39,7 @@ class ElasticMapping {
|| fieldType == FieldType.STORED_ONLY) {
mapping.addString(name);
} else {
throw new IllegalStateException(
"Unsupported field type: " + fieldType.getName());
throw new IllegalStateException("Unsupported field type: " + fieldType.getName());
}
}
return mapping.build();

View File

@@ -26,18 +26,15 @@ import com.google.gerrit.server.query.OrPredicate;
import com.google.gerrit.server.query.Predicate;
import com.google.gerrit.server.query.QueryParseException;
import com.google.gerrit.server.query.change.AfterPredicate;
import java.time.Instant;
import org.apache.lucene.search.BooleanQuery;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import java.time.Instant;
public class ElasticQueryBuilder {
protected <T> QueryBuilder toQueryBuilder(Predicate<T> p)
throws QueryParseException {
protected <T> QueryBuilder toQueryBuilder(Predicate<T> p) throws QueryParseException {
if (p instanceof AndPredicate) {
return and(p);
} else if (p instanceof OrPredicate) {
@@ -51,8 +48,7 @@ public class ElasticQueryBuilder {
}
}
private <T> BoolQueryBuilder and(Predicate<T> p)
throws QueryParseException {
private <T> BoolQueryBuilder and(Predicate<T> p) throws QueryParseException {
try {
BoolQueryBuilder b = QueryBuilders.boolQuery();
for (Predicate<T> c : p.getChildren()) {
@@ -64,8 +60,7 @@ public class ElasticQueryBuilder {
}
}
private <T> BoolQueryBuilder or(Predicate<T> p)
throws QueryParseException {
private <T> BoolQueryBuilder or(Predicate<T> p) throws QueryParseException {
try {
BoolQueryBuilder q = QueryBuilders.boolQuery();
for (Predicate<T> c : p.getChildren()) {
@@ -77,8 +72,7 @@ public class ElasticQueryBuilder {
}
}
private <T> QueryBuilder not(Predicate<T> p)
throws QueryParseException {
private <T> QueryBuilder not(Predicate<T> p) throws QueryParseException {
Predicate<T> n = p.getChild(0);
if (n instanceof TimestampRangePredicate) {
return notTimestamp((TimestampRangePredicate<T>) n);
@@ -91,10 +85,9 @@ public class ElasticQueryBuilder {
return q;
}
private <T> QueryBuilder fieldQuery(IndexPredicate<T> p)
throws QueryParseException {
private <T> QueryBuilder fieldQuery(IndexPredicate<T> p) throws QueryParseException {
FieldType<?> type = p.getType();
FieldDef<?,?> field = p.getField();
FieldDef<?, ?> field = p.getField();
String name = field.getName();
String value = p.getValue();
@@ -118,8 +111,7 @@ public class ElasticQueryBuilder {
}
}
private <T> QueryBuilder intRangeQuery(IndexPredicate<T> p)
throws QueryParseException {
private <T> QueryBuilder intRangeQuery(IndexPredicate<T> p) throws QueryParseException {
if (p instanceof IntegerRangePredicate) {
IntegerRangePredicate<T> r = (IntegerRangePredicate<T>) p;
int minimum = r.getMinimumValue();
@@ -128,15 +120,12 @@ public class ElasticQueryBuilder {
// Just fall back to a standard integer query.
return QueryBuilders.termQuery(p.getField().getName(), minimum);
}
return QueryBuilders.rangeQuery(p.getField().getName())
.gte(minimum)
.lte(maximum);
return QueryBuilders.rangeQuery(p.getField().getName()).gte(minimum).lte(maximum);
}
throw new QueryParseException("not an integer range: " + p);
}
private <T> QueryBuilder notTimestamp(TimestampRangePredicate<T> r)
throws QueryParseException {
private <T> QueryBuilder notTimestamp(TimestampRangePredicate<T> r) throws QueryParseException {
if (r.getMinTimestamp().getTime() == 0) {
return QueryBuilders.rangeQuery(r.getField().getName())
.gt(Instant.ofEpochMilli(r.getMaxTimestamp().getTime()));
@@ -144,11 +133,9 @@ public class ElasticQueryBuilder {
throw new QueryParseException("cannot negate: " + r);
}
private <T> QueryBuilder timestampQuery(IndexPredicate<T> p)
throws QueryParseException {
private <T> QueryBuilder timestampQuery(IndexPredicate<T> p) throws QueryParseException {
if (p instanceof TimestampRangePredicate) {
TimestampRangePredicate<T> r =
(TimestampRangePredicate<T>) p;
TimestampRangePredicate<T> r = (TimestampRangePredicate<T>) p;
if (p instanceof AfterPredicate) {
return QueryBuilders.rangeQuery(r.getField().getName())
.gte(Instant.ofEpochMilli(r.getMinTimestamp().getTime()));
@@ -160,7 +147,7 @@ public class ElasticQueryBuilder {
throw new QueryParseException("not a timestamp: " + p);
}
private <T> QueryBuilder exactQuery(IndexPredicate<T> p){
private <T> QueryBuilder exactQuery(IndexPredicate<T> p) {
String name = p.getField().getName();
String value = p.getValue();
@@ -170,8 +157,7 @@ public class ElasticQueryBuilder {
if (value.startsWith("^")) {
value = value.substring(1);
}
if (value.endsWith("$") && !value.endsWith("\\$")
&& !value.endsWith("\\\\$")) {
if (value.endsWith("$") && !value.endsWith("\\$") && !value.endsWith("\\\\$")) {
value = value.substring(0, value.length() - 1);
}
return QueryBuilders.regexpQuery(name + ".key", value);

View File

@@ -19,20 +19,17 @@ import com.google.gerrit.server.query.account.AbstractQueryAccountsTest;
import com.google.gerrit.testutil.InMemoryModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import java.util.concurrent.ExecutionException;
import org.eclipse.jgit.lib.Config;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import java.util.concurrent.ExecutionException;
public class ElasticQueryAccountsTest extends AbstractQueryAccountsTest {
private static ElasticNodeInfo nodeInfo;
@BeforeClass
public static void startIndexService()
throws InterruptedException, ExecutionException {
public static void startIndexService() throws InterruptedException, ExecutionException {
if (nodeInfo != null) {
// do not start Elasticsearch twice
return;
@@ -63,7 +60,6 @@ public class ElasticQueryAccountsTest extends AbstractQueryAccountsTest {
Config elasticsearchConfig = new Config(config);
InMemoryModule.setDefaults(elasticsearchConfig);
ElasticTestUtils.configure(elasticsearchConfig, nodeInfo.port);
return Guice.createInjector(
new InMemoryModule(elasticsearchConfig, notesMigration));
return Guice.createInjector(new InMemoryModule(elasticsearchConfig, notesMigration));
}
}

View File

@@ -20,7 +20,7 @@ import com.google.gerrit.testutil.InMemoryModule;
import com.google.gerrit.testutil.InMemoryRepositoryManager.Repo;
import com.google.inject.Guice;
import com.google.inject.Injector;
import java.util.concurrent.ExecutionException;
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.Config;
import org.junit.After;
@@ -28,14 +28,11 @@ import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.concurrent.ExecutionException;
public class ElasticQueryChangesTest extends AbstractQueryChangesTest {
private static ElasticNodeInfo nodeInfo;
@BeforeClass
public static void startIndexService()
throws InterruptedException, ExecutionException {
public static void startIndexService() throws InterruptedException, ExecutionException {
if (nodeInfo != null) {
// do not start Elasticsearch twice
return;
@@ -67,8 +64,7 @@ public class ElasticQueryChangesTest extends AbstractQueryChangesTest {
Config elasticsearchConfig = new Config(config);
InMemoryModule.setDefaults(elasticsearchConfig);
ElasticTestUtils.configure(elasticsearchConfig, nodeInfo.port);
return Guice.createInjector(
new InMemoryModule(elasticsearchConfig, notesMigration));
return Guice.createInjector(new InMemoryModule(elasticsearchConfig, notesMigration));
}
@Test
@@ -78,5 +74,4 @@ public class ElasticQueryChangesTest extends AbstractQueryChangesTest {
String nameEmail = user.asIdentifiedUser().getNameEmail();
assertQuery("owner: \"" + nameEmail + "\"\\");
}
}

View File

@@ -19,20 +19,17 @@ import com.google.gerrit.server.query.group.AbstractQueryGroupsTest;
import com.google.gerrit.testutil.InMemoryModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import java.util.concurrent.ExecutionException;
import org.eclipse.jgit.lib.Config;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import java.util.concurrent.ExecutionException;
public class ElasticQueryGroupsTest extends AbstractQueryGroupsTest {
private static ElasticNodeInfo nodeInfo;
@BeforeClass
public static void startIndexService()
throws InterruptedException, ExecutionException {
public static void startIndexService() throws InterruptedException, ExecutionException {
if (nodeInfo != null) {
// do not start Elasticsearch twice
return;
@@ -63,7 +60,6 @@ public class ElasticQueryGroupsTest extends AbstractQueryGroupsTest {
Config elasticsearchConfig = new Config(config);
InMemoryModule.setDefaults(elasticsearchConfig);
ElasticTestUtils.configure(elasticsearchConfig, nodeInfo.port);
return Guice.createInjector(
new InMemoryModule(elasticsearchConfig, notesMigration));
return Guice.createInjector(new InMemoryModule(elasticsearchConfig, notesMigration));
}
}

View File

@@ -37,23 +37,22 @@ import com.google.gerrit.server.query.change.ChangeData;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.File;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import org.eclipse.jgit.lib.Config;
import org.elasticsearch.action.admin.cluster.node.info.NodesInfoRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import java.io.File;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ExecutionException;
final class ElasticTestUtils {
static final Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
static final Gson gson =
new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
static class ElasticNodeInfo {
final Node node;
@@ -75,41 +74,33 @@ final class ElasticTestUtils {
config.setBoolean("index", "elasticsearch", "test", true);
}
static ElasticNodeInfo startElasticsearchNode()
throws InterruptedException, ExecutionException {
static ElasticNodeInfo startElasticsearchNode() throws InterruptedException, ExecutionException {
File elasticDir = Files.createTempDir();
Path elasticDirPath = elasticDir.toPath();
Settings settings = Settings.settingsBuilder()
.put("cluster.name", "gerrit")
.put("node.name", "Gerrit Elasticsearch Test Node")
.put("node.local", true)
.put("discovery.zen.ping.multicast.enabled", false)
.put("index.store.fs.memory.enabled", true)
.put("index.gateway.type", "none")
.put("index.max_result_window", Integer.MAX_VALUE)
.put("gateway.type", "default")
.put("http.port", 0)
.put("discovery.zen.ping.unicast.hosts", "[\"localhost\"]")
.put("path.home", elasticDirPath.toAbsolutePath())
.put("path.data", elasticDirPath.resolve("data").toAbsolutePath())
.put("path.work", elasticDirPath.resolve("work").toAbsolutePath())
.put("path.logs", elasticDirPath.resolve("logs").toAbsolutePath())
.put("transport.tcp.connect_timeout", "60s")
.build();
Settings settings =
Settings.settingsBuilder()
.put("cluster.name", "gerrit")
.put("node.name", "Gerrit Elasticsearch Test Node")
.put("node.local", true)
.put("discovery.zen.ping.multicast.enabled", false)
.put("index.store.fs.memory.enabled", true)
.put("index.gateway.type", "none")
.put("index.max_result_window", Integer.MAX_VALUE)
.put("gateway.type", "default")
.put("http.port", 0)
.put("discovery.zen.ping.unicast.hosts", "[\"localhost\"]")
.put("path.home", elasticDirPath.toAbsolutePath())
.put("path.data", elasticDirPath.resolve("data").toAbsolutePath())
.put("path.work", elasticDirPath.resolve("work").toAbsolutePath())
.put("path.logs", elasticDirPath.resolve("logs").toAbsolutePath())
.put("transport.tcp.connect_timeout", "60s")
.build();
// Start the node
Node node = NodeBuilder.nodeBuilder()
.settings(settings)
.node();
Node node = NodeBuilder.nodeBuilder().settings(settings).node();
// Wait for it to be ready
node.client()
.admin()
.cluster()
.prepareHealth()
.setWaitForYellowStatus()
.execute()
.actionGet();
node.client().admin().cluster().prepareHealth().setWaitForYellowStatus().execute().actionGet();
assertThat(node.isClosed()).isFalse();
return new ElasticNodeInfo(node, elasticDir, getHttpPort(node));
@@ -128,76 +119,67 @@ final class ElasticTestUtils {
}
static void createAllIndexes(ElasticNodeInfo nodeInfo) {
Schema<ChangeData> changeSchema =
ChangeSchemaDefinitions.INSTANCE.getLatest();
Schema<ChangeData> changeSchema = ChangeSchemaDefinitions.INSTANCE.getLatest();
ChangeMapping openChangesMapping = new ChangeMapping(changeSchema);
ChangeMapping closedChangesMapping = new ChangeMapping(changeSchema);
openChangesMapping.closedChanges = null;
closedChangesMapping.openChanges = null;
nodeInfo.node
nodeInfo
.node
.client()
.admin()
.indices()
.prepareCreate(
String.format("%s%04d", CHANGES_PREFIX, changeSchema.getVersion()))
.prepareCreate(String.format("%s%04d", CHANGES_PREFIX, changeSchema.getVersion()))
.addMapping(OPEN_CHANGES, gson.toJson(openChangesMapping))
.addMapping(CLOSED_CHANGES, gson.toJson(closedChangesMapping))
.execute()
.actionGet();
Schema<AccountState> accountSchema =
AccountSchemaDefinitions.INSTANCE.getLatest();
Schema<AccountState> accountSchema = AccountSchemaDefinitions.INSTANCE.getLatest();
AccountMapping accountMapping = new AccountMapping(accountSchema);
nodeInfo.node
nodeInfo
.node
.client()
.admin()
.indices()
.prepareCreate(
String.format(
"%s%04d", ACCOUNTS_PREFIX, accountSchema.getVersion()))
.prepareCreate(String.format("%s%04d", ACCOUNTS_PREFIX, accountSchema.getVersion()))
.addMapping(ElasticAccountIndex.ACCOUNTS, gson.toJson(accountMapping))
.execute()
.actionGet();
Schema<AccountGroup> groupSchema =
GroupSchemaDefinitions.INSTANCE.getLatest();
Schema<AccountGroup> groupSchema = GroupSchemaDefinitions.INSTANCE.getLatest();
GroupMapping groupMapping = new GroupMapping(groupSchema);
nodeInfo.node
nodeInfo
.node
.client()
.admin()
.indices()
.prepareCreate(
String.format(
"%s%04d", GROUPS_PREFIX, groupSchema.getVersion()))
.prepareCreate(String.format("%s%04d", GROUPS_PREFIX, groupSchema.getVersion()))
.addMapping(ElasticGroupIndex.GROUPS, gson.toJson(groupMapping))
.execute()
.actionGet();
}
private static String getHttpPort(Node node)
throws InterruptedException, ExecutionException {
String nodes = node.client().admin().cluster()
.nodesInfo(new NodesInfoRequest("*")).get().toString();
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
private static String getHttpPort(Node node) throws InterruptedException, ExecutionException {
String nodes =
node.client().admin().cluster().nodesInfo(new NodesInfoRequest("*")).get().toString();
Gson gson =
new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
Info info = gson.fromJson(nodes, Info.class);
if (info.nodes == null || info.nodes.size() != 1) {
throw new RuntimeException(
"Cannot extract local Elasticsearch http port");
throw new RuntimeException("Cannot extract local Elasticsearch http port");
}
Iterator<NodeInfo> values = info.nodes.values().iterator();
String httpAddress = values.next().httpAddress;
if (Strings.isNullOrEmpty(httpAddress)) {
throw new RuntimeException(
"Cannot extract local Elasticsearch http port");
throw new RuntimeException("Cannot extract local Elasticsearch http port");
}
if (httpAddress.indexOf(':') < 0) {
throw new RuntimeException(
"Seems that port is not included in Elasticsearch http_address");
throw new RuntimeException("Seems that port is not included in Elasticsearch http_address");
}
return httpAddress.substring(httpAddress.indexOf(':') + 1,
httpAddress.length());
return httpAddress.substring(httpAddress.indexOf(':') + 1, httpAddress.length());
}
private ElasticTestUtils() {