Skip to content

Commit

Permalink
Revert "[Backport] [2.x] Restore support for Java 8 (opensearch-proje…
Browse files Browse the repository at this point in the history
…ct#767) (opensearch-project#777)"

This reverts commit 081a17e.

Signed-off-by: Andriy Redko <[email protected]>
  • Loading branch information
reta committed Jan 8, 2024
1 parent e8d49fd commit b212db9
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 118 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test-integration-unreleased.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ jobs:
- { opensearch_ref: '1.x', java: 11 }
- { opensearch_ref: '2.x', java: 11 }
- { opensearch_ref: '2.x', java: 17 }
- { opensearch_ref: '2.x', java: 21 }
- { opensearch_ref: 'main', java: 11 }
- { opensearch_ref: 'main', java: 17 }
- { opensearch_ref: 'main', java: 21 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ public CatRequestBase(CatRequestBaseBuilder<?> builder) {

protected final Map<String, String> queryParameters() {
Map<String, String> params = new HashMap<>();
if (headers != null && !headers.trim().isEmpty()) {
if (headers != null && !headers.isBlank()) {
params.put("h", headers);
}
if (sort != null && !sort.trim().isEmpty()) {
if (sort != null && !sort.isBlank()) {
params.put("s", sort);
}
params.put("format", "json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

package org.opensearch.client.transport;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -99,7 +98,7 @@ public Builder addHeader(String name, String value) {
if (headers.isEmpty()) {
headers = new ArrayList<>();
}
headers.add(new AbstractMap.SimpleEntry<>(name, value));
headers.add(Map.entry(name, value));
return this;
}

Expand Down Expand Up @@ -136,8 +135,8 @@ class DefaultImpl implements TransportOptions {
private final Function<List<String>, Boolean> onWarnings;

protected DefaultImpl(BuilderImpl builder) {
this.headers = builder.headers.isEmpty() ? Collections.emptyList() : new ArrayList<>(builder.headers);
this.params = builder.queryParameters.isEmpty() ? Collections.emptyMap() : new HashMap<>(builder.queryParameters);
this.headers = builder.headers.isEmpty() ? Collections.emptyList() : List.copyOf(builder.headers);
this.params = builder.queryParameters.isEmpty() ? Collections.emptyMap() : Map.copyOf(builder.queryParameters);
this.onWarnings = builder.onWarnings;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Supplier;
import java.util.zip.GZIPInputStream;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -262,12 +261,11 @@ private <RequestT> OpenSearchRequestBodyBuffer prepareRequestBody(
.map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null)
.map(AwsSdk2TransportOptions::mapper)
.orElse(defaultMapper);
final int maxUncompressedSize = or(
Optional.ofNullable(options)
.map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null)
.map(AwsSdk2TransportOptions::requestCompressionSize),
() -> Optional.ofNullable(transportOptions.requestCompressionSize())
).orElse(DEFAULT_REQUEST_COMPRESSION_SIZE);
final int maxUncompressedSize = Optional.ofNullable(options)
.map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null)
.map(AwsSdk2TransportOptions::requestCompressionSize)
.or(() -> Optional.ofNullable(transportOptions.requestCompressionSize()))
.orElse(DEFAULT_REQUEST_COMPRESSION_SIZE);

OpenSearchRequestBodyBuffer buffer = new OpenSearchRequestBodyBuffer(mapper, maxUncompressedSize);
buffer.addContent(request);
Expand All @@ -282,7 +280,7 @@ private <RequestT> SdkHttpFullRequest prepareRequest(
Endpoint<RequestT, ?, ?> endpoint,
@CheckForNull TransportOptions options,
@CheckForNull OpenSearchRequestBodyBuffer body
) throws UnsupportedEncodingException {
) {
SdkHttpFullRequest.Builder req = SdkHttpFullRequest.builder().method(SdkHttpMethod.fromValue(endpoint.method(request)));

StringBuilder url = new StringBuilder();
Expand All @@ -295,9 +293,9 @@ private <RequestT> SdkHttpFullRequest prepareRequest(
Map<String, String> params = endpoint.queryParameters(request);
if (params != null && !params.isEmpty()) {
char sep = '?';
for (Map.Entry<String, String> ent : params.entrySet()) {
for (var ent : params.entrySet()) {
url.append(sep).append(ent.getKey()).append('=');
url.append(URLEncoder.encode(ent.getValue(), "UTF-8"));
url.append(URLEncoder.encode(ent.getValue(), StandardCharsets.UTF_8));
sep = '&';
}
}
Expand All @@ -323,24 +321,22 @@ private <RequestT> SdkHttpFullRequest prepareRequest(
req.putHeader("x-amz-content-sha256", "required");
}

boolean responseCompression = or(
Optional.ofNullable(options)
.map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null)
.map(AwsSdk2TransportOptions::responseCompression),
() -> Optional.ofNullable(transportOptions.responseCompression())
).orElse(Boolean.TRUE);
boolean responseCompression = Optional.ofNullable(options)
.map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null)
.map(AwsSdk2TransportOptions::responseCompression)
.or(() -> Optional.ofNullable(transportOptions.responseCompression()))
.orElse(Boolean.TRUE);
if (responseCompression) {
req.putHeader("Accept-Encoding", "gzip");
} else {
req.removeHeader("Accept-Encoding");
}

final AwsCredentialsProvider credentials = or(
Optional.ofNullable(options)
.map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null)
.map(AwsSdk2TransportOptions::credentials),
() -> Optional.ofNullable(transportOptions.credentials())
).orElse(DefaultCredentialsProvider.create());
final AwsCredentialsProvider credentials = Optional.ofNullable(options)
.map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null)
.map(AwsSdk2TransportOptions::credentials)
.or(() -> Optional.ofNullable(transportOptions.credentials()))
.orElse(DefaultCredentialsProvider.create());

Aws4SignerParams signerParams = Aws4SignerParams.builder()
.awsCredentials(credentials.resolveCredentials())
Expand All @@ -350,7 +346,7 @@ private <RequestT> SdkHttpFullRequest prepareRequest(
return Aws4Signer.create().sign(req.build(), signerParams);
}

private void applyOptionsParams(StringBuilder url, TransportOptions options) throws UnsupportedEncodingException {
private void applyOptionsParams(StringBuilder url, TransportOptions options) {
if (options == null) {
return;
}
Expand All @@ -359,7 +355,7 @@ private void applyOptionsParams(StringBuilder url, TransportOptions options) thr
char sep = url.indexOf("?") < 0 ? '?' : '&';
for (Map.Entry<String, String> param : params.entrySet()) {
url.append(sep).append(param.getKey()).append('=');
url.append(URLEncoder.encode(param.getValue(), "UTF-8"));
url.append(URLEncoder.encode(param.getValue(), StandardCharsets.UTF_8));
sep = '?';
}
}
Expand Down Expand Up @@ -528,16 +524,4 @@ private <ResponseT, ErrorT> ResponseT parseResponse(
}
}
}

private static <T> Optional<T> or(Optional<T> opt, Supplier<? extends Optional<? extends T>> supplier) {
Objects.requireNonNull(opt);
Objects.requireNonNull(supplier);
if (opt.isPresent()) {
return opt;
} else {
@SuppressWarnings("unchecked")
Optional<T> r = (Optional<T>) supplier.get();
return Objects.requireNonNull(r);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -932,15 +932,9 @@ public long getContentLength() {
if (chunkedEnabled.get()) {
return -1L;
} else {
long size = 0;
final byte[] buf = new byte[8192];
int nread = 0;

long size;
try (InputStream is = getContent()) {
// read to EOF which may read more or less than buffer size
while ((nread = is.read(buf)) > 0) {
size += nread;
}
size = is.readAllBytes().length;
} catch (IOException ex) {
size = -1L;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import jakarta.json.stream.JsonParser;
import java.io.StringReader;
import java.util.Collections;
import java.util.Map;
import junit.framework.TestCase;
import org.junit.Test;
import org.opensearch.client.json.JsonData;
Expand All @@ -30,7 +30,7 @@ public void testCreateKnnVectorMethodWithAll() {
KnnVectorMethod knnVectorMethod = new KnnVectorMethod.Builder().name("hnsw")
.spaceType("l2")
.engine("nmslib")
.parameters(Collections.singletonMap("ef_construction", JsonData.of(128)))
.parameters(Map.of("ef_construction", JsonData.of(128)))
.build();
assertEquals("hnsw", knnVectorMethod.name());
assertEquals("l2", knnVectorMethod.spaceType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.json.stream.JsonParser;
import java.io.StringReader;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -13,9 +12,7 @@
import org.opensearch.client.json.jsonb.JsonbJsonpMapper;
import org.opensearch.client.opensearch._types.mapping.IcuCollationKeywordProperty;
import org.opensearch.client.opensearch._types.mapping.Property;
import org.opensearch.client.opensearch._types.mapping.TypeMapping;
import org.opensearch.client.opensearch.indices.GetTemplateResponse;
import org.opensearch.client.opensearch.indices.TemplateMapping;

public class GetMappingsResponseTest extends Assert {

Expand All @@ -39,27 +36,32 @@ public void deserialize_IcuCollationKeywordExists_propertyDeserializes() throws
icuCollationConfig.put("strength", "quaternary");
icuCollationConfig.put("variable_top", "$");

final Map<String, Object> mappingTemplateMap = new HashMap<>();
mappingTemplateMap.put("aliases", Collections.emptyMap());
mappingTemplateMap.put("index_patterns", Collections.singletonList("test-pattern*"));
mappingTemplateMap.put(
"mappings",
Collections.singletonMap("properties", Collections.singletonMap("icu_test_field", icuCollationConfig))
mappingTemplate.put(
"test-index",
Map.of(
"aliases",
Collections.emptyMap(),
"index_patterns",
Collections.singletonList("test-pattern*"),
"mappings",
Map.of("properties", Map.of("icu_test_field", icuCollationConfig)),
"order",
0,
"settings",
Collections.emptyMap(),
"version",
1
)
);
mappingTemplateMap.put("order", 0);
mappingTemplateMap.put("settings", Collections.emptyMap());
mappingTemplateMap.put("version", 1);
mappingTemplate.put("test-index", mappingTemplateMap);

final JsonpMapper mapper = new JsonbJsonpMapper();
final String indexTemplate = new ObjectMapper().writeValueAsString(mappingTemplate);
final JsonParser parser = mapper.jsonProvider().createParser(new StringReader(indexTemplate));
final var parser = mapper.jsonProvider().createParser(new StringReader(indexTemplate));

final GetTemplateResponse response = GetTemplateResponse._DESERIALIZER.deserialize(parser, mapper);
final TemplateMapping template = response.get("test-index");
final TypeMapping mappings = template.mappings();
final Map<String, Property> properties = mappings.properties();
final Property property = properties.get("icu_test_field");
final var template = response.get("test-index");
final var mappings = template.mappings();
final var properties = mappings.properties();
final var property = properties.get("icu_test_field");
final IcuCollationKeywordProperty icu = property.icuCollationKeyword();

assertEquals(property._kind(), Property.Kind.IcuCollationKeyword);
Expand Down
Loading

0 comments on commit b212db9

Please sign in to comment.