AbstractElasticIndex: Factor out more variants of {post,perform}Request

Factor out more variants of these methods that eventually end up in the
one that actually makes the call to the RestClient instance. This will
allow to make any necessary adjustments to the URI or request parameters
in a single place.

At the same time, reorder the arguments to make the order consistent
across methods.

Change-Id: I3ced32e8df6cbd5a5f05f4d3241f84a7e7bebb10
This commit is contained in:
David Pursehouse
2018-09-25 08:54:51 +09:00
parent 8bddc312f1
commit d185b59c49
5 changed files with 33 additions and 15 deletions

View File

@@ -7,6 +7,7 @@ java_library(
visibility = ["//visibility:public"],
deps = [
"//gerrit-antlr:query_exception",
"//gerrit-common:annotations",
"//gerrit-extension-api:api",
"//gerrit-reviewdb:server",
"//gerrit-server:server",

View File

@@ -21,6 +21,7 @@ import static org.apache.commons.codec.binary.Base64.decodeBase64;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.CharStreams;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.elasticsearch.ElasticMapping.MappingProperties;
import com.google.gerrit.elasticsearch.builders.SearchSourceBuilder;
import com.google.gerrit.elasticsearch.bulk.DeleteRequest;
@@ -135,7 +136,7 @@ abstract class AbstractElasticIndex<K, V> implements Index<K, V> {
@Override
public void delete(K id) throws IOException {
String uri = getURI(type, BULK);
Response response = postRequest(getDeleteActions(id), uri, getRefreshParam());
Response response = postRequest(uri, getDeleteActions(id), getRefreshParam());
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
throw new IOException(
@@ -147,10 +148,10 @@ abstract class AbstractElasticIndex<K, V> implements Index<K, V> {
public void deleteAll() throws IOException {
// Delete the index, if it exists.
String endpoint = indexName + client.adapter().indicesExistParam();
Response response = client.get().performRequest(new Request("HEAD", endpoint));
Response response = performRequest("HEAD", endpoint);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
response = client.get().performRequest(new Request("DELETE", indexName));
response = performRequest("DELETE", indexName);
statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
throw new IOException(
@@ -160,7 +161,7 @@ abstract class AbstractElasticIndex<K, V> implements Index<K, V> {
// Recreate the index.
String indexCreationFields = concatJsonString(getSettings(), getMappings());
response = performRequest("PUT", indexCreationFields, indexName, Collections.emptyMap());
response = performRequest("PUT", indexName, indexCreationFields);
statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
String error = String.format("Failed to create index %s: %s", indexName, statusCode);
@@ -228,20 +229,36 @@ abstract class AbstractElasticIndex<K, V> implements Index<K, V> {
return encodedIndexName + "/" + encodedType + "/" + request;
}
protected Response postRequest(Object payload, String uri, Map<String, String> params)
protected Response postRequest(String uri, Object payload) throws IOException {
return performRequest("POST", uri, payload);
}
protected Response postRequest(String uri, Object payload, Map<String, String> params)
throws IOException {
return performRequest("POST", payload, uri, params);
return performRequest("POST", uri, payload, params);
}
private String concatJsonString(String target, String addition) {
return target.substring(0, target.length() - 1) + "," + addition.substring(1);
}
private Response performRequest(String method, String uri) throws IOException {
return performRequest(method, uri, null);
}
private Response performRequest(String method, String uri, @Nullable Object payload)
throws IOException {
return performRequest(method, uri, payload, Collections.emptyMap());
}
private Response performRequest(
String method, Object payload, String uri, Map<String, String> params) throws IOException {
String method, String uri, @Nullable Object payload, Map<String, String> params)
throws IOException {
Request request = new Request(method, uri);
String payloadStr = payload instanceof String ? (String) payload : payload.toString();
request.setEntity(new NStringEntity(payloadStr, ContentType.APPLICATION_JSON));
if (payload != null) {
String payloadStr = payload instanceof String ? (String) payload : payload.toString();
request.setEntity(new NStringEntity(payloadStr, ContentType.APPLICATION_JSON));
}
for (Map.Entry<String, String> entry : params.entrySet()) {
request.addParameter(entry.getKey(), entry.getValue());
}

View File

@@ -93,7 +93,7 @@ public class ElasticAccountIndex extends AbstractElasticIndex<Account.Id, Accoun
.add(new UpdateRequest<>(schema, as));
String uri = getURI(type, BULK);
Response response = postRequest(bulk, uri, getRefreshParam());
Response response = postRequest(uri, bulk, getRefreshParam());
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
throw new IOException(
@@ -152,7 +152,7 @@ public class ElasticAccountIndex extends AbstractElasticIndex<Account.Id, Accoun
try {
List<AccountState> results = Collections.emptyList();
String uri = getURI(type, SEARCH);
Response response = postRequest(search, uri, Collections.emptyMap());
Response response = postRequest(uri, search);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
String content = getContent(response);

View File

@@ -148,7 +148,7 @@ public class ElasticChangeIndex extends AbstractElasticIndex<Change.Id, ChangeDa
}
String uri = getURI(type, BULK);
Response response = postRequest(bulk, uri, getRefreshParam());
Response response = postRequest(uri, bulk, getRefreshParam());
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
throw new IOException(
@@ -229,7 +229,7 @@ public class ElasticChangeIndex extends AbstractElasticIndex<Change.Id, ChangeDa
try {
List<ChangeData> results = Collections.emptyList();
String uri = getURI(types);
Response response = postRequest(search, uri, Collections.emptyMap());
Response response = postRequest(uri, search);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
String content = getContent(response);

View File

@@ -90,7 +90,7 @@ public class ElasticGroupIndex extends AbstractElasticIndex<AccountGroup.UUID, A
.add(new UpdateRequest<>(schema, group));
String uri = getURI(type, BULK);
Response response = postRequest(bulk, uri, getRefreshParam());
Response response = postRequest(uri, bulk, getRefreshParam());
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
throw new IOException(
@@ -149,7 +149,7 @@ public class ElasticGroupIndex extends AbstractElasticIndex<AccountGroup.UUID, A
try {
List<AccountGroup> results = Collections.emptyList();
String uri = getURI(type, SEARCH);
Response response = postRequest(search, uri, Collections.emptyMap());
Response response = postRequest(uri, search);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
String content = getContent(response);