Skip to content

Commit

Permalink
Merge branch 'dev/sl_GoogleJavaFormat17' into testing_aug17_s
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchellGale committed Aug 17, 2023
2 parents 2ad8d7a + a3b7023 commit 977c7bd
Show file tree
Hide file tree
Showing 79 changed files with 2,393 additions and 2,767 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ spotless {
'spark/**/*.java',
'plugin/**/*.java',
'ppl/**/*.java',
'integ-test/**/*java'
'integ-test/**/*java',
'core/**/*.java',
'opensearch/**/*.java'
exclude '**/build/**', '**/build-*/**'
}
importOrder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.opensearch.client;

import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -40,18 +39,16 @@ public class OpenSearchNodeClient implements OpenSearchClient {
/** Node client provided by OpenSearch container. */
private final NodeClient client;

/**
* Constructor of OpenSearchNodeClient.
*/
/** Constructor of OpenSearchNodeClient. */
public OpenSearchNodeClient(NodeClient client) {
this.client = client;
}

@Override
public boolean exists(String indexName) {
try {
IndicesExistsResponse checkExistResponse = client.admin().indices()
.exists(new IndicesExistsRequest(indexName)).actionGet();
IndicesExistsResponse checkExistResponse =
client.admin().indices().exists(new IndicesExistsRequest(indexName)).actionGet();
return checkExistResponse.isExists();
} catch (Exception e) {
throw new IllegalStateException("Failed to check if index [" + indexName + "] exists", e);
Expand Down Expand Up @@ -83,13 +80,12 @@ public void createIndex(String indexName, Map<String, Object> mappings) {
@Override
public Map<String, IndexMapping> getIndexMappings(String... indexExpression) {
try {
GetMappingsResponse mappingsResponse = client.admin().indices()
.prepareGetMappings(indexExpression)
.setLocal(true)
.get();
return mappingsResponse.mappings().entrySet().stream().collect(Collectors.toUnmodifiableMap(
Map.Entry::getKey,
cursor -> new IndexMapping(cursor.getValue())));
GetMappingsResponse mappingsResponse =
client.admin().indices().prepareGetMappings(indexExpression).setLocal(true).get();
return mappingsResponse.mappings().entrySet().stream()
.collect(
Collectors.toUnmodifiableMap(
Map.Entry::getKey, cursor -> new IndexMapping(cursor.getValue())));
} catch (IndexNotFoundException e) {
// Re-throw directly to be treated as client error finally
throw e;
Expand Down Expand Up @@ -127,15 +123,11 @@ public Map<String, Integer> getIndexMaxResultWindows(String... indexExpression)
}
}

/**
* TODO: Scroll doesn't work for aggregation. Support aggregation later.
*/
/** TODO: Scroll doesn't work for aggregation. Support aggregation later. */
@Override
public OpenSearchResponse search(OpenSearchRequest request) {
return request.search(
req -> client.search(req).actionGet(),
req -> client.searchScroll(req).actionGet()
);
req -> client.search(req).actionGet(), req -> client.searchScroll(req).actionGet());
}

/**
Expand All @@ -145,13 +137,12 @@ public OpenSearchResponse search(OpenSearchRequest request) {
*/
@Override
public List<String> indices() {
final GetIndexResponse indexResponse = client.admin().indices()
.prepareGetIndex()
.setLocal(true)
.get();
final GetIndexResponse indexResponse =
client.admin().indices().prepareGetIndex().setLocal(true).get();
final Stream<String> aliasStream =
ImmutableList.copyOf(indexResponse.aliases().values()).stream()
.flatMap(Collection::stream).map(AliasMetadata::alias);
.flatMap(Collection::stream)
.map(AliasMetadata::alias);

return Stream.concat(Arrays.stream(indexResponse.getIndices()), aliasStream)
.collect(Collectors.toList());
Expand All @@ -164,20 +155,20 @@ public List<String> indices() {
*/
@Override
public Map<String, String> meta() {
return ImmutableMap.of(META_CLUSTER_NAME,
client.settings().get("cluster.name", "opensearch"));
return ImmutableMap.of(META_CLUSTER_NAME, client.settings().get("cluster.name", "opensearch"));
}

@Override
public void cleanup(OpenSearchRequest request) {
request.clean(scrollId -> {
try {
client.prepareClearScroll().addScrollId(scrollId).get();
} catch (Exception e) {
throw new IllegalStateException(
"Failed to clean up resources for search request " + request, e);
}
});
request.clean(
scrollId -> {
try {
client.prepareClearScroll().addScrollId(scrollId).get();
} catch (Exception e) {
throw new IllegalStateException(
"Failed to clean up resources for search request " + request, e);
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.opensearch.client;

import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -49,8 +48,7 @@ public class OpenSearchRestClient implements OpenSearchClient {
@Override
public boolean exists(String indexName) {
try {
return client.indices().exists(
new GetIndexRequest(indexName), RequestOptions.DEFAULT);
return client.indices().exists(new GetIndexRequest(indexName), RequestOptions.DEFAULT);
} catch (IOException e) {
throw new IllegalStateException("Failed to check if index [" + indexName + "] exist", e);
}
Expand All @@ -59,8 +57,9 @@ public boolean exists(String indexName) {
@Override
public void createIndex(String indexName, Map<String, Object> mappings) {
try {
client.indices().create(
new CreateIndexRequest(indexName).mapping(mappings), RequestOptions.DEFAULT);
client
.indices()
.create(new CreateIndexRequest(indexName).mapping(mappings), RequestOptions.DEFAULT);
} catch (IOException e) {
throw new IllegalStateException("Failed to create index [" + indexName + "]", e);
}
Expand All @@ -80,27 +79,29 @@ public Map<String, IndexMapping> getIndexMappings(String... indexExpression) {

@Override
public Map<String, Integer> getIndexMaxResultWindows(String... indexExpression) {
GetSettingsRequest request = new GetSettingsRequest()
.indices(indexExpression).includeDefaults(true);
GetSettingsRequest request =
new GetSettingsRequest().indices(indexExpression).includeDefaults(true);
try {
GetSettingsResponse response = client.indices().getSettings(request, RequestOptions.DEFAULT);
Map<String, Settings> settings = response.getIndexToSettings();
Map<String, Settings> defaultSettings = response.getIndexToDefaultSettings();
Map<String, Integer> result = new HashMap<>();

defaultSettings.forEach((key, value) -> {
Integer maxResultWindow = value.getAsInt("index.max_result_window", null);
if (maxResultWindow != null) {
result.put(key, maxResultWindow);
}
});

settings.forEach((key, value) -> {
Integer maxResultWindow = value.getAsInt("index.max_result_window", null);
if (maxResultWindow != null) {
result.put(key, maxResultWindow);
}
});
defaultSettings.forEach(
(key, value) -> {
Integer maxResultWindow = value.getAsInt("index.max_result_window", null);
if (maxResultWindow != null) {
result.put(key, maxResultWindow);
}
});

settings.forEach(
(key, value) -> {
Integer maxResultWindow = value.getAsInt("index.max_result_window", null);
if (maxResultWindow != null) {
result.put(key, maxResultWindow);
}
});

return result;
} catch (IOException e) {
Expand All @@ -126,8 +127,7 @@ public OpenSearchResponse search(OpenSearchRequest request) {
throw new IllegalStateException(
"Failed to perform scroll operation with request " + req, e);
}
}
);
});
}

/**
Expand All @@ -142,7 +142,8 @@ public List<String> indices() {
client.indices().get(new GetIndexRequest(), RequestOptions.DEFAULT);
final Stream<String> aliasStream =
ImmutableList.copyOf(indexResponse.getAliases().values()).stream()
.flatMap(Collection::stream).map(AliasMetadata::alias);
.flatMap(Collection::stream)
.map(AliasMetadata::alias);
return Stream.concat(Arrays.stream(indexResponse.getIndices()), aliasStream)
.collect(Collectors.toList());
} catch (IOException e) {
Expand Down Expand Up @@ -173,16 +174,17 @@ public Map<String, String> meta() {

@Override
public void cleanup(OpenSearchRequest request) {
request.clean(scrollId -> {
try {
ClearScrollRequest clearRequest = new ClearScrollRequest();
clearRequest.addScrollId(scrollId);
client.clearScroll(clearRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
throw new IllegalStateException(
"Failed to clean up resources for search request " + request, e);
}
});
request.clean(
scrollId -> {
try {
ClearScrollRequest clearRequest = new ClearScrollRequest();
clearRequest.addScrollId(scrollId);
client.clearScroll(clearRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
throw new IllegalStateException(
"Failed to clean up resources for search request " + request, e);
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.opensearch.data.type;

import static org.opensearch.sql.data.type.ExprCoreType.UNKNOWN;

import lombok.EqualsAndHashCode;

/**
* The type of a geo_point value. See
* <a href="https://opensearch.org/docs/latest/opensearch/supported-field-types/geo-point/">doc</a>
* The type of a geo_point value. See <a
* href="https://opensearch.org/docs/latest/opensearch/supported-field-types/geo-point/">doc</a>
*/
@EqualsAndHashCode(callSuper = false)
public class OpenSearchGeoPointType extends OpenSearchDataType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.opensearch.data.type;

import static org.opensearch.sql.data.type.ExprCoreType.UNKNOWN;

import lombok.EqualsAndHashCode;

/**
* The type of an ip value. See
* <a href="https://opensearch.org/docs/latest/opensearch/supported-field-types/ip/">doc</a>
* The type of an ip value. See <a
* href="https://opensearch.org/docs/latest/opensearch/supported-field-types/ip/">doc</a>
*/
@EqualsAndHashCode(callSuper = false)
public class OpenSearchIpType extends OpenSearchDataType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@
import org.opensearch.sql.data.type.ExprType;

/**
* The type of a text value. See
* <a href="https://opensearch.org/docs/latest/opensearch/supported-field-types/text/">doc</a>
* The type of a text value. See <a
* href="https://opensearch.org/docs/latest/opensearch/supported-field-types/text/">doc</a>
*/
public class OpenSearchTextType extends OpenSearchDataType {

private static final OpenSearchTextType instance = new OpenSearchTextType();

// text could have fields
// a read-only collection
@EqualsAndHashCode.Exclude
Map<String, OpenSearchDataType> fields = ImmutableMap.of();
@EqualsAndHashCode.Exclude Map<String, OpenSearchDataType> fields = ImmutableMap.of();

private OpenSearchTextType() {
super(MappingType.Text);
Expand All @@ -34,6 +33,7 @@ private OpenSearchTextType() {

/**
* Constructs a Text Type using the passed in fields argument.
*
* @param fields The fields to be used to construct the text type.
* @return A new OpenSeachTextTypeObject
*/
Expand Down Expand Up @@ -67,8 +67,8 @@ protected OpenSearchDataType cloneEmpty() {
}

/**
* Text field doesn't have doc value (exception thrown even when you call "get")
* Limitation: assume inner field name is always "keyword".
* Text field doesn't have doc value (exception thrown even when you call "get") Limitation:
* assume inner field name is always "keyword".
*/
public static String convertTextToKeyword(String fieldName, ExprType fieldType) {
if (fieldType instanceof OpenSearchTextType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.opensearch.data.utils;

import com.fasterxml.jackson.databind.JsonNode;
Expand All @@ -14,9 +13,7 @@
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.tuple.Pair;

/**
* The Implementation of Content to represent {@link JsonNode}.
*/
/** The Implementation of Content to represent {@link JsonNode}. */
@RequiredArgsConstructor
public class OpenSearchJsonContent implements Content {

Expand Down Expand Up @@ -68,8 +65,7 @@ public Iterator<Map.Entry<String, Content>> map() {
final JsonNode mapValue = value();
mapValue
.fieldNames()
.forEachRemaining(
field -> map.put(field, new OpenSearchJsonContent(mapValue.get(field))));
.forEachRemaining(field -> map.put(field, new OpenSearchJsonContent(mapValue.get(field))));
return map.entrySet().iterator();
}

Expand Down Expand Up @@ -133,33 +129,27 @@ public Pair<Double, Double> geoValue() {
lat = extractDoubleValue(value.get("lat"));
} catch (Exception exception) {
throw new IllegalStateException(
"latitude must be number value, but got value: " + value.get(
"lat"));
"latitude must be number value, but got value: " + value.get("lat"));
}
try {
lon = extractDoubleValue(value.get("lon"));
} catch (Exception exception) {
throw new IllegalStateException(
"longitude must be number value, but got value: " + value.get(
"lon"));
"longitude must be number value, but got value: " + value.get("lon"));
}
return Pair.of(lat, lon);
} else {
throw new IllegalStateException("geo point must in format of {\"lat\": number, \"lon\": "
+ "number}");
throw new IllegalStateException(
"geo point must in format of {\"lat\": number, \"lon\": " + "number}");
}
}

/**
* Getter for value. If value is array the whole array is returned.
*/
/** Getter for value. If value is array the whole array is returned. */
private JsonNode value() {
return value;
}

/**
* Get doubleValue from JsonNode if possible.
*/
/** Get doubleValue from JsonNode if possible. */
private Double extractDoubleValue(JsonNode node) {
if (node.isTextual()) {
return Double.valueOf(node.textValue());
Expand Down
Loading

0 comments on commit 977c7bd

Please sign in to comment.