-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Andriy Redko <andriy.redko@aiven.io>
- Loading branch information
Showing
32 changed files
with
7,693 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
spring-data-opensearch/src/main/java/org/opensearch/data/client/osc/Aggregation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2022-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.opensearch.data.client.osc; | ||
|
||
import org.opensearch.client.opensearch._types.aggregations.Aggregate; | ||
|
||
/** | ||
* Class to combine an OpenSearch {@link org.opensearch.client.opensearch._types.aggregations.Aggregate} with its | ||
* name. Necessary as the OpenSearch Aggregate does not know its name. | ||
* | ||
* @author Peter-Josef Meisch | ||
* @since 4.4 | ||
*/ | ||
public class Aggregation { | ||
|
||
private final String name; | ||
private final Aggregate aggregate; | ||
|
||
public Aggregation(String name, Aggregate aggregate) { | ||
this.name = name; | ||
this.aggregate = aggregate; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public Aggregate getAggregate() { | ||
return aggregate; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...pensearch/src/main/java/org/opensearch/data/client/osc/AutoCloseableOpenSearchClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2021-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.opensearch.data.client.osc; | ||
|
||
import org.opensearch.client.RestClient; | ||
import org.opensearch.client.opensearch.OpenSearchClient; | ||
import org.opensearch.client.opensearch.cluster.OpenSearchClusterClient; | ||
import org.opensearch.client.transport.OpenSearchTransport; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Extension of the {@link OpenSearchClient} class that implements {@link AutoCloseable}. As the underlying | ||
* {@link RestClient} must be closed properly this is handled in the {@link #close()} method. | ||
* | ||
* @author Peter-Josef Meisch | ||
* @since 4.4 | ||
*/ | ||
public class AutoCloseableOpenSearchClient extends OpenSearchClient implements AutoCloseable { | ||
|
||
public AutoCloseableOpenSearchClient(OpenSearchTransport transport) { | ||
super(transport); | ||
Assert.notNull(transport, "transport must not be null"); | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
transport.close(); | ||
} | ||
|
||
@Override | ||
public OpenSearchClusterClient cluster() { | ||
return super.cluster(); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
spring-data-opensearch/src/main/java/org/opensearch/data/client/osc/ChildTemplate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2021-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.opensearch.data.client.osc; | ||
|
||
import java.io.IOException; | ||
import org.opensearch.client.ApiClient; | ||
import org.opensearch.client.json.JsonpMapper; | ||
import org.opensearch.client.opensearch.cluster.OpenSearchClusterClient; | ||
import org.opensearch.client.transport.Transport; | ||
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* base class for a template that uses one of the {@link org.opensearch.client.opensearch.OpenSearchClient}'s child | ||
* clients like {@link OpenSearchClusterClient} or | ||
* {@link org.opensearch.client.opensearch.indices.OpenSearchIndicesClient}. | ||
* | ||
* @author Peter-Josef Meisch | ||
* @since 4.4 | ||
*/ | ||
public abstract class ChildTemplate<T extends Transport, CLIENT extends ApiClient<T, CLIENT>> { | ||
|
||
protected final CLIENT client; | ||
protected final RequestConverter requestConverter; | ||
protected final ResponseConverter responseConverter; | ||
protected final OpenSearchExceptionTranslator exceptionTranslator; | ||
|
||
public ChildTemplate(CLIENT client, ElasticsearchConverter elasticsearchConverter) { | ||
this.client = client; | ||
JsonpMapper jsonpMapper = client._transport().jsonpMapper(); | ||
requestConverter = new RequestConverter(elasticsearchConverter, jsonpMapper); | ||
responseConverter = new ResponseConverter(jsonpMapper); | ||
exceptionTranslator = new OpenSearchExceptionTranslator(jsonpMapper); | ||
} | ||
|
||
/** | ||
* Callback interface to be used with {@link #execute(ClientCallback)} for operating directly on the client. | ||
*/ | ||
@FunctionalInterface | ||
public interface ClientCallback<CLIENT, RESULT> { | ||
RESULT doWithClient(CLIENT client) throws IOException; | ||
} | ||
|
||
/** | ||
* Execute a callback with the client and provide exception translation. | ||
* | ||
* @param callback the callback to execute, must not be {@literal null} | ||
* @param <RESULT> the type returned from the callback | ||
* @return the callback result | ||
*/ | ||
public <RESULT> RESULT execute(ClientCallback<CLIENT, RESULT> callback) { | ||
|
||
Assert.notNull(callback, "callback must not be null"); | ||
|
||
try { | ||
return callback.doWithClient(client); | ||
} catch (IOException | RuntimeException e) { | ||
throw exceptionTranslator.translateException(e); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
spring-data-opensearch/src/main/java/org/opensearch/data/client/osc/ClusterTemplate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2021-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.opensearch.data.client.osc; | ||
|
||
import org.opensearch.client.opensearch.cluster.HealthRequest; | ||
import org.opensearch.client.opensearch.cluster.HealthResponse; | ||
import org.opensearch.client.opensearch.cluster.OpenSearchClusterClient; | ||
import org.opensearch.client.transport.OpenSearchTransport; | ||
import org.springframework.data.elasticsearch.core.cluster.ClusterHealth; | ||
import org.springframework.data.elasticsearch.core.cluster.ClusterOperations; | ||
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter; | ||
|
||
/** | ||
* Implementation of the {@link ClusterOperations} interface using en {@link OpenSearchClusterClient}. | ||
* | ||
* @author Peter-Josef Meisch | ||
* @since 4.4 | ||
*/ | ||
public class ClusterTemplate extends ChildTemplate<OpenSearchTransport, OpenSearchClusterClient> | ||
implements ClusterOperations { | ||
|
||
public ClusterTemplate(OpenSearchClusterClient client, ElasticsearchConverter elasticsearchConverter) { | ||
super(client, elasticsearchConverter); | ||
} | ||
|
||
@Override | ||
public ClusterHealth health() { | ||
|
||
HealthRequest healthRequest = requestConverter.clusterHealthRequest(); | ||
HealthResponse healthResponse = execute(client -> client.health(healthRequest)); | ||
return responseConverter.clusterHealth(healthResponse); | ||
} | ||
|
||
} |
Oops, something went wrong.