Skip to content

Improve warning value extraction performance in Response #50208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 13, 2020
Merged
31 changes: 24 additions & 7 deletions client/rest/src/main/java/org/elasticsearch/client/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,36 @@ public HttpEntity getEntity() {
"GMT" + // GMT
"\")?"); // closing quote (optional, since an older version can still send a warn-date)

/**
* Refer to org.elasticsearch.common.logging.DeprecationLogger
*/
private static String extractWarningValueFromWarningHeader(final String s) {
final int firstQuote = s.indexOf('\"');
final int lastQuote = s.length() - 1;
final String warningValue = s.substring(firstQuote + 1, lastQuote);
assert assertWarningValue(s, warningValue);
return warningValue;
}

/**
* Refer to org.elasticsearch.common.logging.DeprecationLogger
*/
private static boolean assertWarningValue(final String s, final String warningValue) {
final Matcher matcher = WARNING_HEADER_PATTERN.matcher(s);
final boolean matches = matcher.matches();
assert matches;
return matcher.group(1).equals(warningValue);
}

/**
* Returns a list of all warning headers returned in the response.
*/
public List<String> getWarnings() {
List<String> warnings = new ArrayList<>();
for (Header header : response.getHeaders("Warning")) {
String warning = header.getValue();
final Matcher matcher = WARNING_HEADER_PATTERN.matcher(warning);
if (matcher.matches()) {
warnings.add(matcher.group(1));
} else {
warnings.add(warning);
}
final String warning = header.getValue();
final String warningHeaderValue = extractWarningValueFromWarningHeader(warning);
warnings.add(warningHeaderValue);
}
return warnings;
}
Expand Down