Created
November 9, 2016 20:12
-
-
Save martijnvg/ff48df064ad60ee983ec4fd7c7471a4c to your computer and use it in GitHub Desktop.
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
public class Abcd { | |
public static void main(String[] args) throws Exception { | |
Settings settings = Settings.builder() | |
.put("http.enabled", "false") | |
.put("transport.type", "local") | |
.put("discovery.type", "local") | |
.put("path.home", "/home/mvg/temp") | |
.put("cluster.name", "bwc_index_5.0.0") | |
.put("repositories.url.allowed_urls", "http://snapshot.test*") | |
.build(); | |
Node node = new N(settings); | |
node.start(); | |
Client client = node.client(); | |
client.admin().cluster().preparePutRepository("repo") | |
.setType("url") | |
.setSettings(Settings.builder().put("url", "http://snapshot.test")) | |
.get(); | |
client.admin().cluster().preparePutStoredScript() | |
.setScriptLang(MockScriptEngine.NAME) | |
.setId("key") | |
.setSource(new BytesArray("{\"script\": \"key\"")) | |
.get(); | |
client.admin().cluster().preparePutPipeline("id", new BytesArray("{\"processors\": []}")) | |
.get(); | |
client.admin().indices().prepareCreate("test") | |
.addCustom(new TestPlugin.IMDC("value")) | |
.get(); | |
client.admin().cluster().prepareHealth().setWaitForYellowStatus().get(); | |
client.close(); | |
node.close(); | |
} | |
static class N extends Node { | |
public N(Settings settings) { | |
super(InternalSettingsPreparer.prepareEnvironment(settings, null), Arrays.asList(TestPlugin.class, S.class)); | |
} | |
} | |
public static class TestPlugin extends Plugin { | |
static { | |
IndexMetaData.registerPrototype("custom", TestPlugin.IMDC.PROTO); | |
} | |
public static class IMDC implements IndexMetaData.Custom { | |
public static final TestPlugin.IMDC PROTO = new TestPlugin.IMDC(null); | |
private final String field; | |
public IMDC(String field) { | |
this.field = field; | |
} | |
@Override | |
public Diff<IndexMetaData.Custom> diff(IndexMetaData.Custom previousState) { | |
return new TestPlugin.IMDC.IMDCDiff((TestPlugin.IMDC) previousState); | |
} | |
@Override | |
public Diff<IndexMetaData.Custom> readDiffFrom(StreamInput in) throws IOException { | |
return new TestPlugin.IMDC.IMDCDiff(new TestPlugin.IMDC(in.readString())); | |
} | |
@Override | |
public IndexMetaData.Custom readFrom(StreamInput in) throws IOException { | |
return new TestPlugin.IMDC(in.readString()); | |
} | |
@Override | |
public void writeTo(StreamOutput out) throws IOException { | |
out.writeString(field); | |
} | |
@Override | |
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | |
builder.field("field", field); | |
return builder; | |
} | |
@Override | |
public String type() { | |
return "custom"; | |
} | |
@Override | |
public IndexMetaData.Custom fromMap(Map<String, Object> map) throws IOException { | |
return new TestPlugin.IMDC((String) map.get("field")); | |
} | |
@Override | |
public IndexMetaData.Custom fromXContent(XContentParser parser) throws IOException { | |
return new TestPlugin.IMDC(parser.text()); | |
} | |
@Override | |
public IndexMetaData.Custom mergeWith(IndexMetaData.Custom another) { | |
TestPlugin.IMDC other = (TestPlugin.IMDC) another; | |
return new TestPlugin.IMDC(field + other.field); | |
} | |
static class IMDCDiff implements Diff<IndexMetaData.Custom> { | |
private final TestPlugin.IMDC previous; | |
IMDCDiff(TestPlugin.IMDC previous) { | |
this.previous = previous; | |
} | |
@Override | |
public IndexMetaData.Custom apply(IndexMetaData.Custom part) { | |
return new TestPlugin.IMDC(((TestPlugin.IMDC) part).field); | |
} | |
@Override | |
public void writeTo(StreamOutput out) throws IOException { | |
out.writeString(previous.field); | |
} | |
} | |
} | |
} | |
public static class S extends MockScriptPlugin { | |
@Override | |
protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() { | |
return Collections.singletonMap("key", params -> "value"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment