Skip to content

Commit 24d40bb

Browse files
committed
Refactor geoip cache for MaxMind databases (elastic#125527)
1 parent 935e9e3 commit 24d40bb

File tree

5 files changed

+145
-73
lines changed

5 files changed

+145
-73
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,11 @@ interface IpDataLookup {
2828
* @return the set of properties this lookup will provide
2929
*/
3030
Set<Database.Property> getProperties();
31+
32+
/**
33+
* A helper record that holds other records. Every ip data lookup will have an associated ip address that was looked up, as well
34+
* as a network for which the record applies. Having a helper record prevents each individual response record from needing to
35+
* track these bits of information.
36+
*/
37+
record Result<T>(T result, String ip, String network) {}
3138
}

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

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,15 @@ static class Asn extends AbstractBase<AsnResult> {
264264

265265
@Override
266266
protected Map<String, Object> transform(final Result<AsnResult> result) {
267-
AsnResult response = result.result;
267+
AsnResult response = result.result();
268268
Long asn = response.asn;
269269
String organizationName = response.name;
270-
String network = result.network;
270+
String network = result.network();
271271

272272
Map<String, Object> data = new HashMap<>();
273273
for (Database.Property property : this.properties) {
274274
switch (property) {
275-
case IP -> data.put("ip", result.ip);
275+
case IP -> data.put("ip", result.ip());
276276
case ASN -> {
277277
if (asn != null) {
278278
data.put("asn", asn);
@@ -316,12 +316,12 @@ static class Country extends AbstractBase<CountryResult> {
316316

317317
@Override
318318
protected Map<String, Object> transform(final Result<CountryResult> result) {
319-
CountryResult response = result.result;
319+
CountryResult response = result.result();
320320

321321
Map<String, Object> data = new HashMap<>();
322322
for (Database.Property property : this.properties) {
323323
switch (property) {
324-
case IP -> data.put("ip", result.ip);
324+
case IP -> data.put("ip", result.ip());
325325
case COUNTRY_ISO_CODE -> {
326326
String countryIsoCode = response.country;
327327
if (countryIsoCode != null) {
@@ -359,12 +359,12 @@ static class Geolocation extends AbstractBase<GeolocationResult> {
359359

360360
@Override
361361
protected Map<String, Object> transform(final Result<GeolocationResult> result) {
362-
GeolocationResult response = result.result;
362+
GeolocationResult response = result.result();
363363

364364
Map<String, Object> data = new HashMap<>();
365365
for (Database.Property property : this.properties) {
366366
switch (property) {
367-
case IP -> data.put("ip", result.ip);
367+
case IP -> data.put("ip", result.ip());
368368
case COUNTRY_ISO_CODE -> {
369369
String countryIsoCode = response.country;
370370
if (countryIsoCode != null) {
@@ -418,12 +418,12 @@ static class PrivacyDetection extends AbstractBase<PrivacyDetectionResult> {
418418

419419
@Override
420420
protected Map<String, Object> transform(final Result<PrivacyDetectionResult> result) {
421-
PrivacyDetectionResult response = result.result;
421+
PrivacyDetectionResult response = result.result();
422422

423423
Map<String, Object> data = new HashMap<>();
424424
for (Database.Property property : this.properties) {
425425
switch (property) {
426-
case IP -> data.put("ip", result.ip);
426+
case IP -> data.put("ip", result.ip());
427427
case HOSTING -> {
428428
if (response.hosting != null) {
429429
data.put("hosting", response.hosting);
@@ -460,16 +460,9 @@ protected Map<String, Object> transform(final Result<PrivacyDetectionResult> res
460460
}
461461
}
462462

463-
/**
464-
* Just a little record holder -- there's the data that we receive via the binding to our record objects from the Reader via the
465-
* getRecord call, but then we also need to capture the passed-in ip address that came from the caller as well as the network for
466-
* the returned DatabaseRecord from the Reader.
467-
*/
468-
public record Result<T>(T result, String ip, String network) {}
469-
470463
/**
471464
* The {@link IpinfoIpDataLookups.AbstractBase} is an abstract base implementation of {@link IpDataLookup} that
472-
* provides common functionality for getting a {@link IpinfoIpDataLookups.Result} that wraps a record from a {@link IpDatabase}.
465+
* provides common functionality for getting a {@link IpDataLookup.Result} that wraps a record from a {@link IpDatabase}.
473466
*
474467
* @param <RESPONSE> the record type that will be wrapped and returned
475468
*/
@@ -491,19 +484,20 @@ public Set<Database.Property> getProperties() {
491484
@Override
492485
public final Map<String, Object> getData(final IpDatabase ipDatabase, final String ipAddress) {
493486
final Result<RESPONSE> response = ipDatabase.getResponse(ipAddress, this::lookup);
494-
return (response == null || response.result == null) ? Map.of() : transform(response);
487+
return (response == null || response.result() == null) ? Map.of() : transform(response);
495488
}
496489

497490
@Nullable
498491
private Result<RESPONSE> lookup(final Reader reader, final String ipAddress) throws IOException {
499492
final InetAddress ip = InetAddresses.forString(ipAddress);
500-
final DatabaseRecord<RESPONSE> record = reader.getRecord(ip, clazz);
501-
final RESPONSE data = record.getData();
502-
return (data == null) ? null : new Result<>(data, NetworkAddress.format(ip), record.getNetwork().toString());
493+
final DatabaseRecord<RESPONSE> entry = reader.getRecord(ip, clazz);
494+
final RESPONSE data = entry.getData();
495+
return (data == null) ? null : new Result<>(data, NetworkAddress.format(ip), entry.getNetwork().toString());
503496
}
504497

505498
/**
506-
* Extract the configured properties from the retrieved response
499+
* Extract the configured properties from the retrieved response.
500+
*
507501
* @param response the non-null response that was retrieved
508502
* @return a mapping of properties for the ip from the response
509503
*/

0 commit comments

Comments
 (0)