Skip to content

Commit 0f1738d

Browse files
Move base and rename response interface
1 parent f61726c commit 0f1738d

File tree

8 files changed

+39
-36
lines changed

8 files changed

+39
-36
lines changed

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/DatabaseReaderLazyLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ int current() {
108108
@Override
109109
@Nullable
110110
@FixForMultiProject // do not use ProjectId.DEFAULT
111-
public <RESPONSE extends GeoIpCache.CacheableValue> RESPONSE getResponse(
111+
public <RESPONSE extends Response> RESPONSE getResponse(
112112
String ipAddress,
113113
CheckedBiFunction<Reader, String, RESPONSE, Exception> responseProvider
114114
) {

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpCache.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,11 @@
3131
*/
3232
public final class GeoIpCache {
3333

34-
public interface CacheableValue {
35-
36-
// TODO PETE: Remove this default implementation and implement in all implementing classes instead
37-
default long sizeInBytes() {
38-
return 0;
39-
}
40-
}
41-
4234
static GeoIpCache createGeoIpCacheWithMaxCount(long maxSize) {
4335
if (maxSize < 0) {
4436
throw new IllegalArgumentException("geoip max cache size must be 0 or greater");
4537
}
46-
return new GeoIpCache(System::nanoTime, CacheBuilder.<CacheKey, CacheableValue>builder().setMaximumWeight(maxSize).build());
38+
return new GeoIpCache(System::nanoTime, CacheBuilder.<CacheKey, IpDatabase.Response>builder().setMaximumWeight(maxSize).build());
4739
}
4840

4941
static GeoIpCache createGeoIpCacheWithMaxBytes(ByteSizeValue maxByteSize) {
@@ -52,7 +44,7 @@ static GeoIpCache createGeoIpCacheWithMaxBytes(ByteSizeValue maxByteSize) {
5244
}
5345
return new GeoIpCache(
5446
System::nanoTime,
55-
CacheBuilder.<CacheKey, CacheableValue>builder()
47+
CacheBuilder.<CacheKey, IpDatabase.Response>builder()
5648
.setMaximumWeight(maxByteSize.getBytes())
5749
.weigher((key, value) -> key.sizeInBytes() + value.sizeInBytes())
5850
.build()
@@ -61,7 +53,10 @@ static GeoIpCache createGeoIpCacheWithMaxBytes(ByteSizeValue maxByteSize) {
6153

6254
// package private for testing
6355
static GeoIpCache createGeoIpCacheWithMaxCountAndCustomTimeProvider(long maxSize, LongSupplier relativeNanoTimeProvider) {
64-
return new GeoIpCache(relativeNanoTimeProvider, CacheBuilder.<CacheKey, CacheableValue>builder().setMaximumWeight(maxSize).build());
56+
return new GeoIpCache(
57+
relativeNanoTimeProvider,
58+
CacheBuilder.<CacheKey, IpDatabase.Response>builder().setMaximumWeight(maxSize).build()
59+
);
6560
}
6661

6762
/**
@@ -70,25 +65,25 @@ static GeoIpCache createGeoIpCacheWithMaxCountAndCustomTimeProvider(long maxSize
7065
* something not being in the cache because the data doesn't exist in the database.
7166
*/
7267
// visible for testing
73-
static final CacheableValue NO_RESULT = new CacheableValue() {
68+
static final IpDatabase.Response NO_RESULT = new IpDatabase.Response() {
7469
@Override
7570
public String toString() {
7671
return "NO_RESULT";
7772
}
7873
};
7974

8075
private final LongSupplier relativeNanoTimeProvider;
81-
private final Cache<CacheKey, CacheableValue> cache;
76+
private final Cache<CacheKey, IpDatabase.Response> cache;
8277
private final AtomicLong hitsTimeInNanos = new AtomicLong(0);
8378
private final AtomicLong missesTimeInNanos = new AtomicLong(0);
8479

85-
private GeoIpCache(LongSupplier relativeNanoTimeProvider, Cache<CacheKey, CacheableValue> cache) {
80+
private GeoIpCache(LongSupplier relativeNanoTimeProvider, Cache<CacheKey, IpDatabase.Response> cache) {
8681
this.relativeNanoTimeProvider = relativeNanoTimeProvider;
8782
this.cache = cache;
8883
}
8984

9085
@SuppressWarnings("unchecked")
91-
<RESPONSE extends CacheableValue> RESPONSE putIfAbsent(
86+
<RESPONSE extends IpDatabase.Response> RESPONSE putIfAbsent(
9287
ProjectId projectId,
9388
String ip,
9489
String databasePath,
@@ -98,7 +93,7 @@ <RESPONSE extends CacheableValue> RESPONSE putIfAbsent(
9893
CacheKey cacheKey = new CacheKey(projectId, ip, databasePath);
9994
long cacheStart = relativeNanoTimeProvider.getAsLong();
10095
// intentionally non-locking for simplicity...it's OK if we re-put the same key/value in the cache during a race condition.
101-
CacheableValue response = cache.get(cacheKey);
96+
IpDatabase.Response response = cache.get(cacheKey);
10297
long cacheRequestTime = relativeNanoTimeProvider.getAsLong() - cacheStart;
10398

10499
// populate the cache for this key, if necessary

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/IpDataLookup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ interface IpDataLookup {
3434
* as a network for which the record applies. Having a helper record prevents each individual response record from needing to
3535
* track these bits of information.
3636
*/
37-
record Result<T extends GeoIpCache.CacheableValue>(T result, String ip, String network) implements GeoIpCache.CacheableValue {}
37+
record Result<T extends IpDatabase.Response>(T result, String ip, String network) implements IpDatabase.Response {}
3838
}

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/IpDatabase.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public interface IpDatabase extends AutoCloseable {
3535
* @return a possibly-null response
3636
*/
3737
@Nullable
38-
<RESPONSE extends GeoIpCache.CacheableValue> RESPONSE getResponse(
38+
<RESPONSE extends Response> RESPONSE getResponse(
3939
String ipAddress,
4040
CheckedBiFunction<Reader, String, RESPONSE, Exception> responseProvider
4141
);
@@ -48,4 +48,12 @@ <RESPONSE extends GeoIpCache.CacheableValue> RESPONSE getResponse(
4848
*/
4949
@Override
5050
void close() throws IOException;
51+
52+
interface Response {
53+
54+
// TODO PETE: Remove this default implementation and implement in all implementing classes instead
55+
default long sizeInBytes() {
56+
return 0;
57+
}
58+
}
5159
}

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/IpinfoIpDataLookups.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public record AsnResult(
191191
String domain,
192192
String name,
193193
@Nullable String type // not present in the free asn database
194-
) implements GeoIpCache.CacheableValue {
194+
) implements IpDatabase.Response {
195195
@SuppressWarnings("checkstyle:RedundantModifier")
196196
@MaxMindDbConstructor
197197
public AsnResult(
@@ -210,14 +210,14 @@ public record CountryResult(
210210
@MaxMindDbParameter(name = "continent_name") String continentName,
211211
@MaxMindDbParameter(name = "country") String country,
212212
@MaxMindDbParameter(name = "country_name") String countryName
213-
) implements GeoIpCache.CacheableValue {
213+
) implements IpDatabase.Response {
214214
@MaxMindDbConstructor
215215
public CountryResult {}
216216
}
217217

218218
public record GeolocationResult(String city, String country, Double lat, Double lng, String postalCode, String region, String timezone)
219219
implements
220-
GeoIpCache.CacheableValue {
220+
IpDatabase.Response {
221221
@SuppressWarnings("checkstyle:RedundantModifier")
222222
@MaxMindDbConstructor
223223
public GeolocationResult(
@@ -237,7 +237,7 @@ public GeolocationResult(
237237

238238
public record PrivacyDetectionResult(Boolean hosting, Boolean proxy, Boolean relay, String service, Boolean tor, Boolean vpn)
239239
implements
240-
GeoIpCache.CacheableValue {
240+
IpDatabase.Response {
241241
@SuppressWarnings("checkstyle:RedundantModifier")
242242
@MaxMindDbConstructor
243243
public PrivacyDetectionResult(
@@ -462,7 +462,7 @@ protected Map<String, Object> transform(final Result<PrivacyDetectionResult> res
462462
*
463463
* @param <RESPONSE> the record type that will be wrapped and returned
464464
*/
465-
private abstract static class AbstractBase<RESPONSE extends GeoIpCache.CacheableValue> implements IpDataLookup {
465+
private abstract static class AbstractBase<RESPONSE extends IpDatabase.Response> implements IpDataLookup {
466466

467467
protected final Set<Database.Property> properties;
468468
protected final Class<RESPONSE> clazz;

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/MaxmindIpDataLookups.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ static Function<Set<Database.Property>, IpDataLookup> getMaxmindLookup(final Dat
109109
};
110110
}
111111

112-
static class CacheableAnonymousIpResponse extends AnonymousIpResponse implements GeoIpCache.CacheableValue {
112+
static class CacheableAnonymousIpResponse extends AnonymousIpResponse implements IpDatabase.Response {
113113

114114
CacheableAnonymousIpResponse(AnonymousIpResponse response) {
115115
super(
@@ -176,7 +176,7 @@ protected Map<String, Object> transform(final CacheableAnonymousIpResponse respo
176176
}
177177
}
178178

179-
static class CacheableAsnResponse extends AsnResponse implements GeoIpCache.CacheableValue {
179+
static class CacheableAsnResponse extends AsnResponse implements IpDatabase.Response {
180180

181181
CacheableAsnResponse(AsnResponse response) {
182182
super(
@@ -246,7 +246,7 @@ record CacheableCityResponse(
246246
Boolean registeredCountryIsInEuropeanUnion,
247247
String registeredCountryIsoCode,
248248
String registeredCountryName
249-
) implements GeoIpCache.CacheableValue {}
249+
) implements IpDatabase.Response {}
250250

251251
static class City extends AbstractBase<CityResponse, Result<CacheableCityResponse>> {
252252
City(final Set<Database.Property> properties) {
@@ -382,7 +382,7 @@ protected Map<String, Object> transform(final Result<CacheableCityResponse> resu
382382
}
383383
}
384384

385-
static class CacheableConnectionTypeResponse extends ConnectionTypeResponse implements GeoIpCache.CacheableValue {
385+
static class CacheableConnectionTypeResponse extends ConnectionTypeResponse implements IpDatabase.Response {
386386

387387
CacheableConnectionTypeResponse(ConnectionTypeResponse response) {
388388
super(response.getConnectionType(), response.getIpAddress(), response.getNetwork());
@@ -431,7 +431,7 @@ record CacheableCountryResponse(
431431
Boolean registeredCountryIsInEuropeanUnion,
432432
String registeredCountryIsoCode,
433433
String registeredCountryName
434-
) implements GeoIpCache.CacheableValue {}
434+
) implements IpDatabase.Response {}
435435

436436
static class Country extends AbstractBase<CountryResponse, Result<CacheableCountryResponse>> {
437437
Country(final Set<Database.Property> properties) {
@@ -514,7 +514,7 @@ protected Map<String, Object> transform(final Result<CacheableCountryResponse> r
514514
}
515515
}
516516

517-
static class CacheableDomainResponse extends DomainResponse implements GeoIpCache.CacheableValue {
517+
static class CacheableDomainResponse extends DomainResponse implements IpDatabase.Response {
518518

519519
CacheableDomainResponse(DomainResponse response) {
520520
super(response.getDomain(), response.getIpAddress(), response.getNetwork());
@@ -589,7 +589,7 @@ record CacheableEnterpriseResponse(
589589
Boolean registeredCountryIsInEuropeanUnion,
590590
String registeredCountryIsoCode,
591591
String registeredCountryName
592-
) implements GeoIpCache.CacheableValue {}
592+
) implements IpDatabase.Response {}
593593

594594
static class Enterprise extends AbstractBase<EnterpriseResponse, Result<CacheableEnterpriseResponse>> {
595595
Enterprise(final Set<Database.Property> properties) {
@@ -826,7 +826,7 @@ protected Map<String, Object> transform(final Result<CacheableEnterpriseResponse
826826
}
827827
}
828828

829-
static class CacheableIspResponse extends IspResponse implements GeoIpCache.CacheableValue {
829+
static class CacheableIspResponse extends IspResponse implements IpDatabase.Response {
830830

831831
CacheableIspResponse(IspResponse response) {
832832
super(response, response.getIpAddress(), response.getNetwork());
@@ -916,7 +916,7 @@ private interface ResponseBuilder<RESPONSE extends AbstractResponse> {
916916
*
917917
* @param <RESPONSE> the intermediate type of {@link AbstractResponse}
918918
*/
919-
private abstract static class AbstractBase<RESPONSE extends AbstractResponse, RECORD extends GeoIpCache.CacheableValue>
919+
private abstract static class AbstractBase<RESPONSE extends AbstractResponse, RECORD extends IpDatabase.Response>
920920
implements
921921
IpDataLookup {
922922

modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpCacheTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
public class GeoIpCacheTests extends ESTestCase {
2727

28-
private record FakeResponse(long sizeInBytes) implements GeoIpCache.CacheableValue {}
28+
private record FakeResponse(long sizeInBytes) implements IpDatabase.Response {}
2929

3030
public void testCachesAndEvictsResults_maxCount() {
3131
GeoIpCache cache = GeoIpCache.createGeoIpCacheWithMaxCount(1);

modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpTestUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static void copyDefaultDatabases(final Path directory, ConfigDatabases configDat
6767
}
6868
}
6969

70-
public record SimpleCity(String cityName) implements GeoIpCache.CacheableValue {}
70+
public record SimpleCity(String cityName) implements IpDatabase.Response {}
7171

7272
/**
7373
* A static city-specific responseProvider for use with {@link IpDatabase#getResponse(String, CheckedBiFunction)} in
@@ -81,7 +81,7 @@ public static SimpleCity getCity(Reader reader, String ip) throws IOException {
8181
return data == null ? null : new SimpleCity(new CityResponse(data, ip, record.getNetwork(), List.of("en")).getCity().getName());
8282
}
8383

84-
public record SimpleCountry(String countryName) implements GeoIpCache.CacheableValue {}
84+
public record SimpleCountry(String countryName) implements IpDatabase.Response {}
8585

8686
/**
8787
* A static country-specific responseProvider for use with {@link IpDatabase#getResponse(String, CheckedBiFunction)} in

0 commit comments

Comments
 (0)