-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added getNamedWriteableRegistry() API for extensions (#553)
* Created ScriptExtension interface #318 Signed-off-by: Aisara Imangaliyeva <imangaliyeva.aisara@gmail.com> * Created ScriptExtension interface #318 Signed-off-by: Aisara Imangaliyeva <imangaliyeva.aisara@gmail.com> * Created ScriptExtension interface #318 Signed-off-by: Aisara Imangaliyeva <imangaliyeva.aisara@gmail.com> * Created ScriptExtension interface #318 Signed-off-by: Aisara Imangaliyeva <imangaliyeva.aisara@gmail.com> * Added getNamedWriteableRegistry() API for extensions #291 Signed-off-by: Aisara Imangaliyeva <imangaliyeva.aisara@gmail.com> * Added getNamedWriteableRegistry() API for extensions #291 Signed-off-by: Aisara Imangaliyeva <imangaliyeva.aisara@gmail.com> * Created ScriptExtension interface #318 Signed-off-by: Aisara Imangaliyeva <imangaliyeva.aisara@gmail.com> * Created ScriptExtension interface #318 Signed-off-by: Aisara Imangaliyeva <imangaliyeva.aisara@gmail.com> * Added getNamedWriteableRegistry() API for extensions #291 Signed-off-by: Aisara Imangaliyeva <imangaliyeva.aisara@gmail.com> * Added getNamedWriteableRegistry() API for extensions #291 Signed-off-by: Aisara Imangaliyeva <imangaliyeva.aisara@gmail.com> * Fixed Tests. Added getNamedWriteableRegistry() API for extensions #291 Signed-off-by: Aisara Imangaliyeva <imangaliyeva.aisara@gmail.com> * Fixed Tests. Added getNamedWriteableRegistry() API for extensions #291 Signed-off-by: Aisara Imangaliyeva <imangaliyeva.aisara@gmail.com> * Fixed Tests. Added getNamedWriteableRegistry() API for extensions #291 Signed-off-by: Aisara Imangaliyeva <imangaliyeva.aisara@gmail.com> * Fixed Tests. Added getNamedWriteableRegistry() API for extensions #291 Signed-off-by: Aisara Imangaliyeva <imangaliyeva.aisara@gmail.com> * Fixed Tests. Added getNamedWriteableRegistry() API for extensions #291 Signed-off-by: Aisara Imangaliyeva <imangaliyeva.aisara@gmail.com> --------- Signed-off-by: Aisara Imangaliyeva <imangaliyeva.aisara@gmail.com> Signed-off-by: petiveriaalliacea <77691894+petiveriaalliacea@users.noreply.github.com> Co-authored-by: Aisara Imangaliyeva <imangaliyeva.aisara@gmail.com> Co-authored-by: Ryan Bogan <10944539+ryanbogan@users.noreply.github.com>
- Loading branch information
1 parent
ebc684a
commit e13aed8
Showing
6 changed files
with
239 additions
and
22 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
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
78 changes: 78 additions & 0 deletions
78
src/main/java/org/opensearch/sdk/SDKNamedWriteableRegistry.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,78 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.sdk; | ||
|
||
import org.opensearch.cluster.ClusterModule; | ||
import org.opensearch.common.io.stream.NamedWriteableRegistry; | ||
import org.opensearch.common.io.stream.NamedWriteableRegistry.Entry; | ||
import org.opensearch.common.network.NetworkModule; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.indices.IndicesModule; | ||
import org.opensearch.search.SearchModule; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
import java.util.stream.Stream; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
|
||
/** | ||
* Combines Extension NamedWriteable with core OpenSearch NamedWriteable | ||
*/ | ||
public class SDKNamedWriteableRegistry { | ||
private NamedWriteableRegistry namedWriteableRegistry; | ||
|
||
/** | ||
* Creates and populates a NamedWriteableRegistry with the NamedWriteableRegistry entries for this extension and locally defined content. | ||
* | ||
* @param runner The ExtensionsRunner instance. | ||
*/ | ||
public SDKNamedWriteableRegistry(ExtensionsRunner runner) { | ||
this.namedWriteableRegistry = createRegistry(runner.getEnvironmentSettings(), runner.getCustomNamedWriteables()); | ||
} | ||
|
||
/** | ||
* Updates the NamedWriteableRegistry with the NamedWriteableRegistry entries for this extension and locally defined content. | ||
* <p> | ||
* Only necessary if environment settings have changed. | ||
* | ||
* @param runner The ExtensionsRunner instance. | ||
*/ | ||
public void updateNamedWriteableRegistry(ExtensionsRunner runner) { | ||
this.namedWriteableRegistry = createRegistry(runner.getEnvironmentSettings(), runner.getCustomNamedWriteables()); | ||
} | ||
|
||
private NamedWriteableRegistry createRegistry(Settings settings, List<Entry> extensionNamedWriteable) { | ||
Stream<Entry> extensionContent = extensionNamedWriteable == null ? Stream.empty() : extensionNamedWriteable.stream(); | ||
return new NamedWriteableRegistry( | ||
Stream.of( | ||
extensionContent, | ||
NetworkModule.getNamedWriteables().stream(), | ||
new IndicesModule(Collections.emptyList()).getNamedWriteables().stream(), | ||
new SearchModule(settings, Collections.emptyList()).getNamedWriteables().stream(), | ||
ClusterModule.getNamedWriteables().stream() | ||
).flatMap(Function.identity()).collect(toList()) | ||
); | ||
} | ||
|
||
/** | ||
* Gets the NamedWriteableRegistry. | ||
* | ||
* @return The NamedWriteableRegistry. Includes both extension-defined Writeable and core OpenSearch Writeable. | ||
*/ | ||
public NamedWriteableRegistry getRegistry() { | ||
return this.namedWriteableRegistry; | ||
} | ||
|
||
public void setNamedWriteableRegistry(NamedWriteableRegistry namedWriteableRegistry) { | ||
this.namedWriteableRegistry = namedWriteableRegistry; | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
src/test/java/org/opensearch/sdk/TestSDKNamedWriteableRegistry.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,116 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.sdk; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.opensearch.common.io.stream.NamedWriteable; | ||
import org.opensearch.common.io.stream.NamedWriteableRegistry; | ||
import org.opensearch.common.io.stream.StreamInput; | ||
import org.opensearch.common.io.stream.StreamOutput; | ||
import org.opensearch.common.io.stream.Writeable; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class TestSDKNamedWriteableRegistry extends OpenSearchTestCase { | ||
private TestSDKNamedWriteableRegistry.ExampleRunnerForTest runner; | ||
|
||
private static class DummyNamedWriteable implements NamedWriteable { | ||
DummyNamedWriteable(StreamInput in) {} | ||
|
||
@Override | ||
public String getWriteableName() { | ||
return "test"; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException {} | ||
} | ||
|
||
private static class Example implements NamedWriteable { | ||
public static final String NAME = "Example"; | ||
public static final NamedWriteableRegistry.Entry WRITEABLE_REGISTRY = new NamedWriteableRegistry.Entry( | ||
NamedWriteable.class, | ||
NAME, | ||
DummyNamedWriteable::new | ||
); | ||
|
||
private final String name; | ||
|
||
public Example(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String getWriteableName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException {} | ||
} | ||
|
||
private static class ExampleRunnerForTest extends ExtensionsRunnerForTest { | ||
|
||
private List<NamedWriteableRegistry.Entry> testNamedWriteables = Collections.emptyList(); | ||
private final SDKNamedWriteableRegistry sdkNamedWriteableRegistry = new SDKNamedWriteableRegistry(this); | ||
|
||
public ExampleRunnerForTest() throws IOException { | ||
super(); | ||
} | ||
|
||
@Override | ||
public Settings getEnvironmentSettings() { | ||
return Settings.EMPTY; | ||
} | ||
|
||
@Override | ||
public List<NamedWriteableRegistry.Entry> getCustomNamedWriteables() { | ||
return this.testNamedWriteables; | ||
} | ||
|
||
@Override | ||
public void updateNamedWriteableRegistry() { | ||
this.testNamedWriteables = Collections.singletonList(Example.WRITEABLE_REGISTRY); | ||
this.sdkNamedWriteableRegistry.updateNamedWriteableRegistry(this); | ||
} | ||
} | ||
|
||
@Override | ||
@BeforeEach | ||
public void setUp() throws IOException { | ||
this.runner = new TestSDKNamedWriteableRegistry.ExampleRunnerForTest(); | ||
} | ||
|
||
@Test | ||
public void testDefaultNamedWriteableRegistry() throws IOException { | ||
NamedWriteableRegistry registry = runner.sdkNamedWriteableRegistry.getRegistry(); | ||
|
||
IllegalArgumentException ex = assertThrows( | ||
IllegalArgumentException.class, | ||
() -> registry.getReader(TestSDKNamedWriteableRegistry.Example.class, TestSDKNamedWriteableRegistry.Example.NAME) | ||
); | ||
assertEquals("Unknown NamedWriteable category [" + TestSDKNamedWriteableRegistry.Example.class.getName() + "]", ex.getMessage()); | ||
} | ||
|
||
@Test | ||
public void testCustomNamedWriteableRegistry() throws IOException { | ||
// Update the runner before testing | ||
runner.updateNamedWriteableRegistry(); | ||
NamedWriteableRegistry registry = runner.sdkNamedWriteableRegistry.getRegistry(); | ||
|
||
Writeable.Reader<? extends NamedWriteable> reader = registry.getReader(NamedWriteable.class, Example.NAME); | ||
assertNotNull(reader.read(null)); | ||
} | ||
} |