Skip to content

Commit ba80d06

Browse files
authored
Merge pull request #1 from yaauie/stable-api-clarity-external-internal
spike refactor of logstashbridge stable API
2 parents a9981f5 + 290e82a commit ba80d06

17 files changed

+265
-168
lines changed

libs/logstash-bridge/src/main/java/org/elasticsearch/logstashbridge/StableBridgeAPI.java

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,43 +19,51 @@
1919
* upon by the "Elastic Integration Filter Plugin" for Logstash and their external shapes must not change
2020
* without coordination with the maintainers of that project.
2121
*
22-
* @param <T> the actual type of the Elasticsearch API being mirrored
22+
* @param <INTERNAL> the actual type of the Elasticsearch API being mirrored
2323
*/
24-
public interface StableBridgeAPI<T> {
25-
T unwrap();
24+
public interface StableBridgeAPI<INTERNAL> {
25+
INTERNAL toInternal();
2626

27-
static <T> T unwrapNullable(final StableBridgeAPI<T> nullableStableBridgeAPI) {
27+
static <T> T toInternalNullable(final StableBridgeAPI<T> nullableStableBridgeAPI) {
2828
if (Objects.isNull(nullableStableBridgeAPI)) {
2929
return null;
3030
}
31-
return nullableStableBridgeAPI.unwrap();
31+
return nullableStableBridgeAPI.toInternal();
3232
}
3333

34-
static <K, T> Map<K, T> unwrap(final Map<K, ? extends StableBridgeAPI<T>> bridgeMap) {
35-
return bridgeMap.entrySet().stream().collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, e -> e.getValue().unwrap()));
34+
static <K, T> Map<K, T> toInternal(final Map<K, ? extends StableBridgeAPI<T>> bridgeMap) {
35+
return bridgeMap.entrySet().stream().collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, e -> e.getValue().toInternal()));
3636
}
3737

38-
static <K, T, B extends StableBridgeAPI<T>> Map<K, B> wrap(final Map<K, T> rawMap, final Function<T, B> wrapFunction) {
39-
return rawMap.entrySet().stream().collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, e -> wrapFunction.apply(e.getValue())));
38+
static <K, T, B extends StableBridgeAPI<T>> Map<K, B> fromInternal(final Map<K, T> rawMap, final Function<T, B> externalizor) {
39+
return rawMap.entrySet()
40+
.stream()
41+
.collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, e -> externalizor.apply(e.getValue())));
4042
}
4143

