Skip to content

Commit abe7548

Browse files
authored
Merge pull request #3 from yaauie/rye-bridge-refinement-moar
Logstash Bridge Refinement
2 parents 7eb593c + e3ba1cb commit abe7548

35 files changed

+1154
-753
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ static <T, B extends StableBridgeAPI<T>> B fromInternal(final T delegate, final
5050
* An {@code ProxyInternal<INTERNAL>} is an implementation of {@code StableBridgeAPI<INTERNAL>} that
5151
* proxies calls to a delegate that is an actual {@code INTERNAL}.
5252
*
53+
* <p>
54+
* implementations are intended to be <em>opaque</em> to consumers of this library,
55+
* and should <em>NOT</em> have public constructors.
56+
* </p>
57+
*
5358
* @param <INTERNAL>
5459
*/
5560
abstract class ProxyInternal<INTERNAL> implements StableBridgeAPI<INTERNAL> {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.logstashbridge.common;
11+
12+
import org.elasticsearch.cluster.metadata.ProjectId;
13+
import org.elasticsearch.logstashbridge.StableBridgeAPI;
14+
15+
/**
16+
* A {@link StableBridgeAPI} for {@link ProjectId}
17+
*/
18+
public interface ProjectIdBridge extends StableBridgeAPI<ProjectId> {
19+
String id();
20+
21+
static ProjectIdBridge fromInternal(final ProjectId projectId) {
22+
return new ProxyInternal(projectId);
23+
}
24+
25+
static ProjectIdBridge fromId(final String id) {
26+
final ProjectId internal = ProjectId.fromId(id);
27+
return new ProxyInternal(internal);
28+
}
29+
30+
static ProjectIdBridge getDefault() {
31+
return ProxyInternal.DEFAULT;
32+
}
33+
34+
/**
35+
* An implementation of {@link ProjectIdBridge} that proxies calls to
36+
* an internal {@link ProjectId} instance.
37+
*
38+
* @see StableBridgeAPI.ProxyInternal
39+
*/
40+
final class ProxyInternal extends StableBridgeAPI.ProxyInternal<ProjectId> implements ProjectIdBridge {
41+
private static final ProjectIdBridge.ProxyInternal DEFAULT = new ProjectIdBridge.ProxyInternal(ProjectId.DEFAULT);
42+
43+
ProxyInternal(ProjectId internalDelegate) {
44+
super(internalDelegate);
45+
}
46+
47+
@Override
48+
public String id() {
49+
return this.internalDelegate.id();
50+
}
51+
52+
@Override
53+
public ProjectId toInternal() {
54+
return this.internalDelegate;
55+
}
56+
}
57+
}

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

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,27 @@
1212
import org.elasticsearch.logstashbridge.StableBridgeAPI;
1313

1414
/**
15-
* An external bridge for {@link Settings}
15+
* A {@link StableBridgeAPI} for {@link Settings}
1616
*/
17-
public class SettingsBridge extends StableBridgeAPI.ProxyInternal<Settings> {
17+
public interface SettingsBridge extends StableBridgeAPI<Settings> {
1818

19-
public static SettingsBridge fromInternal(final Settings delegate) {
20-
return new SettingsBridge(delegate);
19+
static SettingsBridge fromInternal(final Settings delegate) {
20+
return new ProxyInternal(delegate);
2121
}
2222

23-
public static Builder builder() {
24-
return Builder.fromInternal(Settings.builder());
25-
}
26-
27-
public SettingsBridge(final Settings delegate) {
28-
super(delegate);
29-
}
30-
31-
@Override
32-
public Settings toInternal() {
33-
return this.internalDelegate;
23+
static SettingsBuilderBridge builder() {
24+
return SettingsBuilderBridge.fromInternal(Settings.builder());
3425
}
3526

3627
/**
37-
* An external bridge for {@link Settings.Builder} that proxies calls to a real {@link Settings.Builder}
28+
* An implementation of {@link SettingsBridge} that proxies calls to
29+
* an internal {@link Settings} instance.
30+
*
31+
* @see StableBridgeAPI.ProxyInternal
3832
*/
39-
public static class Builder extends StableBridgeAPI.ProxyInternal<Settings.Builder> {
40-
static Builder fromInternal(final Settings.Builder delegate) {
41-
return new Builder(delegate);
42-
}
43-
44-
private Builder(final Settings.Builder delegate) {
45-
super(delegate);
46-
}
47-
48-
public Builder put(final String key, final String value) {
49-
this.internalDelegate.put(key, value);
50-
return this;
51-
}
52-
53-
public SettingsBridge build() {
54-
return new SettingsBridge(this.internalDelegate.build());
33+
final class ProxyInternal extends StableBridgeAPI.ProxyInternal<Settings> implements SettingsBridge {
34+
ProxyInternal(Settings internalDelegate) {
35+
super(internalDelegate);
5536
}
5637
}
5738
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.logstashbridge.common;
11+
12+
import org.elasticsearch.common.settings.Settings;
13+
import org.elasticsearch.logstashbridge.StableBridgeAPI;
14+
15+
/**
16+
* A {@link StableBridgeAPI} for {@link Settings.Builder}.
17+
*/
18+
public interface SettingsBuilderBridge extends StableBridgeAPI<Settings.Builder> {
19+
20+
SettingsBuilderBridge put(String key, String value);
21+
22+
SettingsBridge build();
23+
24+
static SettingsBuilderBridge fromInternal(final Settings.Builder builder) {
25+
return new ProxyInternal(builder);
26+
}
27+
28+
/**
29+
* An implementation of {@link SettingsBuilderBridge} that proxies calls to
30+
* an internal {@link Settings.Builder} instance.
31+
*
32+
* @see StableBridgeAPI.ProxyInternal
33+
*/
34+
final class ProxyInternal extends StableBridgeAPI.ProxyInternal<Settings.Builder> implements SettingsBuilderBridge {
35+
ProxyInternal(Settings.Builder internalDelegate) {
36+
super(internalDelegate);
37+
}
38+
39+
@Override
40+
public SettingsBuilderBridge put(String key, String value) {
41+
internalDelegate.put(key, value);
42+
return this;
43+
}
44+
45+
@Override
46+
public SettingsBridge build() {
47+
final Settings delegate = internalDelegate.build();
48+
return SettingsBridge.fromInternal(delegate);
49+
}
50+
}
51+
}
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,22 @@
99

1010
package org.elasticsearch.logstashbridge.core;
1111

12-
import org.elasticsearch.ingest.common.FailProcessorException;
12+
import org.elasticsearch.common.CheckedBiFunction;
1313
import org.elasticsearch.logstashbridge.StableBridgeAPI;
1414

15-
public class FailProcessorExceptionBridge extends StableBridgeAPI.ProxyInternal<FailProcessorException> {
16-
protected FailProcessorExceptionBridge(FailProcessorException internalDelegate) {
17-
super(internalDelegate);
18-
}
15+
/**
16+
* A stable interface on top of {@link CheckedBiFunction}.
17+
* @param <T> type of lhs parameter
18+
* @param <U> type of rhs parameter
19+
* @param <R> type of return value
20+
* @param <E> type of anticipated exception
21+
*/
22+
@FunctionalInterface
23+
public interface CheckedBiFunctionBridge<T, U, R, E extends Exception> extends StableBridgeAPI<CheckedBiFunction<T, U, R, E>> {
24+
R apply(T t, U u) throws E;
1925

20-
public static boolean isInstanceOf(Throwable exception) {
21-
return exception instanceof FailProcessorException;
26+
@Override
27+
default CheckedBiFunction<T, U, R, E> toInternal() {
28+
return this::apply;
2229
}
2330
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* An external bridge for {@link IOUtils}
1717
*/
1818
public class IOUtilsBridge {
19+
private IOUtilsBridge() {}
20+
1921
public static void closeWhileHandlingException(final Iterable<? extends Closeable> objects) {
2022
IOUtils.closeWhileHandlingException(objects);
2123
}

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

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,52 @@
1010
package org.elasticsearch.logstashbridge.core;
1111

1212
import org.elasticsearch.action.support.RefCountingRunnable;
13+
import org.elasticsearch.core.Releasable;
1314
import org.elasticsearch.logstashbridge.StableBridgeAPI;
1415

15-
public class RefCountingRunnableBridge extends StableBridgeAPI.ProxyInternal<RefCountingRunnable> {
16+
import java.io.Closeable;
1617

17-
private RefCountingRunnableBridge(final RefCountingRunnable delegate) {
18-
super(delegate);
19-
}
20-
21-
public RefCountingRunnableBridge(final Runnable delegate) {
22-
super(new RefCountingRunnable(delegate));
23-
}
24-
25-
public void close() {
26-
toInternal().close();
27-
}
28-
29-
public ReleasableBridge acquire() {
30-
return new ReleasableBridge.ProxyInternal(toInternal().acquire());
18+
/**
19+
* A {@link StableBridgeAPI} for {@link RefCountingRunnable}
20+
*/
21+
public interface RefCountingRunnableBridge extends StableBridgeAPI<RefCountingRunnable>, Closeable {
22+
23+
@Override // only RuntimeException
24+
void close();
25+
26+
ReleasableBridge acquire();
27+
28+
/**
29+
* An API-stable factory method for {@link RefCountingRunnableBridge}
30+
* @param delegate the {@link Runnable} to execute when all refs are closed
31+
* @return a {@link RefCountingRunnableBridge} that will execute the provided
32+
* block when all refs are closed
33+
*/
34+
static RefCountingRunnableBridge create(final Runnable delegate) {
35+
final RefCountingRunnable refCountingRunnable = new RefCountingRunnable(delegate);
36+
return new ProxyInternal(refCountingRunnable);
3137
}
3238

33-
@Override
34-
public RefCountingRunnable toInternal() {
35-
return this.internalDelegate;
39+
/**
40+
* An implementation of {@link RefCountingRunnableBridge} that proxies calls through
41+
* to an internal {@link RefCountingRunnable}.
42+
* @see StableBridgeAPI.ProxyInternal
43+
*/
44+
final class ProxyInternal extends StableBridgeAPI.ProxyInternal<RefCountingRunnable> implements RefCountingRunnableBridge {
45+
private ProxyInternal(final RefCountingRunnable delegate) {
46+
super(delegate);
47+
}
48+
49+
@Override
50+
public void close() {
51+
toInternal().close();
52+
}
53+
54+
@Override
55+
public ReleasableBridge acquire() {
56+
@SuppressWarnings("resource")
57+
final Releasable releasable = toInternal().acquire();
58+
return ReleasableBridge.fromInternal(releasable);
59+
}
3660
}
3761
}

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,28 @@
1212
import org.elasticsearch.core.Releasable;
1313
import org.elasticsearch.logstashbridge.StableBridgeAPI;
1414

15-
public interface ReleasableBridge extends StableBridgeAPI<Releasable> {
15+
import java.io.Closeable;
1616

17+
/**
18+
* A {@link StableBridgeAPI} for {@link Releasable} for use with {@link RefCountingRunnableBridge}
19+
*/
20+
public interface ReleasableBridge extends StableBridgeAPI<Releasable>, Closeable {
21+
22+
@Override // only RuntimeException
1723
void close();
1824

19-
class ProxyInternal extends StableBridgeAPI.ProxyInternal<Releasable> implements ReleasableBridge {
25+
static ReleasableBridge fromInternal(Releasable releasable) {
26+
return new ProxyInternal(releasable);
27+
}
28+
29+
/**
30+
* An implementation of {@link ReleasableBridge} that proxies calls through
31+
* to an internal {@link Releasable}.
32+
* @see StableBridgeAPI.ProxyInternal
33+
*/
34+
final class ProxyInternal extends StableBridgeAPI.ProxyInternal<Releasable> implements ReleasableBridge {
2035

21-
public ProxyInternal(final Releasable delegate) {
36+
private ProxyInternal(final Releasable delegate) {
2237
super(delegate);
2338
}
2439

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,30 @@
1515
import java.nio.file.Path;
1616

1717
/**
18-
* An external bridge for {@link Environment}
18+
* A {@link StableBridgeAPI} for {@link Environment}
1919
*/
20-
public class EnvironmentBridge extends StableBridgeAPI.ProxyInternal<Environment> {
21-
public static EnvironmentBridge fromInternal(final Environment delegate) {
22-
return new EnvironmentBridge(delegate);
20+
public interface EnvironmentBridge extends StableBridgeAPI<Environment> {
21+
static EnvironmentBridge fromInternal(final Environment delegate) {
22+
return new EnvironmentBridge.ProxyInternal(delegate);
2323
}
2424

25-
public EnvironmentBridge(final SettingsBridge settingsBridge, final Path configPath) {
26-
this(new Environment(settingsBridge.toInternal(), configPath));
25+
static EnvironmentBridge create(final SettingsBridge bridgedSettings, final Path configPath) {
26+
return fromInternal(new Environment(bridgedSettings.toInternal(), configPath));
2727
}
2828

29-
private EnvironmentBridge(final Environment delegate) {
30-
super(delegate);
31-
}
29+
/**
30+
* An implementation of {@link EnvironmentBridge} that proxies calls through
31+
* to an internal {@link Environment}.
32+
* @see StableBridgeAPI.ProxyInternal
33+
*/
34+
final class ProxyInternal extends StableBridgeAPI.ProxyInternal<Environment> implements EnvironmentBridge {
35+
private ProxyInternal(final Environment delegate) {
36+
super(delegate);
37+
}
3238

33-
@Override
34-
public Environment toInternal() {
35-
return this.internalDelegate;
39+
@Override
40+
public Environment toInternal() {
41+
return this.internalDelegate;
42+
}
3643
}
3744
}

0 commit comments

Comments
 (0)