42-
static <T, B extends StableBridgeAPI<T>> B wrap(final T delegate, final Function<T, B> wrapFunction) {
44+
static <T, B extends StableBridgeAPI<T>> B fromInternal(final T delegate, final Function<T, B> externalizor) {
4345
if (Objects.isNull(delegate)) {
4446
return null;
4547
}
46-
return wrapFunction.apply(delegate);
48+
return externalizor.apply(delegate);
4749
}
4850

49-
abstract class Proxy<T> implements StableBridgeAPI<T> {
50-
protected final T delegate;
51+
/**
52+
* An {@code ProxyInternal<INTERNAL>} is an implementation of {@code StableBridgeAPI<INTERNAL>} that
53+
* proxies calls to a delegate that is an actual {@code INTERNAL}.
54+
*
55+
* @param <INTERNAL>
56+
*/
57+
abstract class ProxyInternal<INTERNAL> implements StableBridgeAPI<INTERNAL> {
58+
protected final INTERNAL internalDelegate;
5159

52-
protected Proxy(final T delegate) {
53-
this.delegate = delegate;
60+
protected ProxyInternal(final INTERNAL internalDelegate) {
61+
this.internalDelegate = internalDelegate;
5462
}
5563

5664
@Override
57-
public T unwrap() {
58-
return delegate;
65+
public INTERNAL toInternal() {
66+
return internalDelegate;
5967
}
6068
}
6169
}

libs/logstash-bridge/src/main/java/org/elasticsearch/logstashbridge/common/SettingsBridge.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,33 @@
1111
import org.elasticsearch.common.settings.Settings;
1212
import org.elasticsearch.logstashbridge.StableBridgeAPI;
1313

14-
public class SettingsBridge extends StableBridgeAPI.Proxy<Settings> {
14+
/**
15+
* An external bridge for {@link Settings}
16+
*/
17+
public class SettingsBridge extends StableBridgeAPI.ProxyInternal<Settings> {
1518

16-
public static SettingsBridge wrap(final Settings delegate) {
19+
public static SettingsBridge fromInternal(final Settings delegate) {
1720
return new SettingsBridge(delegate);
1821
}
1922

2023
public static Builder builder() {
21-
return Builder.wrap(Settings.builder());
24+
return Builder.fromInternal(Settings.builder());
2225
}
2326

2427
public SettingsBridge(final Settings delegate) {
2528
super(delegate);
2629
}
2730

2831
@Override
29-
public Settings unwrap() {
30-
return this.delegate;
32+
public Settings toInternal() {
33+
return this.internalDelegate;
3134
}
3235

33-
public static class Builder extends StableBridgeAPI.Proxy<Settings.Builder> {
34-
static Builder wrap(final Settings.Builder delegate) {
36+
/**
37+
* An external bridge for {@link Settings.Builder} that proxies calls to a real {@link Settings.Builder}
38+
*/
39+
public static class Builder extends StableBridgeAPI.ProxyInternal<Settings.Builder> {
40+
static Builder fromInternal(final Settings.Builder delegate) {
3541
return new Builder(delegate);
3642
}
3743

@@ -40,12 +46,12 @@ private Builder(final Settings.Builder delegate) {
4046
}
4147

4248
public Builder put(final String key, final String value) {
43-
this.delegate.put(key, value);
49+
this.internalDelegate.put(key, value);
4450
return this;
4551
}
4652

4753
public SettingsBridge build() {
48-
return new SettingsBridge(this.delegate.build());
54+
return new SettingsBridge(this.internalDelegate.build());
4955
}
5056
}
5157
}

libs/logstash-bridge/src/main/java/org/elasticsearch/logstashbridge/core/IOUtilsBridge.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
import java.io.Closeable;
1414

15+
/**
16+
* An external bridge for {@link IOUtils}
17+
*/
1518
public class IOUtilsBridge {
1619
public static void closeWhileHandlingException(final Iterable<? extends Closeable> objects) {
1720
IOUtils.closeWhileHandlingException(objects);

libs/logstash-bridge/src/main/java/org/elasticsearch/logstashbridge/env/EnvironmentBridge.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,24 @@
1414

1515
import java.nio.file.Path;
1616

17-
public class EnvironmentBridge extends StableBridgeAPI.Proxy<Environment> {
18-
public static EnvironmentBridge wrap(final Environment delegate) {
17+
/**
18+
* An external bridge for {@link Environment}
19+
*/
20+
public class EnvironmentBridge extends StableBridgeAPI.ProxyInternal<Environment> {
21+
public static EnvironmentBridge fromInternal(final Environment delegate) {
1922
return new EnvironmentBridge(delegate);
2023
}
2124

2225
public EnvironmentBridge(final SettingsBridge settingsBridge, final Path configPath) {
23-
this(new Environment(settingsBridge.unwrap(), configPath));
26+
this(new Environment(settingsBridge.toInternal(), configPath));
2427
}
2528

2629
private EnvironmentBridge(final Environment delegate) {
2730
super(delegate);
2831
}
2932

3033
@Override
31-
public Environment unwrap() {
32-
return this.delegate;
34+
public Environment toInternal() {
35+
return this.internalDelegate;
3336
}
3437
}

libs/logstash-bridge/src/main/java/org/elasticsearch/logstashbridge/ingest/ConfigurationUtilsBridge.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
import java.util.Map;
1616

17+
/**
18+
* An external bridge for {@link ConfigurationUtils}
19+
*/
1720
public class ConfigurationUtilsBridge {
1821
public static TemplateScriptBridge.Factory compileTemplate(
1922
final String processorType,
@@ -23,7 +26,7 @@ public static TemplateScriptBridge.Factory compileTemplate(
2326
final ScriptServiceBridge scriptServiceBridge
2427
) {
2528
return new TemplateScriptBridge.Factory(
26-
ConfigurationUtils.compileTemplate(processorType, processorTag, propertyName, propertyValue, scriptServiceBridge.unwrap())
29+
ConfigurationUtils.compileTemplate(processorType, processorTag, propertyName, propertyValue, scriptServiceBridge.toInternal())
2730
);
2831
}
2932

libs/logstash-bridge/src/main/java/org/elasticsearch/logstashbridge/ingest/IngestDocumentBridge.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@
1818
import java.util.Set;
1919
import java.util.function.BiConsumer;
2020

21-
public class IngestDocumentBridge extends StableBridgeAPI.Proxy<IngestDocument> {
21+
/**
22+
* An external bridge for {@link IngestDocument} that proxies calls through a real {@link IngestDocument}
23+
*/
24+
public class IngestDocumentBridge extends StableBridgeAPI.ProxyInternal<IngestDocument> {
2225

2326
public static final class Constants {
2427
public static final String METADATA_VERSION_FIELD_NAME = IngestDocument.Metadata.VERSION.getFieldName();
2528

2629
private Constants() {}
2730
}
2831

29-
public static IngestDocumentBridge wrap(final IngestDocument ingestDocument) {
32+
public static IngestDocumentBridge fromInternalNullable(final IngestDocument ingestDocument) {
3033
if (ingestDocument == null) {
3134
return null;
3235
}
@@ -42,54 +45,57 @@ private IngestDocumentBridge(IngestDocument inner) {
4245
}
4346

4447
public MetadataBridge getMetadata() {
45-
return new MetadataBridge(delegate.getMetadata());
48+
return new MetadataBridge(internalDelegate.getMetadata());
4649
}
4750

4851
public Map<String, Object> getSource() {
49-
return delegate.getSource();
52+
return internalDelegate.getSource();
5053
}
5154

5255
public boolean updateIndexHistory(final String index) {
53-
return delegate.updateIndexHistory(index);
56+
return internalDelegate.updateIndexHistory(index);
5457
}
5558

5659
public Set<String> getIndexHistory() {
57-
return Set.copyOf(delegate.getIndexHistory());
60+
return Set.copyOf(internalDelegate.getIndexHistory());
5861
}
5962

6063
public boolean isReroute() {
61-
return LogstashInternalBridge.isReroute(delegate);
64+
return LogstashInternalBridge.isReroute(internalDelegate);
6265
}
6366

6467
public void resetReroute() {
65-
LogstashInternalBridge.resetReroute(delegate);
68+
LogstashInternalBridge.resetReroute(internalDelegate);
6669
}
6770

6871
public Map<String, Object> getIngestMetadata() {
69-
return delegate.getIngestMetadata();
72+
return internalDelegate.getIngestMetadata();
7073
}
7174

7275
public <T> T getFieldValue(final String fieldName, final Class<T> type) {
73-
return delegate.getFieldValue(fieldName, type);
76+
return internalDelegate.getFieldValue(fieldName, type);
7477
}
7578

7679
public <T> T getFieldValue(final String fieldName, final Class<T> type, final boolean ignoreMissing) {
77-
return delegate.getFieldValue(fieldName, type, ignoreMissing);
80+
return internalDelegate.getFieldValue(fieldName, type, ignoreMissing);
7881
}
7982

8083
public String renderTemplate(final TemplateScriptBridge.Factory templateScriptFactory) {
81-
return delegate.renderTemplate(templateScriptFactory.unwrap());
84+
return internalDelegate.renderTemplate(templateScriptFactory.toInternal());
8285
}
8386

8487
public void setFieldValue(final String path, final Object value) {
85-
delegate.setFieldValue(path, value);
88+
internalDelegate.setFieldValue(path, value);
8689
}
8790

8891
public void removeField(final String path) {
89-
delegate.removeField(path);
92+
internalDelegate.removeField(path);
9093
}
9194

9295
public void executePipeline(final PipelineBridge pipelineBridge, final BiConsumer<IngestDocumentBridge, Exception> handler) {
93-
this.delegate.executePipeline(pipelineBridge.unwrap(), (unwrapped, e) -> handler.accept(IngestDocumentBridge.wrap(unwrapped), e));
96+
this.internalDelegate.executePipeline(pipelineBridge.toInternal(),
97+
(ingestDocument, e) -> {
98+
handler.accept(IngestDocumentBridge.fromInternalNullable(ingestDocument), e);
99+
});
94100
}
95101
}

libs/logstash-bridge/src/main/java/org/elasticsearch/logstashbridge/ingest/PipelineBridge.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
import java.util.Map;
1717
import java.util.function.BiConsumer;
1818

19-
public class PipelineBridge extends StableBridgeAPI.Proxy<Pipeline> {
20-
public static PipelineBridge wrap(final Pipeline pipeline) {
19+
/**
20+
* An external bridge for {@link Pipeline}
21+
*/
22+
public class PipelineBridge extends StableBridgeAPI.ProxyInternal<Pipeline> {
23+
public static PipelineBridge fromInternal(final Pipeline pipeline) {
2124
return new PipelineBridge(pipeline);
2225
}
2326

@@ -28,12 +31,12 @@ public static PipelineBridge create(
2831
Map<String, ProcessorBridge.Factory> processorFactories,
2932
ScriptServiceBridge scriptServiceBridge
3033
) throws Exception {
31-
return wrap(
34+
return fromInternal(
3235
Pipeline.create(
3336
id,
3437
config,
35-
StableBridgeAPI.unwrap(processorFactories),
36-
StableBridgeAPI.unwrapNullable(scriptServiceBridge),
38+
StableBridgeAPI.toInternal(processorFactories),
39+
StableBridgeAPI.toInternalNullable(scriptServiceBridge),
3740
null
3841
)
3942
);
@@ -44,13 +47,13 @@ public PipelineBridge(final Pipeline delegate) {
4447
}
4548

4649
public String getId() {
47-
return delegate.getId();
50+
return internalDelegate.getId();
4851
}
4952

5053
public void execute(final IngestDocumentBridge ingestDocumentBridge, final BiConsumer<IngestDocumentBridge, Exception> handler) {
51-
this.delegate.execute(
52-
StableBridgeAPI.unwrapNullable(ingestDocumentBridge),
53-
(unwrapped, e) -> handler.accept(IngestDocumentBridge.wrap(unwrapped), e)
54+
this.internalDelegate.execute(
55+
StableBridgeAPI.toInternalNullable(ingestDocumentBridge),
56+
(ingestDocument, e) -> handler.accept(IngestDocumentBridge.fromInternalNullable(ingestDocument), e)
5457
);
5558
}
5659
}

libs/logstash-bridge/src/main/java/org/elasticsearch/logstashbridge/ingest/PipelineConfigurationBridge.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515

1616
import java.util.Map;
1717

18-
public class PipelineConfigurationBridge extends StableBridgeAPI.Proxy<PipelineConfiguration> {
18+
/**
19+
* An external bridge for {@link PipelineConfiguration}
20+
*/
21+
public class PipelineConfigurationBridge extends StableBridgeAPI.ProxyInternal<PipelineConfiguration> {
1922
public PipelineConfigurationBridge(final PipelineConfiguration delegate) {
2023
super(delegate);
2124
}
@@ -25,33 +28,33 @@ public PipelineConfigurationBridge(final String pipelineId, final String jsonEnc
2528
}
2629

2730
public String getId() {
28-
return delegate.getId();
31+
return internalDelegate.getId();
2932
}
3033

3134
public Map<String, Object> getConfig() {
32-
return delegate.getConfig();
35+
return internalDelegate.getConfig();
3336
}
3437

3538
public Map<String, Object> getConfig(final boolean unmodifiable) {
36-
return delegate.getConfig(unmodifiable);
39+
return internalDelegate.getConfig(unmodifiable);
3740
}
3841

3942
@Override
4043
public int hashCode() {
41-
return delegate.hashCode();
44+
return internalDelegate.hashCode();
4245
}
4346

4447
@Override
4548
public String toString() {
46-
return delegate.toString();
49+
return internalDelegate.toString();
4750
}
4851

4952
@Override
5053
public boolean equals(final Object obj) {
5154
if (this == obj) {
5255
return true;
5356
} else if (obj instanceof PipelineConfigurationBridge other) {
54-
return delegate.equals(other.delegate);
57+
return internalDelegate.equals(other.internalDelegate);
5558
} else {
5659
return false;
5760
}

0 commit comments

Comments
 (0)